BZOJ3249 : [ioi2013]game
线段树套Treap
外层的线段树需要动态开节点
内层Treap需要注意的是,相同y坐标的点不一定是同一个点,所以需要再次离散
空间$O(n\log n)$
时间$O(n\log^2n)$
#include<cstdio>
#include<cstdlib>
#define N 705000
typedef long long ll;
int n,m,q,x1,y1,x2,y2,op,l[N],r[N],tot,R,A,B;ll w,ans,loc,c,d,inf;
inline void read(int&a){char ch;while(!(((ch=getchar())>='0')&&(ch<='9')));a=ch-'0';while(((ch=getchar())>='0')&&(ch<='9'))a*=10,a+=ch-'0';}
ll gcd(ll a,ll b){if(!a&&!b)return 0;if(!a)return b;return b?gcd(b,a%b):a;}
struct node{
int p;ll val,v,sum;node*l,*r;
node(){val=v=sum=p=0;l=r=NULL;}
inline void up(){sum=gcd(v,gcd(l->sum,r->sum));}
}*blank=new(node),*T[N];
inline void Rotatel(node*&x){node*y=x->r;x->r=y->l;x->up();y->l=x;y->up();x=y;}
inline void Rotater(node*&x){node*y=x->l;x->l=y->r;x->up();y->r=x;y->up();x=y;}
void Ins(node*&x){
if(x==blank){
x=new(node);x->val=loc;x->l=x->r=blank;x->v=x->sum=w;x->p=std::rand();
return;
}
if(loc==x->val){x->v=w;x->up();return;}
if(loc<x->val){
Ins(x->l);
if(x->l->p>x->p)Rotater(x);else x->up();
}else{
Ins(x->r);
if(x->r->p>x->p)Rotatel(x);else x->up();
}
}
void Ask(node*&x,ll a,ll b){
if(x==blank)return;
if(c<=a&&b<=d){ans=gcd(ans,x->sum);return;}
if(c<=x->val&&x->val<=d)ans=gcd(ans,x->v);
if(c<x->val)Ask(x->l,a,x->val-1);
if(d>x->val)Ask(x->r,x->val+1,b);
}
void change(int&x,int a,int b){
if(!x)x=++tot,T[x]=blank;
Ins(T[x]);
if(a==b)return;
int mid=(a+b)>>1;
x1<=mid?change(l[x],a,mid):change(r[x],mid+1,b);
}
void ask(int x,int a,int b){
if(!x)return;
if(x1<=a&&b<=x2){Ask(T[x],1,inf);return;}
int mid=(a+b)>>1;
if(x1<=mid)ask(l[x],a,mid);
if(x2>mid)ask(r[x],mid+1,b);
}
int main(){
blank->l=blank->r=blank;
read(n),read(m),read(q);
inf=(ll)(m+1)*n;
while(q--){
read(op),read(x1),read(y1);x1++,y1++;
if(op==1)loc=(ll)y1*n+x1,scanf("%lld",&w),change(R,1,n);
else{
read(x2),read(y2),x2++,y2++;
c=(ll)y1*n+1,d=(ll)(y2+1)*n;
ans=0,ask(R,1,n),printf("%lld\n",ans);
}
}
return 0;
}
BZOJ3249 : [ioi2013]game的更多相关文章
- [IOI2013]Dreaming
link 一道非常类似的题目(link) 试题大意 给你一棵含有$n$个节点的有边权森林,问每次连边将会用$L$的代价,问你若此图通过加边成为树时的最小直径.$n \leq 5\times 10^5$ ...
- bzoj 3246 [Ioi2013]Dreaming 贪心
[Ioi2013]Dreaming Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 638 Solved: 241[Submit][Status][Di ...
- 【IOI2013】【Bzoj3246】Dreaming
http://www.lydsy.com/JudgeOnline/problem.php?id=3246 中文题面 天地之初,世界尚在遥远的梦想之中. Serpent(水蛇)生活的地方有N个水坑,编号 ...
- 【bzoj3246】 Ioi2013—Dreaming
www.lydsy.com/JudgeOnline/problem.php?id=3246 (题目链接) 题意 给出一棵不完全的树,要求在树上连最少的边使得所有点联通,并且使得两点间最大距离最小. S ...
- BZOJ3246 [Ioi2013]Dreaming
Description Serpent(水 蛇)生活的地方有N个水坑,编号为0,...,N - 1,有M条双向小路连接这些水坑.每两个水坑之间至多有一条路径(路径包含一条或多条小路)相互连接,有些水坑 ...
- 洛谷 P5897 - [IOI2013]wombats(决策单调性优化 dp+线段树分块)
题面传送门 首先注意到这次行数与列数不同阶,列数只有 \(200\),而行数高达 \(5000\),因此可以考虑以行为下标建线段树,线段树上每个区间 \([l,r]\) 开一个 \(200\times ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- IOI 2013 袋熊(线段树+分块+决策单调性)
题意 http://www.ioi2013.org/wp-content/uploads/tasks/day1/wombats/Wombats%20zh%20(CHN).pdf 思路 我们设矩形的 ...
- 【BZOJ4456】 [Zjoi2016]旅行者 / 【UOJ #184】 【ZJOI2016】旅行者
Description 小Y来到了一个新的城市旅行.她发现了这个城市的布局是网格状的,也就是有n条从东到西的道路和m条从南到北 的道路,这些道路两两相交形成n×m个路口 (i,j)(1≤i≤n,1≤j ...
随机推荐
- sql注入学习小结
/* 转载请注明出处,By:珍惜少年时 小知识,只是放在博客吃饭时无聊看看,大牛勿喷. */ 珍惜少年时博客,专注网络安全 web渗透测试 00x1爆所有库: mysql> select sch ...
- 前端 解决swiper js 手动滑动一下后不能自动播放
用户操作swiper之后,是否禁止autoplay.默认为true:停止.如果设置为false,用户操作swiper之后自动切换不会停止,每次都会重新启动autoplay.操作包括触碰,拖动,点击pa ...
- 栈应用hanoi
/* 课本p54页*/ #include<stdio.h> #include <iostream> using namespace std; void move(int n, ...
- HDOJ 1590
#include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> u ...
- 在CI中集成phpmailer,方便使用SMTP发送邮件
直接使用phpmailer的话,有时候不是很方便,特别你的很多功能都是基于CI完成的时候,要相互依赖就不方便了,所以在想,那是否可以将phpmailer集成到CI中呢,像使用email类这样使用他,功 ...
- ubuntu14.04中国源
deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://cn.ar ...
- 【云计算】Docker云平台—Docker基础
Docker云平台系列共三讲,此为第一讲:Docker基础 参考资料: Docker官方文档:https://docs.docker.com/ Docker从入门到实践:https://yeasy.g ...
- C++使用throw抛出异常
引用: c++ 使用throw抛出异常 抛出异常(也称为抛弃异常)即检测是否产生异常,在C++中,其采用throw语句来实现,如果检测到产生异常,则抛出异常.该语句的格式为:throw 表达式; ...
- 在Python脚本中判断Python的版本
引自:http://segmentfault.com/q/1010000000127878 如果是给人读,用 sys.version,如果是给机器比较,用 sys.version_info,如果是判断 ...
- windows下批量删除文件
FORFILES /P d:\www /D -7 /S /M ex*.log /C "cmd /c del @path" 删除d:\www目录下7天前ex*.log的所有文件 例子 ...