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 a1a2, ... 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:

  1. li = ((xi + lastmod n) + 1;
  2. ri = ((yi + lastmod n) + 1;
  3. 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

Input
6 2
1 1 1 2 2 2
5
1 6
4 3
1 1
2 6
2 6
Output
2
4
1
3
2

Note

In the first example the real plans are:

  1. 1 2
  2. 1 6
  3. 6 6
  4. 2 4
  5. 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 (主席树---上一题的加强版)的更多相关文章

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

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

  2. 【bzoj3524】【Poi2014】【Couriers】可持久化线段树(主席树)水题

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62485671 向大(hei)佬(e)势力学(di ...

  3. 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665

    如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...

  4. P5385 [Cnoi2019]须臾幻境(LCT+主席树,思维题)

    题目 P5385 [Cnoi2019]须臾幻境 做法 考虑一条边\((u,v)\)是否\([L,R]\)中的贡献:\([L,R]\)中第一条位于\(u,v\)链的边,则减少了一个联通块 实现:\(LC ...

  5. poj2104 主席树模板题

    题意 给出n个数字组成的数字序列,有m组询问.每次询问包含三个数字l,r,k.对于每个询问输出序列区间[l,r]中第k大的数字. 分析 这是主席树的模板题,套板子就可以 #include <cs ...

  6. [主席树]HDOJ2665 && POJ2104 && POJ2761

    主席树真是神奇的物种! Orz一篇资料 题意:给n.m   下面有n个数 (编号1到n) 有m个询问,询问的是上面的数的编号在[l,r]之间第k小的数 n.m的范围都是$10^5$ 是主席树的入门题 ...

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

  8. bzoj 4012: [HNOI2015]开店 主席树

    Description 风见幽香有一个好朋友叫八云紫,她们经常一起看星星看月亮从诗词歌赋谈到 人生哲学.最近她们灵机一动,打算在幻想乡开一家小店来做生意赚点钱.这样的 想法当然非常好啦,但是她们也发现 ...

  9. LuoguP2617 Dynamic Rankings (动态主席树学习理解)

    题目地址 题目链接 题解 动态主席树的板子题.动态主席树其实和静态的有很大差别,虽然同样是n个根,但是节点并不能共用,每个根节点表示bit上的一段区间. 所以其实是个树套树的东西来着,外层是bit,内 ...

随机推荐

  1. 投资人王刚口述:滴滴如何用八十万成为百亿美金公司? zz

    作者|李好福布斯杂志中文版采编 阿里巴巴前高管.滴滴打车天使投资人王刚近日在杭州接受了<福布斯>独家专访,讲述了集齐“阿里的人.百度的技术.腾讯的钱”的滴滴如何从八十万启动资金,在三年内成 ...

  2. pycharm的todo和fixme标记,标志为今后再做和bug点

    使用方法,及查看方法: https://blog.csdn.net/xiemanR/article/details/73368440

  3. git 使用及常用命令

    git在团队项目中的使用流程 1.首先从一个git远程仓库中clone项目到本地 ? 1 git clone 仓库地址 2.创建开发分支 一般我们写代码不会在master分支上面写,而是新建一个分支 ...

  4. Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

    I see pretty much all the answers recommend deleting the lock. I don't recommend doing that as a fir ...

  5. mysql序列号发生器

    mysql序列号发生器 学习了:http://www.jquerycn.cn/a_14577 还可以这样啊:

  6. 需要配置执行path?no

    下面的代码以管理员权限运行即可,保存为bat 2018/2/6(basic) - 初始化版本 @echo off :continue echo 输入exit退出 set /p My_PATH=请输入要 ...

  7. 修改 百度地图 infowindow 默认样式

    1.百度 api 没有 提供可以修改 infowindow 默认样式的 方法. 如需修改,需要 自定义 替换 默认样式. demo.html <!DOCTYPE html> <htm ...

  8. mysql复制表命令

    http://hi.baidu.com/dwspider/item/908bf5e1746275bd2e140b03     上面命令是实现复制表的一种方法,缺陷就是索引等表信息不会复制过去,只是复制 ...

  9. php自定义错误

    function myErrorHandler ( $errno , $errstr , $errfile , $errline ) { if (!( error_reporting () & ...

  10. 递归计算战士打靶S次打了N环一共同拥有多少种可能的问题

    问题描写叙述 一个战士打了10次靶.一共打了90环,问一共同拥有多少种可能,并输出这些可能的组合. 思路 首先.嵌套10层循环进行穷举是不可取的,一是由于速度太慢,二是假设改成打20次靶就完蛋了. 事 ...