[Coci 2009]PATULJCI
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1286 Solved: 553
[Submit][Status][Discuss]
Description
Input
Output
10 3
1 2 1 2 1 2 3 2 3 3
8
1 2
1 3
1 4
1 5
2 5
2 6
6 9
7 10
Sample Input
no
yes 1
no
yes 1
no
yes 2
no
yes 3
yes 1
no
yes 1
no
yes 2
no
yes 3
Sample Output
HINT
Notice:输入第二个整数是序列中权值的范围Lim,即1<=ai(1<=i<=n)<=Lim。
1<=Lim<=10000
题解:
这道题就是可持久化线段树吧,然后维护一下该节点的size即可,权值线段树。
1 #include<cstring> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<cstdio> 6 7 #define N 300007 8 #define M 10000007 9 using namespace std; 10 inline int read() 11 { 12 int x=0,f=1;char ch=getchar(); 13 while(ch>'9'||ch<'0'){if (ch=='-') f=-1;ch=getchar();} 14 while(ch<='9'&&ch>='0') 15 { 16 x=(x<<3)+(x<<1)+ch-'0'; 17 ch=getchar(); 18 } 19 return x*f; 20 } 21 22 int n,m,sz,limit; 23 int root[N],ls[M],rs[M],sum[M]; 24 25 void change(int l,int r,int yl,int &xz,int z) 26 { 27 xz=++sz,sum[xz]=sum[yl]+1; 28 if (l==r) return; 29 ls[xz]=ls[yl],rs[xz]=rs[yl]; 30 int mid=(l+r)>>1; 31 if (z<=mid) change(l,mid,ls[yl],ls[xz],z); 32 else change(mid+1,r,rs[yl],rs[xz],z); 33 } 34 int query(int l,int r,int x,int y,int tmp) 35 { 36 if (l==r) return l; 37 int mid=(l+r)>>1; 38 if (sum[ls[y]]-sum[ls[x]]>tmp) return query(l,mid,ls[x],ls[y],tmp); 39 else if (sum[rs[y]]-sum[rs[x]]>tmp) return query(mid+1,r,rs[x],rs[y],tmp); 40 else return 0; 41 } 42 int main() 43 { 44 n=read(),limit=read(); 45 for (int i=1;i<=n;i++) 46 { 47 int x=read(); 48 change(1,limit,root[i-1],root[i],x); 49 } 50 m=read(); 51 for (int i=1;i<=m;i++) 52 { 53 int x=read(),y=read(); 54 int ans=query(1,limit,root[x-1],root[y],(y-x+1)/2); 55 if (ans==0) printf("no\n"); 56 else printf("yes %d\n",ans); 57 } 58 }