Sequence II


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

 
题意:
  给定一个序列nn,有mm次查询,每次查询一个区间[l,r][l,r],求区间中每一种数在区间中第一次出现的位置的中位数,强制在线。
题解:
  强制在线
  利用主席树求区间不同数的个数
  这里有个技巧
  倒着插入主席树
  在寻找位置的中位数上就可以一个log解决了
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 2e5 + , M = 1e6, mod = 1e9+, inf = 2e9; int T,n,q,a[N],l[N*],r[N*],v[N*],sz,root[N],ans[N],last[N]; void update(int x, int &y, int ll, int rr, int k,int c) {
y = ++sz;
v[y] = v[x] + c;
r[y] = r[x];
l[y] = l[x];
if(ll == rr) return ;
int mid = (ll+rr)>>;
if(k <= mid) update(l[x], l[y], ll, mid, k,c);
else update(r[x], r[y], mid + , rr, k,c);
}
int query(int x,int ll,int rr,int s,int t) {
if (s <= ll && rr <= t) return v[x];
int mid = ll + rr >> , res = ;
if (s <= mid) res += query(l[x], ll, mid, s, t);
if (t > mid) res += query(r[x], mid + , rr, s, t);
return res;
} int finds(int x,int ll,int rr,int k) {
if(ll == rr) return ll;
int mid = ll + rr >> ;
if(v[l[x]] >= k) return finds(l[x],ll,mid,k);
else return finds(r[x],mid+,rr,k-v[l[x]]);
} int main() {
int cas = ;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&q);
sz = ;
memset(last,,sizeof(last));
memset(v,,sizeof(v));
memset(l,,sizeof(l));
memset(r,,sizeof(r));
ans[] = ;
root[n+] = ;
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = n; i >= ; --i) {
int x = root[i] = ;
update(root[i+],x,,n,i,);
if(last[a[i]]) {
update(x,root[i],,n,last[a[i]],-);
} else root[i] = x;
last[a[i]] = i;
}
int L,R;
for(int i = ; i <= q; ++i) {
scanf("%d%d",&L,&R);
L = ((L + ans[i-])%n) + ;
R = ((R + ans[i-])%n) + ;
if(L > R) swap(L,R);
int sum = query(root[L],,n,L,R) + >> ;
ans[i] = finds(root[L],,n,sum);
}
printf("Case #%d: ",++cas);
for(int i = ; i < q; ++i) printf("%d ",ans[i]);
cout<<ans[q]<<endl;
}
return ;
}

HDU 5919 Sequence II 主席树的更多相关文章

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

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

  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. jira与mysql的配合搭建调整

    jira与mysql数据的整合 第一步:电脑上安装mysql数据库,不做多得解释,自己搞定 第二步: 在mysql数据库中建一个名为 jiradb的数据库,账号 root 密码 root 编码格式 u ...

  2. delphi控件属性大全-详解-简介

    http://blog.csdn.net/u011096030/article/details/18716713 button 组件: CAPTION 属性 :用于在按钮上显示文本内容 Cancel ...

  3. 2106 Problem F Shuffling Along 中石油-未提交-->已提交

    题目描述 Most of you have played card games (and if you haven’t, why not???) in which the deck of cards ...

  4. Mathematics:Find a multiple(POJ 2356)

    找组合 题目大意:给你N个自然数,请你求出若干个数的组合的和为N的整数倍的数 经典鸽巢原理题目,鸽巢原理的意思是,有N个物品,放在N-1个集合中,则一定存在一个集合有2个元素或以上. 这一题是说有找出 ...

  5. Mathematics:Prime Path(POJ 3126)

    素数通道 题目大意:给定两个素数a,b,要你找到一种变换,使得每次变换都是素数,如果能从a变换到b,则输出最小步数,否则输出Impossible 水题,因为要求最小步数,所以我们只需要找到到每个素数的 ...

  6. 【linux】ps

    来源:http://blog.chinaunix.net/uid-25681671-id-3201927.html Linux下PS命令详解 要对系统中进程进行监测控制,查看状态,内存,CPU的使用情 ...

  7. 【XLL API 函数】xlGetName

    以字符串格式返回 DLL 文件的长文件名. 原型 Excel12(xlGetName, LPXLOPER12 pxRes, 0); 参数 这个函数没有参数 属性值和返回值 返回文件名和路径 实例 \S ...

  8. ODBC连接发生错误:未发现数据源名称并且未指定默认驱动程序

    程序在使用ODBC方式连接数据库时发生错误: ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序. 什么原因造成的呢? 本人使用&l ...

  9. [Android Pro] Android下toolbox简介

    toolbox是Android 自带的提供shell命令的软件.有点类似于busybox,但功能上好像弱很多.其源码可以从Android source code 中system/core/toolbo ...

  10. September 26th 2016 Week 40th Monday

    The land didn't move, but moved. The sea wasn't still, yet was still. 大地止而亦行,大海动而亦静. Still waters ru ...