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的更多相关文章

  1. 主席树[可持久化线段树](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 ...

  2. Codeforces 813E Army Creation(主席树)

    题目链接  Educational Codeforces Round 22 Problem E 题意  给定一个序列,$q$次查询,询问从$l$到$r$中出现过的数字的出现次数和$k$取较小值后的和 ...

  3. Educational Codeforces Round 22 E. Army Creation

    Educational Codeforces Round 22 E. Army Creation 题意:求区间[L,R]内数字次数不超过k次的这些数字的数量的和 思路:和求区间内不同数字的数量类似,由 ...

  4. 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 ...

  5. 【CF813E】Army Creation(主席树)

    [CF813E]Army Creation(主席树) 题面 CF 洛谷 翻译 by ppl 见洛谷 题解 考虑最多只会有\(K\)个相同的数 那么,也就是说,如果一个数会被选 那么,和它相等的数中,在 ...

  6. Educational Codeforces Round 22 E. Army Creation 主席树 或 分块

    http://codeforces.com/contest/813/problem/E 题目大意: 给出长度为n的数组和k,  大小是1e5级别. 要求在线询问区间[l, r]权值,  权值定义为对于 ...

  7. CodeForces813E:Army Creation (主席树---上一题的加强版)

    As you might remember from our previous rounds, Vova really likes computer games. Now he is playing ...

  8. CF813E Army Creation

    题意 \(n\)个数\(a[i] ,q\)次询问,\(n,a[i],q<=10^5\)每次问\([l,r]\)内最多可以选多少个数,满足同一个数的出现次数不超过\(k\) 强制在线 Sol 处理 ...

  9. codeforces 813E 主席树

    题意: 一个数列多组询问,每次询问[l,r]中最多能选多少个数字,其中每个数字的出现次数不超过k次 题解: 我们保存对于每个位置上,出现超过k次的位置,那么对于每次询问,我们就变成了查询区间[l,r] ...

随机推荐

  1. vue中的指令v-model

    Vue的指令:其实就是单个JavaScript表达式,一般来说是带有v-前缀:都有着对应的官网介绍:https://cn.vuejs.org/v2/guide/forms.html v-model:数 ...

  2. fjwc2019

    机房搬迁.......再加上文化课.......咕了十几天才有空补上....... day0听一个教授讲理论......在学长的带领下咕掉了..... D1 T1:#178. 「2019冬令营提高组」 ...

  3. spring List,Set,Map,Properties,array的配置文件注入方式

    虽然不多,但是有时候在实现的时候,我们还是希望某些参数或者属性通过集合()的方式注入进来,比如配置表参数列表,addresslist,亦或是三方库等等.因为这种改动不是很多,经常一时想不起来,今天做个 ...

  4. Android之udp传输

    注意除了添加Internet权限外,还要添加两行代码 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDi ...

  5. [C++ Primer Plus] 第2章、开始学习c++

    一.程序清单2.1(代码和书略不一样) #include<iostream> using namespace std;//使用std这个命名空间,才能正确找到cin和cout,如果不使用命 ...

  6. 在Ubuntu上更新Chrome到最新的版本

    本操作只限于已经安装了Chrome的Ubuntu系统: Step 1: sudo apt-get update Step 2: sudo apt-get install google-chrome-s ...

  7. ODAC(V9.5.15) 学习笔记(十二)TOraLoader

    名称 类型 说明 Columns TDAColumns 需要载入数据的每个字段定义 LoadMode TLoadMode 载入模式,包括: lmDirect 通过内部数据缓冲区载入到数据库中 lmDM ...

  8. upc组队赛1 闪闪发光 【优先队列】

    闪闪发光 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计--成为偶像, 只要她们成为偶像,学校的名气便会增加, ...

  9. 推荐一个静态博客兼笔记的工具:WDTP

    简介 WDTP(山湖录)不止是一款开源免费的GUI桌面单机版静态网站生成器和简单方便的前端开发工具,更是一款跨平台的集笔记.录音.个人知识管理.写作/创作.博客/网站内容与样式管理等功能于一体的多合一 ...

  10. (转)Awsome Domain-Adaptation

    Awsome Domain-Adaptation 2018-08-06 19:27:54 This blog is copied from: https://github.com/zhaoxin94/ ...