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朵花, ...
随机推荐
- scores
题意: m维偏序问题. 解法: 考虑对每一维按照每一个元素在这一维的数值分块,对于每一个块维护一个大小为 n 的bitset,表示前缀/后缀满足条件的元素集合. 对于每一个询问,我们可以枚举找到相应的 ...
- STM in Clojure
Transactional memory in Clojure is implemented using Multiversion Concurrency Control protocol http: ...
- Apache2.2安装图解
Apache2.2安装图解 2010-12-14 15:32:44| 分类: 不学无术之杂 | 标签:安装 端口 httpd apache2.2 服务器 |字号 订阅 Apache音译 ...
- ASP.NET Core 3.0 自动挡换手动挡:在 Middleware 中执行 Controller Action
最近由于发现奇怪的 System.Data.SqlClient 性能问题(详见之前的博文),被迫提前了向 .NET Core 3.0 的升级工作(3.0 Preview 5 中问题已被修复).郁闷的是 ...
- 获得HttpWebResponse请求的详细错误内容
try { } catch (WebException ex) { HttpWebResponse response = (HttpWebResponse)ex.Response; Console.W ...
- uoj#228. 基础数据结构练习题(线段树)
传送门 只有区间加区间开方我都会--然而加在一起我就gg了-- 然后这题的做法就是对于区间加直接打标记,对于区间开方,如果这个区间的最大值等于最小值就直接区间覆盖(据ljh_2000大佬说这个区间覆盖 ...
- jquery插件fileupload图片上传(前端如何处理)
1.页面首先引入jquery,版本不要低于1.6 <script src="../js/jquery.min.js"></script>2.其次页面引入对应 ...
- java.sql.SQLException: Could not commit with auto-commit set on
This kind of exceptions occur when the Oracle JDBC Driver (ojdbc6.jar) version 12 or above will be u ...
- UVa 11168(凸包、直线一般式)
要点 找凸包上的线很显然 但每条线所有点都求一遍显然不可行,优化方法是:所有点都在一侧所以可以使用直线一般式的距离公式\(\frac{|A* \sum{x}+B* \sum{y}+C*n|}{\sqr ...
- CodeForces - 296A-Yaroslav and Permutations(思维)
Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring a ...