hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)
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+;
int a[N],ans[N];
int t[N],tot;
map<int,int>mp;
struct Node
{
int l,r;
int num;
}tr[*N];
void init()
{
tot=;
mp.clear();
}
int build(int l,int r)
{
int ii=tot++;
tr[ii].num=;
if(l<r)
{
int mid=(l+r)>>;
tr[ii].l=build(l,mid);
tr[ii].r=build(mid+,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)>>;
if(x<=mid) tr[ii].l=update(tr[now].l,l,mid,x,y);
else tr[ii].r=update(tr[now].r,mid+,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)>>;
int sum=;
if(mid>=L) sum+=query(tr[now].l,l ,mid,L,R);
if(mid<R) sum+=query(tr[now].r,mid+,r,L,R);
return sum;
}
int finds(int now,int l,int r,int k)
{
if(l==r) return l;
int mid=(l+r)>>;
if(tr[tr[now].l].num>=k) return finds(tr[now].l,l,mid,k);
else return finds(tr[now].r,mid+,r,k-tr[tr[now].l].num);
}
int main()
{
int T,Case=; cin>>T;
while(T--)
{
init();
int n,m; scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
t[n+]=build(,n);
for(int i=n;i>=;i--)
{
if(mp.count(a[i]))
{
int tmp=update(t[i+],,n,mp[a[i]],-);
t[i]=update(tmp,,n,i,);
}
else t[i]=update(t[i+],,n,i,);
mp[a[i]]=i;
}
ans[]=;
for(int i=;i<=m;i++)
{
int x,y; scanf("%d%d",&x,&y);
int l=min((x+ans[i-])%n+,(y+ans[i-])%n+);
int r=max((x+ans[i-])%n+,(y+ans[i-])%n+);
int k=(query(t[l],,n,l,r)+)/;
ans[i]=finds(t[l],,n,k);
}
printf("Case #%d:",Case++);
for(int i=;i<=m;i++) printf(" %d",ans[i]);
puts("");
}
return ;
}
/**
10 4
1 1 1 1 1 1 1 1 1 1
3 6
6 8
7 10
2 5
*/
hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)的更多相关文章
- HDU 5919 Sequence II 主席树
Sequence II Problem Description Mr. Frog has an integer sequence of length n, which can be denoted ...
- 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[ ...
- hdu 5919 Sequence II (可持久化线段树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...
- HDU - 5919 Sequence II
题意: 给定长度为n的序列和q次询问.每次询问给出一个区间(L,R),求出区间内每个数第一次出现位置的中位数,强制在线. 题解: 用主席树从右向左的插入点.对于当前点i,如果a[i]出现过,则把原位置 ...
- HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)
HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...
- 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掉. 主席树可解 ...
- HDU5919 Sequence II(主席树)
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
随机推荐
- 《编程人生:15位软件先驱访谈录》【PDF】下载
<编程人生:15位软件先驱访谈录>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382231 内容简介 本书适合所有程序员,也适合 ...
- margin相关
1.图片与文字对齐问题 如果图片和文字差不多大时,使用兼容性强的margin负值方法. 方法:img{margin:0 3px -3px 0;} 2.div嵌套后margin出现失效(转移)问题 原因 ...
- 安装puppet
安装puppet服务 先安装ruby语言包.ruby标准库.ruby shadow库 yum install -y ruby ruby-libs ruby-shadow 2.需要添加EPRL库,来支持 ...
- 关于llvm kaleidoscope: 记一次Debug血泪之路
简而言之,慎(bu)用(yong)全局变量! 这次debug基本上花了我一周的时间,我基本上是晚上9点30下自习回然后调试到11点30,如此反复一周直到今天周五终于解决了,,以前都听说前辈们 说尽量不 ...
- 进程间通信 ipcs
在linux系统上借助ipcs命令可以方便地查看进程间通信状态 操作系统:centos7.3 x86_64 应用软件: oracle12c
- Talk 3: Rob Pike on Upspin (Gopherfest 2017)
Talk 3: Rob Pike on Upspin Upspin is an experimental project to build a framework for naming and sha ...
- Linux整合Apache和SVN
1.安装APR-1.2.7和APR-util-1.2.7 (下载地址:http://apr.apache.org/) #tar zxvf apr-1.2.7.tar.gz #cd apr-1. ...
- JavaScript实现ZLOGO: 用语法树实现多层循环
原址: https://zhuanlan.zhihu.com/p/32571516 照例先上演示弱效果图. 演示地址照旧: 代码如下: 开始 循环4次 循环4次 前进50 左转90度 到此为止 右转9 ...
- DevOps/TestOps概念
天下大势分久必合合久必分,早期的软件开发只有软件工程师一人完成,为了提高效率逐渐实行分工模式:开发.测试.运维.不同角色担任不同的任务.分工越来越细之后带来了问题也越来越突出,那就是各角色之间的沟通成 ...
- tornado返回指定的http code
最近做web api,需要在接口返回异常时,返回对应的http code. 查了下tornado的文档,是通过set_status方法来设置返回的http code,做个记录. self.set_st ...