CodeForces813E:Army Creation (主席树---上一题的加强版)
As you might remember from our previous rounds, Vova really likes computer games. Now he is playing a strategy game known as Rage of Empires.
In the game Vova can hire n different warriors; ith warrior has the type ai. Vova wants to create a balanced army hiring some subset of warriors. An army is called balanced if for each type of warrior present in the game there are not more than k warriors of this type in the army. Of course, Vova wants his army to be as large as possible.
To make things more complicated, Vova has to consider q different plans of creating his army. ith plan allows him to hire only warriors whose numbers are not less than li and not greater than ri.
Help Vova to determine the largest size of a balanced army for each plan.
Be aware that the plans are given in a modified way. See input section for details.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 100000).
The second line contains n integers a1, a2, ... an (1 ≤ ai ≤ 100000).
The third line contains one integer q (1 ≤ q ≤ 100000).
Then q lines follow. ith line contains two numbers xi and yi which represent ith plan (1 ≤ xi, yi ≤ n).
You have to keep track of the answer to the last plan (let's call it last). In the beginning last = 0. Then to restore values of li and ri for the ith plan, you have to do the following:
- li = ((xi + last) mod n) + 1;
- ri = ((yi + last) mod n) + 1;
- If li > ri, swap li and ri.
Output
Print q numbers. ith number must be equal to the maximum size of a balanced army when considering ith plan.
Example
- 6 2
1 1 1 2 2 2
5
1 6
4 3
1 1
2 6
2 6
- 2
4
1
3
2
Note
In the first example the real plans are:
- 1 2
- 1 6
- 6 6
- 2 4
- 4 6
题意:给定N个数,以及Q个询问,每个询问给出L和R,现在问在这个区间选最多的数,使得每个数出现次数不能大于K。
思路:有了上一题的积累,这题就是一眼题了。上一题相当于的K=1。此题,我们任然算前缀的贡献,[1,i]区间建立在[1,i-1]基础上,如果前缀[1,i]里a[i]出现的次数大于等于K,我们删去最前面那一个的贡献1,加上i的贡献1,保证每个前缀不同数的贡献小于等于K。
- #include<bits/stdc++.h>
- using namespace std;
- const int maxn=;
- struct node{
- int val,l,r;
- node() {}
- node(int L,int R,int V):l(L),r(R),val(V){}
- }s[maxn*];
- int rt[maxn],cnt,ans;
- queue<int>q[maxn];
- void insert(int &now,int pre,int L,int R,int pos,int val)
- {
- s[now=++cnt]=node(s[pre].l,s[pre].r,s[pre].val+val); //先假设都等
- if(L==R) return ;
- int Mid=(L+R)>>;
- if(pos<=Mid) insert(s[now].l,s[pre].l,L,Mid,pos,val); //这里再把不等的改掉
- else insert(s[now].r,s[pre].r,Mid+,R,pos,val);
- }
- int query(int now,int pos,int L,int R)
- {
- if(L==R) return s[now].val;
- int Mid=(L+R)>>;
- if(pos<=Mid) return query(s[now].l,pos,L,Mid)+s[s[now].r].val;
- return query(s[now].r,pos,Mid+,R);
- }
- int main()
- {
- int N,Q,K,x,y,i;
- scanf("%d%d",&N,&K);
- for(i=;i<=N;i++){
- scanf("%d",&x);
- if(q[x].size()<K) insert(rt[i],rt[i-],,N,i,);
- else {
- int last=q[x].front(); q[x].pop();
- insert(y,rt[i-],,N,last,-);
- insert(rt[i],y,,N,i,);
- }
- q[x].push(i);
- }
- scanf("%d",&Q);
- while(Q--){
- scanf("%d%d",&x,&y);
- x=(x+ans)%N+;
- y=(y+ans)%N+;
- if(x>y) swap(x,y);
- ans=query(rt[y],x,,N);
- printf("%d\n",ans);
- }
- return ;
- }
CodeForces813E:Army Creation (主席树---上一题的加强版)的更多相关文章
- Educational Codeforces Round 22 E. Army Creation 主席树 或 分块
http://codeforces.com/contest/813/problem/E 题目大意: 给出长度为n的数组和k, 大小是1e5级别. 要求在线询问区间[l, r]权值, 权值定义为对于 ...
- 【bzoj3524】【Poi2014】【Couriers】可持久化线段树(主席树)水题
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62485671 向大(hei)佬(e)势力学(di ...
- 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665
如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...
- P5385 [Cnoi2019]须臾幻境(LCT+主席树,思维题)
题目 P5385 [Cnoi2019]须臾幻境 做法 考虑一条边\((u,v)\)是否\([L,R]\)中的贡献:\([L,R]\)中第一条位于\(u,v\)链的边,则减少了一个联通块 实现:\(LC ...
- poj2104 主席树模板题
题意 给出n个数字组成的数字序列,有m组询问.每次询问包含三个数字l,r,k.对于每个询问输出序列区间[l,r]中第k大的数字. 分析 这是主席树的模板题,套板子就可以 #include <cs ...
- [主席树]HDOJ2665 && POJ2104 && POJ2761
主席树真是神奇的物种! Orz一篇资料 题意:给n.m 下面有n个数 (编号1到n) 有m个询问,询问的是上面的数的编号在[l,r]之间第k小的数 n.m的范围都是$10^5$ 是主席树的入门题 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- bzoj 4012: [HNOI2015]开店 主席树
Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...
- LuoguP2617 Dynamic Rankings (动态主席树学习理解)
题目地址 题目链接 题解 动态主席树的板子题.动态主席树其实和静态的有很大差别,虽然同样是n个根,但是节点并不能共用,每个根节点表示bit上的一段区间. 所以其实是个树套树的东西来着,外层是bit,内 ...
随机推荐
- iOS -- 十进制、十六进制字符串,byte,data等之间的转换
十进制->十六进制 Byte bytes[]={0xA6,0x27,0x0A}; NSString *strIdL = [NSStringstringWithFormat:]]]; 十六进制-& ...
- dubbo开发者指南
开发者指南 参与 流程 任务 版本管理 源码构建 框架设计 整体设计 模块分包 依赖关系 调用链 暴露服务时序 引用服务时序 领域模型 基本原则 扩展点加载 扩展点配置 扩展点自动包装 扩展点自动装配 ...
- HBase 基本操作
如何添加列族 很简单,跟rdbms一样 直接用alter,但是alter之前必须先disable这个表 ---->disable 'test' ...
- PS 如何制作WIN7的玻璃化透明窗口效果
1 绘制一个圆角矩形,并将不透明度设为16%以及添加投影效果 2 再次添加外发光效果 3 新建一个图层,再填充一下这个圆角矩形(可以填充为任意颜色,只要和别的颜色区分开来) 4 选中这个区 ...
- (学习笔记3)BMP位图的读取与显示
在(学习笔记2)中.我们已经具体说明怎样去创建MFC.在这节中.主要解决BMP位图照片的读取和显示问题. 我们新建一个projectdemo1.创建步骤请看(学习笔记2)中具体说明. 创建成功后,例如 ...
- js 节点操作
添加和删除节点(HTML 元素). 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素追加该元素. 实例 <div id=& ...
- Spring集成JDBC
不同spring版本合成的方式,有时候不一样,需要查看帮助文档来看如何集成,帮助文档在spring发行包中. 1.导入spring的包(这里吧Spring-3.1.3 Release的所有jar包都导 ...
- hibernate5(10)注解映射[2]一对多单向关联
在上一篇文章里.我们从端方向一端建立关联关系,完毕了从文章到作者的关联关系建立.但在实际的博客站点中,用户肯定还须要获取自己所写的文章,这时能够建立用户(一)对文章(多)的单向关联映射. 先来看我们的 ...
- java操作redis学习(一):安装及连接
文章参考自:http://www.cnblogs.com/edisonfeng/p/3571870.html,在此基础上进行了修改. 一.下载安装Redis redis官网显示暂时不支持Windows ...
- mysql查询sql中检索条件为大批量数据时处理
当userIdArr数组值为大批量时,应如此优化代码实现