Gym101138D Strange Queries/BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥
THUWC2019D1T1撞题可还行
以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及……
设\(que_{i,j}\)表示询问\((1,i,1,j)\)的答案,那么询问\((a,b,c,d)=que_{b,d} - que_{a-1 , d} - que_{b , c - 1} + que_{a - 1 , c - 1}\)
把一个询问拆成\(4\)个询问,然后对这\(4\)个询问莫队就可以了
不知道怎么回事THUWC上想到了莫队想到了前缀和想到了容斥就是没想到莫队+前缀和+容斥……
#include<bits/stdc++.h>
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return f ? -a : a;
}
const int MAXN = 5e4 + 3;
int N , M , T , cnt , cntQ;
int arr[MAXN] , times[MAXN][2];
long long ans[MAXN] , cur;
struct query{
int p1 , p2 , ind , flg;
query(int a = 0 , int b = 0 , int c = 0 , int d = 0):p1(a) , p2(b) , ind(c) , flg(d){}
bool operator <(const query b)const{
return p1 / T == b.p1 / T ? p2 < b.p2 : p1 < b.p1;
}
}que[MAXN << 2];
inline void add(int a , int ind){
cur += times[a][ind ^ 1];
++times[a][ind];
}
inline void del(int a , int ind){
cur -= times[a][ind ^ 1];
--times[a][ind];
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
N = read();
T = sqrt(N);
for(int i = 1 ; i <= N ; ++i)
arr[i] = read();
M = read();
for(int i = 1 ; i <= M ; ++i){
int a = read() , b = read() , c = read() , d = read();
que[++cntQ] = query(b , d , i , 1);
if(a - 1)
que[++cntQ] = query(a - 1 , d , i , -1);
if(b - 1)
que[++cntQ] = query(b , c - 1 , i , -1);
if(a - 1 && b - 1)
que[++cntQ] = query(a - 1 , c - 1 , i , 1);
}
sort(que + 1 , que + cntQ + 1);
int l1 = 0 , l2 = 0;
for(int i = 1 ; i <= cntQ ; ++i){
while(l1 < que[i].p1)
add(arr[++l1] , 0);
while(l1 > que[i].p1)
del(arr[l1--] , 0);
while(l2 < que[i].p2)
add(arr[++l2] , 1);
while(l2 > que[i].p2)
del(arr[l2--] , 1);
ans[que[i].ind] += que[i].flg * cur;
}
for(int i = 1 ; i <= M ; ++i)
cout << ans[i] << '\n';
return 0;
}
Gym101138D Strange Queries/BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥的更多相关文章
- BZOJ5016:[SNOI2017]一个简单的询问(莫队)
Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...
- 【BZOJ5016】[Snoi2017]一个简单的询问 莫队
[BZOJ5016][Snoi2017]一个简单的询问 Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计 ...
- 【bzoj5016】[Snoi2017]一个简单的询问 莫队算法
题目描述 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. 输入 第一行,一个数字N,表 ...
- bzoj5016 & loj2254 [Snoi2017]一个简单的询问 莫队
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5016 https://loj.ac/problem/2254 题解 原式是这样的 \[ \su ...
- [bzoj5016][Snoi2017]一个简单的询问
来自FallDream的博客,未经允许,请勿转载,谢谢. 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中 ...
- BZOJ5016 Snoi2017一个简单的询问(莫队)
容易想到区间转化成前缀和.这样每个询问有了二维坐标,莫队即可. #include<iostream> #include<cstdio> #include<cmath> ...
- [SNOI2017]一个简单的询问
[SNOI2017]一个简单的询问 题目大意: 给定一个长度为\(n(n\le50000)\)的序列\(A(1\le A_i\le n)\),定义\(\operatorname{get}(l,r,x) ...
- bzoj P5016[Snoi2017]一个简单的询问——solution
Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input ...
- bzoj 5016: [Snoi2017]一个简单的询问
Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...
随机推荐
- C#字典Dictionay多线程读是否是安全的
答案:是线程安全的,只读不写多线程下,完全不需要加锁! 测试代码: using System; using System.Diagnostics; using System.Threading; us ...
- 章节四、3-While循环-DoWhile语句
一.while死循环 package introduction5; public class WhileDemo { public static void main(String[] args) { ...
- 应用生命周期终极 DevOps 工具包
[编者按]本文作者为 Kevin Goldberg,主要介绍了在开发.运营应用的完整生命周期当中,可能用到的 DevOps 工具大集合.文章系 OneAPM 工程师编译整理. DevOps工具包中合适 ...
- 内核线程的进程描述符task_struct中的mm和active_mm
task_struct进程描述符中包含两个跟进程地址空间相关的字段mm, active_mm, struct task_struct { // ... struct mm_struct *mm; st ...
- 【Git学习一】Git 初始化
在开始Git之旅之前,我们需要设置一下Git的配置变量. 1.告诉Git当前用户的姓名和邮件地址,配置用户名和邮件地址将在版本库提交时用到. 例子: ------------------------- ...
- vscode中配置php的xdebug
vscode中配置php的xdebug vscode配置php的xdebug,步骤如下: 1. 安装phpdebug插件: PHP Debug 2.网上下载php的xdebug扩展(注意根据自己的ph ...
- ASP.NET -- WebForm -- ViewState
ASP.NET -- WebForm -- ViewState 1. ViewState的作用 当 ASP .NET 中的表单被提交时,表单会随所有表单值一同重新出现.这是由于 ASP .NET 维 ...
- JDK动态代理和cglib代理详解
JDK动态代理 先做一下简单的描述,通过代理之后返回的对象已并非原类所new出来的对象,而是代理对象.JDK的动态代理是基于接口的,也就是说,被代理类必须实现一个或多个接口.主要原因是JDK的代理原理 ...
- keepalived脑裂问题查找
在自己环境做keepalived+redis实验时,当重启了备用redies机器后,发现两台redies主机都拿到了VIP [root@redis2 ~]# ip addr list 1: lo: & ...
- InfluxDB部署
InfluxDB介绍 官网:https://www.influxdata.com/ 文档:https://docs.influxdata.com/influxdb/v1.2/introduction/ ...