码迷,mamicode.com
首页 > 其他好文 > 详细

Army Creation

时间:2018-05-17 22:26:49      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:主席树   arm   #define   must   more   disable   sts   diff   wan   

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 2
1 1 1 2 2 2
5
1 6
4 3
1 1
2 6
2 6
Output
Copy
2
4
1
3
2
题解: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;
}

 

 

 

 

 

 

 

Army Creation

标签:主席树   arm   #define   must   more   disable   sts   diff   wan   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!