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. 首先 ...
随机推荐
- windows Server 2008各版本有何区别?
windows Server 2008有几个版本,先一一列出来把: Windows Server 2008 Standard Edition (标准版) Windows Server 2008 ...
- leetcode696
本题先寻找字符串中0变1,或者1变0的位置作为分隔位置.然后从这个分隔位置同时向左.右两侧搜索. 找到的左连续串和右连续串,都进行累计. public class Solution { public ...
- 【转载】用原生JS和html5进行视频截图并保存到本地
支持并尊重原创!原文地址:http://www.cnblogs.com/xieshuxin/p/6731637.html <!doctype html> <html> < ...
- Just a Hook(树状数组)
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. T ...
- Spring Cloud Eureka 2 (Eureka Server搭建服务注册中心)
工具:IntelliJ IDEA 2017.1.2 x64.maven3.3.9 打开IDE file===>new===>project next next 选择相应的依赖 next ...
- html-select
<html><head> <title>select选择标签</title> <meta charset="UTF-8"> ...
- Linux的基本指令--其他命令
一 . 终端翻页: shift-pageup shift-pagedown 二 . 看手册:man man man 2 read 查看read系统函数的man page(在第二个section中,表示 ...
- java Web中页面跳转方式之请求转发
1.在一个请求中跨越多个Servlet 2.多个Servlet在一个请求中,他们共享request对象.就是在Servlet01中setAttribute()保存数据在Servlet02中由getAt ...
- libevent源码深度剖析十二
libevent源码深度剖析十二 ——让libevent支持多线程 张亮 Libevent本身不是多线程安全的,在多核的时代,如何能充分利用CPU的能力呢,这一节来说说如何在多线程环境中使用libev ...
- 扩展卡尔曼滤波EKF与多传感器融合
参考:https://blog.csdn.net/young_gy/article/details/78468153 Extended Kalman Filter(扩展卡尔曼滤波)是卡尔曼滤波的非线性 ...