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 ...
随机推荐
- [Python]round四舍五入精度缺失的解决
环境: os: win7 64bit python:2.7.5 32bit 对python四舍五入的解决方案 现象: 一般的四舍五入操作都是使用内置的round方法 In [14]: round ...
- asp.net mvc cooike 购物车 如何实现
先上代码: 1. ShoppingCartService 类 using System; using System.Collections.Generic; using System.Linq; us ...
- 《python源代码剖析》笔记 python中的Dict对象
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.PyDictObject对象 --> C++ STL中的map是基于RB-tre ...
- c++大作业--学籍管理系统--
1.题目描写叙述 学籍管理系统: 依据信息管理系统的业务流程.要求以及所要实现的目标,完毕下面功能: (1)建立学生档案的管理和维护.实现计算机自己主动化管理体制. (2)建立学生成绩管理机制,在计算 ...
- Android 推断当前Activity是不是最后一个Activity 以及 应用或Activity是否存在
推断当前Activity是最后一个Activity: 在Activity的方法中, 有一个方法isTaskRoot()方法, 这种方法能够推断当前Activity是否是最后一个Activity, 假 ...
- Ubuntu Wpa wifi connection
最近做一个项目,需要做一个WIFI连接模块,这几天都在折腾,终于,今天终于是连上网络了,只不过连网的过程有点慢,还有一些缺点,先写下来以备忘记. 1.环境建立: sudo apt-get instal ...
- Eclipse中如何安装和使用GrepCode插件
GrepCode(GC)Eclipse插件允许Eclipse用户在Eclipse IDE中搜索由GrepCode提供的工厂类.本教程介绍如何安装和使用插件.使用Eclipse3.5(Galileo)的 ...
- Mac下Qt连接MySQL 驱动问题
Mac OS X下Qt的mySQL driver编译安装 原创文章,采用CC协议发布,转载请注明: 转载自canX.me 本文链接地址: Mac OS X下Qt的mySQL driver编译安装 – ...
- C#高级编程第1章-.NET体系结构
内容提要: (1)编译和运行面向对象.NET代码 (2)IL/MSIL(Microsoft Intermediate Language)中间语言的优点 (3)值类型与引用类型 (4)数据类型化 (5) ...
- SQL流程控制语句学习(三):while break continue
1.while语法 while 布尔表达式 {sql语句或语句块} break --跳出本层循环 {sql语句或语句块} continue --跳出本次循环 {sql语句或语句块} 2.whi ...