Lights


Time 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:

  1. L R status - Query the GCD (Greatest Common Divisor) of the value of the given status lights in range [LR]. 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.
  2. i value status - Add a light just behind to ith light. The initial status and the value is given also.
  3. i - Remove the ith light.
  4. i - If ith light is on, turn it off, else turn it on.
  5. 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大法,熟练了之后 这种题就算是裸题了。。。 基本上都会用到 旋转 rotate, 伸展splay,获取第k的元素的位置 Get_kth,翻转reverse,删除delete函数。

这题因为数组开小TLE一上午,昨天晚上明明就已经是正确解法了,害我一直调试。

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 3e5+;
int siz[maxn],pre[maxn],ch[maxn][],st[maxn],s[maxn];
int key[maxn],GCD[][maxn];
int tot1,tot2,root,n,q;
int gcd(int x,int y)
{
return y == ? x : gcd (y,x % y);
}
void NewNode(int &r,int father,int k,int status)
{
if (tot2)
r = s[tot2--];
else
r = ++tot1;
pre[r] = father;
ch[r][] = ch[r][] = ;
st[r] = status;
key[r] = k;
siz[r] = ;
GCD[][r] = GCD[][r] = ;
GCD[st[r]][r] = k;
}
void push_up(int r)
{
siz[r] = siz[ch[r][]] + siz[ch[r][]] + ;
if (GCD[st[r]][r] == )
{
GCD[st[r]][r] = key[r];
}
GCD[][r] = gcd(GCD[][ch[r][]],GCD[][ch[r][]]);
GCD[][r] = gcd(GCD[][ch[r][]],GCD[][ch[r][]]);
GCD[st[r]][r] = gcd(GCD[st[r]][r],key[r]);
}
int A[maxn];
int B[maxn]; // A B 数组分别储存每个light的值以及状态
void build(int &x,int l,int r,int father)
{
if (l > r)
return;
int mid = (l + r) >> ;
NewNode(x,father,A[mid],B[mid]);
build(ch[x][],l,mid-,x);
build(ch[x][],mid+,r,x);
push_up(x);
}
void init()
{
root = tot1 = tot2 = ;
for (int i = ; i <= n; i++)
scanf ("%d%d",A+i,B+i);
NewNode(root,,,);
NewNode(ch[root][],root,,);
build(ch[ch[root][]][],,n,ch[root][]);
push_up(ch[root][]);
push_up(root);
}
void Rotate(int x,int kind)
{
int y = pre[x];
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if (pre[y])
ch[pre[y]][ch[pre[y]][] == y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
}
void Splay(int r,int goal)
{
while (pre[r] != goal)
{
if (pre[pre[r]] == goal)
{
Rotate(r,ch[pre[r]][] == r);
}
else
{
int y = pre[r];
int kind = (ch[pre[y]][] == y);
if (ch[y][kind] == r)
{
Rotate(y,!kind);
Rotate(r,!kind);
}
else
{
Rotate(r,kind);
Rotate(r,!kind);
}
}
}
push_up(r);
if (goal == )
root = r;
}
int Get_kth(int r,int k)
{
int t = siz[ch[r][]] + ;
if (k == t)
return r;
if (k > t)
return Get_kth(ch[r][],k-t);
else
return Get_kth(ch[r][],k);
}
void Insert(int pos,int val,int status)
{
Splay(Get_kth(root,pos+),);
Splay(Get_kth(root,pos+),root);
NewNode(ch[ch[root][]][],ch[root][],val,status);
push_up(ch[root][]);
push_up(root);
}
void eraser(int r)
{
if (!r)
return;
s[++tot2] = r;
eraser(ch[r][]);
eraser(ch[r][]);
}
void Delete(int pos)
{
Splay(Get_kth(root,pos),);
Splay(Get_kth(root,pos+),root);
eraser(ch[ch[root][]][]);
pre[ch[ch[root][]][]] = ;
ch[ch[root][]][] = ;
push_up(ch[root][]);
push_up(root);
}
void modify (int pos,int val)
{
Splay(Get_kth(root,pos),);
Splay(Get_kth(root,pos+),root);
int key_value = ch[ch[root][]][];
key[key_value] = val;
push_up(ch[ch[root][]][]);
push_up(ch[root][]);
push_up(root);
}
void reset(int pos)
{
Splay(Get_kth(root,pos),);
Splay(Get_kth(root,pos+),root);
int key_value = ch[ch[root][]][];
st[key_value] ^= ;
push_up(ch[ch[root][]][]);
push_up(ch[root][]);
push_up(root);
}
int query(int x,int y,int status)
{
Splay(Get_kth(root,x),);
Splay(Get_kth(root,y+),root);
push_up(ch[ch[root][]][]);
return GCD[status][ch[ch[root][]][]];
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif while (~scanf ("%d%d",&n,&q))
{
init();
for (int i = ; i < q; i++)
{
char op[];
scanf ("%s",op);
if (op[] == 'Q')
{
int x,y,z;
scanf ("%d%d%d",&x,&y,&z);
int ans = query(x,y,z);
printf("%d\n",ans > ? ans : -);
}
if (op[] == 'I')
{
int x,z,y;
scanf ("%d%d%d",&x,&y,&z);
Insert(x,y,z);
}
if (op[] == 'D')
{
int x;
scanf ("%d",&x);
Delete(x);
}
if (op[] == 'M')
{
int x, z;
scanf ("%d%d",&x,&z);
modify(x,z);
}
if (op[] == 'R')
{
int x;
scanf ("%d",&x);
reset(x);
}
}
}
return ;
}

ZOJ3765---Lights (Splay伸展树)的更多相关文章

  1. Splay伸展树学习笔记

    Splay伸展树 有篇Splay入门必看文章 —— CSDN链接 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 创造者:Daniel Sleator 和 Robert Ta ...

  2. 【学时总结】◆学时·VI◆ SPLAY伸展树

    ◆学时·VI◆ SPLAY伸展树 平衡树之多,学之不尽也…… ◇算法概述 二叉排序树的一种,自动平衡,由 Tarjan 提出并实现.得名于特有的 Splay 操作. Splay操作:将节点u通过单旋. ...

  3. Splay 伸展树

    废话不说,有篇论文可供参考:杨思雨:<伸展树的基本操作与应用> Splay的好处可以快速分裂和合并. ===============================14.07.26更新== ...

  4. [Splay伸展树]splay树入门级教程

    首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...

  5. Splay伸展树入门(单点操作,区间维护)附例题模板

    Pps:终于学会了伸展树的区间操作,做一个完整的总结,总结一下自己的伸展树的单点操作和区间维护,顺便给未来的自己总结复习用. splay是一种平衡树,[平均]操作复杂度O(nlogn).首先平衡树先是 ...

  6. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

  7. UVA 11922 Permutation Transformer —— splay伸展树

    题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...

  8. [算法] 数据结构 splay(伸展树)解析

    前言 splay学了已经很久了,只不过一直没有总结,鸽了好久来写一篇总结. 先介绍 splay:亦称伸展树,为二叉搜索树的一种,部分操作能在 \(O( \log n)\) 内完成,如插入.查找.删除. ...

  9. ZOJ3765 Lights Splay树

    非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...

  10. ZOJ 3765 Lights (伸展树splay)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3765 Lights Time Limit: 8 Seconds ...

随机推荐

  1. iOS - 排序的队列中插入数值

    http://stackoverflow.com/questions/8180115/nsmutablearray-add-object-with-order 用Selector http://sta ...

  2. 【转】 基于TFTP协议的远程升级设计

    版权声明:本文为博主原创文章,未经博主允许不得转载.联系邮箱:zhzhchang@126.com 说明:由于CSDN博客编辑器对word格式近乎不支持,因此对表格使用了图片方式(最后一个表格未使用图片 ...

  3. hdu 1331 Function Run Fun

    Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...

  4. 关于IE7 兼容问题

    关于a标签的写法(目前测试只针对IE7,IE8及谷歌浏览器): <a  onclick = 方法名(参数);></a>  此写法在 IE8以上及谷歌浏览器使用都没有问题,但在I ...

  5. IOS-连接

    http://blog.sina.com.cn/s/articlelist_2659739627_0_2.html

  6. 苹果推送APNS总结 (

    开发状态服务器地址 gateway.sandbox.push.apple.com 2195产品状态服务器地址 gateway.push.apple.com         2195 Developme ...

  7. php增删改查,自己写的demo

    1.链接数据库通用方法:conn.php <?php //第一步:链接数据库 $conn=@mysql_connect("localhost:3306","root ...

  8. java开发webservice的几种方式

    webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2 Axis是apache下一个开源的webservice开发组件 ...

  9. 用Ant实现Java项目的自动构建和部署(转)

    Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作. 一.            ...

  10. js中substring/substr和C#中Substring的用法

    一:在js中截取字符串的方法有两个:substring和substr 1.方法: substring(int stringIndex,[int endIndex]) 截取从索引为stringIndex ...