【BZOJ3932】任务查询系统(主席树)

题意:若干有优先级的任务会持续一段时间,给出所有任务,询问每个时刻前k小优先级任务的优先级总和,询问强制在线。


对于100%的数据,1≤m,n,Si,Ei,Ci≤100000,0≤Ai,Bi≤100000,1≤Pi≤10000000,Xi为1到n的一个排列

思路:不用离散化,主席树继承并保存每个时刻的个数和与权值和,对于任务差分后插入,询问时二分即可

注意个数不够时特判一波(我是这么干的) 

询问中l=r时小心处理

  1 var t:array[0..8100000,0..1]of longint;
  2     sum1:array[0..8100000]of longint;
  3     sum2:array[0..8100000]of int64;
  4     a,b,c:array[1..210000]of longint;
  5     root:array[0..210000]of longint;
  6     m1,n1,i,j,cnt,k,n,st,ed,x,y,z,a1,b1,c1:longint;
  7     ans:int64;
  8 
  9 procedure swap(var x,y:longint);
 10 var t:longint;
 11 begin
 12  t:=x; x:=y; y:=t;
 13 end;
 14 
 15 function min(x,y:longint):longint;
 16 begin
 17  if x<y then exit(x);
 18  exit(y);
 19 end;
 20 
 21 function max(x,y:longint):longint;
 22 begin
 23  if x>y then exit(x);
 24  exit(y);
 25 end;
 26 
 27 procedure qsort(l,r:longint);
 28 var i,j,mid:longint;
 29 begin
 30  i:=l; j:=r; mid:=a[(l+r)>>1];
 31  repeat
 32   while mid>a[i] do inc(i);
 33   while mid<a[j] do dec(j);
 34   if i<=j then
 35   begin
 36    swap(a[i],a[j]); swap(b[i],b[j]); swap(c[i],c[j]);
 37    inc(i); dec(j);
 38   end;
 39  until i>j;
 40  if l<j then qsort(l,j);
 41  if i<r then qsort(i,r);
 42 end;
 43 
 44 procedure update(l,r,x,v:longint;var p:longint);
 45 var mid:longint;
 46     tmp:int64;
 47 begin
 48  inc(cnt); t[cnt]:=t[p]; sum1[cnt]:=sum1[p]; sum2[cnt]:=sum2[p];
 49  p:=cnt;
 50  sum1[p]:=sum1[p]+v;
 51  tmp:=x; tmp:=tmp*v;
 52  sum2[p]:=sum2[p]+tmp;
 53  if l=r then exit;
 54  mid:=(l+r)>>1;
 55  if x<=mid then update(l,mid,x,v,t[p,0])
 56   else update(mid+1,r,x,v,t[p,1]);
 57 end;
 58 
 59 
 60 function query(l,r,x,p:longint):int64;
 61 var mid,tmp:longint;
 62 begin
 63  if x=0 then exit(0);
 64  if l=r then
 65  begin
 66   query:=l; query:=query*x;
 67   exit;
 68  end;
 69  if sum1[p]<=x then exit(sum2[p]);
 70  tmp:=sum1[t[p,0]];
 71  mid:=(l+r)>>1;
 72  if tmp>=x then exit(query(l,mid,x,t[p,0]))
 73   else exit(sum2[t[p,0]]+query(mid+1,r,x-tmp,t[p,1]));
 74 end;
 75 
 76 begin
 77  assign(input,'bzoj3932.in'); reset(input);
 78  assign(output,'bzoj3932.out'); rewrite(output);
 79  readln(m1,n1);
 80  st:=maxlongint; ed:=-maxlongint;
 81  for i:=1 to m1 do
 82  begin
 83   readln(x,y,z);
 84   ed:=max(ed,z);
 85   inc(n); a[n]:=x; b[n]:=z; c[n]:=1;
 86   if y+1<=n1 then
 87   begin
 88    inc(n); a[n]:=y+1; b[n]:=z; c[n]:=-1;
 89   end;
 90  // st:=min(st,x); ed:=max(ed,y);
 91  end;
 92  qsort(1,n); j:=1;
 93  for i:=1 to n1 do
 94  begin
 95   root[i]:=root[i-1];
 96   while (j<=n)and(a[j]=i) do
 97   begin
 98    update(1,ed,b[j],c[j],root[i]);
 99    inc(j);
100   end;
101  end;
102  ans:=1;
103  for i:=1 to n1 do
104  begin
105   readln(x,a1,b1,c1);
106   k:=1+(ans*a1+b1) mod c1;
107   ans:=query(1,ed,k,root[x]);
108   writeln(ans);
109  end;
110  close(input);
111  close(output);
112 end.

 

posted on 2017-02-24 14:05  myx12345  阅读(181)  评论(0编辑  收藏  举报

导航