HDU 5919 Sequence II (主席树+求区间不同数个数)
来源:互联网 发布:淘宝lee代购是正品吗 编辑:程序博客网 时间:2024/06/11 23:50
Sequence II
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2355 Accepted Submission(s): 628
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 integersli 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 asp(i)1,p(i)2,⋯,p(i)ki (in ascending order, i.e.,p(i)1<p(i)2<⋯<p(i)ki ).
Note thatki 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
We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as
Note that
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 integersli 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 tol‘i,r‘i(1≤l‘i≤n,1≤r‘i≤n) . As a result, the problem became more exciting.
We can denote the answers asans1,ans2,⋯,ansm . Note that for each test case ans0=0 .
You can get the correct inputli,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}
Each test case starts with two integers n (
There are two integers
However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to
We can denote the answers as
You can get the correct input
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:
Sample Input
25 23 3 1 5 42 24 45 22 5 2 1 22 32 4
Sample Output
Case #1: 3 3Case #2: 3 1
题意:N个数,计算每个区间 每个不同的数第一次出现的位置,并对位置排序,求出中位数
分析:求区间【L,R】不同数的个数的变形。
从后往前加入数字,在这个位置权值+1, 如果出现过,在上次的位置权值-1, 建立主席树
求出对应区间不同数个数K,最后在第L棵线段树上直接求第(K+1)/2小值。
代码:
#include<bits/stdc++.h>#define mem(p,k) memset(p,k,sizeof(p));#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long long#define inf 0x3f3f3f3fusing namespace std;const int N=300010;int n,q,tot;int a[N],root[N],bk[N];int ls[N*30],rs[N*30],sum[N*30];void update(int l,int r,int &now,int pre,int k,int val){ now=tot++; ls[now]=ls[pre]; rs[now]=rs[pre]; sum[now]=sum[pre]+val; if(l==r)return; int m=(l+r)>>1; if(k<=m) update(l,m,ls[now],ls[pre],k,val); else update(m+1,r,rs[now],rs[pre],k,val);}int query(int L,int R,int l,int r,int rt){ if(L<=l && r<=R){ return sum[rt]; } int m=(l+r)>>1,s=0; if(L<=m) s+=query(L,R,l,m,ls[rt]); if(R>m) s+=query(L,R,m+1,r,rs[rt]); return s;}int fin(int k,int l,int r,int rt){ if(l==r)return l; int m=(l+r)>>1; if(k<=sum[ls[rt]]) return fin(k,l,m,ls[rt]); return fin(k-sum[ls[rt]],m+1,r,rs[rt]);}int main(){ int T,cur=1; cin>>T; while(T--){ mem(bk,0); mem(ls,0); mem(rs,0); mem(sum,0); scanf("%d%d",&n,&q); for(int i=1;i<=n;i++)scanf("%d",a+i); root[n+1]=0; tot=1; for(int i=n;i;i--){ if(bk[a[i]]){//cout<<i<<endl; update(1,n,root[i],root[i+1],bk[a[i]],-1); update(1,n,root[i],root[i],i,1); //cout<<query(1,n,1,n,root[i])<<endl; } else update(1,n,root[i],root[i+1],i,1); bk[a[i]]=i; } int ans=0; cout<<"Case #"<<cur++<<":"; while(q--){ int l,r; scanf("%d%d",&l,&r); l=(l+ans)%n+1; r=(r+ans)%n+1; if(l>r)swap(l,r); int s=query(l,r,1,n,root[l]); s=(s+1)>>1; ans=fin(s,1,n,root[l]); printf(" %d",ans); } cout<<endl; } return 0;}
阅读全文
0 0
- hdu 5919 Sequence II (主席树,求区间不同数的个数)
- hdu 5919 Sequence II(主席树,求区间不同数的个数)
- HDU 5919 Sequence II (主席树+求区间不同数个数)
- Hdu-5919 Sequence II(主席树在线求区间不同数)
- hdu 5919 主席树(区间不同数个数+区间第k个数)
- SPOJ-DQUERY-主席树求区间不同数个数模板
- HDU 5919Sequence II (主席树,不同元素个数+第K大)
- hdu5919 主席树求区间不同数
- hdu5919 Sequence II(主席树求区间数种数和k大查找)
- SPOJ DQUERY 求区间内不同数的个数 主席树
- spoj 3267. D-query 主席树求区间不同数的个数
- SPOJ D-query 主席树在线(求区间不同的数的个数)
- SPOJ DQUERY 求区间内不同数的个数 (主席树)
- hdu 5790 prefix 主席树在线维护区间不同数的个数
- SPOJ DQUERY 区间内不同数的个数 主席树
- HDU 5919 Sequence II(主席树)
- hdu 5919 Sequence II 主席树
- HDU 5919 Sequence II(主席树)
- 图文讲解Zabbix 分布式监控平台安装过程
- 规律:HDU
- Blockchain学习2_以太坊开发--truffle和testrpc使用介绍
- 运行提示 error C2653: “CMFCVisualManagerScenic”: 不是类或命名空间名称。
- 类与对象第二题
- HDU 5919 Sequence II (主席树+求区间不同数个数)
- UVa 11478
- call和apply的用法(详细介绍)
- tensorflow cross_entropy 四种交叉熵计算函数
- 欢迎使用CSDN-markdown编辑器
- 垃圾收集器与内存分配策略
- w命令详解
- 用java开发贪吃蛇游戏
- heap、stack的内存分配的区别