题意

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

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

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

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

  3. Codeforces 813E - Army Creation

    813E - Army Creation 思路: 线段树+二分 先预处理每个点往后走k步的下标 线段树二叉树的每个节点用vector维护这些下标,给这些下标排个序 询问区间L,R,那么把下标小于等于R ...

  4. Educational Codeforces Round 22 E. Army Creation

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

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

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

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

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

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

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

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

  9. [Educational Codeforces Round#22]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 晚上去clj博客逛来逛去很开心,突然同学提醒了一下,发现cf已经开始40分钟了,慌的一B,从B题开始写,写完了B到E最后收掉了A,结果太着急B ...

随机推荐

  1. C/C++语言简介之发展历史

    C语言之所以命名为C,是因为 C语言源自Ken Thompson发明的B语言,而 B语言则源自BCPL语言. 1967年,剑桥大学的Martin Richards对CPL语言进行了简化,于是产生了BC ...

  2. Linux常用命令详解(二) -- 查找常用命令

    locate:    作用:在后台数据库中按文件名搜索,搜索速度更快    命令格式:locate 文件名    选项或参数:            -l    num(要显示的行数)         ...

  3. 对TCP三次握手四次分手还不清楚的速度进,超简单解析,明白了就很好记!

    关于TCP三次握手四次分手,之前看资料解释的都很笼统,很多地方都不是很明白,所以很难记,前几天看的一个博客豁然开朗,可惜现在找不到了.现在把之前的疑惑总结起来,方便一下大家. 先上个TCP三次握手和四 ...

  4. 报错信息 The jsp:param action must not be used outside the jsp:include, jsp:forward, or jsp:params elements 的原因及解决办法

    如果你的代码是这样的话就会报错 <jsp:forward page="02.jsp"></jsp:forward> <jsp:param value= ...

  5. Flask從入門到入土(五)——Flask与数据库

    import sqlite3 def init_db(): sql = 'create table user(id INT,name TEXT)' conn = sqlite3.connect(&qu ...

  6. BloomFilter(布隆过滤器)

    原文链接:http://blog.csdn.net/qq_38646470/article/details/79431659 1.概念: 如果想判断一个元素是不是在一个集合里,一般想到的是将所有元素保 ...

  7. C++学习笔记第三天:类、虚函数、双冒号

    类 class Box { public: double length; // 盒子的长度 double breadth; // 盒子的宽度 double height; // 盒子的高度 }; 类成 ...

  8. Python 实现单例模式的一些思考

    一.问题:Python中如何实现单例模式 单例模式指一个类只能实例化一个对象. 二.解决方案: 所有资料参考于: http://python.jobbole.com/87294/ https://ww ...

  9. Codeforces13E - Holes

    Portal Description \(n(n\leq10^5)\)个洞排成一条直线,第\(i\)个洞有力量值\(a_i\),当一个球掉进洞\(i\)时就会被立刻弹到\(i+a_i\),直到超出\( ...

  10. linux Cacti监控服务器搭建

    搭建Cacti监控服务器 部署安装环境(lamp) [root@zhuji1 ~]# yum -y install httpd [root@zhuji1 ~]# yum -y install php ...