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朵花, ...
随机推荐
- LeetCode: 371 Sum of Two Integers(easy)
题目: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. ...
- JavaScript 检验变量
创建: 2019/02/20 迁入: 删除[WIP]标签(因为随时更新, 不存在完成不完成) 从[JavaScript 式与运算符]迁入typeof 更新: 2019/03/25 补充静态变量与参 ...
- SAS批量导出sas7bdata至excel
/*创建输出excel的宏*/ %macro export(inlib,intbl,outpath,outfile); proc export data=&inlib..&intbl ...
- ue4 3dui材质参数修改
- Noip2016day1 玩具迷题toy
题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...
- codevs2924 数独挑战
2924 数独挑战 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description "芬兰数学家因卡拉,花费3个月时间设计出了世界 ...
- Maven入门 项目的生命周期&pom.xml配置&仓库
- css布局全总结
一 居 中 布 局 水平居中 1. 使用inline-block+text-align(1)原理.用法 原理:先将子框由块级元素改变为行内块元素,再通过设置行内块元素居中以达到水平居中. 用法:对子 ...
- [Java]String、 StringBuffer、StringBuilder的区别
一.异同点: 1) 都是 final 类, 都不允许被继承; 2) String 长度是不可变的, StringBuffer.StringBuilder 长度是可变的; 3) StringBuffer ...
- Codeforces 1154G(枚举)
我预处理\(1e7log(1e7)\)的因数被T掉了,就不敢往这个复杂度想了--无奈去看AC代码 结果怎么暴举gcd剪一剪小枝就接近3s卡过去了!vector有锅(确信 const int maxn ...