QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#18976 | #1877. Matryoshka Dolls | Lanuxhem | RE | 0ms | 0kb | C++20 | 2.7kb | 2022-01-27 17:57:14 | 2022-05-06 03:27:27 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define giao 1ll
using namespace std;
const int N=600001;
ll read()
{
ll x=0,w=0;
char ch=0;
while(!isdigit(ch))
{
w|=ch=='-';
ch=getchar();
}
while(isdigit(ch))
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return w?-x:x;
}
ll n,m,a[N],cnt,sec,len,nowl,nowr,befans,nowans,secans,b[N],ans[N],befa[N],befb[N],nowa[N],nowb[N];
vector<int> hm[1001];
struct lhm1
{
int l1;
int r1;
int k;
}f[N];
bool cmp1(lhm1 c,lhm1 d)
{
if(b[c.l1]==b[d.l1]) return c.r1>d.r1;
else return b[c.l1]<b[d.l1];
}
struct lhm2
{
int fst;
int lst;
}p[N];
bool cmp2(lhm2 c,lhm2 d)
{
return c.fst<d.fst;
}
void befbs(int wwx)
{
int l=befa[wwx],r=befb[wwx];
if(l!=0)
{
befans=befans-giao*abs(wwx-l);
befb[l]=r;
}
if(r!=0)
{
befans=befans-giao*abs(wwx-r);
befa[r]=l;
}
if(l!=0 and r!=0) befans=befans+giao*abs(l-r);
return;
}
struct lhm3
{
int sjh;
int wwx;
int cyc;
}q[N];
void nowbs(int wwx)
{
int l=nowa[wwx],r=nowb[wwx];
if(l!=0)
{
nowans=nowans-giao*abs(wwx-l);
len++;
q[len].sjh=1;
q[len].wwx=l;
q[len].cyc=nowb[l];
nowb[l]=r;
}
if(r!=0)
{
nowans=nowans-giao*abs(wwx-r);
len++;
q[len].sjh=0;
q[len].wwx=r;
q[len].cyc=nowa[r];
nowa[r]=l;
}
if(l!=0 and r!=0) nowans=nowans+giao*abs(l-r);
return;
}
int main()
{
freopen("rrads.in","r",stdin);
freopen("rrads.out","w",stdout);
memset(ans,0,sizeof(ans));
cin>>n>>m;
cnt=sqrt(n);
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) b[i]=(i-1)/cnt+1;
for(int i=1;i<=m;i++)
{
f[i].k=i;
cin>>f[i].l1>>f[i].r1;
}
sort(f+1,f+m+1,cmp1);
for(int i=1;i<=m;i++) hm[b[f[i].l1]].push_back(i);
for(int i=1;i<=n;i++)
{
sec++;
p[sec].fst=a[i];
p[sec].lst=i;
}
sort(p+1,p+1+sec,cmp2);
for(int i=1;i<=sec;i++)
{
if(i!=sec) befans+=abs(p[i].lst-p[i+1].lst);
befb[p[i].lst]=p[i+1].lst;
if(i!=sec) befa[p[i+1].lst]=p[i].lst;
}
for(int i=1;i<=b[n];i++)
{
memcpy(nowa,befa,sizeof(befa));
memcpy(nowb,befb,sizeof(befb));
nowans=befans;
nowl=(i-1)*cnt+1;
nowr=n;
for(int now=0;now<hm[i].size();now++)
{
while(nowr>f[hm[i][now]].r1)
{
nowbs(nowr);
nowr--;
}
secans=nowans;
len=0;
while(nowl<f[hm[i][now]].l1)
{
nowbs(nowl);
nowl++;
}
ans[f[hm[i][now]].k]=nowans;
while(len!=0)
{
if(q[len].sjh==0) nowa[q[len].wwx]=q[len].cyc;
else nowb[q[len].wwx]=q[len].cyc;
len--;
}
nowans=secans;
nowl=(i-1)*cnt+1;
}
int l1=(i-1)*cnt+1,r1=cnt*i;
for(int j=l1;j<=r1;j++) befbs(j);
}
for(int i=1;i<=m;i++) cout<<ans[i]<<endl;
return 0;
}
詳細信息
Test #1:
score: 0
Dangerous Syscalls
input:
5 5 1 5 2 4 3 1 5 1 4 1 3 1 2 1 1