首页 > 代码库 > hdu-5919 Sequence II(主席树)
hdu-5919 Sequence II(主席树)
题目链接:
Sequence II
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 628 Accepted Submission(s): 160
Problem Description
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,?,an There are m queries.
In the i-th query, you are given two integers li and ri. Consider the subsequence ali,ali+1,ali+2,?,ari.
We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as p(i)1,p(i)2,?,p(i)ki (in ascending order, i.e.,p(i)1<p(i)2<?<p(i)ki).
Note that ki is the number of different integers in this subsequence. You should output p(i)⌈ki2⌉for the i-th query.
In the i-th query, you are given two integers li and ri. Consider the subsequence ali,ali+1,ali+2,?,ari.
We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as p(i)1,p(i)2,?,p(i)ki (in ascending order, i.e.,p(i)1<p(i)2<?<p(i)ki).
Note that ki is the number of different integers in this subsequence. You should output p(i)⌈ki2⌉for the i-th query.
Input
In the first line of input, there is an integer T (T≤2) denoting the number of test cases.
Each test case starts with two integers n (n≤2×105) and m (m≤2×105). There are n integers in the next line, which indicate the integers in the sequence(i.e., a1,a2,?,an,0≤ai≤2×105).
There are two integers li and ri in the following m lines.
However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to l‘i,r‘i(1≤l‘i≤n,1≤r‘i≤n). As a result, the problem became more exciting.
We can denote the answers as ans1,ans2,?,ansm. Note that for each test case ans0=0.
You can get the correct input li,ri from what you read (we denote them as l‘i,r‘i)by the following formula:
Each test case starts with two integers n (n≤2×105) and m (m≤2×105). There are n integers in the next line, which indicate the integers in the sequence(i.e., a1,a2,?,an,0≤ai≤2×105).
There are two integers li and ri in the following m lines.
However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to l‘i,r‘i(1≤l‘i≤n,1≤r‘i≤n). As a result, the problem became more exciting.
We can denote the answers as ans1,ans2,?,ansm. Note that for each test case ans0=0.
You can get the correct input li,ri from what you read (we denote them as l‘i,r‘i)by the following formula:
li=min{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}
ri=max{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}
Output
You should output one single line for each test case.
For each test case, output one line “Case #x: p1,p2,?,pm”, where x is the case number (starting from 1) and p1,p2,?,pm is the answer.
For each test case, output one line “Case #x: p1,p2,?,pm”, where x is the case number (starting from 1) and p1,p2,?,pm is the answer.
Sample Input
25 23 3 1 5 42 24 45 22 5 2 1 22 32 4
Sample Output
Case #1: 3 3
Case #2: 3 1
题意:
求给定一个区间,在对前边一个答案进行运算后得到一个新的区间,然后问这个区间里面有k个不同的数,把它们第一次出现的位置从小到大排序,问第k/2个位置是什么;
思路:
主席树的题目,主席树可以用来求区间有多少个不同的数,和询问区间的第k大,这个题目两个都考察了;
主席树求区间不同数的数目的时候,从后往前插入,同时在插入前删除之前插入的那个,然后求一个区间的个数就是在左端点的那个树上求询问区间的数的个数;
求完数目后然后再求位置的中位数,就是求区间k大了;也是在左端点那棵树上求了,这就是线段树的操作了,
我的主席树的写法跟网上找到的都不一样;不过是对的就行;
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <bits/stdc++.h>#include <stack>#include <map>using namespace std;#define For(i,j,n) for(int i=j;i<=n;i++)#define mst(ss,b) memset(ss,b,sizeof(ss));typedef long long LL;typedef unsigned long long ULL;template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar()); for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar()); F && (num=-num);}int stk[70], tp;template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + ‘0‘); putchar(‘\n‘);}const LL mod=1e9+7;const double PI=acos(-1.0);const int inf=1e9;const int N=1e4+120;const int maxn=2e5+220;const double eps=1e-12;int n,m,root[maxn],a[maxn],pre[maxn],cnt;struct node{ int l,r,sum;}T[maxn*40];void update(int l,int r,int &x,int y,int pos,int val){ T[++cnt]=T[y],T[cnt].sum+=val;x=cnt; if(l==r)return ; int mid=(l+r)>>1; if(pos<=mid)update(l,mid,T[x].l,T[y].l,pos,val); else update(mid+1,r,T[x].r,T[y].r,pos,val); }int query(int o,int l,int r,int L,int R){ if(L<=l&&R>=r)return T[o].sum; int mid=(l+r)>>1; int ans=0; if(L<=mid)ans+=query(T[o].l,l,mid,L,R); if(R>mid)ans+=query(T[o].r,mid+1,r,L,R); return ans;}int getans(int o,int l,int r,int k){ if(l==r)return l; int ans=T[T[o].l].sum; int mid=(l+r)>>1; if(ans>=k)return getans(T[o].l,l,mid,k); else return getans(T[o].r,mid+1,r,k-ans);}int main(){ int t,Case=0; read(t); while(t--) { int u,v,ans=0; read(n);read(m); for(int i=1;i<=n;i++)read(a[i]); mst(pre,-1); mst(root,0); cnt=0; T[n+1].l=T[n+1].r=T[n+1].sum=0; for(int i=n;i>=1;i--) { if(pre[a[i]]==-1)update(1,n,root[i],root[i+1],i,1); else { int temp; update(1,n,temp,root[i+1],pre[a[i]],-1); update(1,n,root[i],temp,i,1); } pre[a[i]]=i; } printf("Case #%d:",++Case); for(int i=1;i<=m;i++) { read(u);read(v); u=(u+ans)%n+1; v=(v+ans)%n+1; if(u>v)swap(u,v); int k=(query(root[u],1,n,u,v)+1)>>1; ans=getans(root[u],1,n,k); printf(" %d",ans); } printf("\n"); } return 0;}
hdu-5919 Sequence II(主席树)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。