题意:小 M 的实验室有很多电源插排。这些插排的编号从 1 到 N,由左向右排成一排。
每天早晨,这些插排都是没有被使用的。每当一个学生来到实验室,他就将自己的笔记本电源插到某一个未被使用的插排上。实验室的同学们都很奇怪,他们完成这个过程是这样的:首先,他们找到还没有被使用的插排的最长区间。如果有多个区间长度相同,他们就选择最靠右的那个。然后将自己的电源插到该区间的中间。如果区间长度是偶数,他们同样选择靠右的那个。当一个同学离开实验室时,他会将自己的电源拔出来。数据保证每一个同学来到实验室时,至少有一个空的插排。
现在给你实验室同学的来到和离开事件,和一些询问。对于每一个询问,你需要计算在区间 [L,R] 已经有多少个插排被使用了。

数据范围: n<=10^9,Q<=10^5
大力写个splay,每个节点表示一个被使用的插座或一段连续的未使用的插座形成的区间,对区间进行分裂合并.
vijos上T了...明明本机的随机极限数据在0.2s以内...
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
struct node{
node* ch[],*prt;
int typ,l,r;
int sz;//number of black nodes
int Maxlen,Maxpos;
node(){}
node(int _l,int _r){//white interval (include l==r)
l=_l;r=_r;ch[]=ch[]=prt=;
typ=;sz=;Maxlen=r-l+;Maxpos=l;
}
node(int x){//black
l=r=x;sz=;typ=;Maxlen=Maxpos=;ch[]=ch[]=prt=;
}
void update(){
sz=typ;
if(ch[])sz+=ch[]->sz;
if(ch[])sz+=ch[]->sz;
if(typ==){
Maxlen=r-l+;Maxpos=l;
}else{
Maxlen=Maxpos=;
}
if(ch[]){
if(ch[]->Maxlen>Maxlen){
Maxlen=ch[]->Maxlen;Maxpos=ch[]->Maxpos;
}
}
if(ch[]){
if(ch[]->Maxlen>=Maxlen){
Maxlen=ch[]->Maxlen;Maxpos=ch[]->Maxpos;
}
}
}
}t[maxn*];int tot=;
node* newnode(int l,int r){
t[++tot]=node(l,r);return t+tot;
}
node* newnode(int x){
t[++tot]=node(x);return t+tot;
}
node *root,*c0,*c1;
node* findpos(node* rt,int pos){//ordinate assert:pos exist
if(rt->l<=pos&&pos<=rt->r)return rt;
if(pos>rt->r)return findpos(rt->ch[],pos);
if(pos<rt->l)return findpos(rt->ch[],pos);
}
inline int isch1(node* rt){return rt==rt->prt->ch[];}
void rot(node* rt,int t,node* &root){
node* p=rt->prt,*c=rt->ch[t];
if(!p)root=c;
else p->ch[isch1(rt)]=c;
c->prt=p;
rt->ch[t]=c->ch[t^];
if(c->ch[t^])c->ch[t^]->prt=rt;
c->ch[t^]=rt;rt->prt=c;
rt->update();c->update();
}
void splay(node* rt,node* &root){
node *p,*g;
while((p=rt->prt)&&(g=p->prt)){
int t1=isch1(rt),t2=isch1(p);
if(t1==t2){rot(g,t2,root);rot(p,t1,root);}
else {rot(p,t1,root);rot(g,t2,root);}
}
if(p!=){
rot(p,isch1(rt),root);
}
}
void split(int l,int r){//l,r:ordinate
splay(findpos(root,l),root);
c0=root->ch[];
if(c0){
root->ch[]=;c0->prt=;root->update();
}
splay(findpos(root,r),root);
c1=root->ch[];
if(c1){
root->ch[]=;c1->prt=;root->update();
}
}
node* leftmost(node* rt){
if(rt->ch[])return leftmost(rt->ch[]);
else return rt;
}
node* rightmost(node* rt){
if(rt->ch[])return rightmost(rt->ch[]);
else return rt;
}
void merge(){
if(c0){
splay(leftmost(root),root);
root->ch[]=c0;c0->prt=root;root->update();
}
if(c1){
splay(rightmost(root),root);
root->ch[]=c1;c1->prt=root;root->update();
}
}
void query(int l,int r){
split(l,r);
printf("%d\n",root->sz);
merge();
}
void Remove(int pos){ split(pos,pos);root=newnode(pos,pos);
if(c0){
splay(rightmost(c0),c0);
if(c0->typ==){
root->ch[]=c0;c0->prt=root;root->update();c0=;
}else{
c0->r=root->r;c0->prt=;root=c0;root->update();c0=;
}
}
if(c1){
splay(leftmost(c1),c1);
if(c1->typ==){
root->ch[]=c1;c1->prt=root;root->update();c1=;
}else{//printf("!");printf("%d\n",leftmost(root)->l);
c1->l=root->l;c1->ch[]=root->ch[];
if(root->ch[])root->ch[]->prt=c1;
c1->prt=;
root=c1;root->update();c1=;
}
}
}
int Add(){
int pos=root->Maxpos+root->Maxlen/;
split(pos,pos);
if(pos>root->l){
root->ch[]=newnode(root->l,pos-);root->ch[]->prt=root; }
if(pos<root->r){
root->ch[]=newnode(pos+,root->r);root->ch[]->prt=root;
}
root->typ=;root->sz=;root->l=root->r=pos;root->Maxlen=root->Maxpos=;
root->update();
merge();
return pos;
}
int n,q;
int pos[maxn];
int L[maxn],R[maxn],K[maxn],seq[maxn],cnt;
bool cmp(const int &a,const int &b){
return K[a]<K[b];
}
int main(){
scanf("%d%d",&n,&q);
root=newnode(,n);
for(int i=;i<=q;++i){
scanf("%d",&K[i]);
if(K[i]==)scanf("%d%d",&L[i],&R[i]);
else seq[++cnt]=i;
}
sort(seq+,seq+cnt+,cmp);
int old=-,tot=;//discrete to [0,...
for(int i=;i<=cnt;++i){
if(old!=K[seq[i]]){
old=K[seq[i]];++tot;
}
K[seq[i]]=tot;
}
for(int i=;i<=q;++i){
if(K[i]==){
query(L[i],R[i]);
}else{
if(pos[K[i]]==){
pos[K[i]]=Add();
}else{
Remove(pos[K[i]]);pos[K[i]]=;
}
}
}
return ;
}

最新文章

  1. asp.net mvc 权限过滤和单点登录(禁止重复登录)
  2. Iterate Files by Tcltk
  3. 类似 Google Tips 页面的卡片式提示和翻转特效
  4. 联想 thinkpad fn键关闭,优化使用
  5. Q_INIT_RESOURCE宏
  6. 2 - Annotations标注
  7. Connect the Cities(prime)
  8. POJ1469_COURSES(二部图最大匹配)
  9. Adaptive Server Enterprise ODBC driver connection strings
  10. (转)Java并发编程:深入剖析ThreadLocal
  11. J.U.C JMM. pipeline.指令重排序,happen-before
  12. mongodb基本的配置和使用
  13. [CC-MINXOR]XOR Minimization
  14. Django:模板template(二)
  15. centos7 rocketmq 4.2.0
  16. unicode转中文以及str形态的unicode转中文
  17. Python基础一数据类型之数字类型
  18. Linux服务器---关闭selinux
  19. NOIP 华容道
  20. springjdbc使用c3p0连接池报错 java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

热门文章

  1. 武汉Uber优步司机奖励政策(12月28日到1月3日)
  2. Ubuntu adb 报错:no permissions (user in plugdev group; are your udev rules wrong?);
  3. 基于Kafka的服务端用户行为日志采集
  4. 分享一个 UiPath Studio 相关的公众号
  5. mysql5.6 无法远程连接问题解决
  6. Python :编写条件分支代码的技巧
  7. Python汉诺塔问题递归算法与程序
  8. linux学习总结----mongoDB总结
  9. 【第二章】Shell 变量
  10. HDU 1232 并查集板子题