25 23 3 1 5 42 24 45 22 5 2 1 22 32 4 Sample OutputCase #1: 3 3Case #2: 3 1HintSource2016中國大學生程序設計競賽(長春)-重現賽
題意:有長度為
學完主席樹就想過來把這題過了,拖到了現在。這是我參加的第一場區預賽-長春賽站現場賽的題,當時因為不會這道而沒進銀牌區。這題也是類似spoj-dquery(感覺這題是主席樹精髓啊==),其實會dquery這道題非常容易。n 的序列,強制在線詢問[l,r] 這段區間中所有不同數出現的第一個位置,按照位置從小到大排完序以后的中間(向上取整)的那個位置是多少?即把各個數字在這個區間中第一次出現的位置記作p1,p2,?,pk ,滿足p1<p2<?<pk ,求p?k2? #include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>#include<map>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int inf=0x3fffffff;const ll mod=1000000007;const int maxn=2e5+100;struct node{ int l,r,sum;}T[maxn*40];int root[maxn];int cnt;void update(int l,int r,int& x,int y,int pos,int val){ T[++cnt]=T[y],x=cnt,T[x].sum+=val; if(l==r) return; int m=(l+r)/2; if(pos<=m) update(l,m,T[x].l,T[y].l,pos,val); else update(m+1,r,T[x].r,T[y].r,pos,val);}int query(int l,int r,int x,int pos){ if(l==r) return T[x].sum; int m=(l+r)/2; if(pos<=m) return query(l,m,T[x].l,pos); else return T[T[x].l].sum+query(m+1,r,T[x].r,pos);}int query1(int l,int r,int x,int k){ if(l==r) return l; int m=(l+r)/2; if(k>T[T[x].l].sum) return query1(m+1,r,T[x].r,k-T[T[x].l].sum); else return query1(l,m,T[x].l,k);}int a[maxn];map<int,int> mp;int main(){ int cas; scanf("%d",&cas); int kase=0; while(cas--) { cnt=0; int n,m; scanf("%d%d",&n,&m); memset(T,0,sizeof(T)); memset(root,0,sizeof(root)); mp.clear(); rep(i,1,n+1) scanf("%d",&a[i]); int ans=0,l,r; per(i,1,n+1) { if(mp.find(a[i])==mp.end()) { mp[a[i]]=i; update(1,n,root[i],root[i+1],i,1); } else { update(1,n,root[i],root[i+1],mp[a[i]],-1); update(1,n,root[i],root[i],i,1); mp[a[i]]=i; } } printf("Case #%d: ",++kase); while(m--) { scanf("%d%d",&l,&r); l=(l+ans)%n+1,r=(r+ans)%n+1; if(l>r) swap(l,r); int k=query(1,n,root[l],r); k=(k+1)/2; ans=query1(1,n,root[l],k); printf("%d",ans); if(m) printf(" "); } puts(""); } return 0;}
新聞熱點
疑難解答