1619. [HEOI2012]采花
1619. [HEOI2012]采花
★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比
时间限制:5 s
内存限制:128 MB
【题目描述】
【输入格式】
【输出格式】
【样例输入】
5 3 5
1 2 2 3 1
1 5
1 2
2 2
2 3
3 5
【样例输出】
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的花各一朵,公主不采。
【提示】
【数据范围】
对于100%的数据,1 ≤ n ≤ 10^6,c ≤ n,m ≤10^6。
【来源】
【题目来源】
一开始以为这题是线段树,,后来发现线段树好像没有这个功能
然后就想莫队,看了看时间发现还有一个半小时,以为这道题做不出来了,就打了个暴力去做T2
临收卷还有二十分钟左右的时候老师说T3莫队20分,(后来我用莫队AC了,,,实力打脸)
暴力思路:暴力
正解:
1.裸莫队

1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #include<algorithm>
6 using namespace std;
7 const int MAXN=1000001;
8 int colornum[MAXN],n,color,m,a[MAXN];
9 int base,pos[MAXN],out[MAXN];
10 struct node
11 {
12 int l,r,id;
13 }q[MAXN];
14 int ans=0;
15 int read(int & n)
16 {
17 char c='/';int flag=0,x=0;
18 while(c<'0'||c>'9')
19 {c=getchar();}
20 while(c>='0'&&c<='9')
21 {x=(x<<3)+(x<<1)+(c-48);
22 c=getchar();}
23 n=x;
24 }
25 inline void dele(int where)
26 {
27 if(colornum[a[where]]==2)
28 ans--;
29 colornum[a[where]]--;
30 }
31 inline void add(int where)
32 {
33 if(colornum[a[where]]==1)
34 ans++;
35 colornum[a[where]]++;
36 }
37 inline int comp(const node & a,const node & b)
38 {
39 if(pos[a.l]==pos[b.l])
40 return a.r<b.r;
41 else return pos[a.l]<pos[b.l];
42 }
43 inline void modui()
44 {
45 int l=1,r=0;
46 for(int i=1;i<=m;++i)
47 {
48 for(;l<q[i].l;++l)
49 dele(l);
50 for(;l>q[i].l;--l)
51 add(l-1);
52 for(;r<q[i].r;++r)
53 add(r+1);
54 for(;r>q[i].r;--r)
55 dele(r);
56 out[q[i].id]=ans;
57 }
58 }
59 int main()
60 {
61 freopen("1flower.in","r",stdin);
62 freopen("1flower.out","w",stdout);
63 read(n);read(color);read(m);
64 base=sqrt(n);
65 for(int i=1;i<=n;++i)
66 read(a[i]);
67 for(int i=1;i<=n;++i)
68 pos[i]=(i-1)/base+1;
69 for(int i=1;i<=m;++i)
70 {
71 read(q[i].l);
72 read(q[i].r);
73 q[i].id=i;
74 }
75 sort(q+1,q+m+1,comp);
76 modui();
77 for(int i=1;i<=m;++i)
78 {
79 printf("%d\n",out[i]);
80 }
81 return 0;
82 }

2.因为一个颜色只有出现两次的时候才会被采
那么我们可以记录这个颜色出现的位置,但这个颜色出现的次数达到两次的时候,我们把这个区间+1
维护区间可以用树状数组实现

1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #include<algorithm>
6 using namespace std;
7 const int MAXN=1000001;
8 int a[MAXN];
9 int lb(int x)
10 {return x&-x;}
11 int read(int & n)
12 {
13 char c='/';int flag=0,x=0;
14 while(c<'0'||c>'9')
15 {if(c=='-')flag=1;
16 c=getchar();}
17 while(c>='0'&&c<='9')
18 {x=x*10+(c-48);
19 c=getchar();}
20 if(flag)n=-x;
21 else n=x;
22 }
23 int n,colornum,m;
24 struct node
25 {
26 int l,r,id;
27 }q[MAXN];
28 int first[MAXN],second[MAXN],tree[MAXN],ans[MAXN];
29 int comp(const node & a ,const node & b)
30 {return a.r<b.r;}
31 void add(int pos,int v)
32 {
33 while(pos<=n)
34 {
35 tree[pos]+=v;
36 pos=pos+lb(pos);
37 }
38
39 }
40 int query(int pos)
41 {
42 int tot=0;
43 while(pos)
44 {
45 tot=tot+tree[pos];
46 pos=pos-lb(pos);// 等差序列维护所以从尾加到头
47 }
48 return tot;
49 }
50 int main()
51 {
52 //freopen("1flower.in","r",stdin);
53 //freopen("1flower.out","w",stdout);
54 read(n);read(colornum);read(m);
55 for(int i=1;i<=n;i++)
56 {
57 read(a[i]);
58 second[i]=first[a[i]];// 如果a[i]出现过的话,那么second一定是记录的第二次的位置
59 first[a[i]]=i;
60 }
61 for(int i=1;i<=m;i++)
62 {
63 read(q[i].l);read(q[i].r);
64 q[i].id=i;
65 }
66 sort(q+1,q+m+1,comp);
67 int last=1;
68 for(int i=1;i<=n;i++)
69 {
70 if(second[i])
71 {
72 add(second[i]+1,-1);
73 add(second[second[i]]+1,1);
74 }
75 while(i==q[last].r)
76 {
77 ans[q[last].id]=query(q[last].l);
78 last++;
79 }
80 }
81 for(int i=1;i<=m;i++)
82 printf("%d\n",ans[i]);
83 return 0;
84 }
1619. [HEOI2012]采花的更多相关文章
- cogs:1619. [HEOI2012]采花/luogu P2056
1619. [HEOI2012]采花 ★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿是 ...
- COGS 1619. [HEOI2012]采花
★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿是古国的公主,平时的一大爱好是采花. 今 ...
- cogs1619. [HEOI2012]采花 x
1619. [HEOI2012]采花 ★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿是 ...
- BZOJ 2743: [HEOI2012]采花
2743: [HEOI2012]采花 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 2056 Solved: 1059[Submit][Status ...
- [bzoj2743][HEOI2012]采花(树状数组+离线)
2743: [HEOI2012]采花 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 1832 Solved: 954[Submit][Status] ...
- BZOJ 2743: [HEOI2012]采花( 离线 + BIT )
处理出每个数下一个出现的位置, 然后按左端点排序回答询问.处理当前数去除的影响 ------------------------------------------------------------ ...
- BZOJ_2743_[HEOI2012]采花_离线+树状数组
BZOJ_2743_[HEOI2012]采花_离线+树状数组 Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大 ...
- BZOJ 2743: [HEOI2012]采花 离线树状数组
2743: [HEOI2012]采花 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 Description 萧芸斓是Z国的公主, ...
- 【BZOJ2743】[HEOI2012]采花 离线+树状数组
[BZOJ2743][HEOI2012]采花 Description 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花, ...
随机推荐
- 选择器的使用(not选择器)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- Map根据value排序ASC DESC
原文:http://blog.csdn.net/k21325/article/details/53259180 需求有点刁钻,写关键词组合匹配标题的时候,遇到关键词像这样 XXX XXX 1222 X ...
- Cookie对象的特点
1.存储少量不重要的数据2.存储在客户端的文本文件中(必须设置有效期,否则不被存储)3.安全性差4.存储的数据类型--字符串5.浏览器窗口无关,但与访问的站点相关6.具体特定的过期时间和日期7.在客户 ...
- js美化压缩工具Mark一下
jscompress https://www.jscompress.cn/
- Hdu2111
<span style="color:#6600cc;">/* J - Saving HDU Time Limit:1000MS Memory Limit:32768K ...
- LeetCode_Mysql_Second Highest Salary
176. Second Highest Salary 1. 问题描写叙述: 写一个sql语句从 Employee 表里获取第二高位的工资. 2. 解决思路: 这道题非常easy,就当热身了.首先用ma ...
- xenserver PXE安装系统错误的解决
刚开始在xenserver里找pxe启动安装系统找了半天,最后在NEW VM里的template里选择other install media 里找到pxe启动,启动之后加载映像,安装到一半又停止了, ...
- Vim i和a差别
i是当前位置插入 a是当前文字的后面插入
- (十七)LU分解
#encoding=utf-8 import numpy as np # 输入数据 # a用来记录x的系数 a=[[2.0,2.0,3.0],[4.0,7.0,7.0],[-2.0,4.0,5.0]] ...
- C#计算运行时间
using System.Diagnostics; private Stopwatch stw = new Stopwatch(); stw.Start(); stw.Stop(); MessageB ...