CodeForces 444 C DZY Loves Colors

DZY Loves Colors

题意:有n个蛋糕,起初第i个蛋糕他的颜色值为i, 现在有2个操作, 1 x  y  c 在[x, y]的蛋糕上都加上一层颜色值为c的蛋糕片,加了这个蛋糕片之后,会产生一个惊喜值为abs(c-a) a为上一层蛋糕片的颜色。

2 x y 求出[x, y]区间内所有蛋糕的惊喜值之和。

题解:暴力分块处理, 如果一个块被盖上了相同的颜色,那么下次这个块又被同一种颜色蛋糕片覆盖了的话就可以打一个lz标记,并且求和。

线段树也能写, 练习分块所以分块搞了半天, 然后一直wa, 重敲了一遍就AC了, 也不知道发生了啥,可能会补上线段树的写法。

代码:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
  4 #define LL long long
  5 #define ULL unsigned LL
  6 #define fi first
  7 #define se second
  8 #define pb push_back
  9 #define lson l,m,rt<<1
 10 #define rson m+1,r,rt<<1|1
 11 #define max3(a,b,c) max(a,max(b,c))
 12 #define min3(a,b,c) min(a,min(b,c))
 13 #define _S(X) cout << x << ' ';
 14 #define __S(x) cout << x << endl;
 15 typedef pair<int,int> pll;
 16 const int INF = 0x3f3f3f3f;
 17 const LL mod =  (int)1e9+7;
 18 const int N = 2e5 + 100;
 19 int l[N], r[N], belong[N];
 20 LL lz[N], sum[N], v[N], a[N], tmp[N];
 21 int n, m, tot;
 22 void Build(){
 23     m = sqrt(n);
 24     tot = n/m;
 25     if(n%m) tot++;
 26     for(int i = 1; i <= n; i++)
 27         belong[i] = (i-1)/m + 1, a[i] = i;
 28     for(int i = 1; i <= tot; i++)
 29         l[i] = (i-1)*m+1, r[i] = i*m;
 30     r[tot] = n;
 31 }
 32 void PushDown(int rt){
 33     for(int i = l[rt]; i <= r[rt]; i++)
 34         a[i] = lz[rt];
 35     lz[rt] = 0;
 36 }
 37 void Update(int x, int y, LL c){
 38     int idx = belong[x], idy = belong[y];
 39     if(idx == idy){
 40         if(lz[idx]) PushDown(idx);
 41         for(int i = x; i <= y; i++){
 42             sum[idx] += abs(c - a[i]);
 43             v[i] += abs(c - a[i]);
 44             a[i] = c;
 45         }
 46     }
 47     else {
 48         if(lz[idx]) PushDown(idx);
 49         if(lz[idy]) PushDown(idy);
 50         for(int i = x; i <= r[idx]; i++){
 51             sum[idx] += abs(c - a[i]);
 52             v[i] += abs(c - a[i]);
 53             a[i] = c;
 54         }
 55         for(int i = l[idy]; i <= y; i++){
 56             sum[idy] += abs(c - a[i]);
 57             v[i] += abs(c - a[i]);
 58             a[i] = c;
 59         }
 60         for(int i = idx+1; i < idy; i++){
 61             if(lz[i]){
 62                 tmp[i] += abs(lz[i] - c);
 63                 sum[i] += (r[i]-l[i]+1)*(abs(lz[i]-c));
 64                 lz[i] = c;
 65             }
 66             else {
 67                 lz[i] = c;
 68                 for(int j = l[i]; j <= r[i]; j++){
 69                     v[j] += abs(a[j] - c);
 70                     sum[i] += abs(a[j]-c);
 71                     a[j] = c;
 72                 }
 73             }
 74         }
 75     }
 76 }
 77 void Query(int x, int y){
 78     int idx = belong[x], idy = belong[y];
 79     LL ans = 0;
 80     if(idx == idy){
 81         for(int i = x; i <= y; i++)
 82             ans += (tmp[idx] + v[i]);
 83     }
 84     else {
 85         for(int i = x; i <= r[idx]; i++)
 86             ans += (tmp[idx] + v[i]);
 87         for(int i = l[idy]; i <= y; i++)
 88             ans += (tmp[idy] + v[i]);
 89         for(int i = idx+1; i < idy; i++)
 90             ans += sum[i];
 91     }
 92     printf("%lld\n", ans);
 93 }
 94 int main(){
 95     int q, x, y, c, op;
 96     scanf("%d%d", &n, &q);
 97     Build();
 98     while(q--){
 99         scanf("%d%d%d", &op, &x, &y);
100         if(op == 1) {
101             scanf("%d", &c);
102             Update(x,y,c);
103         }
104         else {
105             Query(x,y);
106         }
107 
108     }
109     return 0;
110 }
CF 444 C

猜你喜欢

转载自www.cnblogs.com/MingSD/p/9119191.html