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 第 ...
随机推荐
- Python运维开发:初识Python(一)
一.Pythton简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...
- socket和http的区别
1.HTTP连接 HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用 ...
- java数据结构 • 面向对象 • 异常 • 随机数·时间
• 语法基础 • 控制流 • 数据结构 • 面向对象 • 异常 • 随机数 //String常用的方法: indexOf charAt charAt codePointAt compa ...
- mysql之系统默认数据库
相关内容: 系统默认数据库information_schema,performance_schema,mysql,test 的意义 首发时间:2018-02-23 17:10 安装mysql完成后, ...
- Android Demo Android ActionBarCompat-ListPopupMenu
示例显示如何使用v7 appcompat库中的PopupMenu显示弹出式菜单.主界面使用V4支持库的ListFragment显示数据列表,当点击列表子项时,在子项下方弹出下拉菜单,并通过设置setO ...
- [20181108]12c sqlplus rowfetch参数4.txt
[20181108]12c sqlplus rowfetch参数4.txt --//12cR2 可以改变缺省rowfetch参数.11g之前缺省是1.通过一些测试说明问题.--//前几天做的测试有点乱 ...
- 自动化测试基础篇--Selenium鼠标键盘事件
摘自https://www.cnblogs.com/sanzangTst/p/7477382.html 前面几篇文章我们学习了怎么定位元素,同时通过实例也展示了怎么切换到iframe,怎么输入用户名和 ...
- 【公众号系列】SAP S/4 HANA 1809请查收
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[公众号系列]SAP S/4 HANA 1809 ...
- JaveScript 中的正则表达式
1.语法: var expression = /pattern/flags ; pattern: 任何简单或复杂的正则表达式. flags: 可以是 g,i,m 或它们的组合. ...
- June 15. 2018 Week 24th Friday
If at first you don't succeed, then dust yourself off and try again. 失败了没关系,重振旗鼓,从头再来. From Aaliyah, ...