COGS:1822. [AHOI2013]作业
1822. [AHOI 2013] 作业
★★★ 输入文件:ahoi2013_homework.in 输出文件:ahoi2013_homework.out 简单对比
时间限制:20 s 内存限制:512 MB
【题目描述】

【输入格式】

【输出格式】

【样例输入】
3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3
【样例输出】
2 2
1 1
3 2
2 1
【提示】
N=100000,M=1000000
数据极弱(和BZOJ相比),请放心A
【来源】
BZOJ 3236
分析
莫队,用树状数组来维护求值,一个求出现不同数的个数,另一个满足的数字个数。复杂度$O(n\sqrt n log_2n)$
另一种做法:由于这些数都是小于等于n的,所以对权值进行分块,修改$O(1)$,查询$O(\sqrt n)$,总复杂度$O(m \sqrt n)$
交了几次,最后一个点老是TLE,然后想尽办法,读入优化,inline内联函数,最后写上了输出优化。。。还是TLE,之后才发现ans数组开小了,直接开了MAXN,QAQ
代码
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std; const int MAXN = ;
struct Que{
int l,r,a,b,block,id;
bool operator < (const Que &a) const
{
if (block==a.block) return r < a.r;
return block < a.block;
}
}q[];
int a[MAXN],cnt[MAXN],tr[][MAXN],pr[];
int ans1[],ans2[];
int n,m,pos,len; inline 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;
}
inline int lowbit(int x)
{
return x & (-x);
}
inline void change(int x,int c,int t)
{
while (x<=n)
{
tr[t][x] += c;
x += lowbit(x);
}
}
inline int query(int x,int t)
{
int res = ;
while (x)
{
res += tr[t][x];
x -= lowbit(x);
}
return res;
}
inline void update(int x,int c)
{
if (cnt[x]==) change(x,,);
cnt[x] += c;
if (cnt[x]==) change(x,-,);
change(x,c,);
}
inline void solve()
{
int l = , r = ;
for (int i=; i<=m; ++i)
{
for (; l>q[i].l; ) update(a[--l], );
for (; r<q[i].r; ) update(a[++r], );
for (; l<q[i].l; ) update(a[l++], -);
for (; r>q[i].r; ) update(a[r--], -);
ans1[q[i].id] = query(q[i].b, ) - query(q[i].a-, );
ans2[q[i].id] = query(q[i].b, ) - query(q[i].a-, );
}
}
inline void print(int x)
{
while (x)
pr[++len] = x%, x/=;
if (!len) putchar('');
while (len)
putchar(pr[len--]+'');
}
int main()
{
freopen("ahoi2013_homework.in","r",stdin);
freopen("ahoi2013_homework.out","w",stdout);
n = read(); m = read();
pos = (int)sqrt(n);
for (int i=; i<=n; ++i)
a[i] = read();
for (int i=; i<=m; ++i)
{
q[i].l = read(); q[i].r = read();
q[i].a = read(); q[i].b = read();
q[i].block = (q[i].l-)/pos+;
q[i].id = i;
}
sort(q+,q+m+);
solve();
for (int i=; i<=m; ++i)
{
print(ans1[i]),putchar(' ');
print(ans2[i]),putchar('\n');
//printf("%d %d\n",ans1[i],ans2[i]);
}
return ;
}
备注:luogu提交地址
COGS:1822. [AHOI2013]作业的更多相关文章
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- BZOJ 3236: [Ahoi2013]作业
3236: [Ahoi2013]作业 Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1393 Solved: 562[Submit][Status ...
- 树套树专题——bzoj 3110: [Zjoi2013] K大数查询 & 3236 [Ahoi2013] 作业 题解
[原题1] 3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 978 Solved: 476 Descri ...
- Bzoj 3236: [Ahoi2013]作业 莫队,分块
3236: [Ahoi2013]作业 Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1113 Solved: 428[Submit][Status ...
- BZOJ 3236: [Ahoi2013]作业( 莫队 + BIT )
莫队..用两个树状数组计算.时间复杂度应该是O(N1.5logN). 估计我是写残了...跑得很慢... ----------------------------------------------- ...
- BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块
BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块 Description Autumn和Bakser又在研究Gty的妹子序列了 ...
- 【Luogu4396】[AHOI2013]作业(莫队)
[Luogu4396][AHOI2013]作业(莫队) 题面 洛谷 题解 模板题 #include<iostream> #include<cstdio> #include< ...
- [AHOI2013]作业
[AHOI2013]作业 题目大意: 给定一个长度为\(n(n\le10^5)\)的数列\(A(1\le A_i\le n)\).\(m(m\le10^6)\)次询问,每次询问区间\([l,r]\)内 ...
- 【BZOJ3809/3236】Gty的二逼妹子序列 [Ahoi2013]作业 莫队算法+分块
[BZOJ3809]Gty的二逼妹子序列 Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b ...
随机推荐
- excel 在web导入到数据库的操作方法
这个操作的大致步骤是把本地文件存入到服务器端,然后再读取服务端的文件并且使用NPOI这个第三方的插件去读取文件导入到数据库批量插入需要注意的是,前端需要使用form包裹type=file的文件标签,并 ...
- interaction-oriented architecture - MVC
MVC(Model-View-Controller),它是专门针 对交互系统提出的,所以如果我们要构建一个交互系统,那么我们就可以直接应用MVC模式,然后 在该模式所搭建的场景的启发下去发现Model ...
- NGSQC toolkit
一.NGSQCTooklit 使用 主要是去除dapter和低质量的碱基,并有统计结果 可以得到如下的结果 1,每个位置的碱基的平均质量 2,每个GC值对应的reads数 3,每个质量值对应的read ...
- im2rec 修改resize
https://github.com/apache/incubator-mxnet/blob/master/tools/im2rec.py#L196 源码是按照比例修改resize. 现在需要改一个自 ...
- 【[NOI2016]区间】
发现自己的离散化姿势一直有问题 今天终于掌握了正确的姿势 虽然这并不能阻挡我noip退役爆零的历史进程 还是先来看看离散化怎么写吧,我以前都是这么写的 for(std::set<int>: ...
- css层叠样式表总结
一.css css里注释只有一种 /* */ 二.css与HTML四种结合方式 1.行内样式 在标签中加入style属性 内部的写法:key1:value;key2:value; <div s ...
- HDU 6386 Age of Moyu 【BFS + 优先队列优化】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6386 Age of Moyu Time Limit: 5000/2500 MS (Java/Others ...
- SSM框架之多数据源配置
多数据源的应用场景:主要是数据库拆分后,怎样让多个数据库结合起来来达到业务需求. SSM框架(Spring+SpringMVC+MyBatis(MyBatis-Plus))是目前最常用的,此次仍然是m ...
- C#通过指针读取文件
// readfile.cs // 编译时使用:/unsafe // 参数:readfile.txt // C#通过指针读取文件.使用该程序读并显示文本文件. using System; using ...
- 制作二维码java
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...