104932024-04-03 13:50:39111Branch Cuttingcpp17Time limit exceeded 8/1002.099s18452 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

int solve1(vector<int>&v){
	int N=v.size();
	int s=min_element(v.begin(),v.end())-v.begin();
	int ans=0;
	for(int i=0,j=0;i<N;i++){
		if(v[s]<v[(s+1)%N]){
			j++;
		}
		else if(j){
			ans+=(j+1)/2;
			j=0;
		}
		s=(s+1)%N;
	}
	return ans;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N,Q;
	cin>>N>>Q;
	vector<int>v(N),n(N),p(N);
	for(int i=0;i<N;i++){
		cin>>v[i];
		n[i]=i==N-1?0:i+1;
		p[i]=i==0?N-1:i-1;
	}
	int s=min_element(v.begin(),v.end())-v.begin();
	map<int,int>z;
	for(int i=0,j=0;i<N;i++){
		if(v[s]<v[n[s]]){
			j++;
		}
		else if(j){
			z.emplace((s-j+N)%N,s);
			j=0;
		}
		s=n[s];
	}
	int ans=0;
	for(auto[x,y]:z){
		ans+=((y-x+N)%N+1)/2;
	}
	cout<<ans<<' ';
	for(int i=0;i<Q;i++){
		int x;
		cin>>x>>v[x];
		if(!z.empty()){
			auto t=z.upper_bound(x);
			t=t==z.begin()?prev(z.end()):prev(t);
			int s=t->first,e=t->second;
			if(x>=s&&x<=e||x>=s&&s>=e||e>=x&&s>e){
				z.erase(t);
				ans-=((e-s+N)%N+1)/2;
				if(x==s&&x==e){
				}
				else if(x==s){
					s=n[s];
					ans+=((e-s+N)%N+1)/2;
					z.emplace(s,e);
				}
				else if(x==e){
					e=p[e];
					ans+=((e-s+N)%N+1)/2;
					z.emplace(s,e);
				}
				else{
					ans+=((p[x]-s+N)%N+1)/2;
					z.emplace(s,p[x]);
					ans+=((e-n[x]+N)%N+1)/2;
					z.emplace(n[x],e);
				}
			}
		}
		int s=v[p[x]]<v[x]?p[x]:x;
		int e=v[x]<v[n[x]]?n[x]:x;
		if(z.empty()){
			if(s!=e){
				ans+=((e-s+N)%N+1)/2;
				z.emplace(s,e);
			}
		}
		else{
			auto t=z.upper_bound(x);
			if(t==z.end()){
				t=z.begin();
			}
			if(t->first==e){
				auto[ss,ee]=*t;
				t=z.erase(t);
				ans-=((ee-ss+N)%N+1)/2;
				e=ee;
			}
			if(!z.empty()){
				t=t==z.begin()?prev(z.end()):prev(t);
				if(t->second==s){
					auto[ss,ee]=*t;
					t=z.erase(t);
					ans-=((ee-ss+N)%N+1)/2;
					s=ss;
				}
			}
			if(s!=e){
				ans+=((e-s+N)%N+1)/2;
				z.emplace(s,e);
			}
		}
		if(ans!=solve1(v)){
			exit(1);
		}
		cout<<ans<<' ';
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2100 KiB
2Accepted3ms2316 KiB
subtask28/8
3Accepted3ms2424 KiB
4Accepted3ms2496 KiB
5Accepted3ms2688 KiB
6Accepted3ms2908 KiB
7Accepted3ms3132 KiB
8Accepted3ms3244 KiB
9Accepted3ms3208 KiB
10Accepted3ms3212 KiB
11Accepted3ms3280 KiB
subtask30/15
12Time limit exceeded2.099s9560 KiB
13Time limit exceeded2.055s9316 KiB
14Runtime error59ms16216 KiB
15Runtime error1.679s18452 KiB
16Time limit exceeded2.045s10712 KiB
17Runtime error54ms13260 KiB
18Runtime error569ms13512 KiB
subtask40/10
19Accepted3ms4232 KiB
20Accepted3ms4320 KiB
21Accepted27ms4284 KiB
22Runtime error18ms4284 KiB
23Runtime error6ms4372 KiB
24Accepted27ms4296 KiB
25Accepted27ms4380 KiB
26Runtime error18ms4384 KiB
subtask50/25
27Accepted4ms4660 KiB
28Accepted3ms4588 KiB
29Runtime error41ms4740 KiB
30Runtime error264ms4872 KiB
31Runtime error230ms4792 KiB
32Accepted1.511s4560 KiB
33Accepted1.514s4640 KiB
34Runtime error569ms4776 KiB
subtask60/42
35Accepted35ms13464 KiB
36Accepted20ms13588 KiB
37Time limit exceeded2.078s11100 KiB
38Time limit exceeded2.075s9676 KiB
39Time limit exceeded2.069s11720 KiB
40Time limit exceeded2.062s8480 KiB
41Time limit exceeded2.073s8472 KiB
42Time limit exceeded2.078s8348 KiB
43Time limit exceeded2.069s8348 KiB