QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#18838#1877. Matryoshka DollsnewbiewzsWA 35ms21092kbC++204.1kb2022-01-27 14:02:332022-05-06 02:50:19

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 02:50:19]
  • 评测
  • 测评结果:WA
  • 用时:35ms
  • 内存:21092kb
  • [2022-01-27 14:02:33]
  • 提交

answer

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
#include<map>
#include<bitset>
#include<set>
#include<cmath>
#include<ctime>
#include<random>
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define bc(x) __builtin_popcount(x)
#define re register
#define il inline
#define pii pair<int,int>
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define mem0(x) memset(x,0,sizeof(x))
#define mem0x3f(x) memset(x,0x3f,sizeof(x))
#define cpy(x,y) memcpy(x,y,sizeof(x))
//#pragma GCC optimize(3)
//#define int long long
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
namespace IO_BUFF{
	mt19937 rnd(time(0)^(ll)(new char));
	int rend(int x){
		return rnd()%x+1;
	}
	void rendom_shuffle(int *a,int len){
		shuffle(a+1,a+len+1,rnd);
	}
	const int BS=(1<<24)+5;char Buffer[BS],*HD,*TL;
	inline int gc(){
	    if(HD==TL) TL=(HD=Buffer)+fread(Buffer,1,BS,stdin);
	    return (HD==TL)?EOF:*HD++;
	}
	inline int read(){
	    int x,ch,s=1;while((ch=gc())<'0'||ch>'9')if(ch=='-')s=-1;x=ch^'0';
	    while((ch=gc())>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^'0');return x*s;
	}
	char ssss[19999999],tttt[20];int ssl,ttl;
    inline int print(int x)
    {
        if(x<0)ssss[++ssl]='-',x=(-x);
		if(!x) ssss[++ssl]='0';for(ttl=0;x;x/=10) tttt[++ttl]=char(x%10+'0');
        for(;ttl;ttl--) ssss[++ssl]=tttt[ttl];return ssss[++ssl]='\n';
    }
	inline int Flush(){return fwrite(ssss+1,sizeof(char),ssl,stdout),ssl=0,0;}
}using namespace IO_BUFF;
/*namespace CFConTest{
	const int mod=998244353;
	inline int add(const int &x,const int &y){
		return (x+y>=mod?x+y-mod:x+y);
	}
	inline int del(const int &x,const int &y){
		return (x-y<0?x-y+mod:x-y);
	}
	int ksm(int x,int k){
		int base=1;
		while(k){
			if(k&1)base=1ll*base*x%mod;
			k>>=1;
			x=1ll*x*x%mod;
		}
		return base;
	}
};
using namespace CFConTest;*/
inline int Abs(const int &x){
	if(x<0)return -x;
	return x;
}
const int N=5e5+5;
const int B=705;
int n,m,tot,a[N],p[N],id[N],l[N],r[N],L[N],R[N];
ll da[N];
struct node{
	int l,r,id;
}q[N];
int cmp(node x,node y){
	if(id[x.l]!=id[y.l])return x.l<y.l;
	else return x.r>y.r;
}
vi v[N];
int pre[N],nxt[N];
int sx[N],sy[N],sz[N],top;
signed main(){
	#ifdef newbiewzs
		freopen("data.in","r",stdin);
	//	freopen("std.out","w",stdout);
	#else
	#endif
	n=read();m=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
		p[a[i]]=i;
		id[i]=(i-1)/B+1;
	}
	for(int i=1;i<=n;i++){
		if(!L[id[i]])L[id[i]]=i;
		R[id[i]]=i;
	}
	for(int i=1;i<=m;i++){
		l[i]=read();r[i]=read();
		q[++tot]={l[i],r[i],i};
	}
	sort(q+1,q+tot+1,cmp);
	int head=1;
	for(int i=1;i<=id[n];i++){
		int las=0;
		ll ans=0;
		memset(pre,0,sizeof(pre));
		memset(nxt,0,sizeof(nxt));
		top=0;
		while(head<=m && id[q[head].l]==i){
			v[i].pb(q[head].id);
			head++;
		}
		const int SJ=L[i];
		for(int k=1;k<=n;k++){
			if(p[k]>=SJ){
				pre[p[k]]=las;
				nxt[las]=p[k];
				if(las)ans+=Abs(p[k]-las);
				las=p[k];
			}
		}
		int tr=n;
		
		for(auto k:v[i]){
			int tl=L[i];top=0;
			while(tr>r[k]){
				int tx=pre[tr];
				int ty=nxt[tr];
				if(tx)ans-=Abs(tr-tx);
				if(ty)ans-=Abs(tr-ty);
				nxt[tx]=ty;
				pre[ty]=tx;
				pre[tr]=nxt[tr]=0;
				if(tx && ty)ans+=Abs(tx-ty);
				tr--;
			}
			while(tl<l[k]){
				int tx=pre[tl];
				int ty=nxt[tl];
				if(tx)ans-=Abs(tl-tx);
				if(ty)ans-=Abs(tl-ty);
				nxt[tx]=ty;pre[ty]=tx;
				pre[tl]=nxt[tl]=0;
				if(tx && ty)ans+=Abs(tx-ty);
				sx[++top]=tx;
				sy[top]=ty;
				sz[top]=tl;
				tl++;
			}
			da[k]=ans;
			while(top){
				int tx=sx[top];
				int ty=sy[top];
				int tz=sz[top];
				if(tx && ty)ans-=Abs(tx-ty);
				if(tx)ans+=Abs(tz-tx);
				if(ty)ans+=Abs(tz-ty);
				nxt[tx]=tz;
				pre[tz]=tx;
				nxt[tz]=ty;
				pre[ty]=tz;
				top--;
			}
		}
	}
	for(int i=1;i<=m;i++){
		print(da[i]);
	}
	Flush();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 5ms
memory: 19228kb

input:

1 1
1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: -100
Wrong Answer
time: 35ms
memory: 21092kb

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'