PKU A Simple Problem with Integers

/*
sum[x] x为根的和
add[x] x的子树的懒的标记
val[x] x节点的值
  */

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define KeyTree (ch[ch[root][1]][0])
using namespace std;
const int maxn =300005;
typedef __int64 lld;

lld v[maxn];

struct SplayTree {
	int tot,root;
	int pre[maxn];
	int size[maxn];
	int ch[maxn][2];
	inline void chear() {
		pre[0] =ch[0][0] =ch[0][1] =0;
		size[0] =0;
		root =0;
		tot =0 ;
	}
	inline void Rotate(int x, int c) {	
		int y = pre[x];
		push_down(y);	push_down(x);
		ch[y][!c] = ch[x][c];
		if ( ch[x][c] )	pre[ ch[x][c] ] = y;
		pre[x] = pre[y];
		if ( pre[y] )	ch[pre[y]][ ch[pre[y]][1] == y ] = x;
		ch[x][c] = y;
		pre[y] = x;
		push_up(y);
	}
	inline void Splay(int x, int f) {
		push_down(x);
		while ( pre[x] != f ) {
			int y = pre[x], z = pre[y];
			push_down(z);	push_down(y);	push_down(x);
			if ( pre[ pre[x] ] == f ) {
				Rotate(x, ch[ pre[x] ][0] == x);
			}
			else {
				if ( ch[z][0] == y ) {
					if ( ch[y][0] == x ) {
						Rotate(y, 1), Rotate(x, 1);
					}
					else {
						Rotate(x, 0), Rotate(x, 1);
					}
				}
				else {
					if ( ch[y][0] == x ) {
						Rotate(x, 1), Rotate(x, 0);
					}
					else {
						Rotate(y, 0), Rotate(x, 0);
					}
				}
			}
		} 
		push_up(x);
		if ( f == 0 )	root = x;
	}
	inline void Select(int k, int f) {	
		int x = root;
		while ( 1 ) {
			push_down(x);
			if ( k == size[ ch[x][0] ] + 1 ) 
				break;
			if ( k <= size[ ch[x][0] ] ) 
				x = ch[x][0];
			else {
				k -= (size[ ch[x][0] ] + 1);
				x = ch[x][1];
			}
		}
		Splay(x, f);
	}
	inline void push_down(int x){
		if(add[x]){
			add[ch[x][0]]+=add[x];
			add[ch[x][1]]+=add[x];
			val[ch[x][0]]+=add[x];
			val[ch[x][1]]+=add[x];
			sum[ch[x][0]]+=add[x]*size[ch[x][0]];
			sum[ch[x][1]]+=add[x]*size[ch[x][1]];
			add[x]=0;
		}
	}
	inline void push_up(int x){
		sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+val[x];
		size[x]= 1+ size[ch[x][0]]+size[ch[x][1]];
	}
	inline void NewNode(int &x ,int f,int c){
		x = ++tot;
		pre[x]=f;
		ch[x][0]=ch[x][1]=0;
		val[x]=sum[x]=v[c];
		add[x]=0;
		size[x]=1;
	}
	void MakeTree(int &x ,int l,int r,int f){
		if(l>r)return ;
		int mid=(l+r)>>1;
		NewNode(x,f,mid);
		MakeTree(ch[x][0],l,mid-1,x);
		MakeTree(ch[x][1],mid+1,r,x);
		push_up(x);
	}
	inline void init(int n){
		chear();
		NewNode(root,0,0);
		NewNode(ch[root][1],root,0);
		MakeTree(KeyTree,1,n,ch[root][1]);
		push_up(ch[root][1]);
		push_up(root);
	}
	lld query(lld a,lld b){
		Select(a-1+1,0);
		Select(b+1+1,root);
		return sum[KeyTree];
	}
	void update(lld a,lld b,lld c){
		Select(a-1+1,0);
		Select(b+1+1,root);
		sum[KeyTree]+=c*size[KeyTree];
		val[KeyTree]+=c;
		add[KeyTree]+=c;
	}
	lld val[maxn];
	lld sum[maxn];
	lld add[maxn];
}spt;

int main(){
	int n,m;
	int i,j;
	lld a,b,c;
	char cmd[5];
	while(scanf("%d%d",&n,&m)!=EOF){
		for(i=1;i<=n;i++){
			scanf("%I64d",&v[i]);
		}
		spt.init(n);
		for(i=1;i<=m;i++){
			scanf("%s%I64d%I64d",cmd,&a,&b);
			if(cmd[0]=='Q'){
				printf("%I64d\n",spt.query(a,b));
			}else{
				scanf("%I64d",&c);
				spt.update(a,b,c);
			}
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值