您的位置:首页 > 其它

ZOJ 3765 Lights (伸展树splay)

2014-03-02 21:44 417 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3765

LightsTime Limit: 8 Seconds Memory Limit: 131072 KB
Now you have N lights in a line. Don't worry - the lights don't have color. The only status they have is on and off. And, each light has a value, too.

There is a boring student in ZJU. He decides to do some boring operations to the lights:

Q L R status - Query the GCD (Greatest Common Divisor) of the value of the given status lights in range [L, R]. For example, if now we have 3 lights which are {on, off and on}, and their value are {3, 5, 9}, then the GCD of the number of the lights on in [1, 3] is 3, and the lights off is 5.

I i value status - Add a light just behind to ith light. The initial status and the value is given also.

D i - Remove the ith light.

R i - If ith light is on, turn it off, else turn it on.

M i x - Modify the value of ith light to x.

Please help this boring guy to do this boring thing so that he can have time to find a girlfriend!

Input

The input contains multiple test cases. Notice there's no empty line between each test case.

For each test case, the first line of the a case contains two integers, N (1 ≤ N ≤ 200000) and Q (1 ≤ Q ≤ 100000), indicating the number of the lights at first and the number of the operations. In following N lines, each line contains two integers, Numi (1 ≤ Numi ≤ 1000000000) and Statusi (0 ≤ Statusi ≤ 1), indicating the number of the light i and the status of it. In following Q lines, each line indicating an operation, and the format is described above.

It is guaranteed that the range of the operations will be appropriate. For example, if there is only 10 lights, you will not receive an operation like "Q 1 11 0" or "D 11".

Output

For each Query operation, output an integer, which is the answer to the query. If no lights are with such status, please output -1.

Sample Input

3 12
27 1
32 0
9 1
Q 1 3 1
I 3 64 0
Q 2 4 0
Q 2 4 1
I 2 43 1
D 5
Q 1 2 1
M 1 35
Q 1 2 1
R 1
R 3
Q 1 2 1

Sample Output

9
32
9
27
35
-1


很裸的题目了,用splay维护也很简单,练练手而已。

/* ***********************************************
Author        :kuangbin
Created Time  :2014/3/2 20:02:38
File Name     :E:\2014ACM\比赛\ZOJ_March2014\I.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int MAXN = 300010;
int pre[MAXN],ch[MAXN][2],key[MAXN],size[MAXN];
int root,tot1;
int sum0[MAXN],sum1[MAXN];//该子树对应状态的gcd
int st[MAXN]; //该节点的状态
int s[MAXN],tot2;//内存池和容量
//初始节点的值和状态
int a[MAXN];
int status[MAXN];
int n,q;

long long gcd(long long a,long long b)
{
if(b == 0)return a;
else return gcd(b,a%b);
}
void NewNode(int &r,int father,int k,int _st)
{
if(tot2) r = s[tot2--];
else r = ++tot1;
pre[r] = father;
ch[r][0] = ch[r][1] = 0;
key[r] = k;
st[r] = _st;
if(_st == 0){sum0[r] = k; sum1[r] = 0;}
else {sum1[r] = k; sum0[r] = 0;}
size[r] = 1;
}
void push_up(int r)
{
int lson = ch[r][0], rson = ch[r][1];
size[r] = size[lson] + size[rson] + 1;
sum0[r] = sum1[r] = 0;
sum0[r] = gcd(sum0[lson],sum0[rson]);
sum1[r] = gcd(sum1[lson],sum1[rson]);
if(st[r])sum1[r] = gcd(sum1[r],key[r]);
else sum0[r] = gcd(sum0[r],key[r]);
}
void push_down(int r)
{

}
void Build(int &x,int l,int r,int father)
{
if(l > r)return;
int mid = (l + r)/2;
NewNode(x,father,a[mid],status[mid]);
Build(ch[x][0],l,mid-1,x);
Build(ch[x][1],mid+1,r,x);
push_up(x);
}
void Init()
{
root = tot1 = tot2 = 0;
ch[root][0] = ch[root][1] = size[root] = pre[root] = 0;
sum0[root] = sum1[root] = 0;
NewNode(root,0,0,0);
NewNode(ch[root][1],root,0,0);
for(int i = 0;i < n;i++)
scanf("%d%d",&a[i],&status[i]);
Build(Key_value,0,n-1,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 Get_kth(int r,int k)
{
push_down(r);
int t = size[ch[r][0]] + 1;
if(t == k)return r;
if(t > k)return Get_kth(ch[r][0],k);
else return Get_kth(ch[r][1],k-t);
}
//在第pos个数后面插入一个数
void Insert(int pos,int _val,int _st)
{
Splay(Get_kth(root,pos+1),0);
Splay(Get_kth(root,pos+2),root);
NewNode(Key_value,ch[root][1],_val,_st);
push_up(ch[root][1]);
push_up(root);
}
void erase(int r)
{
if(!r)return;
s[++tot2] = r;
erase(ch[r][0]);
erase(ch[r][1]);
}
//删除第pos个数
void Delete(int pos)
{
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+2),root);
erase(Key_value);
pre[Key_value] = 0;
Key_value = 0;
push_up(ch[root][1]);
push_up(root);
}
//改变第pos个数的状态
void Change(int pos)
{
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+2),root);
st[Key_value] ^= 1;
push_up(Key_value);
push_up(ch[root][1]);
push_up(root);
}
//改变第pos个数的值
void Modify(int pos,int _val)
{
Splay(Get_kth(root,pos),0);
Splay(Get_kth(root,pos+2),root);
key[Key_value] = _val;
push_up(Key_value);
push_up(ch[root][1]);
push_up(root);

}
int Query(int L,int R,int _st)
{
int ans ;
Splay(Get_kth(root,L),0);
Splay(Get_kth(root,R+2),root);
if(_st == 0)ans = sum0[Key_value];
else ans = sum1[Key_value];
if(ans == 0)ans = -1;
return ans;
}

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&q) == 2)
{
Init();
char op[10];
while(q--)
{
scanf("%s",op);
if(op[0] == 'Q')
{
int L,R,_st;
scanf("%d%d%d",&L,&R,&_st);
printf("%d\n",Query(L,R,_st));
}
else if(op[0] == 'I')
{
int pos,val,_st;
scanf("%d%d%d",&pos,&val,&_st);
Insert(pos,val,_st);
}
else if(op[0] == 'D')
{
int pos;
scanf("%d",&pos);
Delete(pos);
}
else if(op[0] == 'R')
{
int pos;
scanf("%d",&pos);
Change(pos);
}
else if(op[0] == 'M')
{
int pos,val;
scanf("%d%d",&pos,&val);
Modify(pos,val);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: