QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#19040#1877. Matryoshka DollsDuanQingQiuRE 0ms0kbC++113.5kb2022-01-27 20:03:202022-05-06 03:39:53

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-06 03:39:53]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2022-01-27 20:03:20]
  • 提交

answer

#include<bits/stdc++.h>
#include<iostream>
#define ll long long
#define back return 
#define ri register int 
using namespace std;
ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	back x*f;
}
vector<ll> v;
ll n,m,k,id1[500005],a[500005],L[500005],R[500005],ans1[500005]; 
struct node
{
	ll l,r,id;
}q[500005];
bool cmp1(node a,node b)
{
	back a.l<b.l;
}
bool cmp2(node a,node b)
{
	back a.r<b.r;
}
int main()
{
	freopen("rrads2.in","r",stdin);
	freopen("rrads.out","w",stdout);
	n=read(),m=read();
	for(ri i=1;i<=n;i++)
		a[i]=read(),id1[a[i]]=i;
	for(ri i=1;i<=m;i++)
		q[i].l=read(),q[i].r=read(),q[i].id=i;
	sort(q+1,q+m+1,cmp1);
	int t=sqrt(m),len=m/t;
	for(ri i=1;i<=t;i++)
		L[i]=(i-1)*len+1,R[i]=i*len;
	if(R[t]<m)
		t++,L[t]=R[t-1]+1,R[t]=m;
	for(ri i=1;i<=t;i++)
		sort(q+L[i],q+R[i]+1,cmp2);
	ll lastl=0,lastr=0,lastans=0;
	for(ri i=1;i<=t;i++)
	{
		lastl=lastr=lastans=0;
		v.clear();
		for(ri j=L[i];j<=R[i];j++)
		{
			ans1[q[j].id]=lastans;
			if(j==L[i])
			{
				for(ri k=q[j].l;k<=q[j].r;k++)
				{	
					if(v.empty())
					{
						v.push_back(a[k]);
						continue;
					}
					if(v[v.size()-1]<a[k])
					{
						v.push_back(a[k]);
						ans1[q[j].id]+=abs(id1[a[k]]-id1[v[v.size()-2]]);
						continue;
					}
					int lc=lower_bound(v.begin(),v.end(),a[k])-v.begin();
					if(lc>0)
						ans1[q[j].id]-=abs(id1[v[lc]]-id1[v[lc-1]]);
					v.insert(v.begin()+lc,a[k]);
					if(lc>0)
						ans1[q[j].id]+=abs(id1[v[lc]]-id1[v[lc-1]]);
					if(lc<v.size())
						ans1[q[j].id]+=abs(id1[v[lc]]-id1[v[lc+1]]);
				}
				lastl=q[j].l,lastr=q[j].r,lastans=ans1[q[j].id];
				continue;
			}
			
			if(lastl>=q[j].l)
				for(ri k=q[j].l;k<lastl;k++)
				{	
					if(v.empty())
					{
						v.push_back(a[k]);
						continue;
					}
					if(v[v.size()-1]<a[k])
					{
						v.push_back(a[k]);
						ans1[q[j].id]+=abs(id1[a[k]]-id1[v[v.size()-2]]);
						continue;
					}
					int lc=lower_bound(v.begin(),v.end(),a[k])-v.begin();
					if(lc>0)
						ans1[q[j].id]-=abs(id1[v[lc]]-id1[v[lc-1]]);
					v.insert(v.begin()+lc,a[k]);
					if(lc>0)
						ans1[q[j].id]+=abs(id1[v[lc]]-id1[v[lc-1]]);
					if(lc<v.size()-1)
						ans1[q[j].id]+=abs(id1[v[lc]]-id1[v[lc+1]]);
				}
			else
				for(ri k=lastl;k<q[j].l;k++)
				{
					if(v.empty())
						break;				
					int lc=lower_bound(v.begin(),v.end(),a[k])-v.begin();
					if(lc>0)
						ans1[q[j].id]-=abs(id1[v[lc]]-id1[v[lc-1]]);
					if(lc<v.size()-1)
						ans1[q[j].id]-=abs(id1[v[lc]]-id1[v[lc+1]]);
					v.erase(v.begin()+lc);
					if(lc>0&&lc<v.size())
						ans1[q[j].id]+=abs(id1[v[lc]]-id1[v[lc-1]]);
				}	
			for(ri k=max(q[j].l,lastr+1);k<=q[j].r;k++)
			{
				if(v.empty())
				{
					v.push_back(a[k]);
					continue;
				}
				if(v[v.size()-1]<a[k])
				{
					v.push_back(a[k]);
					ans1[q[j].id]+=abs(id1[a[k]]-id1[v[v.size()-2]]);
					continue;
				}
				int lc=lower_bound(v.begin(),v.end(),a[k])-v.begin();
				if(lc>0)
					ans1[q[j].id]-=abs(id1[v[lc]]-id1[v[lc-1]]);
				v.insert(v.begin()+lc,a[k]);
				if(lc>0)
					ans1[q[j].id]+=abs(id1[v[lc]]-id1[v[lc-1]]);
				if(lc<v.size()-1)
					ans1[q[j].id]+=abs(id1[v[lc]]-id1[v[lc+1]]);
			}
			lastl=q[j].l,lastr=q[j].r,lastans=ans1[q[j].id];
			if(q[j].l==q[j].r)
				ans1[q[j].id]=0,lastans=0;
		}
			
	}
	for(ri i=1;i<=m;i++)
		cout<<ans1[i]<<"\n";
    back 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

output:


result: