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

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.


Input

In 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:

li=min{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}li=min{(li‘+ansi−1) mod n+1,(ri‘+ansi−1) mod n+1}
ri=max{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}ri=max{(li‘+ansi−1) mod n+1,(ri‘+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,⋯,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

给出n个数,然后 m 个查询,查询对应区间的不同的数的个数 k, 以及第 (k+1)/2 个数的位置。

正序插入的主席树可以很容易的求出区间内不同数的个数,但是在求第 (k+1)/2 个数的位置时,由于树中有 L 之前的信息,不好直接查询出。

逆序插入的主席树和正序一样,都可以求出区间内不同数的个数,但在求第 (k+1)/2 个数的时候,没有 L 之前的信息,所以可以把问题转换成求这棵树上第 (k+1)/2 大的位置,就是普通的线段树了。

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std; int n, m;
int cas, tol, T; struct Node{
int l, r;
int cnt;
} node[maxn * 40];
int a[maxn];
int rt[maxn];
int last[maxn]; void init() {
tol = 0;
mes(rt, 0);
mes(last, -1);
} void update(int l, int r, int &x, int y, int p, int v) {
x = ++tol;
node[x] = node[y];
node[x].cnt += v;
if(l == r) return ;
int mid = l + r >> 1;
if(p <= mid)
update(l, mid, node[x].l, node[y].l, p, v);
else
update(mid+1, r, node[x].r, node[y].r, p, v);
} int query_num(int l, int r, int pl, int pr, int rt) {
if(pl <= l && r <= pr) {
return node[rt].cnt;
}
int mid = l + r >> 1;
int ans = 0;
if(pl <= mid)
ans += query_num(l, mid, pl, pr, node[rt].l);
if(pr > mid)
ans += query_num(mid+1, r, pl, pr, node[rt].r);
return ans;
} int query_pos(int l, int r, int x, int k) {
if(l == r) return l;
int mid = l + r >> 1;
int cnt = node[node[x].l].cnt;
if(k <= cnt)
return query_pos(l, mid, node[x].l, k);
else
return query_pos(mid+1, r, node[x].r, k-cnt);
} int main() {
cas = 1;
scanf("%d", &T);
while(T--) {
init();
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++) {
scanf("%d", &a[i]);
}
node[0].cnt = 0;
for(int i=n; i>=1; i--) {
if(last[a[i]] == -1) {
update(1, n, rt[i], rt[i+1], i, 1);
} else {
int tmp;
update(1, n, tmp, rt[i+1], last[a[i]], -1);
update(1, n, rt[i], tmp, i, 1);
}
last[a[i]] = i;
}
printf("Case #%d:", cas++);
int ans = 0;
while(m--) {
int l, r;
scanf("%d%d", &l, &r);
int tl = min((l+ans)%n+1, (r+ans)%n+1);
int tr = max((l+ans)%n+1, (r+ans)%n+1);
int k = query_num(1, n, tl, tr, rt[tl]);
k = (k + 1) / 2;
ans = query_pos(1, n, rt[tl], k);
printf(" %d", ans);
}
printf("\n");
}
return 0;
}

  

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

  1. hdu 5919 主席树(区间不同数的个数 + 区间第k大)

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

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

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

  3. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  4. HDU 2655 主席树

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 2665(主席树,无修改第k小)

    Kth number                                                 Time Limit: 15000/5000 MS (Java/Others)   ...

  6. #194 sequence(搜索+动态规划+主席树)

    考虑按顺序暴搜子序列.如果序列中的数两两不同,显然每次给上一个找到的子序列添上后缀最小值,即为下一个要找的子序列.如果不能再加了就回溯继续考虑后缀次小.第三小……值,直到找到k个子序列. 有重复的数后 ...

  7. HDU 2852 主席树

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  9. HDU 3333 & 主席树

    题意: balabala SOL: 这题用主席树怎么做呢...貌似一模一样...一个一个建n棵的线段树.先把上一棵树复制下来,当a[i]出现过,就把这棵树里的那个位置去掉------一模一样的思维.. ...

随机推荐

  1. shell脚本批量ssh登陆主机并执行命令

    shell脚本批量ssh登陆主机并执行命令 今天在客户现场遇到了这个问题,客户没有管理工具,无法批量登陆主机下发命令,几个个C段啊,让我一个一个登陆,.................. 所以写了个s ...

  2. 网络中的NAT模式

    一.概述 NAT英文全称是"Network Address Translation",中文意思是"网络地址转换",它是一个IETF(Internet Engin ...

  3. LeetCode算法题-Max Consecutive Ones(Java实现)

    这是悦乐书的第242次更新,第255篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第109题(顺位题号是485).给定二进制数组,找到此数组中连续1的最大数量.例如: 输 ...

  4. LInkedHashMap实现最近被使用(LRU)缓存

    在最近的面试中,我曾被多次问到,怎么实现一个最近最少使用(LRU)的缓存.缓存可以通过哈希表来实现,然而为这个缓存增加大小限制会变成另一个有意思的问题.现在我们看一下怎么实现. 最近最少使用缓存的回收 ...

  5. 运行ConnectionDemo时遇到的问题及解决方案

    20175227张雪莹 2018-2019-2 <Java程序设计> 运行ConnectionDemo时遇到的问题及解决方案 老师博客上提供确认数据库连接的代码 import static ...

  6. 初识服务发现及Consul框架的简单使用

    初识服务发现及Consul框架的简单使用   1.什么是服务发现? 服务发现组件记录了(大规模)分布式系统中所有服务的信息,人们或者其它服务可以据此找到这些服务. DNS 就是一个简单的例子. 当然, ...

  7. PuTTY 串口调试,为普通用户增加访问串口设备权限

    一般情况下,只有 root 用户可以使用 PuTTY 访问串口设备,如果要为普通用户增加访问串口设备的权限,可按如下步骤进行: (以 Ubuntu 14.04.3 系统为例,第一个串口设备,会被识别为 ...

  8. bsxfun

    By HYB bsxfun(fun,A,B)偶然间发现了这个函数,强大得不得了呀,它的作用是:对两个矩阵A和B之间的每一个元素进行指定的计算(函数fun指定):并且具有自动扩维的作用 例如,A是一个4 ...

  9. 【原创】微服务为什么一定要用docker

    引言 早在2013年的时候,docker就已经发行,然而那会还是很少人了解docker.一直到2014年,Martin Fowler提出了微服务的概念,两个不相干的技术终于走在了一起,创造了今天的辉煌 ...

  10. 相约南湖,南京都昌信息亮相南湖HIT论坛

    金秋十月,雨过南湖水似油 ,烟雾蒙蒙净长空 2017年10月15日, 南湖HIT论坛迎来了第六届.本次论坛吸引了500名来自全国各地医疗机构.卫生行政主管部门的信息化主管和医疗IT企业的精英,齐聚嘉兴 ...