LOJ - #6284. 数列分块入门 8

题目链接:https://ajax.loj.ac/problem/6284

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int a[maxn], pos[maxn], blo, n, tag[maxn], l, r, c;
void reset(int x)
{
    if(tag[x]== -1) return;
    for (int i = (x-1)*blo+1; i<= x*blo; i++)
    {
        a[i] = tag[x];
    }
    tag[x] = -1;
}
int solve(int l, int r, int c)
{
    int ans = 0;
    reset(pos[l]);
    for (int i = l; i <= min(pos[l]*blo, r); i++){
        if(a[i] != c) a[i] = c;
        else ans++;
    }
    if(pos[l] != pos[r])
    {
        reset(pos[r]);
        for (int i = (pos[r]-1)*blo+1; i <= r; i++)
        {
            if(a[i] != c) a[i] = c;
            else ans++;
        }
    }
    for (int i = pos[l]+1; i <= pos[r]-1; i++)
    {
        if(tag[i] != -1){
            if(tag[i] != c) tag[i] = c;
            else ans += blo;
        }
        else{
            for (int j = (i-1)*blo+1; j <= i*blo; j++){
                if(a[j] != c) a[j] == c;
                else ans++;
            }
            tag[i] = c;
        }
    }
    return ans;
}
int main()
{
    memset(tag, -1, sizeof(tag));
    scanf("%d", &n);
    blo = sqrt(n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), pos[i] = (i-1) / blo + 1;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d%d", &l, &r, &c);
        printf("%d\n", solve(l, r, c));
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37602930/article/details/81280192