QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#18958#1877. Matryoshka DollschenyikaiWA 8ms18032kbC++2.3kb2022-01-27 17:34:022022-05-06 03:24:21

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:24:21]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:18032kb
  • [2022-01-27 17:34:02]
  • 提交

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);
	if(n>2){
		for(int i=2;i<=n;i++)c[i]=c[i-1]+abs(b[i]-b[i-1]);
		for(int i=1;i<=m;i++){
			int res=c[q2[i]+10]-c[q1[i]-10],k;
			for(int j=q1[i]-10;j<min(q1[i]+10,q2[i]-10);j=k){
				k=j+1;
				while(b[k]<q1[i]||b[k]>q2[i])k++;
				if(j==q1[i]-10&&(b[j]<q1[i]||b[j]>q2[i]))res-=c[k]-c[j];
				else res-=c[k]-c[j]-abs(b[k]-b[j]);
			}
			int K=k;
			for(int j=q2[i]+10;j>K&&j>=q2[i]-10;j=k){
				k=j-1;
				while(b[k]<q1[i]||b[k]>q2[i])k--;
				if(j==q2[i]+10&&(b[j]<q1[i]||b[j]>q2[i]))res-=c[j]-c[k];
				else res-=c[j]-c[k]-abs(b[k]-b[j]);
			}
			printf("%d\n",res);
		}
		return 0;
	}
	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;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 13960kb

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: 16004kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 8ms
memory: 18032kb

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'