QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#865190#894. Longest Loose Segmentsz051WA 789ms70296kbC++141.6kb2025-01-21 15:55:092025-01-21 15:55:15

Judging History

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

  • [2025-01-21 15:55:15]
  • 评测
  • 测评结果:WA
  • 用时:789ms
  • 内存:70296kb
  • [2025-01-21 15:55:09]
  • 提交

answer

#include <cstdio> 
#include <cstring>
#include <algorithm>
#include <vector>
#include <bitset>
#include <map>
#include <cassert>
#include <queue>
#include <random>
#include <ctime>
#define int long long
using namespace std;

void read(int &x){
	x=0;
	int f=1;
	char c=getchar();
	while(!('0'<=c && c<='9')){
		if(c=='-'){
			f=-1;
		}
		c=getchar();
	}
	while('0'<=c && c<='9'){
		x=(x<<3)+(x<<1)+(c^48);
		c=getchar();
	}
	x*=f;
}
int n,m,a[1000010],ls[1000010],rs[1000010],len[1000010],mx[1000010],res[1000010];
int stk[1000010],tp=0;
void dfs(int k){
	if(!k){
		len[k]=0;
		mx[k]=-1e18;
		res[k]=-1e18;
		return;
	}
	dfs(ls[k]);
	dfs(rs[k]);
	//printf("[%lld %lld %lld]\n",a[k],ls[k],rs[k]);
	assert((!ls[k] || ls[k]<k) && (!rs[k] || k<rs[k]));
	assert((!ls[k] || a[ls[k]]>=a[k]) && (!rs[k] || a[rs[k]]>=a[k]));
	len[k]=len[ls[k]]+len[rs[k]]+1;
	mx[k]=max(a[k],max(mx[ls[k]],mx[rs[k]]));
	res[k]=max(res[ls[k]],res[rs[k]]);
	if(mx[ls[k]]+a[k]>len[ls[k]]){
		res[k]=max(res[k],min(len[k],mx[ls[k]]+a[k]));
	}
	if(mx[rs[k]]+a[k]>len[rs[k]]){
		res[k]=max(res[k],min(len[k],mx[rs[k]]+a[k]));
	}
}
int work(){
	tp=0;
	for(int i=1;i<=n;i++){
		ls[i]=rs[i]=0;
		while(tp && a[stk[tp]]>a[i]){
			ls[i]=stk[tp--];
		}
		if(tp){
			rs[stk[tp]]=i;
		}
		stk[++tp]=i;
	}
	dfs(stk[1]);
	return res[stk[1]];
}
signed main(){
	read(n);
	read(m);
	for(int i=1;i<=n;i++){
		read(a[i]);
	}
	printf("%lld\n",work());
	while(m--){
		int siz,u,v;
		read(siz);
		while(siz--){
			read(u);
			read(v);
			swap(a[u],a[v]);
		}
		printf("%lld\n",work());
	}
	return 0;
}
/*
5 2
1 2 -2 3 4
1
2 3
1
1 2
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

2
3
4

result:

ok 3 tokens

Test #2:

score: -100
Wrong Answer
time: 789ms
memory: 70296kb

input:

998546 30
998544 998544 998543 998539 998538 998537 998536 998534 998533 998531 998529 998527 998522 998521 998520 998520 998516 998514 998512 998509 998501 998501 998500 998499 998498 998496 998494 998493 998491 998491 998490 998489 998489 998488 998488 998486 998483 998480 998479 998478 998475 998...

output:

212115
16719
12587
9069
8112
7079
7404
7404
5937
5941
5847
5759
6492
5465
5465
5565
5565
5408
5408
5669
5669
5408
6263
6351
6351
5326
4609
4613
4613
4430
4613

result:

wrong answer 1st words differ - expected: '212114', found: '212115'