QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#18809 | #1877. Matryoshka Dolls | 1789 | Compile Error | / | / | C | 2.9kb | 2022-01-27 13:55:17 | 2022-05-18 04:05:17 |
Judging History
你现在查看的是最新测评结果
- [2023-08-10 23:21:45]
- System Update: QOJ starts to keep a history of the judgings of all the submissions.
- [2022-05-18 04:05:17]
- 评测
- 测评结果:Compile Error
- 用时:0ms
- 内存:0kb
- [2022-01-27 13:55:17]
- 提交
answer
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int s=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
s=s*10+c-'0';
c=getchar();
}
return s*f;
}
void write(long long x)
{
if(x<0)
putchar('-'),x=-x;
if(x/10)
write(x/10);
putchar(x%10+'0');
}
int n,m,s,ns;
int num[500007],block[500007],l[717],r[717];
long long anss[500007];
struct Quest{
int l,r,dat;
}q[500007];
bool cmp2(Quest x,Quest y)
{
if(block[x.l]!=block[y.l])
return block[x.l]<block[y.l];
if(block[x.l]&1)
return x.r<y.r;
return x.r>y.r;
}
int wz[500007],L[500007],R[500007];
set <int> S;
long long ans=0;
int main()
{
//freopen("rrads.in","r",stdin);
//freopen("rrads.out","w",stdout);
n=read(),m=read();
s=n/sqrt(m),ns=n/s;
S.insert(0);
S.insert(1e9);
for(int i=1;i<=n;++i)
num[i]=read(),block[i]=(i-1)/s+1,wz[num[i]]=i;
for(int i=1;i<=ns;++i)
l[i]=(i-1)*s+1,r[i]=min(i*s,n);
for(int i=1;i<=m;++i)
q[i].l=read(),q[i].r=read(),q[i].dat=i;
sort(q+1,q+m+1,cmp2);
int l=1,r=0;
for(int ii=1;ii<=m;++ii)
{
while(r<q[ii].r)
{
++r;
int ll,rr;
S.insert(num[r]);
ll=*(--S.find(num[r])),rr=*(++S.find(num[r]));
if(ll>0&&ll<=n)
{
ans+=abs(r-wz[ll]);
R[ll]=num[r],L[num[r]]=ll;
}
if(rr>0&&rr<=n)
{
ans+=abs(r-wz[rr]);
L[rr]=num[r],R[num[r]]=rr;
}
if(ll>0&&ll<=n&&rr>0&&rr<=n)
ans-=abs(wz[ll]-wz[rr]);
// cout<<l<<" "<<r<<" "<<ans<<endl;
}
while(l>q[ii].l)
{
--l;
int ll,rr;
S.insert(num[l]);
ll=*(--S.find(num[l])),rr=*(++S.find(num[l]));
if(ll>0&&ll<=n)
{
ans+=abs(l-wz[ll]);
R[ll]=num[l],L[num[l]]=ll;
}
if(rr>0&&rr<=n)
{
ans+=abs(l-wz[rr]);
L[rr]=num[l],R[num[l]]=rr;
}
if(ll>0&&ll<=n&&rr>0&&rr<=n)
ans-=abs(wz[ll]-wz[rr]);
// cout<<l<<" "<<r<<" "<<ans<<endl;
}
while(r>q[ii].r)
{
R[L[num[r]]]=R[num[r]];
L[R[num[r]]]=L[num[r]];
int ll=L[num[r]],rr=R[num[r]];
if(ll>0&&ll<=n)
ans-=abs(r-wz[ll]);
if(rr>0&&rr<=n)
ans-=abs(r-wz[rr]);
if(ll>0&&ll<=n&&rr>0&&rr<=n)
ans+=abs(wz[rr]-wz[ll]);
S.erase(num[r]);
L[num[r]]=R[num[r]]=0;
--r;
//cout<<l<<" "<<r<<" "<<ans<<endl;
}
while(l<q[ii].l)
{
R[L[num[l]]]=R[num[l]];
L[R[num[l]]]=L[num[l]];
int ll=L[num[l]],rr=R[num[l]];
if(ll>0&&ll<=n)
ans-=abs(l-wz[ll]);
if(rr>0&&rr<=n)
ans-=abs(l-wz[rr]);
// cout<<ans<<endl;
if(ll>0&&ll<=n&&rr>0&&rr<=n)
ans+=abs(wz[rr]-wz[ll]);
S.erase(num[l]);
L[num[l]]=R[num[l]]=0;
++l;
// cout<<ll<<" "<<rr<<" "<<l<<" "<<r<<" "<<ans<<endl;
}
anss[q[ii].dat]=ans;
}
for(int i=1;i<=m;++i)
write(anss[i]),putchar('\n');
fclose(stdin);
fclose(stdout);
return 0;
}
/*
5 15
5 2 4 3 1
1 1
1 2
1 3
1 4
1 5
2 2
2 3
2 4
2 5
3 3
3 4
3 5
4 4
4 5
5 5
*/
详细
answer.code:1:10: fatal error: bits/stdc++.h: No such file or directory #include <bits/stdc++.h> ^~~~~~~~~~~~~~~ compilation terminated.