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,内 ...
随机推荐
- Android 关于view的getLayoutParams().width,getWidth(),getMeasuredWidth();
习惯了使用xml的布局方式,当动态布局的时候就有许多疑点,记录一下,帮助我这老头一样的记忆力. 网上也有许多解析这getLayoutParams().width,getWidth(),getMeasu ...
- 转: 写给想成为前端工程师的同学们 (from 360前端团队)
转自: http://www.75team.com/post/to-be-a-good-frontend-engineer.html 前端工程师是做什么的? 前端工程师是互联网时代软件产品研发 ...
- C++静态库与动态库深入研究
什么是库 库是写好的现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常. 本质上来说库是一种可执行代码的二进制形式,可以被操 ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- [C#]使用 C# 代码实现拓扑排序 dotNet Core WEB程序使用 Nginx反向代理 C#里面获得应用程序的当前路径 关于Nginx设置端口号,在Asp.net 获取不到的,解决办法 .Net程序员 初学Ubuntu ,配置Nignix 夜深了,写了个JQuery的省市区三级级联效果
[C#]使用 C# 代码实现拓扑排序 目录 0.参考资料 1.介绍 2.原理 3.实现 4.深度优先搜索实现 回到顶部 0.参考资料 尊重他人的劳动成果,贴上参考的资料地址,本文仅作学习记录之用. ...
- 获取Android屏幕尺寸、控件尺寸、状态栏/通知栏高度、导航栏高度
1.获取Android屏幕尺寸 我们能够通过getSize()方法获得屏幕的尺寸 Display display = getWindowManager().getDefaultDisplay(); P ...
- Node.js 笔记(一) nodejs、npm、express安装(转)
转载地址:http://blog.csdn.net/haidaochen/article/details/7257655 Windows平台下的node.js安装 直接去nodejs的官网http:/ ...
- shell-判断循环
shell条件测试 test 每个完整的合理的编程语言都具有条件判断的功能. bash可以使用test命令,[]和()操作,还有if/then结构 字符串判断 -n string 判断字符串长度非零 ...
- sublime 快捷键 汇总--长期
Ctrl+P 输入当前项目中的文件名,快速搜索文件 Ctrl+G 输入数字跳转到该行代码 Ctrl+R 输入关键字,查找文件中的函数名 Ctrl+: 输入关键字,查找文件中的变量名.属性名等 Ctrl ...
- sanic官方文档解析之Deploying(部署)和Extension(扩展)
1,Deploying(部署) 通过内置的websocket可以很简单的部署sanic项目,之后通过实例sanic.Sanic,我们可以运行run这个方法通过接下来的关键字参数 host (defau ...