cogs1619. [HEOI2012]采花 x
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。
【来源】
【题目来源】
思路:
1)首先暴力只能够拿到20分
2)莫队(真心不太懂。。。还没写)
3)离线+树状数组维护
上代码:
1)暴力(20)代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio> using namespace std; const int M = 1e6 + ;
int n,c,m,ans;
int color[M],v[M]; inline int reads()
{
int x=,f=;char ch=getchar();
while(ch<'' || ch>'')
{if(ch=='-') f=-;ch=getchar();}
while(ch>='' && ch<='')
{x=*x+ch-'';ch=getchar();}
return x*f;
} void orz(int l,int r)
{
ans=;
memset(v,,sizeof(v));
for(int j=l;j<=r;j++)
v[color[j]]++;
for(int j=;j<=c;j++)
if(v[j]>)
ans++;
printf("%d\n",ans);
} int main()
{
n=reads();c=reads();m=reads();
for(int i=;i<=n;i++)
color[i]=reads();
for(int i=,l,r;i<=m;i++)
{
l=reads();r=reads();
orz(l,r);
}
return ;
}
2)(伪)正解:
据说是莫队???
为什么我写莫队T了2点qwq
T2点代码qwq:
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std; const int M = ;
int n,c,m,size,curL,curR,answer;
int a[M],cnt[M],ans[M]; struct G {
int l,r,id,block;
bool operator < (const G &qwq)const
{
if(block==qwq.block)
return r < qwq.r;
return block < qwq.block;
}
}t[M]; inline int read(int &qwq)
{
char ch=' ';int q=,w=;
for(;(ch!='-')&&((ch<'')||(ch>''));ch=getchar());
if(ch=='-') w=-,ch=getchar();
for(;ch>='' && ch<='';ch=getchar())q=q*+ch-;
qwq=q*w;
return qwq;
} inline void add(int pre)
{
cnt[a[pre]]++;
if(cnt[a[pre]]==)
answer++;
} inline void change(int pre)
{
cnt[a[pre]]--;
if(cnt[a[pre]]==)
answer--;
} inline void solve()
{
curL=,curR=;
for(int i=;i<=m;++i)
{
while(curL<t[i].l)
change(curL),curL++;
while(curL>t[i].l)
curL--,add(curL);
while(curR<t[i].r)
curR++,add(curR);
while(curR>t[i].r)
change(curR),curR--;
ans[t[i].id]=answer;
}
} int main()
{
freopen("1flower.in","r",stdin);
freopen("1flower.out","w",stdout);
read(n),read(c),read(m);
size=sqrt(n);
for(int i=;i<=n;++i)
read(a[i]);
for(int i=;i<=m;++i)
{
read(t[i].l),read(t[i].r);
t[i].block=(t[i].l-)/size+;
t[i].id=i;
}
sort(t+,t+m+);
solve();
for(int i=;i<=m;++i)
printf("%d\n",ans[i]);
return ;
}
难道是我的读入优化写得太丑了???orz
应该不会!!!
3)正解:
离线+树状数组维护
#include<iostream>
#include<cstdio>
#include<algorithm>
#define Lowbit(x) (x&(-x))
using namespace std; const int M = ;
int n;
int a[M],gs[M],c[M],next[M],last[M];
int ans[M]; struct node {
int l,r,id;
bool operator < (const node &qwq)const
{
return l<qwq.l;
}
}q[M]; int read()
{
int x=,f=;
char ch=getchar();
while(ch<'' || ch>'')
{if(ch=='-') f=-;ch=getchar();}
while(ch>='' && ch<='')
{x=x*+(ch-'');ch=getchar();}
return x*f;
} void Update(int k,int x)
{
while(k<=n)
{
c[k]+=x;
k+=Lowbit(k);
}
} int Sum(int k)
{
int sum=;
while(k>)
{
sum+=c[k];
k-=Lowbit(k);
}
return sum;
} int main()
{
freopen("1flower.in","r",stdin);
freopen("1flower.out","w",stdout);
int c,m,i,L;
n=read(),c=read(),m=read();
for(i=; i<=n; i++)
a[i]=read();
for(i=; i<=m; i++)
q[i].l=read(),q[i].r=read(),q[i].id=i;
sort(q+,q+m+);
for(i=; i<=n; i++)
{
if(last[a[i]]!=)
next[last[a[i]]]=i;
last[a[i]]=i;
}
for(i=; i<=n; i++)
{
gs[a[i]]++;
if(gs[a[i]]==)
Update(i,);
}
L=;
for(i=; i<=m; i++)
{
while(L<q[i].l)
{
if(next[L]!=)
Update(next[L],-);
if(next[next[L]]!=)
Update(next[next[L]],);
L++;
}
ans[q[i].id]=Sum(q[i].r)-Sum(q[i].l-);
}
for(i=; i<=m; i++)
printf("%d\n",ans[i]);
return ;
}
cogs1619. [HEOI2012]采花 x的更多相关文章
- 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朵花, ...
- cogs:1619. [HEOI2012]采花/luogu P2056
1619. [HEOI2012]采花 ★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿是 ...
- 1619. [HEOI2012]采花
1619. [HEOI2012]采花 ★★☆ 输入文件:1flower.in 输出文件:1flower.out 简单对比 时间限制:5 s 内存限制:128 MB [题目描述] 萧薰儿 ...
- [bzoj2743][HEOI2012]采花_树状数组
采花 bzoj-2743 HEOI-2012 题目大意:给定n朵花,每朵花有一个种类,m次询问:一段区间中至少出现两朵花的种类的个数. 注释:$1\le n,m\le10^6$. 想法:这个题超级像H ...
随机推荐
- C++序列容器之 vector常见用法总结
一.关于vector 本文默认读者具有一定的c++基础,故大致叙述,但保证代码正确. vector是一个动态的序列容器,相当于一个size可变的数组. 相比于数组,vector会消耗更多的内存以有效的 ...
- CSS(上)
目录 CSS(上) 什么是CSS? CSS的优点 CSS的引入方式 行内样式 内部样式 外部样式 CSS的两大特性 CSS选择器 基本选择器 组合选择器 更多选择器 选择器的优先级 CSS(上) 什么 ...
- Linux-1.2关机重启reboot,shutdown
关机重启:reboot,shutdown reboot 重启操作系统 shutdown -r now 重启,shutdown会给其他用户提示
- Codeforces 1196E. Connected Component on a Chessboard
传送门 注意到棋盘可以看成无限大的,那么只要考虑如何构造一个尽可能合法的情况 不妨假设需要的白色格子比黑色格子少 那么容易发现最好的情况之一就是白色排一排然后中间黑的先连起来,剩下黑色的再全部填白色周 ...
- sql server row_number分页
row_number分页 SELECT TOP 10* --pageSize =10FROM ( SELECT *, row_number () OVER (ORDER BY a.bsqID ...
- 11 Python之初识函数
---恢复内容开始--- 1. 什么是函数? f(x) = x + 1 y = x + 1 函数是对功能或者动作的封装 2. 函数的语法和定义 def 函数名(): 函数体 调用: 函数名() 3. ...
- Caffe之prototxt
1.可视化工具: http://ethereon.github.io/netscope/quickstart.html 2.常用网络模型caffe-model之.prototxt: https://g ...
- Redhat 7修改主机名
修改主机名: Linux master2 3.10.0-693.el7.x86_64 #1 SMP Thu Jul 6 19:56:57 EDT 2017 x86_64 x86_64 x86_64 G ...
- python django网站编程视频教程学习资料下载
“人生苦短,我用python”,学python的小伙伴应该都了解这句话的含义.但是,学python,你真正了了解强大的Django框架吗!?据说Django还是由吉普赛的一个吉他手的名字命名的呢,有木 ...
- Clob类型转换为String
SQL CLOB 是内置类型,它将字符大对象存储为数据库表某一行中的一个列值,使用CHAR来存储数据,如XML文档. 如下是一个Clob转换为String的静态方法,可将其放在自己常用的工具类中,想直 ...