2743: [HEOI2012]采花
Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 1917 Solved: 1004
[ Submit][ Status][ Discuss]
Description
萧芸斓是Z国的公主,平时的一大爱好是采花。
今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花。花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于公主采花。公主每次采花后会统计采到的花的颜色数,颜色数越多她会越高兴!同时,她有一癖好,她不允许最后自己采到的花中,某一颜色的花只有一朵。为此,公主每采一朵花,要么此前已采到此颜色的花,要么有相当正确的直觉告诉她,她必能再次采到此颜色的花。由于时间关系,公主只能走过花园连续的一段进行采花,便让女仆福涵洁安排行程。福涵洁综合各种因素拟定了m个行程,然后一一向你询问公主能采到多少朵花(她知道你是编程高手,定能快速给出答案!),最后会选择令公主最高兴的行程(为了拿到更多奖金!)。
Input
第一行四个空格隔开的整数n、c以及m。接下来一行n个空格隔开的整数,每个数在[1, c]间,第i个数表示第i朵花的颜色。接下来m行每行两个空格隔开的整数l和r(l ≤ r),表示女仆安排的行程为公主经过第l到第r朵花进行采花。
Output
共m行,每行一个整数,第i个数表示公主在女仆的第i个行程中能采到的花的颜色数。
Sample Input
5 3 5
1 2 2 3 1
1 5
1 2
2 2
2 3
3 5
1 2 2 3 1
1 5
1 2
2 2
2 3
3 5
Sample Output
2
0 0 1 0
【样例说明】
询问[1, 5]:公主采颜色为1和2的花,由于颜色3的花只有一朵,公主不采;询问[1, 2]:颜色1和颜色2的花均只有一朵,公主不采;
询问[2, 2]:颜色2的花只有一朵,公主不采;
询问[2, 3]:由于颜色2的花有两朵,公主采颜色2的花;
询问[3, 5]:颜色1、2、3的花各一朵,公主不采。
0 0 1 0
【样例说明】
询问[1, 5]:公主采颜色为1和2的花,由于颜色3的花只有一朵,公主不采;询问[1, 2]:颜色1和颜色2的花均只有一朵,公主不采;
询问[2, 2]:颜色2的花只有一朵,公主不采;
询问[2, 3]:由于颜色2的花有两朵,公主采颜色2的花;
询问[3, 5]:颜色1、2、3的花各一朵,公主不采。
HINT
【数据范围】
对于100%的数据,1 ≤ n ≤ 10^6,c ≤ n,m ≤10^6。
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 1E6 + 10;
struct Query{
int l,r,num; Query(){}
Query(int l,int r,int num): l(l),r(r),num(num){}
bool operator < (const Query &b) const {return r < b.r;}
}Q[maxn];
int n,m,a[maxn],c[maxn][2],po[maxn][2],ans[maxn];
void Modify(int j,int t,int k)
{
for (; j <= n; j += j&-j)
c[j][t] += k;
}
int Sum(int j,int t)
{
int ret = 0;
for (; j > 0; j -= j&-j)
ret += c[j][t];
return ret;
}
void Insert(int k)
{
int t = a[k];
if (!po[t][1]) po[t][1] = k;
else if (!po[t][0])
{
po[t][0] = po[t][1]; po[t][1] = k;
Modify(po[t][0],0,1); Modify(po[t][1],1,1);
}
else
{
Modify(po[t][0],0,-1); Modify(po[t][1],1,-1);
po[t][0] = po[t][1]; po[t][1] = k;
Modify(po[t][0],0,1); Modify(po[t][1],1,1);
}
}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint(); m = getint(); m = getint();
for (int i = 1; i <= n; i++) a[i] = getint();
for (int i = 1; i <= m; i++)
{
int l = getint(),r = getint();
Q[i] = Query(l,r,i);
}
sort(Q + 1,Q + m + 1);
int tail = 0;
for (int i = 1; i <= m; i++)
{
while (tail < Q[i].r) Insert(++tail);
ans[Q[i].num] = Sum(Q[i].r,1) - Sum(Q[i].l-1,0);
}
for (int i = 1; i <= m; i++) printf("%d\n",ans[i]);
return 0;
}