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 ...
随机推荐
- 洛谷P2759 奇怪的函数(log 二分)
题目描述 使得 x^xxx 达到或超过 n 位数字的最小正整数 x 是多少? 输入输出格式 输入格式: 一个正整数 n 输出格式: 使得 x^xxx 达到 n 位数字的最小正整数 x 输入输出样例 输 ...
- 懒下载软件,一行代码连接wifi^_^
按键盘的windows+R,输入cmd,回车键 设置语句netsh wlan set hostednetwork mode=allow ssid=user key=possword 按回车键 启动语句 ...
- C#基础-面向对象-封装
封装 命名空间 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp6 { c ...
- PHP表单安全过滤和防注入 htmlspecialchars() 和test_input()
什么是 htmlspecialchars() 函数? htmlspecialchars() 函数把特殊字符转换为 HTML 实体.这意味着 < 和 > 之类的 HTML 字符会被替换为 & ...
- 子查询,用户管理,pymysql使用
当我们的一条记录 分散不同的表中时,就需要进行多表查询例如 一对一 一对多 多对多 1.笛卡尔积查询 意思就是将两个表中的所有数据 全部关联在一起例如A表有两条 B表有三条 一共有6条会产生大量的错误 ...
- ### Cause: java.lang.reflect.UndeclaredThrowableException
### Cause: java.lang.reflect.UndeclaredThrowableException Caused by: org.apache.ibatis.exceptions.Pe ...
- spark中的RDD以及DAG
今天,我们就先聊一下spark中的DAG以及RDD的相关的内容 1.DAG:有向无环图:有方向,无闭环,代表着数据的流向,这个DAG的边界则是Action方法的执行 2.如何将DAG切分stage,s ...
- 12 Django组件-forms组件
forms组件 校验字段功能 针对一个实例:注册用户讲解. 模型:models.py class UserInfo(models.Model): name=models.CharField(max_l ...
- cocos2d-x 3.0的入门程序:helloworld
看过了这么多不同方向的应用,发现很多程序入门都是helloworldhelloworld是所有程序员的绝对初恋 先看一下程序的运行结果吧 然后就是他的工程代码 工程的目录有两个 Classes:程序中 ...
- Windows系统安装测试redis
因本人电脑是windows系统,从https://github.com/ServiceStack/redis-windows下载了兼容windows系统的redis 下载后直接解压到D:\redis目 ...