题目链接

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.

 
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:

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.

 
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),设区间中有k个不同的数,它们在区间中第一次出现的位置为p1,p2 ,……,pk 并且将它们排序p1<p2<……<pk,现在求p[(k+1)/2]的值?
 
思路:主席树记录当前数出现的位置,即每次在当前数出现的位置(下标)+1,对于序列数a[1]~a[n]建立主席树时,如果当前这个数之前出现过,那么在当前这个版本线段树上对前一次出现的位置-1,在当前位置+1,这样就可以求出区间不同数的个数。现在要求出区间第k大,那么可以从a[n]~a[1]建立线段树,然后直接求k大就行。
 
代码如下:
#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大)的更多相关文章

  1. HDU 5919 Sequence II 主席树

    Sequence II Problem Description   Mr. Frog has an integer sequence of length n, which can be denoted ...

  2. 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[ ...

  3. hdu 5919 Sequence II (可持久化线段树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...

  4. HDU - 5919 Sequence II

    题意: 给定长度为n的序列和q次询问.每次询问给出一个区间(L,R),求出区间内每个数第一次出现位置的中位数,强制在线. 题解: 用主席树从右向左的插入点.对于当前点i,如果a[i]出现过,则把原位置 ...

  5. HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

    HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...

  6. HDU 5919 Sequence II(主席树+逆序思想)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  7. HDU 5919 -- Sequence II (主席树)

    题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...

  8. HDU5919 Sequence II(主席树)

    Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...

  9. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

随机推荐

  1. 理解Vue中的Render渲染函数

    理解Vue中的Render渲染函数 VUE一般使用template来创建HTML,然后在有的时候,我们需要使用javascript来创建html,这时候我们需要使用render函数.比如如下我想要实现 ...

  2. js验证input输入框(字母,数字,符号,中文)

    [javascript]代码库 <h1>js验证输入框内容</h1> <br /> <br /> 只能输入英文 <input type=" ...

  3. Python学习日记day10------函数的命名空间、作用域与闭合函数

    1,参数陷阱 如果默认参数的只是一个可变数据类型,那么每一次调用的时候,如果不传值就共用这个数据类型的资源. 2,三元运算 c=a if a>b else b#如果a>b返回a,否则,返回 ...

  4. Java 多线程笔记

    资料来源于网络,仅供参考学习.   1.A Java program ends when all its threads finish (more specifically, when all its ...

  5. Automata

    A deterministic finite automaton is represented formally by a 5-tuple (Q,Σ,δ,q0,F), where: Q is a fi ...

  6. [编织消息框架][JAVA核心技术]异常基础

    Java异常体系结构 Thorwable类所有异常和错误的超类,有两个子类Error和Exception,分别表示错误和异常. 其中异常类Exception又分为运行时异常(RuntimeExcept ...

  7. 6、投资的一些思考 - CEO之公司管理经验谈

    对于投资,前面笔者写过一个文:IT人经济思维之投资 - 创业与投资系列文章 ,里面列举了笔者自己做过的投资方面的内容.今天就说说公司投资的一些思考问题. 公司投资的问题,笔者还是那句话:关键是找出适合 ...

  8. Linux 下Beanstalk安装

    1.安装 # wget https://github.com/kr/beanstalkd/archive/v1.10.tar.gz # tar xzvf v1.10 # cd beanstalkd-1 ...

  9. Flask快速入门,知识整理

    一.Flask介绍(轻量级的框架,非常快速的就能把程序搭建起来) Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是 ...

  10. spring boot RESTFul API拦截 以及Filter和interceptor 、Aspect区别

    今天学习一下RESTFul api拦截 大概有三种方式 一.通过Filter这个大家很熟悉了吧,这是java规范的一个过滤器,他会拦截请求.在springboot中一般有两种配置方式. 这种过滤器拦截 ...