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. 首先 ...
随机推荐
- 优化深度神经网络(三)Batch Normalization
Coursera吴恩达<优化深度神经网络>课程笔记(3)-- 超参数调试.Batch正则化和编程框架 1. Tuning Process 深度神经网络需要调试的超参数(Hyperparam ...
- pandas索引操作
Pandas的索引操作 索引对象Index 1. Series和DataFrame中的索引都是Index对象 示例代码: print(type(ser_obj.index)) print(type(d ...
- VS2010调用halcon的时候出现试图加载格式不正确的程序(this.hWindowControl1 = new HalconDotNet.HWindowControl();)
[重要错误修改] /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容. /// </summary> private v ...
- FFmpeg新版本(2016年10月份以后) 支持硬件解码
FFmpeg provides a subsystem for hardware acceleration. Hardware acceleration allows to use specific ...
- Vmware中的centos虚拟机克隆之后没有eth0
克隆虚拟机之后,CentOS没有eth0的解决办法 我们常常需要从一台已经安装完成的虚拟机系统克隆出来一个新系统(克隆时候必须要改变网卡物理地址,这一点无需多说),但是新系统启动之后,会发现系统网络工 ...
- java实现 数组中两个元素相加等于指定数的所有组合
package com.algorithm.hash; public class alg1 { public static void main(String argv[]) { int[] arr ...
- 安装gdb insight(6.8.1)
如果之前安装过6.8或其它版本,请先删除以下目录 rm -rf /usr/local/insight rm -rf /usr/share/tcltk 如果之前设置过环境变量,也请删除 unset TC ...
- Cunit编译安装
1. Examples/Makefile.am:26: to 'configure.ac' and run 'autoconf' again. configure.ac:211: error: re ...
- 453D Little Pony and Elements of Harmony
传送门 分析 我们可以将所有的b[i^j]直接对应到b[f(i^j)]上 于是显然可以fwt 我们对b进行t次fwt之后直接将答案与e0卷起来即可 注意由于模数不确定,我们可以将模数扩大$2^m$然后 ...
- SP1557 GSS2 - Can you answer these queries II
一开始看不懂题解,看懂了题解之后觉得还是挺妙的. 好多题解里都提到了HH的项链,但是我觉得关系并不大啊…… 先把所有询问离线下来按照右端点排序,按照询问的要求一个一个加入数字,怎么加入数字,我们设计一 ...