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] ...
随机推荐
- P2219 [HAOI2007]修筑绿化带(单调队列)
P2219 [HAOI2007]修筑绿化带 二维单调队列 写了这题 P2216 [HAOI2007]理想的正方形 后,你发现可以搞个二维单调队列 来保存矩形(i+1,i+A-1)(j+1,j+B-1 ...
- log4j升级到logback
虽然现在log4j已经基本上不更新很久了,但实际上升级log4j到logback最大的难度并不在于本身的替换,而是现有大量的三方jar依然使用log4j,以至于无法100%的exclude掉,所以很有 ...
- Python3 解析XML 层序遍历二叉树
Python3 解析XML 层序遍历二叉树 keyword : python3, xml, xml.dom.minidom, 层序遍历, 层次遍历, 二叉树 part1 问题描述 面对如下 XML 文 ...
- php5.3.x连接MS SQL server2008
开篇 因为毕设老师需求的原因,虚拟旅游网站要求的数据库必须使用MS SQL server. 我最擅长的web编程语言是PHP,但是在PHP中链接MS SQL server是一件非常麻烦的事,我个人分析 ...
- 【RMAN】使用RMAN的 Compressed Backupsets备份压缩技术 (转载)
1.Oracle参考文档中关于RMAN备份压缩的描述1)关于如何通过调整RMAN参数启用取消备份压缩功能http://download.oracle.com/docs/cd/B19306_01/bac ...
- opencv学习之路(10)、ROI与mask掩码
一.ROI #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat img=imread("E://0 ...
- stm32最简单的实现BootLoader
BootLoader大家应该都知道是干什么的,简单的来说就是程序开始运行前的一段程序. 在成熟的产品中,通常都是采用BootLoader方式来升级产品的程序.也就是IAP升级.在了解完基本的实现原理后 ...
- 详解重定向(HTTP状态码301/302/303/307/408)附例子
本文为原创文章,转载请注明出处. 今天打算好好把状态码301.302.303.307.308好好撸一遍,并会测试下一些例子. 状态码的解释 我们都知道重定向与这几种状态码有关,来看下这几种HTTP状态 ...
- Python3 tkinter基础 Label justify 多行字符串左对齐
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Yii使用笔记 2
yii中的 getId等函数, id更多的是一个 string, 而不是数字. CCaptchaAction > CAction > CComponent. 实现是 IAction. yi ...