COGS 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。
【来源】
【题目来源】
居然有人说莫队过不了 哈哈哈
完美卡过~
① 莫队
15.38s
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <cmath>
#define N 1000005
using namespace std;
template<typename T>
inline void Read(T &x)
{
register char ch=getchar();
for(x=;!isdigit(ch);ch=getchar());
for(;isdigit(ch);x=x*+ch-'',ch=getchar());
}
struct node
{
int l,r,bel,id,ans;
}opt[N];
int n,c,m,col[N],sum[N],ans,Ans[N];
bool cmp(node a,node b)
{
if(a.bel!=b.bel) return a.bel<b.bel;
else return a.r<b.r;
}
void update(int pos,int v,int type)
{
sum[col[pos]]+=v;
if(sum[col[pos]]==type) ans+=v;
}
int main(int argc,char *argv[])
{
freopen("1flower.in","r",stdin);
freopen("1flower.out","w",stdout);
Read(n);Read(c);Read(m);
for(int i=;i<=n;++i) Read(col[i]);
int C=sqrt(n);
for(int i=;i<=m;++i)
{
Read(opt[i].l);Read(opt[i].r);
opt[i].bel=(opt[i].l-)/C+;
opt[i].id=i;
}
sort(opt+,opt++m,cmp);
for(int L=,R=,i=;i<=m;++i)
{
while(L<opt[i].l) update(L++,-,);
while(L>opt[i].l) update(--L,,);
while(R<opt[i].r) update(++R,,);
while(R>opt[i].r) update(R--,-,);
Ans[opt[i].id]=ans;
}
for(int i=;i<=m;++i) printf("%d ",Ans[i]);
return ;
fclose(stdin);
fclose(stdout);
}
② 离线树状数组
2.021 s (Rank 1蛤蛤)
#include <algorithm>
#include <cstdio>
#include <cctype>
#define N 1000005
#define BUF 25312312 using namespace std;
char Buf[BUF],*buf=Buf;
template<typename T>
inline void Read(T &x)
{
for(x=;!isdigit(*buf);++buf);
for(;isdigit(*buf);x=x*+*buf-'',++buf);
}
inline int lowbit(int x){return x&(-x);}
int n,c,m,nextt[N],pre[N],col[N],ans[N],tag[N];
struct node
{
int l,r,id;
}opt[N];
bool cmp(node a,node b){return a.l<b.l;}
inline void update(int x,int v)
{
for(;x<=n;x+=lowbit(x)) tag[x]+=v;
}
inline int ask(int x)
{
int ret=;
for(;x;x-=lowbit(x)) ret+=tag[x];
return ret;
}
int Main()
{
freopen("1flower.in","r",stdin);
freopen("1flower.out","w",stdout);
fread(buf,,BUF,stdin);
Read(n);Read(c);Read(m);
for(int i=;i<=n;++i) Read(col[i]);
for(int i=n;i>=;--i) nextt[i]=pre[col[i]],pre[col[i]]=i;
for(int i=;i<=c;++i)
if(nextt[pre[i]])
update(nextt[pre[i]],);
for(int i=;i<=m;++i) Read(opt[i].l),Read(opt[i].r),opt[i].id=i;
sort(opt+,opt++m,cmp);
int L=;
for(int i=;i<=m;++i)
{
while(L<opt[i].l)
{
if(nextt[L]) update(nextt[L],-);
if(nextt[nextt[L]]) update(nextt[nextt[L]],);
L++;
}
ans[opt[i].id]=ask(opt[i].r)-ask(opt[i].l-);
}
for(int i=;i<=m;++i) printf("%d\n",ans[i]);
return ;
}
int sb=Main();
int main(int argc,char *argv[]){;}
COGS 1619. [HEOI2012]采花的更多相关文章
- 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 [题目描述] 萧薰儿 ...
- 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朵花, ...
随机推荐
- Ubuntu下如何禁用IPv6
Ubuntu下如何禁用IPv6 2013-10-16 11:32:02 分类: HADOOP 分布式下的hadoop/hbase运行总出问题,zookeeper连接总是出问题,怀疑可能是ip ...
- js中match的用法
match() 方法将检索字符串 stringObject,以找到一个或多个与 regexp 匹配的文本.这个方法的行为在很大程度上有赖于 regexp 是否具有标志 g. 一.如果 regexp 没 ...
- DOM,date,字符串
ECMAscript Dom doc Bom Browerwindow --窗口. location --地址栏. history --历史. document --文档. statue --任务栏& ...
- ASP.NET学习笔记(五)ASP 对象
1.ASP Response 对象用于从服务器向用户发送输出的结果. 2.ASP Request 对象用于从用户那里取得信息 Request.QueryString 命令用于搜集使用 method=& ...
- .net实现IHttpModule接口顾虑器
这篇文章主要介绍了C#使用IHttpModule接口修改http输出的方法,涉及C#操作IHttpModule接口的相关技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#使用IHtt ...
- 快速发现并解决maven依赖传递冲突
此文已由作者翟曜授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近在测试过程中,遇到了几次maven传递依赖冲突的问题,所以记录下解决的过程,遇到类似问题供参照. 问题现象 ...
- codeforces590E Birthday【AC自动机+Floyd+匈牙利算法】
因为没有重复串,所以把有包含关系的串连边之后是个DAG,也就是二分图,就变成求二分图的最大独立集=n-最小点覆盖=n-最大匹配 关于包含关系,建出AC自动机,然后把串放上去找子串,但是如果每次都一路找 ...
- linux 查看系统版本号(转)
一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@localhost ~]# cat /proc/versionLinux version 2.6.18 ...
- compass初探
1.安装compass: sudo gem install compass 如果你用的是Windows系统,那么要省略前面的sudo. 2.项目初始化 接下来,(首先要进入目标目录.)要创建一个你的C ...
- python之set集合、深浅拷贝
一.基本数据类型补充 1,关于int和str在之前的学习中已经介绍了80%以上了,现在再补充一个字符串的基本操作: li = ['李嘉诚','何炅','海峰','刘嘉玲'] s = "_&q ...