HDU5919 Sequence II(主席树)
In the i-th query, you are given two integers lili and riri. Consider the subsequence ali,ali+1,ali+2,⋯,ariali,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)kip1(i),p2(i),⋯,pki(i) (in ascending order, i.e.,p(i)1<p(i)2<⋯<p(i)kip1(i)<p2(i)<⋯<pki(i)).
Note that kiki is the number of different integers in this subsequence. You should output p(i)⌈ki2⌉p⌈ki2⌉(i)for the i-th query.
InputIn the first line of input, there is an integer T (T≤2T≤2) denoting the number of test cases.
Each test case starts with two integers n (n≤2×105n≤2×105) and m (m≤2×105m≤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×105a1,a2,⋯,an,0≤ai≤2×105).
There are two integers lili and riri 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)li‘,ri‘(1≤li‘≤n,1≤ri‘≤n). As a result, the problem became more exciting.
We can denote the answers as ans1,ans2,⋯,ansmans1,ans2,⋯,ansm. Note that for each test case ans0=0ans0=0.
You can get the correct input li,rili,ri from what you read (we denote them as l‘i,r‘ili‘,ri‘)by the following formula:
OutputYou should output one single line for each test case.
For each test case, output one line “Case #x: p1,p2,⋯,pmp1,p2,⋯,pm”, where x is the case number (starting from 1) and p1,p2,⋯,pmp1,p2,⋯,pm is the answer.Sample Input
2
5 2
3 3 1 5 4
2 2
4 4
5 2
2 5 2 1 2
2 3
2 4
Sample Output
Case #1: 3 3
Case #2: 3 1
Hint
题解:
题目意思是:给你n个数,然后m组询问(强制在线),每组询问L,R。
让你求L到R区间内的去重后的中位数所在的位置。
思路:考虑使用主席树来倒着将数组里面的数字插入主席树,每次插入一个数字a[i],在该树的位置 i 加一,然后判断其是否前面出现过,如果出现过,则在前一棵树的pre[a[i]]的位置减一;
然后只要查询root[L]的L到R区间得和就是L到R区间的数字的种类,然后再去查找root[L]中的第K小的数字在那个位置即可。
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
#define mkp make_pair
typedef long long ll;
const int INF=0x3f3f3f3f;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=2e5+;
int T,n,m,cnt,a[maxn],rt[maxn],pre[maxn],ans[maxn];
struct Node{
int ls,rs;
int sum;
} tr[maxn*]; void update(int &x,int y,int l,int r,int pos,int val)
{
tr[++cnt]=tr[y];tr[cnt].sum+=val;x=cnt;
if(l==r) return ;
int mid=l+r>>;
if(pos<=mid) update(tr[x].ls,tr[y].ls,l,mid,pos,val);
else update(tr[x].rs,tr[y].rs,mid+,r,pos,val);
}
int Query(int t,int l,int r,int L,int R)
{
if(L<=l&&r<=R) return tr[t].sum;
int mid=l+r>>,ret=;
if(L<=mid) ret+=Query(tr[t].ls,l,mid,L,R);
if(R>mid) ret+=Query(tr[t].rs,mid+,r,L,R);
return ret;
} int GetKth(int t,int l,int r,int k)
{
if(l==r) return l;
int mid=l+r>>,num=tr[tr[t].ls].sum;
if(k<=num) return GetKth(tr[t].ls,l,mid,k);
else return GetKth(tr[t].rs,mid+,r,k-num);
} int main()
{
T=read();
for(int cas=;cas<=T;++cas)
{
memset(pre,,sizeof(pre));
memset(rt,,sizeof(rt));
n=read();m=read(); cnt=;
for(int i=;i<=n;++i) a[i]=read();
int L,R; ans[]=; for(int i=n;i>=;--i)
{
if(!pre[a[i]]) update(rt[i],rt[i+],,n,i,),pre[a[i]]=i;
else
{
update(rt[i],rt[i+],,n,i,);
update(rt[i],rt[i],,n,pre[a[i]],-);
pre[a[i]]=i;
}
}
for(int i=;i<=m;++i)
{
L=read();R=read();
L=((L+ans[i-])%n)+;
R=((R+ans[i-])%n)+;
if(L>R) swap(L,R);
int num=Query(rt[L],,n,L,R)+>>;
ans[i]=GetKth(rt[L],,n,num);
}
printf("Case #%d:",cas);
for(int i=;i<m;++i) printf(" %d",ans[i]);
printf(" %d\n",ans[m]);
} return ;
}
HDU5919 Sequence II(主席树)的更多相关文章
- HDU 5919 Sequence II 主席树
Sequence II Problem Description Mr. Frog has an integer sequence of length n, which can be denoted ...
- [HDU5919]Sequence II
[HDU5919]Sequence II 试题描述 Mr. Frog has an integer sequence of length n, which can be denoted as a1,a ...
- hdu_5919_Sequence II(主席树)
题目链接:hdu_5919_Sequence II 题意: 给你n个数,m个询问,每次问你一个区间中每一种数在区间中第一次出现的位置的中位数,强制在线. 题解: 一看就是主席树搞,不过这里要询问第一次 ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- F. Greedy Sequence(主席树区间k的后继)(The Preliminary Contest for ICPC Asia Nanjing 2019)
题意: 查找区间k的后继. 思路: 直接主席树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio&g ...
- HDU 5919 Sequence II(主席树+逆序思想)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- HDU 5919 -- Sequence II (主席树)
题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...
- Sequence II HDU - 5919(主席树)
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...
- HDU 5919 Sequence II(主席树)题解
题意:有A1 ~ An组成的数组,给你l r,L = min((l + ans[i - 1]) % n + 1, (r + ans[i - 1]) % n + 1),R = max((l + ans[ ...
随机推荐
- Linux如何添加硬盘
一.命令操作: df #查看磁盘空间 fdisk #分区/查看分区 mkfs #格式化 df -h(以人类易读) -m(以M为单位读取) #查看硬 ...
- Java 调用 Hbase API 访问接口实现方案
HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google文件 ...
- 2、Linux基础练习题
题目 答案 1.答案 [root@centos7 ~]# date +'%F %T' 2019-07-23 10:21:35 2.答案 [root@centos7 ~]# date +%A -d '- ...
- Error response from daemon ... no space left on device docker启动容器服务报错
docker 启动容器服务的时候,报错no space left on device 1. 检查磁盘是否用光 3.检查inode是否耗光,从截图看到是inode耗光导致出现问题: 进入到/run里面看 ...
- Windows下编译最新版ChezScheme
据说ChezScheme是最快的神级编译器,一秒钟几百万行,王垠说的2秒内编译自身绝不是夸张(看这里<揭秘Chez Scheme>,Scheme中文社区).ChezScheme由美国印第安 ...
- 命令序列 ; & && ||
; 从左到右依次被执行,返回最后一个命令的执行状态 & 该命令将在后台被执行,即在子bash中执行(或ctrl+z,bg, jobs,bg 命令号)(变量$!.$one.$two.$three ...
- ZeroC ICE的远程调用框架 AMD
继上一篇<ZeroC ICE的远程调用框架>,本篇再来说其中的AMD.(本篇需要重写) 当在ice文件中声明某个接口方法Method为["amd"]后,接口方法在stu ...
- opencv 6 图像轮廓与图像分割修复 1 查找并绘制轮廓 寻找物体的凸包
查找并绘制轮廓 寻找轮廓(findContours)函数 绘制轮廓(drawContours()函数) 基础实例程序:轮廓查找 #include <opencv2/opencv.hpp> ...
- JVM 问题排查和性能优化常用的 JDK 工具
JDK 提供了一系列用于监控.诊断 Java 进程的工具,它们在 JDK 安装目录的 bin 目录下,有 jps.jcmd.jstack.jinfo.jmap 等.其中jmc.jconsole.jvi ...
- 投票通过,PHP 8 确认引入 Union Types 2.0
关于是否要在 PHP 8 中引入 Union Types 的投票已于近日结束,投票结果显示有 61 名 PHP 开发组成员投了赞成票,5 名投了反对票. 还留意到鸟哥在投票中投了反对票~) 因此根据投 ...