QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#18996 | #1877. Matryoshka Dolls | chenyikai | WA | 33ms | 22764kb | C++ | 1.7kb | 2022-01-27 18:16:49 | 2022-05-06 03:31:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef set<int>::iterator si;
const int N=500013;
int n,m,a[N],q1[N],q2[N],b[N],c[N],p[N],ord[N],ans[N],sq;
bool cmp(const int&A,const int&B){return p[A]<p[B]||p[A]==p[B]&&q2[A]<q2[B];}
set<int> s;
signed main(){
//freopen("rrads.in","r",stdin);
//freopen("rrads.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",a+i),b[a[i]]=i;
for(int i=1;i<=m;i++)scanf("%d%d",q1+i,q2+i);
sq=floor(sqrt(n));
for(int i=1;i<=m;i++)p[i]=q1[i]/sq,ord[i]=i;
sort(ord+1,ord+m+1,cmp);
int l=1,r=1,res=0;
s.insert(a[1]);
for(int i=1;i<=m;i++){
while(r<q2[ord[i]]){
r++;
s.insert(a[r]);
si it=s.find(a[r]);
si it1=it,it2=it;
it1++,it2--;
if(it!=s.begin())res+=abs(b[*it2]-r);
if(it1!=s.end())res+=abs(b[*it1]-r);
if(it!=s.begin()&&it1!=s.end())res-=abs(b[*it1]-b[*it2]);
}
while(l>q1[ord[i]]){
l--;
s.insert(a[l]);
si it=s.find(a[l]);
si it1=it,it2=it;
it1++,it2--;
if(it!=s.begin())res+=abs(b[*it2]-l);
if(it1!=s.end())res+=abs(b[*it1]-l);
if(it!=s.begin()&&it1!=s.end())res-=abs(b[*it1]-b[*it2]);
}
while(r>q2[ord[i]]){
si it=s.find(a[r]);
si it1=it,it2=it;
it1++,it2--;
if(it!=s.begin())res-=abs(b[*it2]-r);
if(it1!=s.end())res-=abs(b[*it1]-r);
if(it!=s.begin()&&it1!=s.end())res+=abs(b[*it1]-b[*it2]);
r--;
s.erase(it);
}
while(l<q1[ord[i]]){
si it=s.find(a[l]);
si it1=it,it2=it;
it1++,it2--;
if(it!=s.begin())res-=abs(b[*it2]-l);
if(it1!=s.end())res-=abs(b[*it1]-l);
if(it!=s.begin()&&it1!=s.end())res+=abs(b[*it1]-b[*it2]);
l++;
s.erase(it);
}
ans[ord[i]]=res;
}
for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 16068kb
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: 16024kb
input:
1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 33ms
memory: 22764kb
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'