QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#19004 | #1877. Matryoshka Dolls | LSTM | WA | 2ms | 7692kb | C++14 | 1.7kb | 2022-01-27 18:19:07 | 2022-05-06 03:32:24 |
Judging History
answer
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL maxn=5e5+10;
struct ques{
LL l,r,id,ans;
}q[maxn];
LL n,a[maxn],p[maxn];
LL blo,m;
bool cmp(ques x,ques y)
{
if ((x.l-1)/blo+1==(y.l-1)/blo+1)
{
return (((x.l-1)/blo+1)%2==0?x.r>y.r:x.r<y.r);
}
else return (x.l-1)/blo<(y.l-1)/blo;
}
set<LL> s1,s2;
LL ans=0;
inline void add(LL x)
{
LL l=-(*s1.upper_bound(-x)),r=(*s2.upper_bound(x));
if (l>=1 && l<=n) ans+=abs(p[l]-p[x]);
if (r>=1 && r<=n) ans+=abs(p[r]-p[x]);
if (l>=1 && l<=n && r>=1 && r<=n) ans-=abs(p[l]-p[r]);
s1.insert(-x); s2.insert(x);
// cout<<"add:x="<<x<<" ans="<<ans<<" l="<<l<<" r="<<r<<endl;
}
inline void del(LL x)
{
LL l=-(*s1.upper_bound(-x)),r=(*s2.upper_bound(x));
if (l>=1 && l<=n) ans-=abs(p[l]-p[x]);
if (r>=1 && r<=n) ans-=abs(p[r]-p[x]);
if (l>=1 && l<=n && r>=1 && r<=n) ans+=abs(p[l]-p[r]);
s1.erase(-x); s2.erase(x);
// cout<<"del:x="<<x<<" ans="<<ans<<" l="<<l<<" r="<<r<<endl;
}
bool cmp2(ques x,ques y)
{
return x.id<y.id;
}
int main()
{
// freopen("rrads.in","r",stdin);
// freopen("rrads.out","w",stdout);
cin>>n>>m;
blo=sqrt(n)+1;
for (LL i=1;i<=n;i++)
{
cin>>a[i];
p[a[i]]=i;
}
for (LL i=1;i<=m;i++)
{
LL l,r; cin>>l>>r;
q[i]=(ques){l,r,i,0};
}
sort(q+1,q+m+1,cmp);
LL l=1,r=1; s1.insert(-a[1]),s2.insert(a[1]);
for (LL i=1;i<=m;i++)
{
while (r<q[i].r) r++,add(a[r]);
while (l>q[i].l) l--,add(a[l]);
while (l<q[i].l) del(a[l]),l++;
while (r>q[i].r) del(a[r]),r--;
q[i].ans=ans;
// if (i%1000==0) cout<<i<<endl;
}
sort(q+1,q+m+1,cmp2);
for (LL i=1;i<=m;i++)
{
cout<<q[i].ans<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 7692kb
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1
output:
8 6 4 2 0
result:
wrong answer 1st numbers differ - expected: '7', found: '8'