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:

  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

Copy

6 21 1 1 2 2 251 64 31 12 62 6

Output

Copy

24132

题解:b [ i ] 表示从 i 开始数 k 个和 a [ i ] 相同颜色的数的位置。那么在[ l , r ]区间内只有 b [ i ] 大于 r 的数才能被选中,等价于 [ l , r ] 区间内有多少个数大于r。套上主席树就能使用前缀和。算b[i]时要注意一下!!!

#pragma warning(disable:4996)
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define mem(arr,in) memset(arr,in,sizeof(arr))
using namespace std;

const int maxn = 1e5 + 5;

int n, m, q, cnt;
int root[maxn], a[maxn], b[maxn];

struct node { int l, r, sum; } T[maxn * 40];

vector<int> pos[maxn];

void Cal()
{
    for (int i = n; i >= 1; i--)
    {
        int sz = pos[a[i]].size();
        if (sz<m)
            b[i] = n + 1;
        else
            b[i] = pos[a[i]][sz - m];
        pos[a[i]].push_back(i);   //这句放在开头会WA!!!
    }
}

void Update(int l, int r, int &rt, int pre, int p) {
    T[++cnt] = T[pre], T[cnt].sum++, rt = cnt;
    if (l == r) return;
    int mid = (l + r) >> 1;
    if (mid >= p) Update(l, mid, T[rt].l, T[pre].l, p);
    else Update(mid + 1, r, T[rt].r, T[pre].r, p);
}

int Query(int l, int r, int rt, int L, int R) {
    if (l > R || r < L) return 0;
    if (L <= l && r <= R) return T[rt].sum;
    int mid = (l + r) >> 1;
    int ans = 0;
    ans += Query(l, mid, T[rt].l, L, R);
    ans += Query(mid + 1, r, T[rt].r, L, R);
    return ans;
}

int main()
{
    while (scanf("%d%d", &n, &m) != EOF) {
        cnt = 0;
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
        Cal();
        for (int i = 1; i <= n; i++) Update(1, n + 1, root[i], root[i - 1], b[i]);
        scanf("%d", &q);
        int l, r, ans, last = 0;
        for (int i = 1; i <= q; i++) {
            scanf("%d%d", &l, &r);
            l = ((l + last) % n) + 1;
            r = ((r + last) % n) + 1;
            if (l > r) swap(l, r);
            ans = Query(1, n + 1, root[r], r + 1, n + 1) - Query(1, n + 1, root[l - 1], r + 1, n + 1);
            last = ans;
            printf("%d\n", ans);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zgglj-com/p/9053607.html

时间: 2024-08-30 17:55:48

Army Creation的相关文章

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

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

Educational Codeforces Round 22 E. Army Creation(主席树)

题目链接:Educational Codeforces Round 22 E. Army Creation 题意: 给你n个数和一个数k,然后有q个询问. 每个询问 有一个区间[l,r],问你这个区间内在满足每一种数不超过k的情况下,最大能选多少个数出来. 强制在线. 题解: 一看就要用到主席树,和主席数求区间内有多少不同的数的个数处理方法相同. 依次将每个数插入,当这个数出现的个数等于k了,就把最前面的那个数删掉. 然后询问就访问root[r]就行了. 第一次写完数据结构没有调试一遍过样例,一

Codeforces 813E - Army Creation

813E - Army Creation 思路: 线段树+二分 先预处理每个点往后走k步的下标 线段树二叉树的每个节点用vector维护这些下标,给这些下标排个序 询问区间L,R,那么把下标小于等于R的位置都减掉,因为只要后面连续k个就够了 代码: #include<bits/stdc++.h> using namespace std; #define LL long long #define pb push_back #define ls rt<<1,l,m #define rs

Army Creation CodeForces - 813E (水题)

题意: 给定序列, 每次询问一个区间[l,r], 问[l,r]中最多能选多少个数且每种数字不超过k 相当于加强版 HH的项链, 对于一个数t, 主席树维护上k次出现的位置pre[t], 每次查询相当于求区间内pre<左端点的总数 #include <iostream> #include <queue> #define REP(i,a,n) for(int i=a;i<=n;++i) #define mid ((l+r)>>1) using namespace

[Educational Codeforces Round#22]

来自FallDream的博客,未经允许,请勿转载,谢谢. 晚上去clj博客逛来逛去很开心,突然同学提醒了一下,发现cf已经开始40分钟了,慌的一B,从B题开始写,写完了B到E最后收掉了A,结果太着急B题忘记写一个等号挂掉了....D题瞎dp也挂了很难受.F题还剩5分钟的时候想出来了,如果没迟应该能写完. A. 你要做n道题 每道题要花费时间ti,有m个可以提交的时间段,你在同一时刻可以交很多代码并且不耗时间,求最早什么时候可以把代码全部交完. 发现只要管最后一道题啥时候交就行了,扫一遍区间. #

POJ 3069 Saruman&#39;s Army (贪心)

题目大意:直线上有N个点,点i的位置是Xi,从这N个点中选取若干,给他们加上标记,对每一个点,其距离为R以内的区域内必须有被标记的点.求至少需要多少个点被标记. 题目思路:设最左边的点:点p的坐标为x,那么离其距离为R的点的坐标为(x+R),我们应该标记的点应为坐标最接近且小于等于(x+R)的点p,则此时[x,p+R]范围内点点均被标记.依次进行下去,直到包含所有点为止. #include<stdio.h> #include<queue> #include<iostream&

Saruman&#39;s Army

第一部分:题目 题目链接:http://poj.org/problem?id=3069 Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6839   Accepted: 3516 Description Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep

poj 3069 Saruman&#39;s Army

Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5760   Accepted: 2949 Description Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes se

Saruman&#39;s Army(贪心)

 Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3069 Description Saruman the White must lead his army along a straight path from Isengard to Helm's Deep. To keep track of his f