QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#19205 | #1877. Matryoshka Dolls | scw | WA | 387ms | 28188kb | C++ | 2.1kb | 2022-01-28 14:29:40 | 2022-05-06 04:26:55 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define MAXN 500007
using namespace std;
int S=707;
int n,m,a[MAXN],p[MAXN],cnt;
ll rs[710][710],ans[MAXN];
int ls[MAXN];
struct data{
int l,r;
}op[MAXN];
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
int minn[710][710],maxx[710][710];
int pr[MAXN],nx[MAXN],v[710];
int pre[710][710],suc[710][710];
inline int calc(int x,int y){
if(!x||!y||x>n||y>n)return 0;
if (p[x]>p[y])
return p[x]-p[y];
else
return p[y]-p[x];
}
/*
5 2
5 4 2 3 1
3 4
2 5
1
5
*/
signed main(){
n=read(),m=read(),cnt=n/S+1;
for(int i=1;i<=n;i++)
a[i]=read();
for(int i=1;i<=n;i++)
p[a[i]]=i;
for(int i=1;i<=m;i++)
op[i].l=read(),op[i].r=read(),ls[i]=-1;
for(int kk=1;kk<=cnt;kk++){
int size=0;
for(int i=1;i<=n;i++){
if((p[i]/S+1)==kk){
++size;
nx[i]=pr[i]=size;
v[size]=a[i];
}
else
pr[i]=nx[i]=0;
}
int w=0;
for(int i=1;i<=n;i++){
if (pr[i])
w=pr[i];
else
pr[i]=w;
}
w=size+1;
for(int i=n;i;i--){
if (nx[i])
w=nx[i];
else
nx[i]=w;
}
for(int i=1;i<=size;i++){
minn[i][i]=maxx[i][i]=v[i];
pre[i][i]=0,suc[i][i]=n+1;
for(int j=i+1;j<=size;j++){
suc[i][j]=suc[i][j-1],pre[i][j]=pre[i][j-1];
minn[i][j]=min(minn[i][j-1],v[j]),maxx[i][j]=max(maxx[i][j-1],v[j]);
if(v[j]<v[i])pre[i][j]=max(pre[i][j],v[j]);
if(v[j]>v[i])suc[i][j]=min(suc[i][j],v[j]);
}
}
for(int i=1;i<=size;i++){
rs[i][i]=0;
for(int j=i-1;j;j--){
int sl=pre[j][i],sr=suc[j][i];
rs[i][j]=rs[i][j+1]+calc(sl,v[j])+calc(v[j],sr)-calc(sl,sr);
}
}
for(int i=1;i<=m;i++){
int L=nx[op[i].l],R=pr[op[i].r];
if(L<=R){
ans[i]+=rs[R][L];
if(~ls[i])
ans[i]+=abs(p[minn[L][R]]-ls[i]);
ls[i]=p[maxx[L][R]];
}
}
}
for(int i=1;i<=m;i++)
printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 26244kb
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: 24060kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 387ms
memory: 28188kb
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:
673113
result:
wrong answer 1st numbers differ - expected: '4999950000', found: '673113'