题目为BZOJ1901.
单点修改区间第k大,如果卡内存你要怎么办:
HQ说线段树套平衡树比树状数组套线段树好得多……
所以我就写了……
然后就写了(n ^ log^3n),在Zju上T掉了QAQ
卡内存还卡时间真是有够过分……
其实就是外层线段树存一下根然后按照正常的Treap写就好了,但是有一个问题就是Treap的删点……这个东西不要写挂就好了……
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cassert>
#define Rep(i,n) for(int i = 1; i <= n ; i ++)
#define RepG(i,x) for(int i = head[x] ;~ i ; i = edge[i].next)
#define Rep_0(i,n) for(int i = 0 ; i < n ; i ++)
#define RD(i,x,n) for(int i = x; i <= n ; i ++)
#define CLR(a,b) memset(a,b,sizeof(a))
#define u t[x]
#define o t[y]
#define lc ch[0]
#define rc ch[1]
#define tc ch[ty]
#define vc ch[!ty]
#define ulfc t[u.lc]
#define urtc t[u.rc]
using namespace std;
const int inf = (int)1e+9;
typedef long long ll;
int read(){
char ch = getchar();
while((ch < '0' || ch > '9') && ch != '-')ch = getchar ();
int x = 0;
bool flag = 0;
if(ch == '-')ch = getchar(),flag = 1;
while(ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + ch - '0',ch = getchar ();
return flag ? -x : x;
}
const int N = 50005;
int n,m,a[N],tot;
struct Treap{int ch[2],sz,cnt,val,fix;void Set(int vl){val = vl,fix = rand(),cnt = sz = 1;}}t[N * 16];
void Upd(int x){if (!x) return;u.sz = ulfc.sz + urtc.sz + u.cnt;}
void Rot(int &x,bool ty){int y = u.tc;u.tc = o.vc,o.vc = x,Upd(x),Upd(x = y);}
void Ins(int &x,int val)
{
if(!x)
{
x = ++ tot;
u.Set(val);
return;
}
if(val == u.val)
{
u.cnt ++, u.sz ++;
return;
}
bool ty = val > u.val;
Ins(u.tc,val);
t[u.tc].fix > u.fix ? Rot(x,ty) : Upd(x);
}
void Del(int &x,int val)
{
if(u.val == val)
{
//u.cnt --;
if(u.cnt == 1)
{
bool ty = urtc.fix > ulfc.fix;
if(!u.tc){x = 0;return;}
Rot(x,ty),Del(u.vc,val);
}
else u.cnt --;
}
else Del(u.ch[val > u.val],val);
Upd(x);
}
int GetRank(int x,int w)
{
int Ranking = 0;
for(;w != u.val && x;)w < u.val ? x = u.lc : (Ranking += (ulfc.sz + u.cnt),x = u.rc);
return Ranking + (ulfc.sz + u.cnt) * bool(x);
}
#define RT 1,1,n
#define lson x << 1,l,mid
#define rson x << 1 | 1,mid + 1,r
int sg[N << 2];
void Build(int x,int l,int r)
{
RD(i,l,r)
Ins(sg[x],a[i]);
if(l == r)return;
int mid = l + r >> 1;
Build(lson),Build(rson);
}
void SegC(int x,int l,int r,int pos,int s,int val)
{
Del(sg[x],val);
Ins(sg[x],s);
if(l == r)return;
int mid = l + r >> 1;
if(pos <= mid)SegC(lson,pos,s,val);
else SegC(rson,pos,s,val);
}
void Change(int pos,int s){
SegC(RT,pos,s,a[pos]);
a[pos] = s;
}
int calc(int x,int l,int r,int ql,int qr,int w)
{
if(l >= ql && r <= qr){
// printf("From: %d %d %d %d\n",l,r,w,GetRank(sg[x].rt,w));
return GetRank(sg[x],w);
}
int mid = l + r >> 1,Ranking = 0;
if(ql <= mid)Ranking += calc(lson,ql,qr,w);
if(mid < qr)Ranking += calc(rson,ql,qr,w);
return Ranking;
}
int main (){
srand(233);
//freopen("data.in","r",stdin);
//freopen("data1.out","w",stdout);
n = read(),m = read();
Rep(i,n)a[i] = read();
Build(RT);
Rep(i,m)
{
char s[3];
scanf("%s",s);
if(s[0] == 'C')
{
int pos = read(); int s = read();
Change(pos,s);
}
else {
int ql = read(); int qr = read(); int k = read();
int l = 0,r = inf;
while(l < r)
{
int mid = l + r >> 1;
if(calc(RT,ql,qr,mid) >= k)r = mid;
else l = mid + 1;
}
printf("%d\n",l == inf ? -1 : l);
}
}
return 0;
}
在删除时,如果在if之前减掉的话,就会挂掉,因为你要递归下去删这个节点,这样的话cnt就不是0了,然后就挂掉了。
其实好像挺简单的……