[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. RNN(2) ------ “《A Critical Review of Recurrent Neural Networks for Sequence Learning》RNN综述性论文讲解”(转载)

    原文链接:http://blog.csdn.net/xizero00/article/details/51225065 一.论文所解决的问题 现有的关于RNN这一类网络的综述太少了,并且论文之间的符号 ...

  2. Python创建、删除桌面、启动组快捷方式的例子分享

    一.Python创桌面建快捷方式的2个例子 例子一: 代码如下: import osimport pythoncomfrom win32com.shell import shell    from w ...

  3. 查看tomcat运行状态

    实时查看tomcat并发连接数: netstat -na | grep ESTAB | grep 8080 | wc -l 实时查看apache并发连接数: netstat -na | grep ES ...

  4. cocos2d-x在App中的应用

    cocos2d-x是一个应用广泛的开源游戏引擎,主要是应用与开发2D游戏,开源运行于多个平台,如果只是针对于移动端平台而言,可以运行于android和ios平台. cocos2d-x目前的版本是3.1 ...

  5. transfer pdf to png

    #! /bin/bash # # transfer pdf to png if [ $# != 1 ] ; then echo "USAGE: $0 PDF FILE ABSOLUTELY ...

  6. 错误/异常:java.io.FileNotFoundException: .\src\db.properties (系统找不到指定的路径。);的解决方法

    1.异常视图 2.解决方法 与之相关的部分代码: static{ try { //读取db.properties Properties props = new Properties(); FileIn ...

  7. Java8实战系列一

    从java7到java8,最主要的变化可以总结为 □Lambda表达式 □ 方法引用 □流和默认方法 让我们通过一个小例子感受一下 情景 1 集合对象排序 (对list中的苹果按照重量排序) Coll ...

  8. ipython+notebook使用教程(转载)

    ipython是python交互环境的增强版 IPython notebook目前已经成为用Python做教学.计算.科研的一个重要工具.IPython Notebook使用浏览器作为界面,向后台的I ...

  9. php协程

    多任务 (并行和并发) 在讲协程之前,先谈谈多进程.多线程.并行和并发. 对于单核处理器,多进程实现多任务的原理是让操作系统给一个任务每次分配一定的 CPU 时间片,然后中断.让下一个任务执行一定的时 ...

  10. 开始写博客,学习Linq(2)

    linq的功能是什么? 它将极大地改变应用程序或组件处理数据的方式.这是第一个功能. LINQ to Objects.LINQ to SQL和LINQ to XML,是LINQ三大主要功能,当然LIN ...