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] ...
随机推荐
- NATS—消息通信模型
消息通信模型 NATS的消息通信是这样的:应用程序的数据被编码为一条消息,并通过发布者发送出去:订阅者接收到消息,进行解码,再处理.订阅者处理NATS消息可以是同步的或异步的. * 异步处理 异步处 ...
- pagehelper调用mybatis报错java.lang.NoSuchMethodError:org.apache.ibatis.reflection.MetaObject.forObject
最近在升新的基础框架到spring cloud,启动时pagehelper报错,如下: java.lang.NoSuchMethodError:org.apache.ibatis.reflection ...
- py4CV例子2汽车检测和svm算法
1.什么是汽车检测数据集: ) pos, neg = , ) matcher = cv2.FlannBasedMatcher(flann_params, {}) bow_kmeans_trainer ...
- Python3基础 list list()生成空列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- ng-model绑定的是ng-option中的什么?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- ubuntu18.04无法安装libesd0-dev【学习笔记】
执行如下命令安装: sudo apt-get install libesd0-dev 却报了这个错误: 解决办法: sudo vim /etc/apt/sources.list //在行尾添加如下两行 ...
- 没有使用Material组件和使用了的对比
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...
- win10中命令操作Zookeeper
目录 zk客户端命令: 连接: 命令: 四字命令: 常用命令: 返回参数说明: 参考: zk客户端命令: 连接: C:\Users\qhong\Desktop $ zkCli.cmd -server ...
- P2057 [SHOI2007]善意的投票
思路 简单的最小割模型 最小割的模型就是选出一些边,把点集划分成S和T两个部分,使得代价最小 到这题上就是板子了 代码 #include <cstdio> #include <alg ...
- 常用处理数组、字符串API → forEach every some sort map filter slice split indexOf concat substring substr splice join toString replace
Object与Array的语法糖 var arr = [1,2,3]; // [] 是 new Array(1,2,3) 的语法糖(简写) var obj = {'name':2,'age':3}; ...