bzoj 4923: K小值查询 splay

来源:互联网 发布:php继承 编辑:程序博客网 时间:2024/06/06 03:42

题意

维护一个长度为n的正整数序列a_1,a_2,…,a_n,支持以下两种操作:
1 k,将序列a从小到大排序,输出a_k的值。
2 k,将所有严格大于k的数a_i减去k。
n,m<=100000,ai.k<=1e9

分析

咋一看还以为是什么玄学的分块题。。。

题解挺巧妙的说。
对于2操作,[1,k]内的数不改变,[2k+1,inf]内的数的顺序也是不改变的,[k+1,2k]内的数至少减少一半,所以我们可以用一棵splay来维护这个序列。因为每个数最多顺序被改变log次,所以只要每次暴力把[k+1,2k]内的数重新插入,[2k+1,inf]内的数只要打标记就好了。

一开始T了是因为在某些操作中忘了splay。。。

代码

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<stack>using namespace std;typedef long long LL;const int N=100005;int n,m,root;LL a[N];struct tree{int fa,l,r,s,tag;LL val;}t[N];stack<int> sta;int read(){    int x=0,f=1;char ch=getchar();    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    return x*f;}void pushdown(int x){    if (!t[x].tag) return;    int w=t[x].tag;t[x].tag=0;    if (t[x].l) t[t[x].l].tag+=w,t[t[x].l].val-=w;    if (t[x].r) t[t[x].r].tag+=w,t[t[x].r].val-=w;}void remove(int x){    while (x) sta.push(x),x=t[x].fa;    while (!sta.empty()) x=sta.top(),sta.pop(),pushdown(x);}void updata(int x){    t[x].s=t[t[x].l].s+t[t[x].r].s+1;}int build(int l,int r){    int mid=(l+r)/2;    t[mid].val=a[mid];    if (l<mid) t[mid].l=build(l,mid-1),t[t[mid].l].fa=mid;    if (r>mid) t[mid].r=build(mid+1,r),t[t[mid].r].fa=mid;    updata(mid);    return mid;}void rttr(int x){    int y=t[x].l;    if (x==root) root=y;    t[x].l=t[y].r;t[t[y].r].fa=x;    if (x==t[t[x].fa].l) t[t[x].fa].l=y;    else if (x==t[t[x].fa].r) t[t[x].fa].r=y;    t[y].fa=t[x].fa;    t[y].r=x;t[x].fa=y;    updata(x);updata(y);}void rttl(int x){    int y=t[x].r;    if (x==root) root=y;    t[x].r=t[y].l;t[t[y].l].fa=x;    if (x==t[t[x].fa].l) t[t[x].fa].l=y;    else if (x==t[t[x].fa].r) t[t[x].fa].r=y;    t[y].fa=t[x].fa;    t[y].l=x;t[x].fa=y;    updata(x);updata(y);}void splay(int x,int y){    remove(x);    while (t[x].fa!=y)    {        int p=t[x].fa,g=t[p].fa;        if (g==y)        {            if (x==t[p].l) rttr(p);            else rttl(p);            break;        }        if (x==t[p].l)            if (p==t[g].l) rttr(g),rttr(p);            else rttr(p),rttl(g);        else            if (p==t[g].r) rttl(g),rttl(p);            else rttl(p),rttr(g);    }}int kth(int k){    int x=root;    while (x)    {        pushdown(x);        if (t[t[x].l].s==k-1)        {            splay(x,0);            return t[x].val;        }        else if (t[t[x].l].s>k-1) x=t[x].l;        else k-=t[t[x].l].s+1,x=t[x].r;    }}void ins(int d,int x){    pushdown(d);    if (t[x].val<=t[d].val)        if (!t[d].l) t[d].l=x,t[x].fa=d,t[x].s=1,splay(x,0);        else ins(t[d].l,x);    else        if (!t[d].r) t[d].r=x,t[x].fa=d,t[x].s=1,splay(x,0);        else ins(t[d].r,x);}int lower(int k){    int x=root,y;    while (x)    {        pushdown(x);        if (t[x].val<k) y=x,x=t[x].r;        else x=t[x].l;    }    return y;}int upper(int k){    int x=root,y;    while (x)    {        pushdown(x);        if (t[x].val>k) y=x,x=t[x].l;        else x=t[x].r;    }    return y;}void rebuild(int x,int k){    pushdown(x);    if (t[x].l) rebuild(t[x].l,k);    if (t[x].r) rebuild(t[x].r,k);    t[x].l=t[x].r=t[x].fa=0;t[x].val-=k;    ins(root,x);}void solve(int k){    int l=lower(k+1),r=upper(k*2);    splay(l,0);splay(r,l);    int x=t[r].l;t[r].l=0;t[r].tag+=k;t[r].val-=k;    if (x) rebuild(x,k);}int main(){    n=read();m=read();    for (int i=1;i<=n;i++) a[i]=read();    a[n+1]=0;a[n+2]=1e15;    sort(a+1,a+n+3);    root=build(1,n+2);    while (m--)    {        int op=read(),k=read();        if (op==1) printf("%d\n",kth(k+1));        else solve(k);    }    return 0;}
原创粉丝点击