Codeforces Round #254 (Div. 1) C. DZY Loves Colors【序列分块】

题目链接

题意

有一个序列 a [ 1... n ] a[1...n] a[1...n],序列每个点都有一个权值 b b b ,初始 a [ i ] = i , b [ i ] = 0 a[i]=i,b[i]=0 a[i]=ib[i]=0,进行 m m m 次操作,操作有两种

  1. [ X , Y ] [X,Y] [X,Y] 区间每个 a [ i [ a[i[ a[i[ 都变成 Z Z Z,同时 b [ i ] + = a b s ( a [ i ] − Z ) b[i]+=abs(a[i]-Z) b[i]+=abs(a[i]Z)
  2. 查询 [ X , Y ] [X,Y] [X,Y] 区间的权值和

题解

n ≤ 1 0 5 n\le10^5 n105 所以可以考虑分块, t a g [ i ] tag[i] tag[i]标记每一块是否同色, t m p [ i ] tmp[i] tmp[i]表示第 i i i 块中元素增加值

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))

const int mod = 1e9+7;
int qpow(int a,int b,int ans=1){
	do{
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
	}while(b>>=1);
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5+10;
int a[N],B=313,block[N],l[N],r[N];
int tag[N],add[N],sum[N],w[N],tmp[N];
void pd(int b){
	for(int i=l[b];i<=r[b];i++)a[i]=tag[b];
	tag[b]=0;
} 
#define endl '\n'
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n,m; cin>>n>>m;
	for(int i=1;i<=n;i++){
		a[i]=i; block[i]=i/B+1;
		if(!l[block[i]])l[block[i]]=i;
		r[block[i]]=i;
	}
	while(m--){
		int op,x,y,z; cin>>op>>x>>y;
		int xb=block[x],yb=block[y];
		if(op==1){
			cin>>z;
			if(xb==yb){
				if(tag[xb])pd(xb);
				for(int i=x;i<=y;i++){
					sum[xb]+=abs(z-a[i]);
					w[i]+=abs(z-a[i]);
					a[i]=z;
				}
			}
			else{
				if(tag[xb])pd(xb);
				if(tag[yb])pd(yb);
				for(int i=x;i<=r[xb];i++){
					sum[xb]+=abs(z-a[i]);
					w[i]+=abs(z-a[i]);
					a[i]=z;
				}
				for(int i=l[yb];i<=y;i++){
					sum[yb]+=abs(z-a[i]);
					w[i]+=abs(z-a[i]);
					a[i]=z;
				}
				for(int i=xb+1;i<=yb-1;i++){
					if(tag[i]){
						tmp[i]+=abs(z-tag[i]);
						sum[i]+=(r[i]-l[i]+1)*abs(z-tag[i]);
						tag[i]=z;
					}
					else{
						tag[i]=z;
						for(int j=l[i];j<=r[i];j++){
							w[j]+=abs(z-a[j]);
							sum[i]+=abs(z-a[j]);
							a[j]=z;
						} 
					}
				}
			}
		}
		if(op==2){
			int ans=0;
			if(xb==yb){
				for(int i=x;i<=y;i++)ans+=tmp[xb]+w[i];
			}
			else{
				for(int i=x;i<=r[xb];i++)ans+=tmp[xb]+w[i];
				for(int i=l[yb];i<=y;i++)ans+=tmp[yb]+w[i];
				for(int i=xb+1;i<=yb-1;i++)ans+=sum[i];
			}
			cout<<ans<<endl;
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值