QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#848371#9865. DollsshuopihuaWA 109ms9408kbC++141.8kb2025-01-08 20:03:202025-01-08 20:03:26

Judging History

你现在查看的是最新测评结果

  • [2025-01-08 20:03:26]
  • 评测
  • 测评结果:WA
  • 用时:109ms
  • 内存:9408kb
  • [2025-01-08 20:03:20]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

int T,n;
int a[100005];
int t[400005];
int lf[100005];
int fl[100005];
int st[100005];
int rt[100005];
struct node{int l,r,x;};
vector <node> g[100005];

inline void upd(int u,int l,int r,int k,int x){
	if(l==r){t[u]=x;return ;}
	int mid=(l+r)>>1;
	if(k<=mid) upd(u<<1,l,mid,k,x);
	else upd(u<<1|1,mid+1,r,k,x);
	t[u]=max(t[u<<1],t[u<<1|1]);
	return ;
}

inline int qry(int u,int l,int r,int L,int R){
	if(L<=l&&r<=R) return t[u];
	int mid=(l+r)>>1;
	if(L>mid) return qry(u<<1|1,mid+1,r,L,R);
	if(R<=mid) return qry(u<<1,l,mid,L,R);
	return max(qry(u<<1,l,mid,L,R),qry(u<<1|1,mid+1,r,L,R));
}

inline void in(int &n){
	n=0;
	char c=getchar();
	while(c<'0' || c>'9') c=getchar();
	while(c>='0'&&c<='9') n=n*10+c-'0',c=getchar();
	return ;
}

int main(){
	in(T);
	while(T--){
		in(n);
		for(int i=1;i<=n;i++) in(a[i]);
		for(int i=0;i<=4*n;i++) t[i]=0;
		int top=0;
		for(int i=1;i<=n;i++){
			while(top&&a[st[top]]<a[i]) top--;
			lf[i]=st[top];
			st[++top]=i;
		}
		top=0;
		for(int i=1;i<=n;i++){
			while(top&&a[st[top]]>a[i]) top--;
			fl[i]=st[top];
			st[++top]=i;
		}
		for(int i=3;i<=n;i++){
			if(!lf[i]||!fl[i]) continue;
			int lim=min(lf[i],fl[i])-1;
			if(a[i]-a[i-1]>1) g[lim].push_back({a[i-1]+1,a[i]-1,i-1});
			if(a[i-1]-a[i]>1) g[lim].push_back({a[i]+1,a[i-1]-1,i-1});
		}
		for(int i=1;i<=n+1;i++) rt[i]=n;
		for(int i=1;i<=n;i++){
			upd(1,1,n,a[i],i);
			for(auto tmp:g[i]){
				int x=qry(1,1,n,tmp.l,tmp.r);
				if(x) rt[x]=min(rt[x],tmp.x);
			}
		}
		for(int i=n-1;i>=1;i--) rt[i]=min(rt[i],rt[i+1]);
		int s=0,l=1;
		while(l<=n) s+=min(n,rt[l])-l,l=rt[l]+1;
		printf("%d\n",s);
	}

	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 9408kb

input:

8
4
2 1 4 3
4
1 4 2 3
4
3 1 4 2
5
1 3 5 2 4
5
1 4 2 5 3
5
2 5 3 1 4
6
1 3 6 5 2 4
6
2 5 1 3 6 4

output:

3
3
2
3
3
3
4
4

result:

ok 8 numbers

Test #2:

score: -100
Wrong Answer
time: 109ms
memory: 9016kb

input:

5913
1
1
2
1 2
2
2 1
3
1 2 3
3
1 3 2
3
2 1 3
3
2 3 1
3
3 1 2
3
3 2 1
4
1 2 3 4
4
1 2 4 3
4
1 3 2 4
4
1 3 4 2
4
1 4 2 3
4
1 4 3 2
4
2 1 3 4
4
2 1 4 3
4
2 3 1 4
4
2 3 4 1
4
2 4 1 3
4
2 4 3 1
4
3 1 2 4
4
3 1 4 2
4
3 2 1 4
4
3 2 4 1
4
3 4 1 2
4
3 4 2 1
4
4 1 2 3
4
4 1 3 2
4
4 2 1 3
4
4 2 3 1
4
4 3 1 2
4...

output:

0
1
1
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
2
2
3
2
2
2
2
2
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
3
3
4
3
3
3
3
3
4
4
4
4
4
4
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
...

result:

wrong answer 21st numbers differ - expected: '3', found: '2'