题目链接:http://acm.nankai.edu.cn/p1248.html
题目思路:splay,需要用到up函数。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
#include<stack>
#include<list>
#include<iostream>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
#define M 200000
#define keytree (ch[ch[root][1]][0])
int max(int a,int b)
{
return a>b?a:b;
}
int min(int a,int b)
{
return a<b?a:b;
}
int p[M],ch[M][2],s[M],v[M];
int root,n,m,level,change,top1;
/*void debug(int x)
{
printf("x %d p %d ch0 %d ch1 %d s %d\n",x,p[x],ch[x][0],ch[x][1],s[x]);
if(ch[x][0])
debug(ch[x][0]);
if(ch[x][1])
debug(ch[x][1]);
}*/
void newnode(int &x,int val,int pre)
{
x=++top1;
ch[x][0]=ch[x][1]=0;
s[x]=1;
v[x]=val;
p[x]=pre;
}
void init()
{
top1=0;
p[0]=ch[0][0]=ch[0][1]=s[0]=v[0]=0;
change=0;
newnode(root,-inf,0);
newnode(ch[root][1],inf,root);
s[root]=2;
}
void up(int x)
{
if(!x)return;
int l=ch[x][0],r=ch[x][1];
s[x]=s[l]+1+s[r];
}
void rot(int x,int f)
{
int y=p[x];
ch[y][!f]=ch[x][f];
p[ch[x][f]]=y;
p[x]=p[y];
ch[p[y]][ch[p[y]][1]==y]=x;
p[y]=x;
ch[x][f]=y;
up(y);
}
void splay(int x,int goal)
{
while(p[x]!=goal)
{
if(p[p[x]]==goal) rot(x,ch[p[x]][0]==x);
else
{
int y=p[x],f=ch[p[y]][0]==y;
if(ch[y][f]==x)
{
rot(x,!f);
}
else
rot(y,f);
rot(x,f);
}
}
up(x);
if(goal==0)
root=x;
}
int suc(int val)
{
int x=root;
int tmp;
while(x)
{
if(v[x]==val)
return x;
if(v[x]>val)
tmp=x;
x=ch[x][v[x]<val];
}
return tmp;
}
void insert(int val)
{
int x=root;
while(ch[x][v[x]<val])
{
x=ch[x][v[x]<val];
}
newnode(ch[x][v[x]<val],val,x);
splay(ch[x][v[x]<val],0);
}
void del()
{
splay(1,0);
if(suc(level-change)==1)
return;
splay(suc(level-change),root);
keytree=0;
up(ch[root][1]);
up(root);
}
int search(int k)
{
int x=root;
while(s[ch[x][1]]!=k)
{
if(s[ch[x][1]]>k)
x=ch[x][1];
else
{
k=k-s[ch[x][1]]-1;
x=ch[x][0];
}
}
return v[x];
}
int main()
{
char op[4];
int k;
int num=0;
while(scanf("%d%d",&n,&level)!=EOF)
{
init();
while(n--)
{
scanf("%s%d",op,&k);
if(op[0]=='I')
{
if(k>=level)
{
insert(k-change);
num++;
}
}
if(op[0]=='A')
{
change+=k;
}
if(op[0]=='S')
{
change-=k;
del();
}
if(op[0]=='F')
{
if(s[root]-2<k)
{
printf("-1\n");
continue;
}
printf("%d\n",search(k)+change);
}
}
printf("%d\n",num-s[root]+2);
}
}
分享到:
相关推荐
博客“NKOJ——P1085——网页浏览”的代码
博客“NKOJ——P1925——【平衡树】营业额统计”的代码。
博客“NKOJ——P3725——[基础]合法括号序列”的代码。
博客“NKOJ——P1095——气球游戏”的代码。
博客“NKOJ——P3891——地平线上的城市”的代码。
二分查找 二分思想 NKOJ
博客“NKOJ——P3767——仰望”的代码。
博客“NKOJ——P1914——火车调度”的代码。
博客“NKOJ——P1385——笨笨种西瓜”的代码。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
How To Installinstall node 10.0.0+run npm install on this folderrun npm install pm2 -g to install pm2 and save it globallyrun pm2 start bin/init --name api --watch on this folderrun pm2 monit or pm2 ...
P019,P1021,P1072,P1091,P1718,P1889,P3170,P3171,P3189,P3569,P5109,P5216,P5232,P5663,P6189,P7657,P7712,P8123 可AC
题目大意 有两个数列a,ba,ba,b,定义如下。已知数列aaa有一个性质:对于任意的正整数xxx,存在唯一的整数对(p,q)(p,q)(p,q),满足aq−ap=xa_q-a_p=xaq−ap=x。现在有nnn个询问,每个询问给出一个正整数xix_ixi...