LibreOJ #6284. 数列分块入门 8

题目链接:

https://loj.ac/problem/6284

题意:

给你一个 n n n个整数的序列 a a a,让你进行一种操作:

  • 查询 [ l , r ] [l,r] [l,r]内的数字 x x x的个数,然后把 [ l , r ] [l,r] [l,r]内的数字都变成 x x x

分析:

       正常分块,标记则标记的是块中元素的值是否相同,若相同则标记则为块中元素值,否则设置一个不可能的数用来标识;
       对于区间两端不完整块进行暴力,对于完整块,则看它是否元素为同一种,若为同一种还要看它是不是我们查询的元素,若不为同一种,则对块进行暴力修改和查询(简单想一下,这也是可行的,时间复杂度可得到保障);

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define inf 0x7f7f7f7f
#define maxn 100050
#define N 100100
#define P 2

typedef long long ll;
typedef struct {
   int u, v, next, w;
} Edge;
Edge e[2];
int cnt, head[1];

inline void add1(int u, int v, int w) {
   e[cnt].u = u;
   e[cnt].v = v;
   e[cnt].w = w;
   // e[cnt].f=f;
   e[cnt].next = head[u];
   head[u] = cnt++;
   e[cnt].u = v;
   e[cnt].v = u;
   e[cnt].w = w;
   //    e[cnt].f=-f;
   e[cnt].next = head[v];
   head[v] = cnt++;
}

inline void write(int x) {
   if (x < 0)
       putchar('-'), x = -x;
   if (x > 9)
       write(x / 10);
   putchar(x % 10 + '0');
}

inline int read() {
   int x = 0, f = 1;
   char c = getchar();
   while (c < '0' || c > '9') {
       if (c == '-')
           f = -1;
       c = getchar();
   }
   while (c >= '0' && c <= '9') {
       x = x * 10 + c - '0';
       c = getchar();
   }
   return x * f;
}

int opt, l, r, c, sz, n, pos[maxn];
int a[maxn], mark[maxn];
// vectorvec[505];
inline void judge(int t) {
   mark[t] = 0;
   for (int i = t * sz + 1; t <= min(n, (t + 1) * sz) - 1; i++)
       if (a[i] != a[i + 1]) {
           mark[t] = 1;
           break;
       }
}

int query(int l, int r, int val) {
   int res = 0, pl = pos[l], pr = pos[r];
   if (mark[pl]) {
       for (int i = pl * sz + 1; i <= min(n, (pl + 1) * sz); i++) a[i] = mark[pl];
       if (val != mark[pl])
           mark[pl] = 0;
   }
   for (int i = l; i <= min(r, (pl + 1) * sz); i++)
       if (a[i] == val)
           res++;
       else
           a[i] = val;
   if (pl != pr) {
       if (mark[pr]) {
           for (int i = pr * sz + 1; i <= min(n, (pr + 1) * sz); i++) a[i] = mark[pr];
           if (val != mark[pr])
               mark[pr] = 0;
       }
       for (int i = pr * sz + 1; i <= r; i++) {
           if (a[i] == val)
               res++;
           else
               a[i] = val;
       }
   }
   for (int i = pl + 1; i < pr; i++) {
       if (mark[i]) {
           if (mark[i] == val)
               res += sz;
           else
               mark[i] = val;
       } else {
           for (int j = i * sz + 1; j <= (i + 1) * sz; j++) {
               if (a[j] == val)
                   res++;
               else
                   a[j] = val;
           }
           mark[i] = val;
       }
   }
   return res;
}
int main() {
   cin >> n;
   sz = sqrt(n);
   for (int i = 1; i <= n; i++) {
       a[i] = read();
       pos[i] = (i - 1) / sz;
   }
   for (int i = 1; i <= n; i++) {
       l = read(), r = read(), c = read();
       printf("%d\n", query(l, r, c));
   }
   return 0;
}

我们坚持一件事情,并不是因为这样做了会有效果,而是坚信,这样做是对的。
——哈维尔

你可能感兴趣的:(分块)