QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#18804#1877. Matryoshka DollsguoboWA 3ms9680kbC++1.9kb2022-01-27 13:54:122022-05-06 02:41:56

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:41:56]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:9680kb
  • [2022-01-27 13:54:12]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=555555,mod=998244353;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline int read(){
	char ch=getchar();int x=0,f=0;
	while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
	while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
	return f?-x:x;
}
inline int qpow(int a,int b){
	int ans=1;
	for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod;
	return ans;
}
int n,m,sq,a[maxn],b[maxn];
ll sum,ans[maxn];
set<int> s;
struct ques{
	int l,r,id,bl;
	bool operator<(const ques &q)const{
		if(bl!=q.bl) return bl<q.bl;
		if(bl&1) return r>q.r;
		return r<q.r;
	}
}q[maxn];
inline int f(int x,int y){return abs(b[x]-b[y]);}
void ins(int p){
	auto it=s.insert(a[p]).first;
	auto it1=it,it2=it;
	if(++it2!=s.end()){
		sum+=f(*it,*it2);
	}
	if(it1!=s.begin()){
		sum+=f(*it,*--it1);
		if(it2!=s.end()) sum-=f(*it1,*it2);
	}
}
void del(int p){
	auto it=s.find(a[p]);
	auto it1=it,it2=it;
	if(++it2!=s.end()){
		sum-=f(*it,*it2);
	}
	if(it1!=s.begin()){
		sum-=f(*it,*--it1);
		if(it2!=s.end()) sum+=f(*it1,*it2);
	}
	s.erase(it);
}
int main(){
//	freopen("rrads.in","r",stdin);
//	freopen("rrads.out","w",stdout);
	n=read();m=read();
	sq=n/sqrt(m/2);
	sq=max(sq,1);
	FOR(i,1,n) b[a[i]=read()]=i;
	FOR(i,1,m){
		int l=read(),r=read();
		q[i]=(ques){l,r,i,l/sq};
	}
	sort(q+1,q+m+1);
	int ql=1,qr=0;
	FOR(i,1,m){
		int l=q[i].l,r=q[i].r;
		while(ql>l) ins(--ql);
		while(qr<r) ins(++qr);
		while(ql<l) del(ql++);
		while(qr>r) del(qr--);
		ans[q[i].id]=sum; 
	}
//	FOR(i,1,m) printf("%lld\n",ans[i]);
}
// -std=c++11 -Wl,--stack=1024000000 -DGUOBO -O2 -Wall -Wextra -Wshadow -D_GLIBCXX_ASSERTIONS -ggdb3 -fmax-errors=2

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 9680kb

input:

5 5
1 5 2 4 3
1 5
1 4
1 3
1 2
1 1

output:


result:

wrong answer Answer contains longer sequence [length = 5], but output contains 0 elements