3524: [poi2014] courierstime limit: 20 sec memory limit: 128 MB
Submit: 466 solved: 141
[Submit] [Status] Description
Give a sequence a with the length of N. 1 ≤ A [I] ≤ n.
The M group asks if a [L, R] interval exists and whether a number appears in [L, R] More than (R-l + 1)/2. If yes, this number is output; otherwise, 0 is output.
Input
The first row has two numbers N and M.
Number of N in the second row, a [I].
In the next m row, there are two numbers in each row, L and R, indicating to ask about the range [L, R.
Output
M rows. Each row corresponds to one answer.
Sample input7 5
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6
Sample output1
0
3
0
4
Hint
[Data Scope]
N, m ≤ 500000
Source
By dzy
Question:
The Chairman tree bug also exists...
Because the range Subtraction is satisfied, we can obtain the weight information of the range [L, R] In a tree-like array, and then this question is bare...
Code:
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm> 10 11 #include<iostream> 12 13 #include<vector> 14 15 #include<map> 16 17 #include<set> 18 19 #include<queue> 20 21 #include<string> 22 23 #define inf 1000000000 24 25 #define maxn 10000000+1000 26 27 #define maxm 500000+1000 28 29 #define eps 1e-10 30 31 #define ll long long 32 33 #define pa pair<int,int> 34 35 #define for0(i,n) for(int i=0;i<=(n);i++) 36 37 #define for1(i,n) for(int i=1;i<=(n);i++) 38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 42 43 #define mod 1000000007 44 45 using namespace std; 46 47 inline int read() 48 49 { 50 51 int x=0,f=1;char ch=getchar(); 52 53 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 54 55 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 56 57 return x*f; 58 59 } 60 int n,m,tot,root[maxm],ls[maxn],rs[maxn],s[maxn]; 61 void update(int l,int r,int x,int &y,int z) 62 { 63 y=++tot; 64 s[y]=s[x]+1; 65 if(l==r)return; 66 ls[y]=ls[x];rs[y]=rs[x]; 67 int mid=(l+r)>>1; 68 if(z<=mid)update(l,mid,ls[x],ls[y],z);else update(mid+1,r,rs[x],rs[y],z); 69 } 70 int query(int x,int y) 71 { 72 int l=1,r=n,mid,xx=root[x-1],yy=root[y],tmp=(y-x+1)>>1; 73 while(l!=r) 74 { 75 mid=(l+r)>>1; 76 if(s[yy]-s[xx]<tmp)return 0; 77 if(s[ls[yy]]-s[ls[xx]]>tmp){r=mid;xx=ls[xx];yy=ls[yy];} 78 else if(s[rs[yy]]-s[rs[xx]]>tmp){l=mid+1;xx=rs[xx];yy=rs[yy];} 79 else return 0; 80 } 81 return l; 82 } 83 84 int main() 85 86 { 87 88 freopen("input.txt","r",stdin); 89 90 freopen("output.txt","w",stdout); 91 92 n=read();m=read(); 93 for1(i,n) 94 { 95 int x=read();update(1,n,root[i-1],root[i],x); 96 } 97 while(m--) 98 { 99 int x=read(),y=read();100 printf("%d\n",query(x,y));101 }102 103 return 0;104 105 }View code
Bzoj3524: [poi2014] couriers