[BZOJ2653]middle (two points + chairman tree)

2653: middle

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 2252  Solved: 1252
[Submit][Status][Discuss]

Description

A sequence a of length n is set to be b after sorting, and its number of digits is defined as b[n/2], where a and b are numbered from 0, and the division is rounded off. Give you one
A sequence s of length n. Answer Q such queries: the largest median in the subsequence with the left endpoint of s between [a, b] and the right endpoint between [c, d].
where a<b<c<d. Positions are also numbered starting from 0. I will use some means to force you to be online.

 

Input

The first line sequence length n. The next n lines give the numbers in a in order.
The next line is Q. Then each line of Q line a, b, c, d, we make the answer to the previous query is
x (x=0 if this is the first query).
Let the array q={(a+x)%n,(b+x)%n,(c+x)%n,(d+x)%n}.
After sorting q from small to large, let the real
a=q[0], b=q[1], c=q[2], d=q[3] to be asked.  
The input guarantees that the condition is met.
The so-called "sorted" in the first line refers to sorting from large to small!
 

 

Output

Line Q gives the answer to the query in turn.

 

Sample Input

5
170337785
271451044
22430280
969056313
206452321
3
3 1 0 2
2 3 1 4
3 1 4 0

271451044
271451044
969056313

Sample Output

 

HINT

 

  0:n,Q<=100

1,...,5:n<=2000

0,...,19:n<=20000,Q<=25000


 

 

Source

 
[ Submit ][ Status ][ Discuss ]

Questions about the median are generally first divided into two answers, and then judged according to the number greater than or less than this number. In this question, if the number greater than or equal to this number - the number less than this number results in a result greater than 0, it means the median The number may be larger.

In other words, all positions greater than or equal to are assigned 1, and less than are assigned -1, then a range sum is non-negative.

But do you have to assign all the two points every time? No need, because considering that the number of each adjacent two weights changing from 1 to -1 is not much, it is enough to use the chairman tree to build all of them first.

Three values ​​need to be maintained at the same time: lmax, rmax, sum. It is found that sum is required when lmax and rmax are merged. Is this complexity guaranteed? The answer is yes, because at most a constant * 2 for each point, the complexity will not change. Of course, three points can also be merged together each time, and the complexity remains unchanged.

Finally, the sorting of the first line of this question refers to the sorting from small to large.

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define lson ls[x],L,mid
 4 #define rson rs[x],mid+1,R
 5 #define rep(i,l,r) for (int i=l; i<=r; i++)
 6 using namespace std;
 7 
 8 const int N=5000100;
 9 struct P{
10     int sm,lmx,rmx; P(){};
11     P(int _sm,int _lmx,int _rmx):sm(_sm),lmx(_lmx),rmx(_rmx){}
12 }seg[N];
13 int ls[N],rs[N],T[30010],q[4],ans,nd,n,Q;
14 struct A{
15     int x,y;
16     bool operator <(const A &a)const{ return (x==a.x) ? y<a.y : x<a.x; }
17 }a[30010];
18 
19 P operator +(const P &a,const P &b){ return P(a.sm+b.sm,max(a.lmx,a.sm+b.lmx),max(b.rmx,b.sm+a.rmx)); }
20 
21 void build(int &x,int L,int R){
22     x=++nd;
23     if (L==R){ seg[x]=P(1,1,1); return; }
24     int mid=(L+R)>>1; build(lson); build(rson);
25     seg[x]=seg[ls[x]]+seg[rs[x]];
26 }
27 
28 void mdf(int y,int &x,int L,int R,int pos){
29     x=++nd; ls[x]=ls[y]; rs[x]=rs[y];
30     if (L==R){ seg[x]=P(-1,-1,-1); return; }
31     int mid=(L+R)>>1;
32     if (pos<=mid) mdf(ls[y],lson,pos); else mdf(rs[y],rson,pos);
33     seg[x]=seg[ls[x]]+seg[rs[x]];
34 }
35 
36 P ask(int x,int L,int R,int l,int r){
37     if (l>r) return P(0,0,0);
38     if (L==l && r==R) return seg[x];
39     int mid=(L+R)>>1;
40     if (r<=mid) return ask(lson,l,r);
41     else if (l>mid) return ask(rson,l,r);
42         else return ask(lson,l,mid)+ask(rson,mid+1,r);
43 }
44 
45 int jud(int k){ return ask(T[k],1,n,q[0],q[1]).rmx+ask(T[k],1,n,q[1]+1,q[2]-1).sm+ask(T[k],1,n,q[2],q[3]).lmx>=0; }
46 
47 int main(){
48     freopen("bzoj2653.in","r",stdin);
49     freopen("bzoj2653.out","w",stdout);
50     scanf("%d",&n);
51     rep(i,1,n) scanf("%d",&a[a[i].y=i].x);
52     sort(a+1,a+n+1); build(T[1],1,n);
53     rep(i,2,n) mdf(T[i-1],T[i],1,n,a[i-1].y);
54     scanf("%d",&Q);
55     while (Q--){
56         scanf("%d%d%d%d",&q[0],&q[1],&q[2],&q[3]);
57         q[0]=(q[0]+ans)%n+1; q[1]=(q[1]+ans)%n+1;
58         q[2]=(q[2]+ans)%n+1; q[3]=(q[3]+ans)%n+1; sort(q,q+4);
59         int L=1,R=n;
60         while (L<=R){
61             int mid=(L+R)>>1;
62             if (jud(mid)) L=mid+1; else R=mid-1;
63         }
64         printf("%d\n",ans=a[L-1].x);
65     }
66     return 0;
67 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325234624&siteId=291194637