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 ...
随机推荐
- Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream)
Java基础-IO流对象之压缩流(ZipOutputStream)与解压缩流(ZipInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 之前我已经分享过很多的J ...
- 何凯文每日一句打卡||DAY13
- 蓝桥杯 带分数 DFS应用
问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3546 / 197. 注意特征:带分数中,数字1~9分别出现且只出现一次( ...
- (一)Git时间--初识版本控制工具
//配置一下你的身份 git config --global use.name "Douzi" git config --global use.email "jdouzi ...
- Spring RedisTemplate操作-Set操作(5)
@Autowired @Resource(name="redisTemplate") private RedisTemplate<String, String> rt; ...
- 优雅地搭建整合ssm项目
spring + spring mvc + mybatis 三大框架建议观看 黑马程序员出品的 Springmvc+Mybatis由浅入深全套视频教程 Spring框架2016版视频 观看顺序 ,我个 ...
- js调试系列: 断点与动态调试[基础篇]
js调试系列目录: - 额,我说的不是张敬轩的 断点 这首歌,是调试用到的断点,进入正题吧. 昨天留的课后练习 1. 分析 votePost 函数是如何实现 推荐 的.其实我们已经看到了源码,只要读下 ...
- Spring Mvc Web 配置拦截规则与访问静态资源 (三)
拦截规则配置 1. *.do <!-- Processes application requests --> <servlet> <servlet-name>app ...
- 玩转Hook——Android权限管理功能探讨(二)
距离我上一篇研究ptrace的随笔http://www.cnblogs.com/zealotrouge/p/3544147.html已经过去半年了,最近不忙的时候抽空继续研究了下.同样,参考了Prad ...
- artDialog4.1.7 摘自网络
jquery.artDialog.source.js学习 1 关键的对象关系 art = jQuery = $ function artDialog() {...} artDialog.fn = ar ...