104912024-04-03 13:17:22111Branch Cuttingcpp17Wrong answer 8/100354ms31692 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

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()){
			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;
				}
			}
			ans+=((e-s+N)%N+1)/2;
			z.emplace(s,e);
		}
		cout<<ans<<' ';
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2104 KiB
2Accepted3ms2216 KiB
subtask28/8
3Accepted3ms2392 KiB
4Accepted3ms2504 KiB
5Accepted3ms2572 KiB
6Accepted3ms2796 KiB
7Accepted3ms2836 KiB
8Accepted3ms2872 KiB
9Accepted3ms2968 KiB
10Accepted3ms3100 KiB
11Accepted3ms3316 KiB
subtask30/15
12Wrong answer335ms28844 KiB
13Wrong answer314ms28452 KiB
14Wrong answer307ms28168 KiB
15Wrong answer331ms28864 KiB
16Wrong answer354ms31692 KiB
17Wrong answer280ms28520 KiB
18Wrong answer279ms28372 KiB
subtask40/10
19Accepted3ms3656 KiB
20Accepted3ms3612 KiB
21Accepted4ms3632 KiB
22Accepted4ms3632 KiB
23Wrong answer4ms3636 KiB
24Accepted3ms3668 KiB
25Accepted4ms3928 KiB
26Accepted4ms3976 KiB
subtask50/25
27Accepted4ms4376 KiB
28Accepted3ms4396 KiB
29Wrong answer13ms4880 KiB
30Wrong answer13ms4964 KiB
31Wrong answer13ms4548 KiB
32Accepted10ms4440 KiB
33Accepted10ms4480 KiB
34Accepted13ms4844 KiB
subtask60/42
35Accepted35ms13200 KiB
36Accepted20ms13196 KiB
37Wrong answer352ms20156 KiB
38Wrong answer321ms16976 KiB
39Wrong answer344ms20300 KiB
40Accepted225ms13660 KiB
41Accepted224ms13644 KiB
42Accepted224ms13636 KiB
43Accepted347ms24460 KiB