从后向前建主席树,以位置为下标建树,然后查询区间出现次数的第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Ⅱ的更多相关文章

  1. [HDU5919]Sequence II

    [HDU5919]Sequence II 试题描述 Mr. Frog has an integer sequence of length n, which can be denoted as a1,a ...

  2. HDU5919 Sequence II(主席树)

    Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,⋯,ana1,a2,⋯,anThere are ...

  3. HDU5919:Sequence II

    题面 Vjudge Sol 给一个数列,有m个询问,每次问数列[l,r]区间中所有数的第一次出现的位置的中位数是多少,强制在线 主席树 询问区间内不同的数的个数 树上二分找到那个中位数 # inclu ...

  4. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  5. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  6. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  7. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  9. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

随机推荐

  1. Spark记录-Scala记录(基础程序例子)

    import scala.util.control._ object learnning { def main(args:Array[String]):Unit={ val n:Int=10 prin ...

  2. bzoj千题计划256:bzoj2194: 快速傅立叶之二

    http://www.lydsy.com/JudgeOnline/problem.php?id=2194 相乘两项的下标 的 差相同 那么把某一个反过来就是卷积形式 fft优化 #include< ...

  3. 【整理】Git相关资料

    http://book.51cto.com/art/201107/278731.htm http://book.51cto.com/art/201107/278828.htm  

  4. 利用JS实现图片的缓存

    web页面使用HTML的<img>元素来嵌入图片,和所有HTML元素一样,<img>元素也是可以通过脚本来操控的(设置元素的src属性,将其指向一个新的URL会导致浏览器载入并 ...

  5. Javascript摸拟自由落体与上抛运动 说明!

    JavaScript 代码 //**************************************** //名称:Javascript摸拟自由落体与上抛运动! //作者:Gloot //邮箱 ...

  6. java7,java8 中HashMap和ConcurrentHashMap简介

    一:Java7 中的HashMap 结构: HashMap 里面是一个数组,然后数组中每个元素是一个单向链表.链表中每个元素称为一个Entry 实例,Entry 包含四个属性:key, value, ...

  7. Anaconda+django写出第一个web app(七)

    今天来实现如何在页面弹出一些信息,比如注册成功后弹出注册成功的信息.这一点可以通过materialize里的Toasts来实现. django自带的messages可以告诉我们是否注册成功,以及注册失 ...

  8. 【Python】【持续项目】Python-安全项目搜集

    1.前言 Python发展以来,除了web安全方向,二进制方向也早已经积累有很多用Python写的项目.作为搜集者当然不能错过! 2.项目分类 安全编程 多功能Python键盘记录工具:Radium ...

  9. Nagios介绍

    Nagios介绍 Nagios是一款功能强大.优秀的开源监控系统,它能够让你发现和解决IT架构中存在的问题,避免这些问题影响到关键业务流程. Nagios最早于1999年发布,它在开源社区的影响力是相 ...

  10. Java与.NET的WebServices相互调用

    一:简介 本文介绍了Java与.NET开发的Web Services相互调用的技术.本文包括两个部分,第一部分介绍了如何用.NET做客户端调用Java写的Web Services,第二部分介绍了如何用 ...