Sequence II

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1422    Accepted Submission(s): 362

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

题目链接:HDU 5919

区间内的求和+第K小的结合题,难点就在用倒序的方式维护第一次出现的位置,每一颗树都是维护原序列i~n的后缀,从后往前更新的时候把每一个位置都更新掉,这样第一次出现的位置就是最新的位置,然后统计的时候直接统计L~n即可,因为在p序列中L~R与L~n是等效的,后面多出现的无任何影响。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=2e5+7;
struct seg
{
int lson,rson;
int cnt;
inline void init()
{
lson=rson=cnt=0;
}
};
seg T[N*40];
int root[N],tot;
int arr[N],ans[N],pre[N]; void init(int n)
{
CLR(root,0);
tot=0;
T[n+1].init();
ans[0]=0;
CLR(pre,-1);
}
inline void update(int &cur,int ori,int l,int r,int pos,int v)
{
cur=++tot;
T[cur]=T[ori];
T[cur].cnt+=v;
if(l==r)
return ;
int mid=MID(l,r);
if(pos<=mid)
update(T[cur].lson,T[ori].lson,l,mid,pos,v);
else
update(T[cur].rson,T[ori].rson,mid+1,r,pos,v);
}
int query(int S,int E,int l,int r,int ql,int qr)
{
if(ql<=l&&r<=qr)
return T[E].cnt-T[S].cnt;
else
{
int mid=MID(l,r);
if(qr<=mid)
return query(T[S].lson,T[E].lson,l,mid,ql,qr);
else if(ql>mid)
return query(T[S].rson,T[E].rson,mid+1,r,ql,qr);
else
return query(T[S].lson,T[E].lson,l,mid,ql,mid)+query(T[S].rson,T[E].rson,mid+1,r,mid+1,qr);
}
}
int findkth(int S,int E,int l,int r,int k)
{
if(l==r)
return l;
else
{
int cnt=T[T[E].lson].cnt-T[T[S].lson].cnt;
int mid=MID(l,r);
if(k<=cnt)
return findkth(T[S].lson,T[E].lson,l,mid,k);
else
return findkth(T[S].rson,T[E].rson,mid+1,r,k-cnt);
}
}
int main(void)
{
int tcase,n,m,i,l,r,L,R;
scanf("%d",&tcase);
for (int q=1; q<=tcase; ++q)
{
scanf("%d%d",&n,&m);
init(n);
for (i=1; i<=n; ++i)
scanf("%d",&arr[i]);
int temp_rt=0;
for (i=1; i<=1; ++i)
{
if(pre[arr[i]]==-1)
update(root[i],root[i+1],1,n,i,1);
else
{
update(temp_rt,root[i+1],1,n,pre[arr[i]],-1);
update(root[i],temp_rt,1,n,i,1);
}
pre[arr[i]]=i;
}
for (i=1; i<=m; ++i)
{
scanf("%d%d",&l,&r);
L=(l+ans[i-1])%n+1;
R=(r+ans[i-1])%n+1;
if(L>R)
swap(L,R);
int D=query(root[n+1],root[L],1,n,L,R);
ans[i]=findkth(root[n+1],root[L],1,n,(D+1)/2);
}
printf("Case #%d:",q);
for (i=1; i<=m; ++i)
printf(" %d",ans[i]);
putchar('\n');
}
return 0;
}

HDU 5919 Sequence II(主席树+逆序思想)的更多相关文章

  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 (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

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

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

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

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

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

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

  6. HDU5919 Sequence II(主席树)

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

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

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

  8. HDU - 5919 Sequence II

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

  9. HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5919 题意:给出一串序列,每次给出区间,求出该区间内不同数的个数k和第一个数出现的位置(将这些位置组 ...

随机推荐

  1. PHP 汉字转拼音

    <?php/** *+------------------------------------------------------ * PHP 汉字转拼音 *+----------------- ...

  2. java 常见下载合集

    J2SE SDK (JDK): Windows: http://download.oracle.com/auth/otn-pub/java/jdk/6u25-b06/jdk-6u25-windows- ...

  3. 键盘按钮keyCode大全,keyCode列表对照表

    目录 [显示] 1js获取键盘按键对应的keyCode 2字母和数字键的键码值 3数字键盘上的键的键码值和功能键键码值 4控制键键码值 js获取键盘按键对应的keyCode <!doctype ...

  4. MySql 查询数据库中所有表名

    查询数据库中所有表名select table_name from information_schema.tables where table_schema='csdb' and table_type= ...

  5. MFC 重载退出(窗口顶上最右边的x按钮)

    其实可以在*Dlg.cpp中的BEGIN_MESSAGE_MAP中对IDCANCEL和自定义函数进行匹配就可以了. 如: 自定义的退出函数是OnClose(),则在BEGIN_MESSAGE_MAP中 ...

  6. Shell 脚本

    近期在别人的工作基础上完善了几个shell自动安装脚本. 1. 循环远程访问机器并安装 #!/bin/bash IpPrefix=. User=root Pwd= SMNIP=52.1.123.79 ...

  7. HDU 3089 (快速约瑟夫环)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3089 题目大意:一共n人.从1号开始,每k个人T掉.问最后的人.n超大. 解题思路: 除去超大的n之 ...

  8. iframe高度自适应内容

    JS自适应高度,其实就是设置iframe的高度,使其等于内嵌网页的高度,从而看不出来滚动条和嵌套痕迹.对于用户体验和网站美观起着重要作用. 如果内容是固定的,那么我们可以通过CSS来给它直接定义一个高 ...

  9. AJAX 跨域请求 - JSONP获取JSON数据

    Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流行术语为 Web 2.0 站点)的关键技术.Ajax 允许在不干扰 Web 应用程序的显示 ...

  10. NOI 题库 1792

    1792  迷宫 描述 一天Extense在森林里探险的时候不小心走入了一个迷宫,迷宫可以看成是由n * n的格点组成,每个格点只有2种状态,.和#,前者表示可以通行后者表示不能通行.同时当Exten ...