题目链接  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(主席树)的更多相关文章

  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

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

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

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

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

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

  5. Codeforces 1000F One Occurrence 主席树|| 离线+线段树

    One Occurrence 为什么我半年前这么菜呀, 这种场只A三题... 我们在主席树 || 线段树上维护每个数的右边和它一样的数在哪里, 然后就变成了区间求最大值. 注意加进去的时候要把它右边一 ...

  6. 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, ...

  7. codeforces 1262D Optimal Subsequences 主席树询问第k小

    题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...

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

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

  9. codeforces 813E 主席树

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

随机推荐

  1. 《Cracking the Coding Interview》——第5章:位操作——题目1

    2014-03-19 05:45 题目:给定两个数M和N,将N按照二进制位,覆盖到M的特定段位中去. 解法:位操作,请看代码. 代码: // 5.1 Insert one number into th ...

  2. Http状态码枚举(摘自 Microsoft 程序集 System.dll)

    // 摘要: // 包含为 HTTP 定义的状态代码的值. public enum HttpStatusCode { // 摘要: // 等效于 HTTP 状态 100. System.Net.Htt ...

  3. mybatis批量添加、批量删除

    <!-- 批量添加 --> <insert id="insertNameListSynHisBatch" parameterType="java.uti ...

  4. Ubuntu15.04 python升级到python-3.6.x

    简略记录步骤,容后补充:   sudo add-apt-repository ppa:jonathonf/python-3.6   sudo apt-get update sudo apt-get i ...

  5. NVIDIA/DIGITS:Building DIGITS

    在 Prerequisites中的 sudo apt-get update命令发生错误: W: GPG 错误:http://developer.download.nvidia.com/compute/ ...

  6. realloc在aarch64_be-gcc的奇怪表现

    最近遇到一个使用aarch64_be-gcc编译的ssh服务器出现不能通过ssh1协议使用密钥+passphrase不能正常登陆的问题. (⊙o⊙)…不要奇怪为啥还在用SSH1,我也在奇怪.. 一顿捣 ...

  7. GDI+绘制可滚动的窗口

    在winform中绘制图形,可以使用gdi+来完成. 当绘制的图形大于目前窗口大小时,就需要滚动条来帮忙显示. 设置属性:Form.AutoScrollMinSize为要显示内容的大小. privat ...

  8. 锚点自适应 map area coords

    最近做MOBILE的HTML5开发,人体图和页面一样,需要自适应不同的手机屏幕,蛋疼的是coords里面的标记是固定的,图片自适应后,锚点的标记就会产生空白区域,看了下https://github.c ...

  9. 积累: .net里有个线程安全的int+1类

     Interlocked.Increment(ref id); 

  10. lua中是 ffi 解析 【是如何处理数据包的/pkt是如何传进去的】 fsfsfs

    lua中的ffi是如何解析的呢? 拿bcc中对proto的解析说起: metatype是有大学问的: ffi.metatype(ffi.typeof('struct ip_t'), { __index ...