hdu 5919--Sequence II(主席树--求区间不同数…
2018-06-17 21:46:28来源:未知 阅读 ()
题目链接
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.
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:
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.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <map> using namespace std; typedef long long LL; const int N=2e5+5; int a[N],ans[N]; int t[N],tot; map<int,int>mp; struct Node { int l,r; int num; }tr[40*N]; void init() { tot=0; mp.clear(); } int build(int l,int r) { int ii=tot++; tr[ii].num=0; if(l<r) { int mid=(l+r)>>1; tr[ii].l=build(l,mid); tr[ii].r=build(mid+1,r); } return ii; } int update(int now,int l,int r,int x,int y) { int ii=tot++; tr[ii].num=tr[now].num+y; tr[ii].l=tr[now].l; tr[ii].r=tr[now].r; if(l<r) { int mid=(l+r)>>1; if(x<=mid) tr[ii].l=update(tr[now].l,l,mid,x,y); else tr[ii].r=update(tr[now].r,mid+1,r,x,y); } return ii; } int query(int now,int l,int r,int L,int R) { if(L<=l&&r<=R) return tr[now].num; int mid=(l+r)>>1; int sum=0; if(mid>=L) sum+=query(tr[now].l,l ,mid,L,R); if(mid<R) sum+=query(tr[now].r,mid+1,r,L,R); return sum; } int finds(int now,int l,int r,int k) { if(l==r) return l; int mid=(l+r)>>1; if(tr[tr[now].l].num>=k) return finds(tr[now].l,l,mid,k); else return finds(tr[now].r,mid+1,r,k-tr[tr[now].l].num); } int main() { int T,Case=1; cin>>T; while(T--) { init(); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); t[n+1]=build(1,n); for(int i=n;i>=1;i--) { if(mp.count(a[i])) { int tmp=update(t[i+1],1,n,mp[a[i]],-1); t[i]=update(tmp,1,n,i,1); } else t[i]=update(t[i+1],1,n,i,1); mp[a[i]]=i; } ans[0]=0; for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); int l=min((x+ans[i-1])%n+1,(y+ans[i-1])%n+1); int r=max((x+ans[i-1])%n+1,(y+ans[i-1])%n+1); int k=(query(t[l],1,n,l,r)+1)/2; ans[i]=finds(t[l],1,n,k); } printf("Case #%d:",Case++); for(int i=1;i<=m;i++) printf(" %d",ans[i]); puts(""); } return 0; } /** 10 4 1 1 1 1 1 1 1 1 1 1 3 6 6 8 7 10 2 5 */
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- HDU-2955-Robberies(0-1背包) 2020-03-30
- hdu1455 拼木棍(经典dfs) 2020-02-29
- anniversary party_hdu1520 2020-02-16
- hdu1062 text reverse 2020-01-27
- hdu4841 2020-01-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash