HDU - 5919 Sequence II
题意:
给定长度为n的序列和q次询问。每次询问给出一个区间(L,R),求出区间内每个数第一次出现位置的中位数,强制在线。
题解:
用主席树从右向左的插入点。对于当前点i,如果a[i]出现过,则把原位置-1,i处+1。这样保证了每个点只出现1次。
对于询问区间(L,R),求出L节点[L,R]的值即为区间内有多少不同的数。最后就是主席树求k_th的操作。倒着插省去了二分的复杂度。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 2e5+;
int t, n, q, tot;
int l, r;
int a[N];
int root[N], vis[N];
int ans[N];
struct node {
int l, r, sum;
}tre[N*];
void update(int l, int r, int &x, int y, int pos, int val) {
tre[++tot] = tre[y];
tre[tot].sum += val;
x = tot;
if(l==r) return ;
int mid = l+r>>;
if(pos<=mid) update(l, mid, tre[x].l, tre[y].l, pos, val);
else update(mid+, r, tre[x].r, tre[y].r, pos, val);
}
int query(int l, int r, int x, int ql, int qr) {
if(ql<=l&&r<=qr) return tre[x].sum;
int mid = l+r>>;
int res = ;
if(ql<=mid) res += query(l, mid, tre[x].l, ql, qr);
if(qr>mid) res += query(mid+, r, tre[x].r, ql, qr);
return res;
}
int find_kth(int l, int r, int x, int k) {
if(l==r) return l;
int mid = l+r>>;
int t = tre[tre[x].l].sum;
if(t>=k) return find_kth(l, mid, tre[x].l, k);
return find_kth(mid+, r, tre[x].r, k-t);
}
int main() {
scanf("%d", &t);
for(int casee = ; casee <= t; casee++) {
memset(vis, , sizeof(vis));
tot = ;
scanf("%d%d", &n, &q);
root[n+] = ;
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = n; i >= ; i--) {
update(, n, root[i], root[i+], i, );
if(vis[a[i]]) update(, n, root[i], root[i], vis[a[i]], -);
vis[a[i]] = i;
}
ans[] = ;
printf("Case #%d: ", casee);
for(int i = ; i <= q; i++) {
scanf("%d%d", &l, &r);
int tt = l;
l = min((l+ans[i-])%n+, (r+ans[i-])%n+);
r = max((tt+ans[i-])%n+, (r+ans[i-])%n+);
int k = query(, n, root[l], l, r);
ans[i] = find_kth(, n, root[l], (k+)/);
printf("%d", ans[i]);
if(i!=q) printf(" ");
}
puts("");
}
}
HDU - 5919 Sequence II的更多相关文章
- HDU 5919 Sequence II(主席树+逆序思想)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- 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 (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)
HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...
- HDU 5919 Sequence II(可持久化线段树)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...
- hdu 5919 Sequence II (可持久化线段树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...
- HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5919 题意:给出一串序列,每次给出区间,求出该区间内不同数的个数k和第一个数出现的位置(将这些位置组 ...
- HDU 5919 -- Sequence II (主席树)
题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...
- 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[ ...
- hdu 5147 Sequence II 树状数组
Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
随机推荐
- JSPatch库, 一个Apple官方支持的实现在线更新iOS应用的库
简介 项目主页: https://github.com/bang590/JSPatch 示例下载: https://github.com/ios122/ios122 JSPatch 可以让你用 Jav ...
- tree树形
/** * tree * @param menuBeans * @param pid * @return */ public JSON makeTree(List<MenuBean& ...
- Java分享笔记:FileOutputStream流的write方法
/*------------------------ FileOutputStream: ....//输出流,字节流 ....//write(byte[] b)方法: 将b.length个字节从指定字 ...
- Salt-ssh 自动安装salt-minion
作用:为了不手动去安装一台一台去salt-minion,并进重复的配置 一.环境 系统环境: #cat /etc/redhat-release CentOS Linux release 7.4.170 ...
- py2exe安装使用
一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序. py2e ...
- Ajax全解析
什么是Ajax Ajax基本概念 Ajax(Asynchronous JavaScript and XML):翻译成中文就是异步的JavaScript和XML. 从功能上来看是一种在无需重新加载整个网 ...
- uniqueidentifier数据类型转换
cast(id as varchar(36))
- Flume是什么
分布式流式实时收集日志文件系统,便于实时在线的流式计算,常配合 Storm 和 spark streming 使用. Flume is a distributed分布式的, reliable可靠的, ...
- 1,Python常用库之一:Numpy
Numpy支持大量的维度数组和矩阵运算,对数组运算提供了大量的数学函数库! Numpy比Python列表更具优势,其中一个优势便是速度.在对大型数组执行操作时,Numpy的速度比Python列表的速度 ...
- 自己定义的TryParse()
out 参数的练习,自己定义的TryParse()... 一开始写错了,错在:判断str[i] >= '0'&& str[i]<='9'时,把str[i]当数字去判断了.. ...