QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#19270 | #1877. Matryoshka Dolls | JohnAlfnov | WA | 4ms | 20008kb | C++20 | 3.6kb | 2022-01-28 18:57:35 | 2022-05-06 04:34:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
namespace file_read{
namespace input_file_io{
char ib[1<<26],*ip1=ib,*ip2=ib;
inline char gc(){
return (ip1==ip2&&(ip2=(ip1=ib)+fread(ib,1,1<<24,stdin)),
ip1==ip2?EOF:*ip1++);
}
inline int read(){
int x=0;
char c=gc();
while(c<'0'||c>'9'){
c=gc();
}
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+(c^'0');
c=gc();
}
return x;
}
inline auto read2(){
long long x=0;
char c=gc();
while(c<'0'||c>'9'){
c=gc();
}
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+(c^'0');
c=gc();
}
return x;
}
};
namespace output_file_io{
char ob[1<<26],*op=ob;
inline void pc(char c){
*op++=c;
}
void write(auto x){
if(x<0){
pc('-');
x=-x;
}
if(x==0)pc('0');
static int number_stack[40];
int total_count=0;
while(x)number_stack[++total_count]=x%10,x/=10;
while(total_count){
pc(number_stack[total_count]+'0');
--total_count;
}
}
inline void final_write(){
fwrite(ob,op-ob,1,stdout);
}
};
using namespace input_file_io;
using namespace output_file_io;
};
using namespace file_read;
inline int Abs(int x){
return x>0?x:-x;
}
int L[500005],R[500005];
int nxt[500005],pre[500005];
int a[500005],b[500005];
int bl[500005];
long long an[500005];
struct apple{
int wz,l,r;
bool operator<(const apple &other)const{
if(bl[l]!=bl[other.l])return l<other.l;
return r<other.r;
}
}e[500005];
struct syz{
int wz,zz,zx;
syz(int wz=0,int zz=0,int zx=0):
wz(wz),zz(zz),zx(zx){}
};
int n,m;
int aa[500005],bb[500005];
long long ans;
inline int calc(int x,int y){
if(x==0||y==0)return 0;
if(x==n+1||y==n+1)return 0;
return Abs(x-y);
}
stack<syz>vc;
void del(int x,int bh){
if(bh==1){
vc.emplace(syz(pre[x],1,x));
vc.emplace(syz(nxt[x],0,x));
}
ans-=calc(b[x],b[pre[x]]);
ans-=calc(b[x],b[nxt[x]]);
ans+=calc(b[pre[x]],b[nxt[x]]);
nxt[pre[x]]=pre[nxt[x]]=x;
}
int main(){
n=read(),m=read();
for(int i=1;i<=n;++i)a[i]=read(),b[a[i]]=i;
b[0]=0,b[n+1]=n+1;
for(int i=1;i<=n;++i)aa[i]=a[i];
int S=sqrt(n+0.5);
int SS=(n-1)/S+1;
for(int i=1;i<SS;++i){
L[i]=(i-1)*S+1,R[i]=i*S;
for(int j=L[i];j<=R[i];++j)bl[j]=i;
}
L[SS]=(SS-1)*S+1,R[SS]=n;
for(int j=L[SS];j<=R[SS];++j)bl[j]=SS;
for(int i=1;i<=m;++i){
e[i].wz=i;
e[i].l=read();
e[i].r=read();
}
sort(e+1,e+m+1);
int ll=1,rr=0;
for(int i=0;i<=n+1;++i){
pre[i]=0;
nxt[i]=n+1;
}
int wz=m;
aa[0]=0,aa[n+1]=n+1;
for(int i=SS;i>=1;--i){
ll=L[i],rr=n;
merge(aa+L[i],aa+R[i]+1,aa+R[i]+1,aa+n+1,bb+L[i]);
for(int j=L[i];j<=n;++j)aa[j]=bb[j],bb[j]=0;
int ss=aa[L[i]-1];
aa[L[i]-1]=0;
ans=0;
for(int j=L[i];j<=n;++j){
pre[aa[j]]=aa[j-1];
ans+=calc(b[aa[j-1]],b[aa[j]]);
nxt[aa[j]]=aa[j+1];
}
while(wz>=1&&bl[e[wz].l]==i){
while(rr>e[wz].r)del(a[rr--],0);
while(ll<e[wz].l)del(a[ll++],1);
an[e[wz].wz]=ans;
--wz;
while(vc.size()){
auto at=vc.top();
if(at.zz==1)nxt[at.wz]=at.zx;
else pre[at.wz]=at.zx;
vc.pop();
}
}
aa[L[i]-1]=ss;
}
for(int i=1;i<=m;++i)write(an[i]),pc('\n');
final_write();
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 20008kb
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1
output:
7 5 5 5 3
result:
wrong answer 3rd numbers differ - expected: '3', found: '5'