POJ-3468 A Simple Problem with Integers(Splay实现)

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 88130   Accepted: 27382
Case Time Limit: 2000MS

Description

You have N integers, A1, A2, ... ,AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1,A2, ... ,AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... ,Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly--2007.11.25, Yang Yi

splay模板。
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAXN 100010
#define Key_value ch[ch[root][1]][0]
using namespace std;
int pre[MAXN],ch[MAXN][2],root,tot,n,q;
int size[MAXN],add[MAXN],key[MAXN],a[MAXN];
long long sum[MAXN];
void NewNode(int &r,int father,int k)
{
	r = ++tot;
	pre[r] = father;
	size[r] = 1;
	add[r] = 0;
	key[r] = sum[r] = k;
	ch[r][0] = ch[r][1] = 0;
}
void Update_Add(int r,int ADD)
{
	if(r == 0) return;
	add[r] += ADD;
	key[r] += ADD;
	sum[r] += 1ll*ADD*size[r]; 
}
void push_up(int r)
{
	size[r] = size[ch[r][0]] + size[ch[r][1]] + 1;
	sum[r] = sum[ch[r][0]] + sum[ch[r][1]] + key[r];
}
void push_down(int r)
{
	if(add[r])
	{
		Update_Add(ch[r][0],add[r]);
		Update_Add(ch[r][1],add[r]);
		add[r] = 0;
	}
}
void Build(int &x,int l,int r,int father)
{
	if(l > r) return;
	int mid = (l+r) >> 1;
	NewNode(x,father,a[mid]);
	Build(ch[x][0],l,mid-1,x);
	Build(ch[x][1],mid+1,r,x);
	push_up(x);
}
void Init()
{
	root = tot = 0;
	NewNode(root,0,-1);
	NewNode(ch[root][1],root,-1);
	for(int i = 1;i <= n;i++)
	 scanf("%d",&a[i]);
	Build(Key_value,1,n,ch[root][1]);
	push_up(ch[root][1]);
	push_up(root);
}
void Rotate(int x,int kind)
{
	int y = pre[x];
	push_down(y);
	push_down(x);
	ch[y][!kind] = ch[x][kind];
	pre[ch[x][kind]] = y;
	if(pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x;
	pre[x] = pre[y];
	ch[x][kind] = y;
	pre[y] = x;
	push_up(y);
}
void Splay(int r,int goal)
{
	push_down(r);
	while(pre[r] != goal)
	{
		if(pre[pre[r]] == goal) Rotate(r,ch[pre[r]][0] == r);
		else
		{
			int y = pre[r];
			int kind = ch[pre[y]][0] == y;
			if(ch[y][kind] == r)
			{
				 Rotate(r,!kind);
				 Rotate(r,kind);
			}
			else
			{
				Rotate(y,kind);
				Rotate(r,kind);
			}
		}
	}
	push_up(r);
	if(goal == 0) root = r;
}
int Find(int r,int k)
{
	push_down(r);
	int t = size[ch[r][0]] + 1;
	if(t == k) return r;
	if(t > k) return Find(ch[r][0],k);
	else return Find(ch[r][1],k-t);
}
void ADD(int l,int r,int D)
{
	Splay(Find(root,l),0);
	Splay(Find(root,r+2),root);
	Update_Add(Key_value,D);
	push_up(ch[root][1]);
	push_up(root);
}
long long Query_sum(int l,int r)
{
	Splay(Find(root,l),0);
	Splay(Find(root,r+2),root);
	return sum[Key_value];
}
int main()
{
	scanf("%d%d",&n,&q);
	Init();
	for(int i = 1;i <= q;i++)
	{
		char op;
		int x,y,z;
		scanf("%c%c",&op,&op);
		if(op == 'Q')
		{
			scanf("%d%d",&x,&y);
			printf("%I64d\n",Query_sum(x,y));
		}
		else
		{
			scanf("%d%d%d",&x,&y,&z);
			ADD(x,y,z);
		}
	}
}


你可能感兴趣的:(ACM,水题,伸展树)