HDU5919 SequenceⅡ
从后向前建主席树,以位置为下标建树,然后查询区间出现次数的第k/2大即可。
复杂度O(nlogn)
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+;
int num,ans[N],T,pre[N],rt[N],a[N];
struct node
{
int s,l,r;
}t[N*];
void init()
{
memset(rt,,sizeof(rt));num=;
memset(pre,,sizeof(pre));
}
void change(int &x,int y,int l,int r,int p,int w)
{
x=++num;t[x]=t[y];
if(l==r){t[x].s+=w;return;}
int m=l+r>>;
if(m<p)change(t[x].r,t[y].r,m+,r,p,w);
else change(t[x].l,t[y].l,l,m,p,w);
t[x].s=t[t[x].l].s+t[t[x].r].s;
}
int query(int x,int l,int r,int L,int R)
{
if(l==L&&r==R)return t[x].s;
int m=l+r>>;
if(L>m)return query(t[x].r,m+,r,L,R);
else if(m>=R)return query(t[x].l,l,m,L,R);
else return query(t[x].l,l,m,L,m)+query(t[x].r,m+,r,m+,R);
}
int find(int x,int l,int r,int p)
{
if(l==r)return l;
int m=l+r>>;
if(t[t[x].l].s>=p)return find(t[x].l,l,m,p);
return find(t[x].r,m+,r,p-t[t[x].l].s);
}
int main()
{
scanf("%d",&T);int n,m,cnt;
for(int ii=;ii<=T;++ii)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<=n;++i)scanf("%d",&a[i]);
for(int i=n;i;--i)
{
change(rt[i],rt[i+],,n,i,);
if(pre[a[i]])change(rt[i],rt[i],,n,pre[a[i]],-);
pre[a[i]]=i;
}ans[]=;
for(int i=;i<=m;++i)
{
int x,y;
scanf("%d%d",&x,&y);
x=(x+ans[i-])%n+;
y=(y+ans[i-])%n+;
if(x>y)swap(x,y);
int k=query(rt[x],,n,x,y);
ans[i]=find(rt[x],,n,(k+)/);
}
printf("Case #%d:",ii);
for(int i=;i<=m;++i)printf(" %d",ans[i]);
puts("");
}
return ;
}
HDU5919 SequenceⅡ的更多相关文章
- [HDU5919]Sequence II
[HDU5919]Sequence II 试题描述 Mr. Frog has an integer sequence of length n, which can be denoted as a1,a ...
- HDU5919 Sequence II(主席树)
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...
- HDU5919:Sequence II
题面 Vjudge Sol 给一个数列,有m个询问,每次问数列[l,r]区间中所有数的第一次出现的位置的中位数是多少,强制在线 主席树 询问区间内不同的数的个数 树上二分找到那个中位数 # inclu ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
随机推荐
- Spark记录-源码编译spark2.2.0(结合Hive on Spark/Hive on MR2/Spark on Yarn)
#spark2.2.0源码编译 #组件:mvn-3.3.9 jdk-1.8 #wget http://mirror.bit.edu.cn/apache/spark/spark-2.2.0/spark- ...
- Facebook人工智能实验室的前世今生
Facebook人工智能实验室的前世今生 是时候停止把Facebook当作纯粹的社交媒体公司来看了.它用无人机提供互联网服务,为了发展虚拟现实而收购Oculus,不懈追求人工智能,Facebook已经 ...
- js 正则学习小记之匹配字符串字面量优化篇
昨天在<js 正则学习小记之匹配字符串字面量>谈到 个字符,除了第一个 个,只有 个转义( 个字符),所以 次,只有 次成功.这 次匹配失败,需要回溯后用 [^"] 才能匹配成功 ...
- html5 canvas用图案填充形状
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- spring事务详解(二)实例
在Spring中,事务有两种实现方式: 编程式事务管理: 编程式事务管理使用底层源码可实现更细粒度的事务控制.spring推荐使用TransactionTemplate,典型的模板模式. 申明式事务管 ...
- Identical Binary Tree
Check if two binary trees are identical. Identical means the two binary trees have the same structur ...
- linux下查看各硬件型号
查看主板型号 # dmidecode |grep -A 8 "System Information"System Information 上网查DELL CS24-TY,找到说主板 ...
- 在windows上实现多个java jdk的共存解决办法
转自:https://www.cnblogs.com/jianyungsun/p/6918024.html 分析问题 为了多快好省的解决当前的问题,我的想法是在windows中同时安装jdk1.6和j ...
- 爬虫、框架scrapy
阅读目录 一 介绍 二 安装 三 命令行工具 四 项目结构以及爬虫应用简介 五 Spiders 六 Selectors 七 Items 八 Item Pipeline 九 Dowloader Midd ...
- Project Euler Problem1
Multiples of 3 and 5 Problem 1 If we list all the natural numbers below 10 that are multiples of 3 o ...