标签:主席树 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.
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:
Print q numbers. ith number must be equal to the maximum size of a balanced army when considering ith plan.
6 2
1 1 1 2 2 2
5
1 6
4 3
1 1
2 6
2 6
2
4
1
3
2
#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; }
标签:主席树 arm #define must more disable sts diff wan
原文地址:https://www.cnblogs.com/zgglj-com/p/9053607.html