CF813E Army Creation
题意
\(n\)个数\(a[i] ,q\)次询问,\(n,a[i],q<=10^5\)每次问\([l,r]\)内最多可以选多少个数,满足同一个数的出现次数不超过\(k\)
强制在线
Sol
处理出每个数往前数第\(k+1\)个与它相同的位置
没有则为\(0\)
那么就是求区间内所有的该值小于\(l\)的数
主席树来做就好了
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, k, rt[_], tot;
struct HJT{
int ls, rs, sz;
} T[_ * 20];
vector <int> num[_];
IL void Modify(RG int &x, RG int l, RG int r, RG int p){
T[++tot] = T[x], ++T[x = tot].sz;
if(l == r) return;
RG int mid = (l + r) >> 1;
if(p <= mid) Modify(T[x].ls, l, mid, p);
else Modify(T[x].rs, mid + 1, r, p);
}
IL int Query(RG int x, RG int l, RG int r, RG int p){
if(!x) return 0;
if(l == r) return T[x].sz;
RG int mid = (l + r) >> 1;
if(p <= mid) return Query(T[x].ls, l, mid, p);
return T[T[x].ls].sz + Query(T[x].rs, mid + 1, r, p);
}
int main(RG int argc, RG char* argv[]){
n = Input(), k = Input();
for(RG int i = 1, a, b; i <= n; ++i){
a = Input(), num[a].push_back(i);
RG int l = num[a].size();
if(l <= k) b = 0;
else b = num[a][l - k - 1];
rt[i] = rt[i - 1], Modify(rt[i], 0, n, b);
}
for(RG int q = Input(), ans = 0; q; --q){
RG int l = Input(), r = Input();
l = (l + ans) % n + 1, r = (r + ans) % n + 1;
if(l > r) swap(l, r);
printf("%d\n", ans = Query(rt[r], 0, n, l - 1) - Query(rt[l - 1], 0, n, l - 1));
}
return 0;
}
CF813E Army Creation的更多相关文章
- 【CF813E】Army Creation(主席树)
[CF813E]Army Creation(主席树) 题面 CF 洛谷 翻译 by ppl 见洛谷 题解 考虑最多只会有\(K\)个相同的数 那么,也就是说,如果一个数会被选 那么,和它相等的数中,在 ...
- 主席树[可持久化线段树](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
813E - Army Creation 思路: 线段树+二分 先预处理每个点往后走k步的下标 线段树二叉树的每个节点用vector维护这些下标,给这些下标排个序 询问区间L,R,那么把下标小于等于R ...
- 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 ...
- CodeForces813E:Army Creation (主席树---上一题的加强版)
As you might remember from our previous rounds, Vova really likes computer games. Now he is playing ...
- Educational Codeforces Round 22 E. Army Creation 主席树 或 分块
http://codeforces.com/contest/813/problem/E 题目大意: 给出长度为n的数组和k, 大小是1e5级别. 要求在线询问区间[l, r]权值, 权值定义为对于 ...
- Codeforces 813E Army Creation(主席树)
题目链接 Educational Codeforces Round 22 Problem E 题意 给定一个序列,$q$次查询,询问从$l$到$r$中出现过的数字的出现次数和$k$取较小值后的和 ...
- [Educational Codeforces Round#22]
来自FallDream的博客,未经允许,请勿转载,谢谢. 晚上去clj博客逛来逛去很开心,突然同学提醒了一下,发现cf已经开始40分钟了,慌的一B,从B题开始写,写完了B到E最后收掉了A,结果太着急B ...
随机推荐
- CocosCreator游戏开发---菜鸟学习之路(一)
PS(废话): 辞职后在家好久好久了,久到经济不允许了,接着就准备再次出去找工作了,然而工作哪有那么好找,特别是像我这种菜鸟.而且我还准备转行,准备去做游戏,技能等级接近于0,那工作就更难找了.既然如 ...
- Linux中的 awk查找日志中的相关记录
假设要在 api.log.201707201830 文件中,(此文件的多个字段数据以不可见字符^A(键盘上按下Ctrl+V+A)分隔),要输出第70个字段: awk -F '^A' '{print $ ...
- vue父子组件之间的通信
利用props在子组件接受父组件传过来的值1.父组件parentComp.vue <template> <childComp :fromParentToChild="fro ...
- C语言_指针和数组的几种访问形式
敲几行代码来看看几种访问的形式~ #include <stdio.h>;int main() { ] = {, , , , , }; //初始化5个元素的一维数组 int *p = arr ...
- SQL语句学习
看似简单,但其实包含很多技巧思维 1.查询课程表中所有科目大于80的学生 select distinct name from student where name not in (select nam ...
- SpringMVC的数据转换,格式化和数据校验
在SpringMVC中,根据请求方法签名不同,将请求消息中的消息以一定的方式转换并绑定到请求方法的参数中,在请求信息到达真正调用处理方法的这一段时间内,SpringMVC还会完成很多其他的 ...
- mysql数据库基本使用(增删改查)
数据库db,database 数据库:依照某种数据模型进行组织并存放到存储器的数据集合dbms,database management system 数据库管理系统:用来操作和管理数据库的大型服务软件 ...
- DOS下串口通信程序来传送文件的源代码
接收程序: #include <dos.h>#include <fstream.h>#include <conio.h>#include <stdio.h&g ...
- BIOS简介
BIOS简介: BIOS是英文"Basic Input Output System"的缩略词,直译过来后中文名称就是"基本输入输出系统".其实,它是一组固化到计 ...
- php session 和cookie
先简单的说明下session和cookie的区别. 1.session存放在服务器的文件中,或者是内存中.而cookie存在客服端. 2.session比cookie安全 3.session存放在服务 ...