[HDU5919]Sequence II
[HDU5919]Sequence II
试题描述
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.
输入
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:
输出
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的更多相关文章
- HDU5919 Sequence II(主席树)
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...
- HDU 5919 Sequence II 主席树
Sequence II Problem Description Mr. Frog has an integer sequence of length n, which can be denoted ...
- HDU 5919 Sequence II(主席树+逆序思想)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- HDOJ 5147 Sequence II 树阵
树阵: 每个号码的前面维修比其数数少,和大量的这后一种数比他的数字 再枚举每一个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others) ...
- bestcoder#23 1002 Sequence II 树状数组+DP
Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 5147 Sequence II 树状数组
Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- hdu 5147 Sequence II (树状数组 求逆序数)
题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Sequence II
6990: Sequence II 时间限制: 3 Sec 内存限制: 128 MB提交: 206 解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 We define an ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
随机推荐
- 【转载】(0, eval)(‘this’)
var window = this || (0, eval)('this') 在avalon源码中有这么一行代码,var window = this很容易理解 这里复习一下Global Object: ...
- 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering
http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...
- AJPFX:求两个城市之间的距离
键盘录入多个城市: 城市1,城市2,城市3 以 ### 结束输出然后再键盘录入各个城市之间的距离: 格式如下:0,12,4512,0,2245,22,0### 然后按照输入的两个城市,求得两个城市 ...
- 在js中怎样获得checkbox里选中的多个值?(jQuery)
思路:利用name属性值获取checkbox对象,然后循环判断checked属性(true表示被选中,false表示未选中).下面进行实例演示: 1.HTML结构 <input type=&qu ...
- java 获取ip地址
1.使用WIFI 首先设置用户权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"& ...
- 读取Chrome书签文件
使用C#读取Chrome浏览器的本地书签文件,当前文件在C盘下用户文件夹\AppData\Local\Google\Chrome\User Data\Default\下的Bookmarks 打开这个文 ...
- windows快捷键cmd中
windows 中cmd中命令: cls ---------> 清屏 dir ----------> 获取目录 Ctrl + c ----> 结束当前命令 cd .. ------ ...
- billu_b0x靶场刷题
https://www.vulnhub.com/ 里面有很多安全环境,只要下载相关镜像,在虚拟机上面搭建运行就可以练习对应靶场了. 第一步.信息收集 nmap扫描内网开放80端口的存活主机 nmap ...
- integer to roman leetcode c++实现
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 利用pyautogui自动化领取dnf的在线养竹活动的竹子
背景: Dnf的周年庆活动之一,鬼才策划为了在线率想的活动,规律如下 1.在线1分钟可以生成1根竹子,领取竹子以后可以获取到积分,积分满足活动要求后可以领取相应档位的奖励 2.玩家不在线期间,不会生成 ...