HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)
题意:
动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2那个。
思路:
主席树操作,这里的思路是从n到1开始建树。其他就是主席树查询区间第K小,计算区间不同值个数。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
#include <unordered_map>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
#define max3(a,b,c) max(max(a,b),c) typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = ;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = 2e5+;
struct node {
int l,r;
int sum;
} t[maxn*];
int tot;
int vis[maxn],rt[maxn],a[maxn]; void update(int l,int r,int &x, int y,int pos,int val){
tot++; x = tot;
t[x] = t[y];
t[x].sum += val;
if(l == r)return;
int mid = (l + r) >> ;
if(pos <= mid) update(l,mid,t[x].l,t[y].l,pos,val);
else update(mid+,r,t[x].r,t[y].r,pos,val);
} int query(int l,int r,int x,int L, int R){
if(l >= L && r<=R){
return t[x].sum;
}
int mid = (l + r) >> ;
int res = ;
if(mid >= L) res += query(l,mid,t[x].l,L,R);
if(mid < R) res += query(mid+,r,t[x].r,L,R);
return res;
} int getk(int l,int r,int x,int k){
if(l == r)return l;
int cnt = t[t[x].l].sum;
int mid = (l+r)>>;
if(cnt >= k) return getk(l,mid,t[x].l,k);
else return getk(mid+, r,t[x].r,k-cnt);
}
int main(){
int T,n,m; scanf("%d", &T);
for(int tt=; tt<=T; tt++){
tot = ;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++)scanf("%d", &a[i]);
memset(vis, -, sizeof(vis));
memset(rt, , sizeof(rt));
for(int i=n; i>=; i--){
if(vis[a[i]] == -){
update(,n,rt[i],rt[i+],i,);
}
else{
int tmp;
update(,n,tmp,rt[i+], vis[a[i]], -);
update(,n,rt[i],tmp,i,);
}
vis[a[i]] = i;
}
printf("Case #%d:", tt);
int la = ;
for(int i=; i<=m; i++){
int l,r,L,R; scanf("%d%d", &L, &R);
l = min((L+la)%n + , (R+la)%n + );
r = max((L+la)%n + , (R+la)%n + );
int k = query(,n,rt[l],l,r);
k = (k + ) / ;
la = getk(,n,rt[l],k);
printf(" %d", la);
}
puts("");
}
return ;
}
HDU - 5919
HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)的更多相关文章
- hdu 5919 Sequence II (可持久化线段树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5919 大致题意: 给你一个长度为n的序列,q个询问,每次询问是给你两个数x,y,经过与上一次的答案进行运算 ...
- 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 (主席树)
题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...
- HDU 5919 Sequence II(主席树+区间不同数个数+区间第k小)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5919 题意:给出一串序列,每次给出区间,求出该区间内不同数的个数k和第一个数出现的位置(将这些位置组 ...
- 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 - 5919 Sequence II
题意: 给定长度为n的序列和q次询问.每次询问给出一个区间(L,R),求出区间内每个数第一次出现位置的中位数,强制在线. 题解: 用主席树从右向左的插入点.对于当前点i,如果a[i]出现过,则把原位置 ...
- codeforces 1262D Optimal Subsequences 主席树询问第k小
题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...
- HDU 5919 Sequence II(可持久化线段树)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...
随机推荐
- shiro 和 spring boot 的集成
1 添加依赖 使用 shiro-spring-boot-web-starter 在 spring boot 中集成 shiro 只需要再添加一个依赖 <dependency> <gr ...
- 10分钟了解分布式CAP、BASE理论
CAP理论 2000年7月,Eric Brewer教授提出CAP猜想:2年后,Seth Gilbert和Nancy Lynch从理论上证明了CAP:之后,CAP理论正式成为分布式计算领域的公认定理. ...
- Chrome 使用 Evernote 插件
Chrome 插件不能登印象笔记进行裁剪,被困扰有段时间了.昨天偶然在知乎上找到了解决方法: 链接:https://www.zhihu.com/question/20340803/answer/291 ...
- poj2909 欧拉素数筛选
刚刚学了一种新的素数筛选法,效率比原先的要高一些,据说当n趋近于无穷大时这个的时间复杂度趋近O(n).本人水平有限,无法证明. 这是道水题,贴代码出来重点是欧拉筛选法.我把原来普通的筛选法贴出来. / ...
- Linux vim基本的使用方法
一.vim 的三种模式 (1) 插入模式 在插入模式中,才能输入文字:要进入插入模式,可以按键 “i”:如果要进入插入模式时,直接切换到下一行,可以输入“o”: (2) 命令模式 在命令模式中,主要进 ...
- 有容云-【原理】Docker存储驱动之AUFS
编者按:今天聊一聊Docker的Image(镜像)与Container(容器)的存储以及存储驱动之AUFS. Docker存储驱动简介 Docker内置多种存储驱动,每种存储驱动都是基于Linux ...
- JVM和GC的工作原理
转载于https://uestc-dpz.github.io JVM Java 虚拟机 Java 虚拟机(Java virtual machine,JVM)是运行 Java 程序必不可少的机制.JVM ...
- mysql 查询结果显示行号
mysql 查询时,不像oracle那样,可以直接用 rownum 显示结果行号. 可以用定义用户变量来实现 set @myrnum = 0; select (@myrnum := @myrnum + ...
- Re-Architecting the Video Gatekeeper(一)
原文 https://medium.com/netflix-techblog/re-architecting-the-video-gatekeeper-f7b0ac2f6b00 本文介绍了了内容配置工 ...
- RE最全面的正则表达式----字符验证
二.校验字符的表达式汉字:^[一-彪]{0,}$英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$长度为3-20的所有字符:^.{3,20}$由26个英文字母组成的字 ...