Codeforces 813E Army Creation(主席树)
题目链接 Educational Codeforces Round 22 Problem E
题意 给定一个序列,$q$次查询,询问从$l$到$r$中出现过的数字的出现次数和$k$取较小值后的和
设$f(i, 1)$表示满足$a_{j} = a_{i}$并且$j < i$的$j$的最大值,若不存在这样的$j$则$f(i, 1) = -1$
设$f(i, j) = f(f(i, j - 1), 1), j >= 2$。
那么我们要做的就是对于每一次查询,找到$[l, r]$中,满足$l <= i <= r$且$f(i, k) < l$的$i$的个数。
对于$f(i, k)$,$O(n)$预处理,
查询在主席树上操作就可以了。
因此总时间复杂度$O(nlogn)$
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 1e5 + 10;
const int M = 3e6 + 10; int n, k, q;
int a[N], c[N];
int root[N], ls[M], rs[M], s[M];
int tot = 0, ans = 0;
vector <int> v[N]; void update(int &x, int y, int l, int r, int pos){
x = ++tot;
ls[x] = ls[y];
rs[x] = rs[y];
s[x] = s[y] + 1;
if (l == r) return;
int mid = (l + r) >> 1;
if (pos <= mid) update(ls[x], ls[y], l, mid, pos);
else update(rs[x], rs[y], mid + 1, r, pos);
} int query(int x, int y, int L, int R, int l, int r){
if (l <= L && R <= r) return s[y] - s[x];
int ret = 0;
int mid = (L + R) >> 1;
if (l <= mid) ret += query(ls[x], ls[y], L, mid, l, r);
if (r > mid) ret += query(rs[x], rs[y], mid + 1, R, l, r);
return ret;
} int main(){ scanf("%d%d", &n, &k);
rep(i, 1, n){
scanf("%d", a + i);
v[a[i]].push_back(i);
} memset(c, -1, sizeof c);
rep(i, 1, 1e5){
int et = v[i].size();
rep(j, k, et - 1) c[v[i][j]] = v[i][j - k];
} rep(i, 1, n){
if (c[i] == -1) root[i] = root[i - 1];
else update(root[i], root[i - 1], 1, n, c[i]);
} scanf("%d", &q);
while (q--){
int x, y;
scanf("%d%d", &x, &y);
x = (x + ans) % n + 1;
y = (y + ans) % n + 1;
if (x > y) swap(x, y);
printf("%d\n", ans = y - x + 1 - query(root[x - 1], root[y], 1, n, x, y));
} return 0;
}
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
813E - Army Creation 思路: 线段树+二分 先预处理每个点往后走k步的下标 线段树二叉树的每个节点用vector维护这些下标,给这些下标排个序 询问区间L,R,那么把下标小于等于R ...
- 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 ...
- Codeforces 1000F One Occurrence 主席树|| 离线+线段树
One Occurrence 为什么我半年前这么菜呀, 这种场只A三题... 我们在主席树 || 线段树上维护每个数的右边和它一样的数在哪里, 然后就变成了区间求最大值. 注意加进去的时候要把它右边一 ...
- 2018.12.05 codeforces 961E. Tufurama(主席树)
传送门 一眼主席树sbsbsb题(%%%树状数组大佬们). 简化题意:求满足x<y,y≤ax,x≤ayx<y,y\le a_x,x\le a_yx<y,y≤ax,x≤ay的(x, ...
- codeforces 1262D Optimal Subsequences 主席树询问第k小
题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...
- 【CF813E】Army Creation(主席树)
[CF813E]Army Creation(主席树) 题面 CF 洛谷 翻译 by ppl 见洛谷 题解 考虑最多只会有\(K\)个相同的数 那么,也就是说,如果一个数会被选 那么,和它相等的数中,在 ...
- codeforces 813E 主席树
题意: 一个数列多组询问,每次询问[l,r]中最多能选多少个数字,其中每个数字的出现次数不超过k次 题解: 我们保存对于每个位置上,出现超过k次的位置,那么对于每次询问,我们就变成了查询区间[l,r] ...
随机推荐
- pip 代理设置,坑爹的代理继续
Linux ubuntu 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Lin ...
- java线上编程网站
自带测试 http://codingbat.com/prob/p145416
- java开发环境的安装
1.Java是一门面向对象的编程语言,由sun公司开发的,目前公司已经被oracle公司收购.那么作为一门编程语言,它有自己的编程环境.并不是你编写了java代码后,就能在任何平台上运行,它的运行有自 ...
- cloud-init介绍及源码解读
https://zhuanlan.zhihu.com/p/27664869 知乎大神写的
- install ironic-inspector
安装相应的包和组件 yum install openstack-ironic-inspector python-ironic-inspector-client -y 创建user openstack ...
- rtmp jwplayer简单应用
在nginx下使用,放到nginx/html文件夹内,之后访问 首先是播放vod视频 <!DOCTYPE html> <html xmlns="http://www.w3. ...
- c#中RadioButtonList选中后不整体刷新页面保持选中状态
c#中用asp的RadioButtonList控件总会遇到选中了,然后跟着就刷新整体页面,又变为没有选中状态. <%@ Page Language="C#" AutoEven ...
- 在Android和iOS中集成flutter
flutter可能是未来跨平台开发的又一技术框架,那么对于一个app,我们不可能完全用flutter来开发,那么就意味着我们需要在已有的Android和iOS代码中去集成flutter.目前这一技术还 ...
- 《c程序设计语言》读书笔记-第二个字符串任意一个在第一个字符串出现的位置,未出先返回-1
#include <stdio.h> #include <string.h> #define Num 1000 int main() { int c,i,j = 0,m = 0 ...
- Codevs 1710 == POJ 1190 生日蛋糕 == 洛谷P1731
生日蛋糕 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ ...