「SNOI2017」一个简单的询问
「SNOI2017」一个简单的询问
简单的解法
显然可以差分一下。
\]
因为都是1为起始,那么记两个cnt数组,表示前缀,然后可以分成四个区间,莫队。
#include <bits/stdc++.h>
#define rep(q, a, b) for (int q = a, q##_end_ = b; q <= q##_end_; ++q)
#define dep(q, a, b) for (int q = a, q##_end_ = b; q >= q##_end_; --q)
#define mem(a, b) memset(a, b, sizeof a)
#define debug(a) cerr << #a << ' ' << a << "___" << endl
using namespace std;
bool cur1;
char buf[10000000], *p1 = buf, *p2 = buf;
#define Getchar() p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 10000000, stdin), p1 == p2) ? EOF : *p1++
void in(int &r) {
static char c;
r = 0;
while (c = Getchar(), c < 48)
;
do
r = (r << 1) + (r << 3) + (c ^ 48);
while (c = Getchar(), c > 47);
}
const int mn = 50005;
int K, n, val[mn];
long long ans[mn];
struct node {
int l, r, id, ty;
bool operator<(const node &A) const {
return l / K == A.l / K ? (l / K & 1 ? r > A.r : r < A.r) : l / K < A.l / K;
}
} an[mn * 4];
int cnt[mn], cnt1[mn];
long long mid_ans;
bool cur2;
int main() {
// cerr<<(&cur2-&cur1)/1024.0/1024<<endl;
in(n);
K = sqrt(n) + 1;
rep(q, 1, n) in(val[q]);
int Q;
in(Q);
int l, r, l1, r1;
int ct = 0;
rep(q, 1, Q) {
in(l), in(r), in(l1), in(r1);
an[++ct] = { r, r1, q, 1 };
if (l > 1) {
an[++ct] = { l - 1, r1, q, -1 };
if (l1 > 1)
an[++ct] = { l1 - 1, l - 1, q, 1 };
}
if (l1 > 1)
an[++ct] = { l1 - 1, r, q, -1 };
}
sort(an + 1, an + ct + 1);
l = 0, r = 0;
rep(q, 1, ct) {
while (l > an[q].l) mid_ans -= cnt1[val[l]], --cnt[val[l--]];
while (r < an[q].r) mid_ans += cnt[val[++r]], ++cnt1[val[r]];
while (l < an[q].l) mid_ans += cnt1[val[++l]], ++cnt[val[l]];
while (r > an[q].r) mid_ans -= cnt[val[r]], --cnt1[val[r--]];
ans[an[q].id] += mid_ans * an[q].ty;
}
rep(q, 1, Q) printf("%lld\n", ans[q]);
return 0;
}
麻烦的解法
对于两个区间\([l,r],[l1,r1](l \leq l1)\),分三类容斥:
- 没有交
- \([l1,r1]\)被\([l,r]\)包含
- 两个区间相交且没有包含关系
对于1
\([l,r]\)值为K数有x,\([r+1,l1-1]\)值为K数有y,\([l1,r1]\)值为K数有z。
\]
因此可以拆成4个区间计算。
对于2
\([l,l1-1]\)值为K数有x,\([l1,r1]\)值为K数有y,\([r1+1,r]\)值为K数有z。
\]
因此可以拆成4个区间计算。
对于3
\([l,l1-1]\)值为K数有x,\([l1,r]\)值为K数有y,\([r+1,r1]\)值为K数有z。
\]
因此可以拆成4个区间计算。
综上
一遍莫队即可,计算区间中相同数个数的平方的和。
#include<bits/stdc++.h>
#define rep(q,a,b) for(int q=a,q##_end_=b;q<=q##_end_;++q)
#define dep(q,a,b) for(int q=a,q##_end_=b;q>=q##_end_;--q)
#define mem(a,b) memset(a,b,sizeof a )
#define debug(a) cerr<<#a<<' '<<a<<"___"<<endl
using namespace std;
bool cur1;
char buf[10000000],*p1=buf,*p2=buf;
#define Getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,10000000,stdin),p1==p2)?EOF:*p1++
void in(int &r) {
static char c;
r=0;
while(c=Getchar(),c<48);
do r=(r<<1)+(r<<3)+(c^48);
while(c=Getchar(),c>47);
}
const int mn=50005;
int K,n,val[mn];
long long ans[mn];
struct node{
int l,r,id,ty;
bool operator <(const node &A)const{
return l/K==A.l/K?(l/K&1?r>A.r:r<A.r):l/K<A.l/K;
}
}an[mn*4];
int cnt[mn];
long long mid_ans;
bool cur2;
int main(){
// cerr<<(&cur2-&cur1)/1024.0/1024<<endl;
in(n);
K=sqrt(n)+1;
rep(q,1,n)in(val[q]);
int Q;
in(Q);
int l,r,l1,r1;
int ct=0;
rep(q,1,Q){
in(l),in(r),in(l1),in(r1);
if(l>l1)swap(l,l1),swap(r,r1);
if(r<l1){
an[++ct]={l,r1,q,1};
an[++ct]={l,l1-1,q,-1};
an[++ct]={r+1,r1,q,-1};
if(l1-1>=r+1)an[++ct]={r+1,l1-1,q,1};
}else if(r1<=r){
an[++ct]={l,r1,q,1};
an[++ct]={l1,r,q,1};
if(l<=l1-1)an[++ct]={l,l1-1,q,-1};
if(r1+1<=r)an[++ct]={r1+1,r,q,-1};
}else{
an[++ct]={l,r1,q,1};
an[++ct]={l1,r,q,1};
if(l<=l1-1)an[++ct]={l,l1-1,q,-1};
if(r+1<=r1)an[++ct]={r+1,r1,q,-1};
}
}
sort(an+1,an+ct+1);
mid_ans=1,cnt[val[1]]=1;
l=1,r=1;
rep(q,1,ct){
while(l>an[q].l)--l,mid_ans+=(cnt[val[l]]++)<<1|1;
while(r<an[q].r)++r,mid_ans+=(cnt[val[r]]++)<<1|1;
while(l<an[q].l)mid_ans-=(--cnt[val[l]])<<1|1,++l;
while(r>an[q].r)mid_ans-=(--cnt[val[r]])<<1|1,--r;
ans[an[q].id]+=mid_ans*an[q].ty;
}
rep(q,1,Q)printf("%lld\n",ans[q]>>1);
return 0;
}
「SNOI2017」一个简单的询问的更多相关文章
- loj #2254. 「SNOI2017」一个简单的询问
#2254. 「SNOI2017」一个简单的询问 题目描述 给你一个长度为 NNN 的序列 aia_iai,1≤i≤N1\leq i\leq N1≤i≤N,和 qqq 组询问,每组询问读入 l1 ...
- loj2254 「SNOI2017」一个简单的询问
ref #include <algorithm> #include <iostream> #include <cstdio> #include <cmath& ...
- [SNOI2017]一个简单的询问
[SNOI2017]一个简单的询问 题目大意: 给定一个长度为\(n(n\le50000)\)的序列\(A(1\le A_i\le n)\),定义\(\operatorname{get}(l,r,x) ...
- 【BZOJ5016】[Snoi2017]一个简单的询问 莫队
[BZOJ5016][Snoi2017]一个简单的询问 Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计 ...
- loj #2255. 「SNOI2017」炸弹
#2255. 「SNOI2017」炸弹 题目描述 在一条直线上有 NNN 个炸弹,每个炸弹的坐标是 XiX_iXi,爆炸半径是 RiR_iRi,当一个炸弹爆炸时,如果另一个炸弹所在位置 X ...
- loj#2255. 「SNOI2017」炸弹 线段树优化建图,拓扑,缩点
loj#2255. 「SNOI2017」炸弹 线段树优化建图,拓扑,缩点 链接 loj 思路 用交错关系建出图来,发现可以直接缩点,拓扑统计. 完了吗,不,瓶颈在于边数太多了,线段树优化建图. 细节 ...
- bzoj P5016[Snoi2017]一个简单的询问——solution
Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input ...
- LOJ——#2256. 「SNOI2017」英雄联盟
https://loj.ac/problem/2256 题目描述 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不了网友们的嘲讽,决定变强了,他变强 ...
- bzoj 5016: [Snoi2017]一个简单的询问
Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. Input 第 ...
随机推荐
- Oracle之增、删、改、查
结构化查询语言 (Structured Query Language, SQL) SQL的组成: 数据操作语言(DML) 对数据进行查询.插入.删除和修改等操作,例如SELECT.INSERT.UPD ...
- PostgreSQL数据库安装Version10.5
PostgreSQL数据库安装,基于版本10.5安装, 在Linux系统上使用*.gz二进制压缩包手动安装. 操作系统:Red Hat Enterprise Linux Server release ...
- Swoole 协程简介
什么是协程 协程可以简单理解为线程,只不过这个线程是用户态的,不需要操作系统参与,创建.销毁和切换的成本都非常低. 协程不能利用多核 cpu,想利用多核 cpu 需要依赖 Swoole 的多进程模型. ...
- 解决ubuntu18.04重启后蓝牙鼠标需要重新配对的问题
打开bash,运行bluetoothctl命令 # bluetoothctl 列出可用的蓝牙控制器 [bluetooth]# list 选择使用的蓝牙控制器 [bluetooth]# select 0 ...
- Vue下路由History mode 出现404,无法正常刷新
在History mode下,如果直接通过地址栏访问路径,那么会出现404错误,这是因为这是单页应用(废话)-其实是因为调用了history.pushState API 所以所有的跳转之类的操作都是通 ...
- js获取相邻节点的value值
document.getElementById('id').nextElementSibling.value或者document.getElementById('id').previousElemen ...
- Source Insight 4安装图文教程(附链接)
Source Insight 4安装图文教程,附激活文件直链 下载链接:http://naturalporters.vicp.io/uploads/si4_kgen_unis.zip 直链没了,就用下 ...
- Keil MDK STM32系列(四) 基于抽象外设库HAL的STM32F401开发
Keil MDK STM32系列 Keil MDK STM32系列(一) 基于标准外设库SPL的STM32F103开发 Keil MDK STM32系列(二) 基于标准外设库SPL的STM32F401 ...
- 《剑指offer》面试题03. 数组中重复的数字
问题描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...
- NIO-java.nio.ByteBuffer中flip、rewind、clear方法的区别
Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO ...