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,内 ...
随机推荐
- sublime的tab和spaces空格切换的坑
python是严格要求对齐或者叫缩进的: 使用sublime对python进行编程时,可以使用tab或者空格,但是不能混用.特别是从外面把代码拷贝进sublime的时候,更要注意是否一致. 简单介绍一 ...
- hdu 4300 Clairewd’s message(具体解释,扩展KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. ...
- iOS开发 ----- 加载动画之牛顿摆的实现
牛顿摆动画 自己看动画有一段时间了,但是还是不是很能理解其中的一些属性方法之类的东西,琢磨了一下午写了一个牛顿摆的动画,这里记录一下,一遍以后查看先上图 先说下思路 说下牛顿摆的大致运动过程 根据牛顿 ...
- C++ 面试问题
一面 (1) 多态性都有哪些?(静态和动态,然后分别叙述了一下虚函数和函数重载) (2) 动态绑定怎么实现?(就是问了一下基类与派生类指针和引用的转换问题) (3) 类型转换有哪些?(四种类型转换,分 ...
- 算法 binary search
// ------------------------------------------------------------------------------------------------- ...
- Android——通过Intent传递一些二进制数据的方法有哪些
1.方法 (1)使用Serializable接口实现序列化.利用Bundle.putSerializable(Key, Object);这里objec对象需要实现serializable接口. (2) ...
- mysql binlog配置详解
关闭binlog,注释掉mysql配置文件中的log-bin=mysql-bin即可 baidu zone - 关闭binlog方法 cnblogs - linux下mysql配置文件my ...
- 如何将安卓手机中已安装的应用导出一个apk安装包
首先安装ES文件浏览器 然后应用里找到你要导出apk的应用,长按应用,备份应用, 然后在upbacks/apps文件夹里找吧 不得不说 ES浏览器还是屌啊
- Centos 安装Puppet
环境说明: OS:CentOS 5.4 i386puppetmaster 192.168.0.12 hostname: puppetmaster.info.comclient ...
- qt-mingw530-opencv-开发配置
1.安装好Qt和Qtcreator 2.解压OpenCV源码到一个目录下.路径不能带空格和中文. 3.把E:\Qt\qtcreator-2.1.0\mingw\bin添加到系统环境变量中. 4.安装C ...