QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#19285 | #1877. Matryoshka Dolls | Forza_Ferrari | WA | 2ms | 16492kb | C++14 | 2.5kb | 2022-01-28 21:52:46 | 2022-05-06 04:36:18 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m,a[500001],p[500001],block,pos[500001],s[500001],pre[500001],nxt[500001],sum[500001],ans[500001],res;
struct element
{
int l,r,id;
bool operator <(const element &other) const
{
return pos[l]^pos[other.l]? pos[l]<pos[other.l]:r>other.r;
}
}q[500001];
inline int read()
{
int x=0;
char c=getchar();
while(c<'0'||c>'9')
c=getchar();
while(c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x;
}
inline void print(int x)
{
if(x>=10)
print(x/10);
putchar(x%10+'0');
}
inline int Abs(int x)
{
return x>=0? x:-x;
}
inline void insert(int x)
{
++sum[x];
}
inline void del(int x)
{
--sum[x];
if(pre[x])
{
res-=Abs(p[pre[x]]-p[x]);
nxt[pre[x]]=nxt[x];
}
if(nxt[x])
{
res-=Abs(p[nxt[x]]-p[x]);
pre[nxt[x]]=pre[x];
}
if(pre[x]&&nxt[x])
res+=Abs(p[pre[x]]-p[nxt[x]]);
pre[x]=nxt[x]=0;
}
int main()
{
n=read(),m=read();
block=sqrt(n);
for(int i=1;i<=n;++i)
{
p[a[i]=read()]=i;
pos[i]=(i-1)/block+1;
if(pos[i]^pos[i-1])
s[pos[i]]=i;
}
for(int i=1;i<=m;++i)
{
q[i].l=read(),q[i].r=read();
q[i].id=i;
}
sort(q+1,q+m+1);
for(int i=1,l=1,r=0,lst=0;i<=m;++i)
{
if(lst^pos[q[i].l])
{
lst=pos[q[i].l];
for(;l>s[pos[q[i].l]];--l)
insert(a[l-1]);
for(;r<n;++r)
insert(a[r+1]);
for(;l<s[pos[q[i].l]];++l)
del(a[l]);
int k=0;
res=0;
for(int j=1;j<=n;++j)
{
pre[j]=nxt[j]=0;
if(sum[j])
{
pre[j]=k;
if(k)
{
nxt[k]=j;
res+=Abs(p[j]-p[k]);
}
k=j;
}
}
}
for(;r>q[i].r;--r)
del(a[r]);
int tmp=res;
for(;l<q[i].l;++l)
del(a[l]);
ans[q[i].id]=res;
for(;l>s[pos[q[i].l]];--l)
insert(a[l-1]);
res=tmp;
}
for(int i=1;i<=m;++i)
{
print(ans[i]);
putchar('\n');
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15968kb
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: 15864kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 16492kb
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:
704982704
result:
wrong answer 1st numbers differ - expected: '4999950000', found: '704982704'