[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)\)。

思路:

\[\operatorname{ans}(l_1,r_1,l_2,r_2)=\operatorname{ans}(1,r_1,1,r_2)-\operatorname{ans}(1,l_1-1,1,r_2)-\operatorname{ans}(1,r_1,1,l_2-1)+\operatorname{ans}(1,l_1-1,1,l_2-1)
\]

因此将每组询问拆成\(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]一个简单的询问的更多相关文章

  1. 【BZOJ5016】[Snoi2017]一个简单的询问 莫队

    [BZOJ5016][Snoi2017]一个简单的询问 Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计 ...

  2. bzoj P5016[Snoi2017]一个简单的询问——solution

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出   get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input ...

  3. bzoj 5016: [Snoi2017]一个简单的询问

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...

  4. [bzoj5016][Snoi2017]一个简单的询问

    来自FallDream的博客,未经允许,请勿转载,谢谢. 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出   get(l,r,x)表示计算区间[l,r]中 ...

  5. Gym101138D Strange Queries/BZOJ5016 SNOI2017 一个简单的询问 莫队、前缀和、容斥

    传送门--Gym 传送门--BZOJ THUWC2019D1T1撞题可还行 以前有些人做过还问过我,但是我没有珍惜,直到进入考场才追悔莫及-- 设\(que_{i,j}\)表示询问\((1,i,1,j ...

  6. [SNOI2017]一个简单的询问【莫队+容斥原理】

    题目大意 给你一个数列,让你求两个区间内各个数出现次数的乘积的和. 分析 数据范围告诉我们可以用莫队过. 我并不知道什么曼哈顿什么乱七八糟的东西,但是我们可以用容斥原理将这个式子展开来. \[\sum ...

  7. BZOJ5016:[SNOI2017]一个简单的询问(莫队)

    Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...

  8. 【bzoj5016】[Snoi2017]一个简单的询问 莫队算法

    题目描述 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. 输入 第一行,一个数字N,表 ...

  9. bzoj5016 & loj2254 [Snoi2017]一个简单的询问 莫队

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5016 https://loj.ac/problem/2254 题解 原式是这样的 \[ \su ...

随机推荐

  1. script & scriptreplay

    script是什么 scirpt就是一个命令,可以制作一份记录输出到终端的记录.对于那些想要真实记录终端会话的人来说,这很有用.该记录可以保存并在以后再打印出来. 怎么用 默认情况下,我们可以通过在终 ...

  2. shell脚本中冒号

    格式:: your comment here 格式:# your comment here 写代码注释(单行注释). 例如: 格式:: 'comment line1 comment line2 mor ...

  3. sqlyog通过跳板机ssh连接mysql数据库

    方法一: 方法二: 在跳板机上启动sh脚本做ssh端口转发,客户端配置连接 10.0.0.1的8306端口即可 jdbc:mysql://10.0.0.1:8306/testdb?useUnicode ...

  4. Python-ccs动画及阴影

    动画及阴影 0. 什么时候该用什么布局 <!-- 定位布局: 以下两种布局不易解决的问题, 盒子需要脱离文档流处理 --> <!-- 浮动布局: 一般有block特性的盒子,水平排列 ...

  5. mpVue小程序全栈开发

    1.微信小程序,mpVue和wepy的对比 2. 3.es6中关于数组的一些方法 <script> let arr = [,,,] // 遍历 arr.forEach(v => { ...

  6. LeetCode(53):最大子序和

    Easy! 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: ...

  7. bzoj营业额统计

    这个也是板子题吧,很水,求前驱后继即可 /* 插入,求前驱和后继 */ #include<iostream> #include<cstring> #include<cst ...

  8. (一)什么是webservice?

    第一节: 第一节:Webservice 简介 第二节: 第二节:CXF 简介 webservice 有的人一看到这个,估计会认为这个是一种新技术,一种新框架. 其实不是,严格的说,webservice ...

  9. 步步为营-22-xml登录

    说明:通过xml完成用户登录信息的判断 1 搭建UI页面 2 新建一个student类,类中包含以上属性 public class Student { public int ID { get; set ...

  10. PHPexcel的用法

    由于经常要统计学生的考试成绩,就研究了下PHPexcel这个插件 顺便说一下,读取方法只针对xls文件. 如果报错,可以先生存一个xls文件,把需要读取的xls内容复制进去. <?php //读 ...