Codeforces 813E - Army Creation
思路:
线段树+二分
先预处理每个点往后走k步的下标
线段树二叉树的每个节点用vector维护这些下标,给这些下标排个序
询问区间L,R,那么把下标小于等于R的位置都减掉,因为只要后面连续k个就够了
代码:
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define ls rt<<1,l,m
#define rs rt<<1|1,m+1,r
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e5+;
int a[N],now[N],nxt[N],pre[N],nxtk[N],ans=;
vector<int>vc[N<<];
void build(int rt,int l,int r){
if(l==r){
vc[rt].pb(nxtk[l]);
return ;
}
for(int i=l;i<=r;i++)vc[rt].pb(nxtk[i]);
sort(vc[rt].begin(),vc[rt].end());
int m=(l+r)>>;
build(ls);
build(rs);
}
void query(int L,int R,int rt,int l,int r){
if(L<=l&&r<=R){
int T=upper_bound(vc[rt].begin(),vc[rt].end(),R)-vc[rt].begin();
ans-=T;
return ;
}
int m=(l+r)>>;
if(L<=m)query(L,R,ls);
if(R>m)query(L,R,rs);
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n,k,q,l,r;
cin>>n>>k;
for(int i=;i<=n;i++)cin>>a[i];
for(int i=;i<N;i++)now[i]=n+;
nxt[n+]=n+;
for(int i=n;i>=;i--){
nxt[i]=now[a[i]];
pre[now[a[i]]]=i;
now[a[i]]=i;
}
for(int i=;i<=n;i++){
if(i==now[a[i]]){
int t=i,cnt=;
while(t!=n+&&cnt<k){
t=nxt[t];
cnt++;
}
nxtk[i]=t;
}
else{
nxtk[i]=nxt[nxtk[pre[i]]];
}
}
cout<<endl;
build(,,n);
cin>>q;
while(q--){
cin>>l>>r;
l+=ans;
r+=ans;
l=l%n+;
r=r%n+;
if(l>r)swap(l,r);
ans=r-l+;
query(l,r,,,n);
cout<<ans<<endl;
}
return ;
}
Codeforces 813E - Army Creation的更多相关文章
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- Codeforces 813E Army Creation(主席树)
题目链接 Educational Codeforces Round 22 Problem E 题意 给定一个序列,$q$次查询,询问从$l$到$r$中出现过的数字的出现次数和$k$取较小值后的和 ...
- Educational Codeforces Round 22 E. Army Creation
Educational Codeforces Round 22 E. Army Creation 题意:求区间[L,R]内数字次数不超过k次的这些数字的数量的和 思路:和求区间内不同数字的数量类似,由 ...
- Educational Codeforces Round 22 E. Army Creation(分块好题)
E. Army Creation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 【CF813E】Army Creation(主席树)
[CF813E]Army Creation(主席树) 题面 CF 洛谷 翻译 by ppl 见洛谷 题解 考虑最多只会有\(K\)个相同的数 那么,也就是说,如果一个数会被选 那么,和它相等的数中,在 ...
- Educational Codeforces Round 22 E. Army Creation 主席树 或 分块
http://codeforces.com/contest/813/problem/E 题目大意: 给出长度为n的数组和k, 大小是1e5级别. 要求在线询问区间[l, r]权值, 权值定义为对于 ...
- CodeForces813E:Army Creation (主席树---上一题的加强版)
As you might remember from our previous rounds, Vova really likes computer games. Now he is playing ...
- CF813E Army Creation
题意 \(n\)个数\(a[i] ,q\)次询问,\(n,a[i],q<=10^5\)每次问\([l,r]\)内最多可以选多少个数,满足同一个数的出现次数不超过\(k\) 强制在线 Sol 处理 ...
- codeforces 813E 主席树
题意: 一个数列多组询问,每次询问[l,r]中最多能选多少个数字,其中每个数字的出现次数不超过k次 题解: 我们保存对于每个位置上,出现超过k次的位置,那么对于每次询问,我们就变成了查询区间[l,r] ...
随机推荐
- python的类和实例化对象
一切皆对象,类也是对象,类来自于元类type,如果一个类没有声明自己的元类,默认它就是元类. 即类是元类的实例,通过type(类)会显示type,而实例来自于类. 类有两个属性,数据属性和函数属性,下 ...
- 《2015 html5 iweb 峰会观会后感》
今天是2015 html5 iweb 峰会,大会在早上8:30,就开始了,没想到被坑了,看错一个字,把"国际会议中心",看成了"国家会议中心".大约过了10多 ...
- mysql主从(主备)同步一键配置,配自动检测功能
主从一键shell配置 做个笔记. #!/bin/bash #Mysql sync #chenglee #master机器ip MasterIP="192.168.137.174" ...
- 20145308 《网络对抗》 MAL_免杀原理及实践 学习总结
20145308 <网络对抗> MAL_免杀原理及实践 学习总结 实践内容 (1)理解免杀技术原理 (2)正确使用msf编码器,veil-evasion,自己利用shellcode编程等免 ...
- JAVA中拆箱和装箱
浅谈JAVA中拆箱与装箱 一. 什么是装箱?什么是拆箱? 在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i = new Integer(10) ...
- python脚本解析json文件
python脚本解析json文件 没写完.但是有效果.初次尝试,写的比较不简洁... 比较烦的地方在于: 1,中文编码: pSpecs.decode('raw_unicode_escape') 2,花 ...
- c++ vector常见用法
//输出尾巴的元素 cout<<vec.back(); //定义vector迭代器 vector<int>::iterator ite=vec.begin(); for(ite ...
- C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法
https://www.cnblogs.com/johnblogs/p/6006867.html DataTable ds = new DataTable(); //1.lamda 表达式写法(推荐) ...
- Docker Builders:Builder pattern vs. Multi-stage builds in Docker
原文链接 Builder pattern vs. Multi-stage builds in Docker This post looks at two new PRs from the Docker ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...