QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#19128#1877. Matryoshka DollsPeanut_TangTL 1ms7836kbC++141.7kb2022-01-28 11:30:102022-05-06 04:18:05

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 04:18:05]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7836kb
  • [2022-01-28 11:30:10]
  • 提交

answer

#include <bits/stdc++.h>
#define il inline
#define ll long long
#define pb push_back
const int N=50005,B=120;

namespace IO
{
	const int S=1<<20|500; char I[S+5],*s,*t,O[S+5],*o; int p[40],P;

	il char Gc(){if (t==s){t=(s=I)+fread(I,1,1<<20,stdin); if (t==s) return EOF;} return *s++;}

	template<class T> il void Read(T &x)
	{
		char c; while ((c=Gc())<48||c>57); x=c^48;
		while ((c=Gc())>47&&c<58) x=(x<<3)+(x<<1)+(c^48);
	}

	il void Fs(){if (o-O) fwrite(O,1,o-O,stdout); o=O;}
	
	il void Pc(char c){if (*o++=c,o-O==S) Fs();}

	template<class T> il void Print(T x){T y; do y=x/10,p[P++]=x-10*y;while (x=y); while (P) Pc(p[--P]^48);}
	
	struct F{F(){o=O;} ~F(){Fs();}}FF;
}
using IO::Read; using IO::Print; using IO::Pc;

int n,m,a[N],b[N],st[N],tp,L[N],R[N]; ll E,ans[N]; struct node{int l,r,o;}; std::vector<node> g[N]; 

il int Dis(int x,int y){return !x||x>n||!y||y>n?0:std::abs(b[x]-b[y]);}

il void Del(int x){st[++tp]=x,E+=Dis(L[x],R[x])-Dis(x,L[x])-Dis(x,R[x]),L[R[x]]=L[x],R[L[x]]=R[x];}

il void Undo(){int x=st[tp--]; E+=Dis(x,L[x])+Dis(x,R[x])-Dis(L[x],R[x]),L[R[x]]=x,R[L[x]]=x;}

int main()
{
	Read(n),Read(m); int i,j,l,r;
	for (i=1; i<=n; i++) Read(a[i]),b[a[i]]=i;
	for (i=1; i<=m; i++) Read(l),Read(r),g[(l-1)/B+1].pb({l,r,i});

	for (j=1; j<=n; j+=B)
	{
		std::vector<node> &h=g[(j-1)/B+1]; std::sort(h.begin(),h.end(),[](const node &a,const node &b){return a.r==b.r?(a.l==b.l?a.o<b.o:a.l<b.l):a.r>b.r;});
		for (i=j,E=tp=0; i<=n; i++) L[i]=i-1,R[i]=i+1,E+=Dis(i,R[i]); L[j]=0;
		l=j,r=n; for (node v:h)
		{
			for ( ; r>v.r; r--) Del(a[r]);
			for ( ; l<v.l; l++) Del(a[l]);
			for (ans[v.o]=E; l>j; l--) Undo();
		}
	}
	for (i=1; i<=m; i++) Print(ans[i]),Pc('\n');

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7836kb

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: 0ms
memory: 6284kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Time Limit Exceeded

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:


result: