码迷,mamicode.com
首页 > 其他好文 > 详细

【Tyvj1601】魔兽争霸(主席树,树套树)

时间:2016-12-21 20:24:38      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:exit   str   sort   swa   class   wap   reset   out   思路   

题意:要求在N个数的序列中支持以下操作:

1:将第X个元素加上Y

2:询问当前K大值

n<=30000,m<=50000

思路:树状数组套主席树

Tyvj又炸了,还不知道对不对

  1 var t:array[0..12000000]of record
  2                            l,r,s:longint;
  3                           end;
  4     d:array[1..50000,1..3]of longint;
  5     a,save,q,root,hash:array[0..100000]of longint;
  6     b:array[1..200000]of longint;
  7     n,m,i,j,x,cnt,u,que,s,sum,tmp,len:longint;
  8     ch:string;
  9 
 10 function lowbit(x:longint):longint;
 11 begin
 12  exit(x and (-x));
 13 end;
 14 
 15 procedure update(l,r:longint;var p:longint;x,v:longint);
 16 var mid:longint;
 17 begin
 18  inc(cnt); t[cnt]:=t[p];
 19  p:=cnt; t[p].s:=t[p].s+v;
 20  if l=r then exit;
 21  mid:=(l+r)>>1;
 22  if x<=mid then update(l,mid,t[p].l,x,v)
 23   else update(mid+1,r,t[p].r,x,v);
 24 end;
 25 
 26 function query(l,r,k:longint):longint;
 27 var mid,s,i:longint;
 28 begin
 29  if l=r then exit(l);
 30  s:=0;
 31  mid:=(l+r)>>1;
 32  for i:=1 to len do s:=s+t[t[q[i]].l].s;
 33  if s>=k then
 34  begin
 35   for i:=1 to len do q[i]:=t[q[i]].l;
 36   exit(query(l,mid,k));
 37  end
 38   else
 39   begin
 40    for i:=1 to len do q[i]:=t[q[i]].r;
 41    exit(query(mid+1,r,k-s));
 42   end;
 43 end;
 44 
 45 procedure swap(var x,y:longint);
 46 var t:longint;
 47 begin
 48  t:=x; x:=y; y:=t;
 49 end;
 50 
 51 procedure qsort(l,r:longint);
 52 var i,j,mid:longint;
 53 begin
 54  i:=l; j:=r; mid:=b[(l+r)>>1];
 55  repeat
 56   while mid<b[i] do inc(i);
 57   while mid>b[j] do dec(j);
 58   if i<=j then
 59   begin
 60    swap(b[i],b[j]);
 61    inc(i); dec(j);
 62   end;
 63  until i>j;
 64  if l<j then qsort(l,j);
 65  if i<r then qsort(i,r);
 66 end;
 67 
 68 function find(x:longint):longint;
 69 var l,r,mid:longint;
 70 begin
 71  l:=1; r:=u;
 72  while l<=r do
 73  begin
 74   mid:=(l+r)>>1;
 75   if hash[mid]=x then exit(mid);
 76   if hash[mid]>x then l:=mid+1
 77    else r:=mid-1;
 78  end;
 79 end;
 80 
 81 begin
 82  assign(input,tyvj1601.in); reset(input);
 83  assign(output,tyvj1601.out); rewrite(output);
 84  readln(n);
 85  for i:=1 to n do
 86  begin
 87   read(a[i]); b[i]:=a[i];
 88  end;
 89  que:=n;
 90  for i:=1 to n do save[i]:=a[i];
 91  readln(m);
 92  for i:=1 to m do
 93  begin
 94   readln(ch); x:=0;
 95   if ch[1]=Q then
 96   begin
 97    for j:=3 to length(ch) do x:=x*10+ord(ch[j])-ord(0);
 98    d[i,1]:=1; d[i,2]:=x; continue;
 99   end;
100   d[i,1]:=2; s:=2;
101   for j:=3 to length(ch) do
102   begin
103    if ch[j]=  then inc(s)
104     else d[i,s]:=d[i,s]*10+ord(ch[j])-ord(0);
105   end;
106   if ch[1]=A then d[i,3]:=-d[i,3];
107  end;
108  for i:=1 to m do
109   if d[i,1]=2 then
110   begin
111    a[d[i,2]]:=a[d[i,2]]+d[i,3];
112    if a[d[i,2]]>0 then
113    begin
114     inc(que); b[que]:=a[d[i,2]];
115    end;
116   end;
117  qsort(1,que);
118  hash[1]:=b[1]; u:=1;
119  for i:=2 to que do
120   if b[i]<>b[i-1] then begin inc(u); hash[u]:=b[i]; end;
121  sum:=n;
122  for i:=1 to n do
123  begin
124   tmp:=find(save[i]);
125   j:=i;
126   while j<=n do
127   begin
128    update(1,u,root[j],tmp,1);
129    j:=j+lowbit(j);
130   end;
131  end;
132  for i:=1 to m do
133   if d[i,1]=1 then
134   begin
135    len:=0; j:=n;
136    while j>0 do
137    begin
138     inc(len); q[len]:=root[j];
139     j:=j-lowbit(j);
140    end;
141    if sum<d[i,2] then writeln(-1)
142     else writeln(hash[query(1,u,d[i,2])]);
143   end
144    else
145    begin
146     tmp:=find(save[d[i,2]]);
147     j:=d[i,2];
148     while j<=n do
149     begin
150      update(1,u,root[j],tmp,-1);
151      j:=j+lowbit(j);
152     end;
153     save[d[i,2]]:=save[d[i,2]]+d[i,3];
154     if save[d[i,2]]>0 then
155     begin
156      tmp:=find(save[d[i,2]]);
157      j:=d[i,2];
158      while j<=n do
159      begin
160       update(1,u,root[j],tmp,1);
161       j:=j+lowbit(j);
162      end;
163     end
164      else dec(sum);
165    end;
166 
167  writeln(sum);
168  close(input);
169  close(output);
170 end.

 

【Tyvj1601】魔兽争霸(主席树,树套树)

标签:exit   str   sort   swa   class   wap   reset   out   思路   

原文地址:http://www.cnblogs.com/myx12345/p/6208615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!