[SNOI2017]一个简单的询问
[SNOI2017]一个简单的询问
题目大意:
给定一个长度为\(n(n\le50000)\)的序列\(A(1\le A_i\le n)\),定义\(\operatorname{get}(l,r,x)\)为区间\(A_{[l,r]}\)中\(x\)的出现次数。\(m(m\le50000)\)次询问,每次给出\(l_1,r_1,l_2,r_2\),求\(\sum_{x=0}^{\infty}\operatorname{get}(l_1,r_1,x)\cdot\operatorname{get}(l_2,r_2,x)\)。
思路:
\]
因此将每组询问拆成\(4\)个,然后直接套用莫队算法即可。
时间复杂度\(\mathcal O(n\sqrt n)\)。
源代码:
#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=5e4+1,M=5e4;
int n,a[N],block,cnt[2][N];
int64 tmp,ans[M];
struct Query {
int p0,p1,id,type;
bool operator < (const Query &rhs) const {
if(p0/block==rhs.p0/block) return p1<rhs.p1;
return p0/block<rhs.p0/block;
}
};
Query q[M*4];
inline void ins(const bool &t,const int &x) {
tmp-=(int64)cnt[0][x]*cnt[1][x];
cnt[t][x]++;
tmp+=(int64)cnt[0][x]*cnt[1][x];
}
inline void del(const bool &t,const int &x) {
tmp-=(int64)cnt[0][x]*cnt[1][x];
cnt[t][x]--;
tmp+=(int64)cnt[0][x]*cnt[1][x];
}
int main() {
block=sqrt(n=getint());
for(register int i=1;i<=n;i++) a[i]=getint();
const int m=getint();
int tot=0;
for(register int i=0;i<m;i++) {
const int l1=getint(),r1=getint(),l2=getint(),r2=getint();
q[tot++]=(Query){r1,r2,i,1};
if(l2>1) q[tot++]=(Query){r1,l2-1,i,-1};
if(l1>1) q[tot++]=(Query){l1-1,r2,i,-1};
if(l1>1&&l2>1) q[tot++]=(Query){l1-1,l2-1,i,1};
}
std::sort(&q[0],&q[tot]);
for(register int i=0,p0=0,p1=0;i<tot;i++) {
while(p0<q[i].p0) ins(0,a[++p0]);
while(p1<q[i].p1) ins(1,a[++p1]);
while(p0>q[i].p0) del(0,a[p0--]);
while(p1>q[i].p1) del(1,a[p1--]);
ans[q[i].id]+=tmp*q[i].type;
}
for(register int i=0;i<m;i++) {
printf("%lld\n",ans[i]);
}
return 0;
}
[SNOI2017]一个简单的询问的更多相关文章
- 【BZOJ5016】[Snoi2017]一个简单的询问 莫队
[BZOJ5016][Snoi2017]一个简单的询问 Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 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 第 ...
- [bzoj5016][Snoi2017]一个简单的询问
来自FallDream的博客,未经允许,请勿转载,谢谢. 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中 ...
- Gym101138D Strange Queries/BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥
传送门--Gym 传送门--BZOJ THUWC2019D1T1撞题可还行 以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及-- 设\(que_{i,j}\)表示询问\((1,i,1,j ...
- [SNOI2017]一个简单的询问【莫队+容斥原理】
题目大意 给你一个数列,让你求两个区间内各个数出现次数的乘积的和. 分析 数据范围告诉我们可以用莫队过. 我并不知道什么曼哈顿什么乱七八糟的东西,但是我们可以用容斥原理将这个式子展开来. \[\sum ...
- BZOJ5016:[SNOI2017]一个简单的询问(莫队)
Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...
- 【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 ...
随机推荐
- 『转载』Matlab中fmincon函数获取乘子
Matlab中fmincon函数获取乘子 一.输出结构 [x,fval,exitflag,output,lambda] = fmincon(......) 二.结构说明 lambda结构 说 ...
- 读SRE Google运维解密有感(三)
前言 这是读“SRE Google运维解密”有感第三篇,之前的文章可访问www.addops.cn来查看.我们今天来聊聊“on call”也就是运维值班制度, 本人到目前为止也还在参与一线运维的值班, ...
- EntityFramework codefirst Enable-Migrations No context type was found in the assembly “MyApp.Web” error
Enable-Migrations -Force -ContextTypeName "MyApp.Repository.DataContext" -ProjectName &quo ...
- vue系列之Vue-cli
Vue-cli是Vue的脚手架工具 vue-cli 地址:https://github.com/vuejs/vue-cli 安装 npm install -g vue-cli 使用 vue init ...
- vue-cli 搭建的项目处理不同环境下请求不同域名的问题
使用 vue-cli 开发项目过程中, 根据开发环境和正式环境不同, 我们往往需要请求不同域名下的后台接口, 这时候, 该怎么去设置, 达到同一种写法可以根据环境不同而自动切换请求域名呢? 本文将会介 ...
- 前端工程化之webpack中配置babel-loader(四)
安装 安装:npm i -D babel-core babel-loader babel-plugin-transform-runtime 安装:npm i -D babel-preset-es201 ...
- 【splunk】数据输入-文件目录 导入失败
今天用splunk的“数据输入-文件目录”自动监控文件并索引,结果失败了,完全没有出现我要的索引. 解决: 删除文件监控 改为一次性索引 再重新添加连续监控 原因: 尚不明确 https://answ ...
- LINUX UBUNTU 快捷键
一.打开关闭终端 ctrl + alt + t //打开一个新终端 shift + ctrl +n //在打开终端的情况下再打开一个新终端shift + ctrl + q //关闭一个新终端 二.文件 ...
- 完全背包记录路径poj1787 好题
这题有点多重背包的感觉,但还是用完全背包解决,dp[j]表示凑到j元钱时的最大硬币数,pre[j]是前驱,used[j]是凑到j时第i种硬币的用量 △回溯答案时i-pre[i]就是硬币价值 #incl ...
- pytest五:fixture_autouse=True
平常写自动化用例会写一些前置的 fixture 操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传返个参数,会比较麻烦.fixture 里面有个参数 autouse,默讣是 ...