1. 程式人生 > >BZOJ2223: [Coci 2009]PATULJCI

BZOJ2223: [Coci 2009]PATULJCI

str urn desc upd alt can san pda \n

主席樹水題 ....不帶離散化 直接權值主席樹 然後查詢就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\n",ans);
	}
	return 0;
}

2223: [Coci 2009]PATULJCI

Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 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

Sample Output

HINT

Notice:輸入第二個整數是序列中權值的範圍Lim,即1<=ai(1<=i<=n)<=Lim。

1<=Lim<=10000

BZOJ2223: [Coci 2009]PATULJCI