ZOJ3765---Lights (Splay伸展树)
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:
- 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大法,熟练了之后 这种题就算是裸题了。。。 基本上都会用到 旋转 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伸展树)的更多相关文章
- Splay伸展树学习笔记
Splay伸展树 有篇Splay入门必看文章 —— CSDN链接 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 创造者:Daniel Sleator 和 Robert Ta ...
- 【学时总结】◆学时·VI◆ SPLAY伸展树
◆学时·VI◆ SPLAY伸展树 平衡树之多,学之不尽也…… ◇算法概述 二叉排序树的一种,自动平衡,由 Tarjan 提出并实现.得名于特有的 Splay 操作. Splay操作:将节点u通过单旋. ...
- Splay 伸展树
废话不说,有篇论文可供参考:杨思雨:<伸展树的基本操作与应用> Splay的好处可以快速分裂和合并. ===============================14.07.26更新== ...
- [Splay伸展树]splay树入门级教程
首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...
- Splay伸展树入门(单点操作,区间维护)附例题模板
Pps:终于学会了伸展树的区间操作,做一个完整的总结,总结一下自己的伸展树的单点操作和区间维护,顺便给未来的自己总结复习用. splay是一种平衡树,[平均]操作复杂度O(nlogn).首先平衡树先是 ...
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
- UVA 11922 Permutation Transformer —— splay伸展树
题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...
- [算法] 数据结构 splay(伸展树)解析
前言 splay学了已经很久了,只不过一直没有总结,鸽了好久来写一篇总结. 先介绍 splay:亦称伸展树,为二叉搜索树的一种,部分操作能在 \(O( \log n)\) 内完成,如插入.查找.删除. ...
- ZOJ3765 Lights Splay树
非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...
- ZOJ 3765 Lights (伸展树splay)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3765 Lights Time Limit: 8 Seconds ...
随机推荐
- SEO_Alexa排名
1.Alexa排名是评价某一网站访问量的一个指标,对SEO优化没有任何作用,这个数据是可以做假的,考虑Alexa排名纯粹是在浪费时间. 2.Alexa排名是针对已经在浏览器中安装了Alexa Tool ...
- This manual page is part of Xcode Tools version 5.0
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xcodebuild.1.ht ...
- Nyoj 43 24 Point game 【DFS】
24 Point game 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 There is a game which is called 24 Point game ...
- dojo 学习笔记之dojo.query - query(id) 与query(class)的差别
考虑这个样例:动态创建一个页面的时候,用new listtem()生成多个listitem, 且每一个listitem中都生成一个按钮button. 假设想要给每一个按钮都绑定一个click事件,用d ...
- Redis Install
Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部分场合可以对关系数据库起到很好的补充作用.它提供了Pytho ...
- 借助共享缓存redis实现分布式锁
新开发的系统须要控制每一个时刻回收缓存的GC线程有且仅仅有一个在执行,假设有多个线程同一时候执行,会造成系统崩溃.假设仅仅有一个JVM进程那么非常好办.简单的借助synchronizedkeyword ...
- (第三章)Java内存模型(下)
一.happens-before happens-before是JMM最核心的概念.对于Java程序员来说,理解happens-before是理解JMM的关键. 1.1 JMM的设计 从JMM设计者的 ...
- 学习okhttp wiki--HTTPS
HTTPS OkHttp尝试平衡两个相互竞争的要素: 连通性(Connectivity):连接到尽可能多的服务器.这包括运行最新版本 boringssl 的服务器和不太过时的老版本 OpenSSL 的 ...
- c - 每位数字尾部加空格
/* input:一个4位整数. output:每位整数后紧跟一个空格的字符串. */ char * insert(char *s) { int len = strlen(s); * len + ); ...
- root用户安装的软件在普通用户不生效
root安装完成后,有时候需要进入普通用户,修改以下文件才能生效: .在这个文件中添加环境变量 vim ~/.bash_profile .生效 source ~/.bash_profile