从后向前建主席树,以位置为下标建树,然后查询区间出现次数的第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. C语言复习---用筛选法求100之内的素数

    #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int i, j; ] ...

  2. bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...

  3. 如何将U盘转化成NTFS格式

    拷贝数据到U盘,出现 出现错误0x80070052:无法创建目录或文件 然后发现应该是U盘为fat32格式的,感觉应该是这个问题 方法一:如果你是新买的U盘,或者U盘内数据已经备份到电脑,可以使用该条 ...

  4. alloffthelights使用方法

    官网上的demo没有用,因为官网用的jquery是谷歌的CDN,download下来的demo也没有用,因为demo的路径下少了jquery.所以自己写demo的时候要把jquery和alloffth ...

  5. CSS media--(来自网易)

    概述 通过媒体查询为不同的设备和大小配置不同的样式.代码展示 /* media */ /* 横屏 */ @media screen and (orientation:landscape){ } /* ...

  6. 20155306 2016-2017-2 《Java程序设计》第5周学习总结

    20155306 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承架构 Java中所有错误都会被打包为对象,运用try.c ...

  7. PHP复制文件夹及文件夹内的文件

    //1.取被复制的文件夹的名字://2.写出新的文件夹的名字://3.调用此函数,将旧.新文件夹名字作为参数传递://4.如需复制文件夹内的文件,第三个参数传1,否则传0: public functi ...

  8. Informatica学习:3、用户创建与权限管理

    环境:win7 下安装Informatica 9.6.1 服务器端与客户端作为学习之用,Linux大同小异 一.用户创建(服务器端) 1.登陆admin console (1)打开Admin Cons ...

  9. ZYNQ. LwIP.PHY.KSZ9031RNX

    新一块板子 米尔科技的 z-turn  使用的PHY芯片是Micrel的 KSZ9031RNX 而不是zedboard上的Marvell的. 直接使用lwip的echo server demo时会报错 ...

  10. consul服务发现和配置共享的软件,

    Consul 是什么 consul是一个支持多数据中心分布式高可用服务发现和配置共享的服务软件,由HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License 2. ...