主席树水题 ....不带离散化 直接权值主席树 然后查询就ok了
#include <bits/stdc++.h> const int MAXN=3e5+10; using namespace std; typedef struct node{ int l,r,sum; }node; node d[MAXN*21];int cnt; int a[MAXN]; int rt[MAXN]; void update(int &x,int y,int l,int r,int t){ x=++cnt;d[x]=d[y];d[x].sum++; if(l==r)return ; int mid=(l+r)>>1; if(t<=mid)update(d[x].l,d[y].l,l,mid,t); else update(d[x].r,d[y].r,mid+1,r,t); } int ans,vul; void querty(int x,int y,int l,int r){ if(l==r){ans=l;return ;} int mid=(l+r)>>1; if(d[d[y].l].sum-d[d[x].l].sum>vul)querty(d[x].l,d[y].l,l,mid); if(d[d[y].r].sum-d[d[x].r].sum>vul)querty(d[x].r,d[y].r,mid+1,r); } int main(){ int n,m;scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]),update(rt[i],rt[i-1],1,m,a[i]); int l,r,q;scanf("%d",&q); for(int i=1;i<=q;i++){ scanf("%d%d",&l,&r); ans=-1;vul=(r-l+1)/2; //cout<<l<<" "<<r<<" "<<vul<<endl; querty(rt[l-1],rt[r],1,m); if(ans==-1)puts("no"); else printf("yes %d ",ans); } return 0; }
2223: [Coci 2009]PATULJCI
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1503 Solved: 663
[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