从后向前建主席树,以位置为下标建树,然后查询区间出现次数的第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. Python的常用内置函数介绍

    Python的常用内置函数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.取绝对值(abs) #!/usr/bin/env python #_*_coding:utf-8_ ...

  2. 【转载】SQL注入原理讲解

    这几篇文章讲的都很不错,我看了大概清除了sql注入是怎么一回事,打算细细研究一下这个知识,另写一篇博客: 原文地址:http://www.cnblogs.com/rush/archive/2011/1 ...

  3. 工具类 | window批处理杀死指定端口进程

    window批处理杀死指定端口进程,注意保存时使用ansi格式,运行输入端口即可 @echo off setlocal enabledelayedexpansion set /p port=请输入端口 ...

  4. CF876 F 思维 枚举

    给你n个数,问有几个区间满足,区间内或操作大于区间内的任意数. 首先可以知道,两数或操作的结果必定不会小于两者间的最大值,也就是说对于一个区间中,不合法的状态只有两值或相等.那么我们可以考虑枚举每个数 ...

  5. [整理]Visual Studio 的Application Insights

    简单介绍 Application Insights(预览版) Visual Studio 的Application Insights插件简介 Application Insights for Visu ...

  6. AngularJs -- ngMessages(1.3+)

    ngMessages(1.3+) 表单和验证是AngularJS中复杂的组件之一.用AngularJS默认的方式来写,不是特别好,不简洁. 在AngualrJS1.3发布前,表单验证必须以这种方式编写 ...

  7. 20145226 《Java程序设计》第七周学习总结

    教材学习内容总结 学习目标 · 了解Lambda语法 · 了解方法引用 · 了解Fucntional与Stream API · 掌握Date与Calendar的应用 · 会使用JDK8新的时间API ...

  8. es6笔记(1) 概要

    什么是ES6 ECMAScript 6.0 (简称ES6) 是继ECMAScript 5.1以后的javascript 语言的下一代标准,在2015年6月份发布. 他的目标是使javascript语言 ...

  9. 使用 jquery-autocomplete插件 完成文本框输入自动填充联想效果 解决兼容IE输入中文问题

    项目中有时会用到ajax自动补全查询,就像Google的搜索框中那样,输入汉字或者字母的首个字母,则包含这个汉字或者字母的相关条目会显示出来供用户选择,该插件就是实现这样的功能的.autocomple ...

  10. 判断线段之间的关系(D - Intersecting Lines POJ - 1269 )

    题目链接:https://vjudge.net/contest/276358#problem/D 题目大意:每一次给你两条直线,然后问你这两条直线的关系(平行,共线,相交(输出交点)). 具体思路:先 ...