QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#18836 | #1877. Matryoshka Dolls | qwqwqwqwq | WA | 3ms | 4256kb | C++14 | 3.7kb | 2022-01-27 14:02:00 | 2022-05-06 02:49:13 |
Judging History
answer
#include<bits/stdc++.h>
#define R register
#define N 1000201
#define ll long long
template<int T>
struct fast_io{
char p[T],q[T],*p1,*p2,*q1,*q2;
fast_io(){p1=p2=p,q1=q,q2=q+T;}
inline char gc(){return p1==p2&&(p2=(p1=p)+fread(p,1,T,stdin),p1==p2)?EOF:*p1++;}
inline void pc(char c){if(q1==q2)fwrite(q,1,T,stdout),q1=q;*q1++=c;}
~fast_io(){fwrite(q,1,q1-q,stdout);}
};
fast_io<2000000> io;
inline ll Rd(){
ll r=0,neg=1; char ch; //std::cin>>r;return r;
do{ch=io.gc();if(ch=='-')neg=-1;}while(ch<48||ch>57);
do r=r*10+ch-48,ch=io.gc(); while(ch>=48&&ch<=57);
return r*neg;
}
inline int Rhx(char *s){ //base=16
char ch; int len=0;
do ch=io.gc(); while(!(isdigit(ch)||(ch>='A'&&ch<='F')));
do *s++=ch,ch=io.gc(),++len; while(isdigit(ch)||(ch>='A'&&ch<='F'));
*s=0; return len;
}
inline int Ral(char *s){ //a-z,'A'-'Z','0'-'9'
char ch; int len=0;
do ch=io.gc(); while(!isalpha(ch)&&!isdigit(ch));
do *s++=ch,ch=io.gc(),++len; while(isalpha(ch)||isdigit(ch));
*s=0; return len;
}
inline void putint(ll x){
if(x<0){io.pc('-');putint(-x);return;}
if(x/10) putint(x/10);io.pc(48+x%10);
}
inline void No(){
io.pc('N'),io.pc('O'),io.pc('\n');
}
inline void Yes(){
io.pc('Y'),io.pc('E'),io.pc('S'),io.pc('\n');
}
inline void Op(ll x,char ch=' '){putint(x),io.pc(ch);}
//inline ll Sxn(){return ((1LL*rand())<<15)|rand();}
int A[N],B[N],bl[N],a[N],p[N],n,m,vi[N];
ll ans[N],na,anq[N];
int pr[N],nx[N],stx[N],sty[N],stz[N],top,ba[N],blk=799;
struct xw{
int l,r,id;
xw(){} xw(int l_,int r_,int i_){l=l_,r=r_,id=i_;}
inline bool operator<(const xw&ww) const{return bl[l]<bl[ww.l]||(bl[l]==bl[ww.l]&&r>ww.r);}
}q[N];
inline int abss(int x){return x<0?-x:x;}
inline void shan(int x){
R int A=0,B=0; if(pr[x]) A=pr[x],na-=abss(p[pr[x]]-p[x]);
if(nx[x]) B=nx[x],na-=abss(p[nx[x]]-p[x]);
if(A) nx[A]=B; if(B) pr[B]=A; if(A&&B) na+=abss(p[B]-p[A]);
}
inline void shanmem(int x){
R int A=0,B=0; if(pr[x]) A=pr[x],na-=abss(p[pr[x]]-p[x]);
if(nx[x]) B=nx[x],na-=abss(p[nx[x]]-p[x]);
if(A) nx[A]=B; if(B) pr[B]=A; if(A&&B) na+=abss(p[B]-p[A]);
++top,stx[++top]=A,sty[top]=x,stz[top]=B;
}
inline void hyy(){
while(top){
R int A=stx[top],B=stz[top],x=sty[top];
if(A&&B) na-=abss(p[B]-p[A]);
if(A) nx[A]=x,pr[x]=A,na+=abss(p[x]-p[A]);
if(B) pr[B]=x,nx[x]=B,na+=abss(p[x]-p[B]);
--top;
}
}
/*
inline ll bruu(int l,int r){
for(R int i=l;i<=r;++i) ba[i]=i;
R ll ans=0;
std::sort(ba+l,ba+r+1,[&](const int &x,const int &y){return a[x]<a[y];});
for(R int i=l+1;i<=r;++i) ans+=abss(ba[i]-ba[i-1]);
return ans;
}
inline void sra(){
for(R int i=1;i<=n;++i) a[i]=i; //std::random_shuffle(a+1,a+n+1);
for(R int i=1;i<=n;i+=10) std::random_shuffle(a+i,a+i+10);
for(R int i=1;i<=m;++i){
R int la=Sxn()%n+1,ra=Sxn()%n+1;
if(la>ra) std::swap(la,ra); q[i].l=la,q[i].r=ra,q[i].id=i;
}
}
*/
int main(){
// srand(time(0));
n=Rd(),m=Rd(); if(n>1000) return 0;
if(n<=1005) blk=1;
for(R int i=1;i<=n;++i) a[i]=Rd();
for(R int i=1;i<=n;++i) p[a[i]]=i,bl[i]=(i-1)/blk+1;
for(R int i=1;i<=n+1;++i) if(bl[i]^bl[i-1]) A[bl[i]]=i,B[bl[i-1]]=i-1;
for(R int i=1;i<=m;++i) q[i].l=Rd(),q[i].r=Rd(),q[i].id=i;
// for(R int i=1;i<=m;++i) anq[i]=bruu(q[i].l,q[i].r);
std::sort(q+1,q+m+1); R int bo=1;
for(R int i=1;i<=bl[n];++i){
for(R int j=1;j<=n;++j) vi[j]=pr[j]=nx[j]=0; top=0,na=0;
for(R int j=A[i];j<=n;++j) vi[a[j]]=1;
for(R int j=1,pt=0;j<=n;++j) if(vi[j]){if(pt) nx[pt]=j,pr[j]=pt,na+=abss(p[j]-p[pt]); pt=j;}
R int ruc=n;
while(bo<=m&&bl[q[bo].l]==i){
while(ruc>q[bo].r) shan(a[ruc]),--ruc;
for(R int _=A[i];_<q[bo].l;++_) shanmem(a[_]);
ans[q[bo].id]=na; hyy(); ++bo;
}
} for(R int i=1;i<=m;++i) Op(ans[i],'\n');
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3592kb
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1
output:
7 5 3 1 0
result:
ok 5 number(s): "7 5 3 1 0"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3592kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 3ms
memory: 4256kb
input:
100000 1 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 152 154 156 158 160 162 164 166 168 170 172 ...
output:
result:
wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements