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朵花, ...
随机推荐
- Struts2+JQuery+Json登陆实例
Struts2+JQuery+Json登陆实例 博客分类: Struts2 在搭建之前.. 首先,需要准备struts2.0框架的5个核心包, 以及jsonplugin-0.32.jar 以及js ...
- 解析Xml文件的三种方式
1.Sax解析(simple api for xml) 使用流式处理的方式,它并不记录所读内容的相关信息.它是一种以事件为驱动的XML API,解析速度快,占用内存少.使用回调函数来实现. clas ...
- unity3d GUI字体设置
using System.Collections; using System.Collections.Generic; using UnityEngine; public class click001 ...
- js读取excel中日期格式转换问题
在使用js-xlsx插件来读取excel时,会将2018/10/16这种数据自动装换成48264.12584511. 所以需要自己手动再转换回来 // excel读取2018/01/01这种时间格式是 ...
- atcode062D(预处理&优先队列)
题目链接:http://abc062.contest.atcoder.jp/tasks/arc074_b 题意:从3*n个元素中删除n个元素,使得剩余元素中前n个元素的和减后n个元素的和最大: 思路: ...
- 719D(树形dp)
题目链接:http://codeforces.com/contest/791/problem/D 题意:给出一棵树,每两个点之间的距离为1,一步最多可以走距离 k,问要将任意两个点之间的路径都走一遍, ...
- 洛谷P3572 [POI2014]PTA-Little Bird
P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...
- codevs1052 地鼠游戏
1052 地鼠游戏 题目描述 Description 王钢是一名学习成绩优异的学生,在平时的学习中,他总能利用一切时间认真高效地学习,他不但学习刻苦,而且善于经常总结.完善自己的学习方法,所以他总能在 ...
- Maven打包Spark程序Pom配置
scala和java混合的spark程序之前使用其他配置始终有报找不到包的情况,尝试了一下如下配置可以打包成功.<build> <pluginManagement> <p ...
- jQuery EasyUI/TopJUI基本的数字输入框(保留两位小数,带前缀后缀...)
jQuery EasyUI/TopJUI基本的数字输入框(保留两位小数,带前缀后缀...) numberbox(数值输入框) HTML required:必填字段,默认为false:prompt:显示 ...