[HDU5919]Sequence II

试题描述

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 al_i,al_(i+1),al_(i+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)k_i (in ascending order, i.e.,p(i)1<p(i)2<⋯<p(i)k_i).

Note that ki is the number of different integers in this subsequence. You should output p(i)⌈ki/2⌉for the i-th query.

输入

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}

输出

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.

输入示例


输出示例

Case #:
Case #:

数据规模及约定

见“输入

题解

就是这道题再强行套一个二分。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 200010
#define maxnode 4000010 int ToT, sumv[maxnode], lc[maxnode], rc[maxnode];
void update(int& y, int x, int l, int r, int p) {
sumv[y = ++ToT] = sumv[x] + 1;
if(l == r) return ;
int mid = l + r >> 1; lc[y] = lc[x]; rc[y] = rc[x];
if(p <= mid) update(lc[y], lc[x], l, mid, p);
else update(rc[y], rc[x], mid + 1, r, p);
return ;
}
int query(int o, int l, int r, int qr) {
if(!o) return 0;
if(r <= qr) return sumv[o];
int mid = l + r >> 1, ans = query(lc[o], l, mid, qr);
if(qr > mid) ans += query(rc[o], mid + 1, r, qr);
return ans;
} int rt[maxn], lstp[maxn], ANS[maxn], cnt; int len;
char Out[maxn*7];
int main() {
int T = read();
for(int kase = 1; kase <= T; kase++) {
memset(lstp, 0, sizeof(lstp));
memset(sumv, 0, sizeof(sumv));
memset(lc, 0, sizeof(lc));
memset(rc, 0, sizeof(rc));
memset(rt, 0, sizeof(rt));
ToT = 0;
int n = read(), q = read();
for(int i = 1; i <= n; i++) {
int v = read();
update(rt[i], rt[i-1], 0, n, lstp[v]);
lstp[v] = i;
} cnt = 0;
int lst = 0;
while(q--) {
int ql = (read() + lst) % n + 1, qr = (read() + lst) % n + 1;
if(ql > qr) swap(ql, qr);
int l = ql, r = qr, k = query(rt[qr], 0, n, ql - 1) - query(rt[ql-1], 0, n, ql - 1) + 1 >> 1, lval = query(rt[ql-1], 0, n, ql - 1);
while(l < r) {
int mid = l + r >> 1;
if(query(rt[mid], 0, n, ql - 1) - lval < k)
l = mid + 1;
else r = mid;
}
ANS[++cnt] = lst = l;
} printf("Case #%d: ", kase);
len = 0;
int num[10], cntn;
for(int i = 1; i <= cnt; i++) {
int tmp = ANS[i];
if(!tmp) Out[len++] = '0';
cntn = 0; while(tmp) num[++cntn] = tmp % 10, tmp /= 10;
for(int j = cntn; j; j--) Out[len++] = num[j] + '0';
if(i < cnt) Out[len++] = ' ';
}
Out[len] = '\0';
puts(Out);
} return 0;
}

[HDU5919]Sequence II的更多相关文章

  1. HDU5919 Sequence II(主席树)

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

  2. HDU 5919 Sequence II 主席树

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

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

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

  4. HDOJ 5147 Sequence II 树阵

    树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    ...

  5. bestcoder#23 1002 Sequence II 树状数组+DP

    Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. hdu 5147 Sequence II 树状数组

    Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  7. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. Sequence II

    6990: Sequence II 时间限制: 3 Sec  内存限制: 128 MB提交: 206  解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 We define an ...

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

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

随机推荐

  1. [已读]HTML5与CSS3权威指南第二版(下)

    去年下半年前公司给买的(老付对我们确实蛮好的),一人挑一本,我当时一定是秀逗了.看的时候就发现,这本书的内容过时严重,即便它是新出不久的第.二.版.其他没什么说的,总之,不推荐看.

  2. [已读]编写高质量代码 改善JavaScript程序的188个建议

    吐槽一万遍,买的最后悔的一本,没有之一,大量篇幅抄袭<高性能javascript>,我记得还有部分抄袭<javascript精粹>,<javascript模式>有没 ...

  3. scau 1144 数星星 bit + 扫描线的思想

    这题如果用二维树状数组,则会直接爆内存. 那么可以运用扫描线的思路. 就是,它同时被x和y限制了,那么可以在查询的时候,确保x先满足了,(把x按小到大排序) 然后就相当于是关于y的一个一维bit了, ...

  4. C/S WinForm自动升级

    这二天刚好完成一个C/S 自动升级的功能 代码分享一下 /// <summary>    /// 版本检测    /// </summary>    public class ...

  5. Mac OS OneDrive 无法同步.DS_Store文件 出错

    Mac OS OneDrive 无法同步.DS_Store文件 同步出错 解决方案: 第一步:打开 terminal 窗口,到本地同步的目录下: cd "/Users/gkjglobal/G ...

  6. hdu 2192 MagicBuilding

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  7. laravel homestead comoser install 报错

    项目部署的时候composer install报错 说那个依赖包没有安装成功需要回滚删除但是删除不了 解决: 要配置共享文件 注:使用 NFS 的话,需要安装 vagrant-winnfsd 插件.该 ...

  8. Vue 在beaforeCreate时获取data中的数据

    众所周知,vue在beforecreate时期是获取不到data中的 数据的 但是通过一些方法可以实现在beforecreate时获取到data中的数据 暂时想到两种放发可以实现,vue在before ...

  9. 指针-AC自动机

    大家都不喜欢指针,但是这个AC自动机仿佛不用不行…… 先引用我最喜欢的话:“AC自动机,不是自动AC的机器.” 如果写不好还可能一直WA AC自动机是KMP与Trie树的完美结合,适用于多字符串匹配, ...

  10. Python基础2 列表 元祖 字符串 字典 集合 文件操作 -DAY2

    本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...