QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#18960 | #1877. Matryoshka Dolls | lixx | RE | 0ms | 0kb | C++17 | 3.1kb | 2022-01-27 17:34:20 | 2022-05-06 03:24:49 |
Judging History
answer
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int V=25,N=1e6*V+5,Q=1e6;
int tot=1,sz[N],ed[N],ch[N][2];
inline void insert(int v){
int p=1;
for(int i=V;i>=0;--i){
int f=(v&(1<<i))>>i;
if(!ch[p][f])ch[p][f]=++tot;
p=ch[p][f],++sz[p];
}
++ed[p];
}
inline void erase(int v){
int p=1;
for(int i=V;i>=0;--i){
int f=(v&(1<<i))>>i;
if(!ch[p][f])ch[p][f]=++tot;
p=ch[p][f],--sz[p];
}
--ed[p];
}
inline int rnk(int v){
int p=1,rk=1;
for(int i=V;i>=0;--i){
int f=(v&(1<<i))>>i;
if(f)rk+=sz[ch[p][0]];
p=ch[p][f];
}
return rk;
}
inline int kth(int k){
int p=1,ans=0;
for(int i=V;i>=0;--i){
if(k<=sz[ch[p][0]])p=ch[p][0],ans=ans<<1;
else k-=sz[ch[p][0]],p=ch[p][1],ans=ans<<1|1;
}
return ans;
}
inline int pre(int v){return kth(rnk(v)-1);}
inline int suf(int v){return kth(rnk(v+1));}
long long n,m,id[500005],a[500005],qianqu,houji,sum,l,r,ans[500005],fk;
struct node{
long long l,r,bh;
}xunwen[500005];
long long px(struct node a1,struct node a2){
if(a1.l==a2.l){
return a1.r<a2.r;
}
return a1.l<a2.l;
}
inline long long read(){
long long s=0,f=1;
char c=getchar();
while(!isdigit(c)){
if(c=='-'){
f=-f;
}
}
while(isdigit(c)){
s=(s<<3)+(s<<1)+(c^48);
c=getchar();
}
return s*f;
}
int main(){
freopen("rrads.in","r",stdin);
freopen("rrads.out","w",stdout);
n=read();
m=read();
for(int i=1;i<=n;i++){
a[i]=read();
id[a[i]]=i;
}
for(int i=1;i<=m;i++){
xunwen[i].l=read();
xunwen[i].r=read();
xunwen[i].bh=i;
}
sort(xunwen+1,xunwen+1+m,px);
l=1;
r=1;
sum=0;
for(int i=1;i<=m;i++){
while(r<=xunwen[i].r){
insert(a[r]+Q);
qianqu=pre(a[r]+Q)-Q;
houji=suf(a[r]+Q)-Q;
if(qianqu!=-1000000)sum+=abs(id[a[r]]-id[qianqu]);
if(houji!=66108863)sum+=abs(id[a[r]]-id[houji]);
if(qianqu!=-1000000&&houji!=66108863)sum-=abs(id[qianqu]-id[houji]);
r++;
}
while(r>xunwen[i].r+1){
r--;
qianqu=pre(a[r]+Q)-Q;
houji=suf(a[r]+Q)-Q;
if(qianqu!=-1000000)sum-=abs(id[a[r]]-id[qianqu]);
if(houji!=66108863)sum-=abs(id[a[r]]-id[houji]);
if(qianqu!=-1000000&&houji!=66108863)sum+=abs(id[qianqu]-id[houji]);
erase(a[r]+Q);
}
while(l<xunwen[i].l){
qianqu=pre(a[l]+Q)-Q;
houji=suf(a[l]+Q)-Q;
if(qianqu!=-1000000)sum-=abs(id[a[l]]-id[qianqu]);
if(houji!=66108863)sum-=abs(id[a[l]]-id[houji]);
if(qianqu!=-1000000&&houji!=66108863)sum+=abs(id[qianqu]-id[houji]);
erase(a[l]+Q);
l++;
}
while(l>xunwen[i].l){
l--;
insert(a[l]+Q);
qianqu=pre(a[l]+Q)-Q;
houji=suf(a[l]+Q)-Q;
if(qianqu!=-1000000)sum+=abs(id[a[l]]-id[qianqu]);
if(houji!=66108863)sum+=abs(id[a[l]]-id[houji]);
if(qianqu!=-1000000&&houji!=66108863)sum-=abs(id[qianqu]-id[houji]);
}
ans[xunwen[i].bh]=sum;
}
for(int i=1;i<=m;i++){
printf("%lld\n",ans[i]);
}
return 0;
}
詳細信息
Test #1:
score: 0
Dangerous Syscalls
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1