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. v-text v-html等指令的使用

    v-text:以纯文本方式显示数据: v-html:可以识别HTML标签: v-once:只渲染元素或组件一次: v-pre:不进行编译,直接显示内容: v-cloak:可以隐藏未编译的 Mustac ...

  2. gdb调试程序函数名为问号,什么原因?step by step解决方案

    gdb调试程序函数名为问号,什么原因? http://bbs.chinaunix.net/thread-1823649-1-1.html http://www.bubuko.com/infodetai ...

  3. Machine Learning - Andrew Ng - Coursera

    Machine Learning - Andrew Ng - Coursera Contents 1 Notes 1 Notes What is Machine Learning? Two defin ...

  4. k8s device plugin

    基本概念入门: Device Manager Proposal Device plugin offical Doc(中文) device-plugins offical Doc(En) Go thro ...

  5. metasploit(MSF)对windows的ms17-010漏洞利用

    picture 配置exploit msf > use exploit/windows/smb/ms17_010_eternalblue msf exploit(windows/smb/ms17 ...

  6. Android之udp传输

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

  7. noip模拟题 2017.10.28 -kmp -Tarjan -鬼畜的优化

    题目大意 给定A串,选择A串的前lB个字符作为B串,再在B串后增加一个字符,问最长的相等的A串前缀和B串的后缀. Solution 1(KMP) 用1个奇怪的字符连接A串和B串,再用KMP求最长公共前 ...

  8. 大明A+B(大数相加)解题报告

    Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫"大明". 这时他已经不是那个只会做100以内加法的那个"小明 ...

  9. vs 附加进程 iis进程显示

  10. 简单数论总结1——gcd与lcm

    并不重要的前言 最近学习了一些数论知识,但是自己都不懂自己到底学了些什么qwq,在这里把知识一并总结起来. 也不是很难的gcd和lcm 显而易见的结论: 为什么呢? 根据唯一分解定理: a和b都可被分 ...