1058: [ZJOI2007]报表统计
Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 2005 Solved: 705
[ Submit][ Status]
Description
小Q的妈妈是一个出纳,经常需要做一些统计报表的工作。今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一。经过仔细观察,小Q发现统计一张报表实际上是维护一个可能为负数的整数数列,并且进行一些查询操作。在最开始的时候,有一个长度为N的整数序列,并且有以下三种操作: INSERT i k 在原数列的第i个元素后面添加一个新元素k; 如果原数列的第i个元素已经添加了若干元素,则添加在这些元素的最后(见下面的例子) MIN_GAP 查询相邻两个元素的之间差值(绝对值)的最小值 MIN_SORT_GAP 查询所有元素中最接近的两个元素的差值(绝对值) 例如一开始的序列为 5 3 1 执行操作INSERT 2 9将得到: 5 3 9 1 此时MIN_GAP为2,MIN_SORT_GAP为2。 再执行操作INSERT 2 6将得到: 5 3 9 6 1 注意这个时候原序列的第2个元素后面已经添加了一个9,此时添加的6应加在9的后面。这个时候MIN_GAP为2,MIN_SORT_GAP为1。于是小Q写了一个程序,使得程序可以自动完成这些操作,但是他发现对于一些大的报表他的程序运行得很慢,你能帮助他改进程序么?
Input
第一行包含两个整数N,M,分别表示原数列的长度以及操作的次数。第二行为N个整数,为初始序列。接下来的M行每行一个操作,即“INSERT i k”,“MIN_GAP”,“MIN_SORT_GAP”中的一种(无多余空格或者空行)。
Output
对于每一个“MIN_GAP”和“MIN_SORT_GAP”命令,输出一行答案即可。
Sample Input
5 3 1
INSERT 2 9
MIN_SORT_GAP
INSERT 2 6
MIN_GAP
MIN_SORT_GAP
Sample Output
2
1
HINT
对于100%的数据,N , M ≤500000 对于所有的数据,序列内的整数不超过5*10^8。
思路:用set维护MIN_SORT_GAP查询,用splay维护MIN_GAP,特别注意一下pushup操作,其他的都一样
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int maxn=1000010;
const int INF=1000000000;
int N,M;
int a[maxn];
int tree[maxn];
set<int> sp;
int ch[maxn][2],pre[maxn],size[maxn],minv[maxn],key[maxn];
int root,tot1,tot2,s[maxn];
int lval[maxn],rval[maxn];
int min_sort_gap;
void add(int x,int val)
{
while(x<=N)
{
tree[x]+=val;
x+=x&(-x);
}
}
int getsum(int x)
{
int sum=0;
while(x)
{
sum+=tree[x];
x-=(x&(-x));
}
return sum;
}
void NewNode(int &r,int f,int val)
{
if(tot2)r=s[tot2--];
else r=++tot1;
ch[r][0]=ch[r][1]=0;
key[r]=val;
pre[r]=f;
rval[r]=lval[r]=val;
minv[r]=INF;
size[r]=1;
}
void pushup(int r)
{
if(!r)return;
size[r]=size[ch[r][0]]+size[ch[r][1]]+1;
lval[r]=lval[ch[r][0]];
rval[r]=rval[ch[r][1]];
minv[r]=INF;
//下面的更新条件一定要注意,判断是不是两个-1节点
if(ch[r][0]&&ch[r][0]!=1&&ch[r][0]!=2)
{
minv[r]=min(abs(key[r]-rval[ch[r][0]]),minv[ch[r][0]]);
lval[r]=lval[ch[r][0]];
}
else lval[r]=key[r];
if(ch[r][1]&&ch[r][1]!=1&&ch[r][1]!=2)
{
minv[r]=min(minv[r],min(abs(key[r]-lval[ch[r][1]]),minv[ch[r][1]]));
rval[r]=rval[ch[r][1]];
}
else rval[r]=key[r];
}
void init()
{
root=tot1=tot2=0;
ch[root][0]=ch[root][1]=pre[root]=minv[root]=size[root]=0;
NewNode(root,0,-1);
NewNode(ch[root][1],root,-1);
pushup(ch[root][1]);
pushup(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]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
pushup(y);
}
void Splay(int r,int goal)
{
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
{
Rotate(r,ch[pre[r]][0]==r);
}
else
{
int y=pre[r];
int kind=(ch[pre[y]][0]==y);
if(ch[y][kind]==r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
pushup(r);
if(goal==0)root=r;
}
int get_kth(int r,int k)
{
int t=size[ch[r][0]]+1;
if(t==k)return r;
if(t>k)return get_kth(ch[r][0],k);
else return get_kth(ch[r][1],k-t);
}
void debug(int r)
{
if(!r)return;
printf("%d**%d**%d**%d***%d\n",key[r],r,ch[r][0],ch[r][1],pre[r]);
debug(ch[r][0]);
debug(ch[r][1]);
}
void process(int y)
{
if(min_sort_gap==0)return ;
set<int>::iterator it;
it=sp.lower_bound(y);
if(it!=sp.end())
if(abs(*it-y)<min_sort_gap)
min_sort_gap=abs(*it-y);
if(it!=sp.begin())
{
it--;
if(abs(*it-y)<min_sort_gap)
min_sort_gap=abs(*it-y);
}
sp.insert(y);
}
void Insert(int x,int y)
{
int pos=getsum(x);
Splay(get_kth(root,pos+1),0);
Splay(get_kth(root,pos+2),root);
NewNode(Key_value,ch[root][1],y);
pushup(ch[root][1]);
pushup(root);
process(y);
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
sp.clear();
memset(tree,0,sizeof(tree));
min_sort_gap=INF;
init();
for(int i=1;i<=N;i++)
{
scanf("%d",&a[i]);
Insert(i,a[i]);
add(i,1);
}
char op[20];
int x,y;
while(M--)
{
scanf("%s",op);
if(!strcmp(op,"INSERT"))
{
scanf("%d%d",&x,&y);
Insert(x,y);
add(x,1);
}
else if(!strcmp(op,"MIN_SORT_GAP"))
printf("%d\n",min_sort_gap);
else printf("%d\n",minv[root]);
}
}
return 0;
}