首页 技术 正文
技术 2022年11月6日
0 收藏 553 点赞 482 浏览 1463 个字

  容易想到区间转化成前缀和。这样每个询问有了二维坐标,莫队即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 50010
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,m,a[N],cntx[N],cnty[N],t,block;
ll ans[N];
struct data
{
int k,x,y,i,op;
bool operator <(const data&a) const
{
return k<a.k||k==a.k&&(k&?y>a.y:y<a.y);
}
}q[N<<];
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj5016.in","r",stdin);
freopen("bzoj5016.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read();
for (int i=;i<=n;i++) a[i]=read();
m=read();block=sqrt(n);
for (int i=;i<=m;i++)
{
int l1=read(),r1=read(),l2=read(),r2=read();
t++,q[t].x=r1,q[t].y=r2,q[t].i=i,q[t].op=,q[t].k=q[t].x/block;
t++,q[t].x=r1,q[t].y=l2-,q[t].i=i,q[t].op=-,q[t].k=q[t].x/block;
t++,q[t].x=l1-,q[t].y=r2,q[t].i=i,q[t].op=-,q[t].k=q[t].x/block;
t++,q[t].x=l1-,q[t].y=l2-,q[t].i=i,q[t].op=,q[t].k=q[t].x/block;
}
sort(q+,q+t+);
int x=,y=;ll cur=;
for (int i=;i<=t;i++)
{
while (y<q[i].y) y++,cnty[a[y]]++,cur+=cntx[a[y]];
while (y>q[i].y) cur-=cntx[a[y]],cnty[a[y]]--,--y;
while (x<q[i].x) x++,cntx[a[x]]++,cur+=cnty[a[x]];
while (x>q[i].x) cur-=cnty[a[x]],cntx[a[x]]--,--x;
ans[q[i].i]+=q[i].op*cur;
}
for (int i=;i<=m;i++) printf(LL,ans[i]);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860