luoguP4396 [AHOI2013]作业
https://www.luogu.org/problemnew/show/P4396
简单的莫队+树状数组,但博主被卡常了,不保证代码在任何时候都能AC
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &f) {
f = 0; T fu = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') fu = -1; c = getchar();}
while (c >= '0' && c <= '9') {f = (f << 3) + (f << 1) + (c & 15); c = getchar();}
f *= fu;
}
const int N = 3e5 + 5;
int cnt[N], f[2][N], a[N], B[N], pre[N], Ans[N][2];
int n, m, block, len;
struct ele {
int l, r, _l, _r, id;
bool operator < (const ele A) const {
return B[l] < B[A.l] || (B[l] == B[A.l] && r < A.r);
}
}Q[N];
int lowbit(int x) {return x & -x;}
void add(int *f, int x, int y) {for(int i = x; i <= n; i += lowbit(i)) f[i] += y;}
int query(int *f, int x) {int ans = 0; for(int i = x; i; i -= lowbit(i)) ans += f[i]; return ans;}
void change(int x, int y) {
if(y == 1) {
add(f[0], x, 1);
if(cnt[x] == 0) add(f[1], x, 1);
cnt[x]++;
} else {
add(f[0], x, -1);
cnt[x]--;
if(cnt[x] == 0) add(f[1], x, -1);
}
}
int main() {
cin >> n >> m; block = n / (sqrt(m * 2 / 3) + 1) + 1;
for(int i = 1; i <= n; i++) B[i] = (i - 1) / block + 1;
for(int i = 1; i <= n; i++) read(a[i]), pre[++len] = a[i];
for(int i = 1; i <= m; i++) {
int l, r, L, R;
read(l); read(r);
read(L); read(R);
Q[i] = (ele) {l, r, L, R, i};
pre[++len] = L, pre[++len] = R;
}
sort(pre + 1, pre + len + 1);
len = unique(pre + 1, pre + len + 1) - pre - 1;
for(int i = 1; i <= m; i++) {
Q[i]._l = lower_bound(pre + 1, pre + len + 1, Q[i]._l) - pre;
Q[i]._r = lower_bound(pre + 1, pre + len + 1, Q[i]._r) - pre;
}
for(int i = 1; i <= n; i++) a[i] = lower_bound(pre + 1, pre + len + 1, a[i]) - pre;
sort(Q + 1, Q + m + 1);
int l = 1, r = 0;
for(int i = 1; i <= m; i++) {
while(r < Q[i].r) change(a[++r], 1);
while(l > Q[i].l) change(a[--l], 1);
while(r > Q[i].r) change(a[r--], -1);
while(l < Q[i].l) change(a[l++], -1);
Ans[Q[i].id][0] = query(f[0], Q[i]._r) - query(f[0], Q[i]._l - 1);
Ans[Q[i].id][1] = query(f[1], Q[i]._r) - query(f[1], Q[i]._l - 1);
}
for(int i = 1; i <= m; i++) printf("%d %d\n", Ans[i][0], Ans[i][1]);
return 0;
}
luoguP4396 [AHOI2013]作业的更多相关文章
- 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 ...
- BZOJ3236: [AHOI2013]作业
BZOJ3236: [AHOI2013]作业 题目描述 传送门 行,我知道是Please contact lydsy2012@163.com! 传送门2 题目分析 这题两问还是非常,emmmm. 首先 ...
随机推荐
- ceph 创建和删除osd
ceph 创建和删除osd 1.概述 本次主要是使用ceph-deploy工具和使用ceph的相关命令实现在主机上指定磁盘创建和删除osd,本次以主机172.16.1.96(主机名ha ...
- Deep Learning 学习笔记(7):神经网络的求解 与 反向传播算法(Back Propagation)
反向传播算法(Back Propagation): 引言: 在逻辑回归中,我们使用梯度下降法求参数方程的最优解. 这种方法在神经网络中并不能直接使用, 因为神经网络有多层参数(最少两层),(?为何不能 ...
- HTTP及XMLHTTP状态代码一览
(一) HTTP 1.1支持的状态代码 100 Continue 初始的请求已经接受,客户应当继续发送请求的其余部分 101 Switching Protocols 服务器将遵从客户的请求转换到另外一 ...
- Mybatis工具Generator
转自:http://www.cuiyongzhi.com/post/36.html MyBatis Generator(以下简称为MBG),可以逆向生成持久层的基本代码,而且mybatis的实现方案比 ...
- 各大IT/IC公司offer比较
1:本人西电通院2013届毕业硕士,根据今年找工作的情况以及身边同学的汇总,总结各大公司的待遇如下,吐血奉献给各位学弟学妹,公司比较全,你想去的公司不在这里面,基本上是无名小公司了:但无名小公司有时也 ...
- linux驱动模块编译(初学者)
inux 模块编译步骤(转) 本文将直接了当的带你进入linux的模块编译.当然在介绍的过程当中,我也会添加一些必要的注释,以便初学者能够看懂.之所以要写这篇文章,主要是因为从书本上学的话,可能要花更 ...
- phpmailer邮件类
<?php/** * 邮件类 * Enter description here ... * @author df * Mail::getMail()->sendMail(); * */cl ...
- Jquery缩放
$(document).mousemove(function(e) { if (!!this.move) { var posix = !document.move_target ...
- linux加入windows域之完美方案
运行setup工具 认证配置 选择: “use winbind” “use kerberos” “use winbind authertication” 改为: 删除admin server 其余的改 ...
- 使用百度翻译的API接口
http://api.fanyi.baidu.com/api/trans/product/desktop 这是申请的接口地址,会得到一个APPID和一个钥密 然后下载PHP的对应的代码 有一个PHP文 ...