105842024-04-05 23:29:15999Branch Cuttingcpp17Accepted 100/100782ms11684 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;
#define endl ' '

int mod(int num, int m){
	if(num>=0)return num%m;
	else{
		return (num+m)%m;
	}
}

void bruteforce(int n, int q){
	vector<int> v(n);
	int mindex=0;
	for(int i = 0;i<n;i++){
			cin>>v[i];
	}
	for(int i = 0;i<=q;i++){
		if(i!=0){
			int a=0,b=0;	
			cin>>a>>b;
			v[a]=b;
		}
		for(int i = 0;i<n;i++){
			if(v[i]<v[mindex]||(v[i]==v[mindex]&&(i==n-1&&v[0]!=v[mindex]||v[i]!=v[i+1]))){
				mindex=i;
			}
		}
		int ans=0;
		int j=(mindex+1)%n;
		while(j!=mindex){
			if(j==(mindex+1)%n&&ans>0)break;
			if(v[mod(j-1,n)]<v[j]){
				ans++;
				j++;
			}
			j++;
			j%=n;
		}
		cout<<ans<<' ';
	}
}


int n;

bool check(int x, int l, int r) {
    // Check if x lies within the CIRCULAR interval [l, r]
    if (r >= l)
        return x >= l && x <= r;
    else
        return x >= l || x <= r;
}

int getS(int l, int r) {
    // Normalize l and r to be within the range of the array
    l = (l + n) % n;
    r = (r + n) % n;

    // Calculate the size of the circular interval [l, r]
    if (r >= l)
        return r - l + 1;
    else
        return n - l + r + 1;
}

int cir(int x){
	if(x>=0)return x%n;
	else return x+n;
}

map<int,int> s;
int ans;

void ansu(int l,int r){
	if(l==r)return;
	ans+=getS(l,r)/2;
	s[l]=r;
}

void alga(int X){
	auto it=s.upper_bound(X);
	if(it==s.begin()){
		it=s.end();
	}
	it--;
	int l=(*it).first,r=(*it).second;
	if(check(X,l,r)){
		s.erase(l);
		ans-=getS(l,r)/2;
		if(getS(l,r)>2){
			if(l==X){
				ansu(cir(X+1),r);
			}
			else if(r==X){
				ansu(l,cir(X-1));
			}
			else{
				ansu(l,cir(X-1));
				ansu(cir(X+1),r);
			}
		}
	}
}

void belga(int X, int mode){
	if(s.empty()){
		if(mode==1)ansu(X,cir(X+1));
		if(mode==2)ansu(cir(X-1),X);
		if(mode==3)ansu(cir(X-1),cir(X+1));
		return;
	}
	auto it=s.upper_bound(X);
	if(mode==1){
		if(it==s.end())it=s.begin();
		int l=(*it).first,r=(*it).second;
		if(cir(X+1)==l){
			ans-=getS(l,r)/2;
			s.erase(it);
			ansu(X,r);
		}
		else{
			ansu(X,cir(X+1));
		}
	}
	if(mode==2){
		if(it==s.begin())it=s.end();
		it--;
		int l=(*it).first,r=(*it).second;
		if(cir(X-1)==r){
			ans-=getS(l,r)/2;
			s.erase(it);
			ansu(l,X);
		}
		else{
			ansu(cir(X-1),X);
		}
	}
	if(mode==3){
		if(it==s.end())it=s.begin();
		auto itt=it;
		if(it==s.begin())it=s.end();
		it--;
		int l2=(*itt).first,r2=(*itt).second;
		int l=(*it).first,r=(*it).second;
		if(cir(X-1)==r&&cir(X+1)==l2){
			ans-=getS(l,r)/2;
			ans-=getS(l2,r2)/2;
			s.erase(it);
			s.erase(itt);
			ansu(l,r2);
		}
		else if(cir(X-1)==r){
			ans-=getS(l,r)/2;
			s.erase(it);
			ansu(l,cir(X+1));
		}
		else if(cir(X+1)==l2){
			ans-=getS(l2,r2)/2;
			s.erase(itt);
			ansu(cir(X-1),r2);
		}
		else ansu(cir(X-1),cir(X+1));
	}
}

int main() {
	int q;cin>>n>>q;
	if(0)bruteforce(n,q);
	else{
		vector<int> v(n);
		for(int i = 0;i<n;i++){
			cin>>v[i];
		}
		for(int j = 0,i=min_element(v.begin(),v.end())-v.begin();j<n;j++,i=(i+1)%n){
			if(v[i]<v[cir(i+1)]){
				belga(cir(i+1),2);
			}
		}
		cout<<ans<<endl;
		for(int i = 0;i<q;i++){
			int a,b;cin>>a>>b;
			alga(a);
			v[a]=b;
			if(v[a]<v[cir(a+1)]&&v[a]>v[cir(a-1)])belga(a,3);
			else if(v[a]<v[cir(a+1)]) belga(a,1);
			else if(v[a]>v[cir(a-1)]) belga(a,2);
			cout<<ans<<endl;
			/*for(auto w:s){
				cout<<w.first<<' '<<w.second<<'\n';
			}cout<<'\n';*/
		}
	}
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1812 KiB
2Accepted3ms2000 KiB
subtask28/8
3Accepted3ms2216 KiB
4Accepted3ms2464 KiB
5Accepted3ms2544 KiB
6Accepted3ms2628 KiB
7Accepted3ms2656 KiB
8Accepted3ms2732 KiB
9Accepted3ms2964 KiB
10Accepted3ms3048 KiB
11Accepted3ms3176 KiB
subtask315/15
12Accepted509ms8264 KiB
13Accepted508ms7504 KiB
14Accepted479ms6852 KiB
15Accepted512ms8412 KiB
16Accepted518ms8932 KiB
17Accepted533ms4696 KiB
18Accepted393ms4704 KiB
subtask410/10
19Accepted3ms3380 KiB
20Accepted3ms3464 KiB
21Accepted6ms3472 KiB
22Accepted6ms3704 KiB
23Accepted6ms3940 KiB
24Accepted6ms3904 KiB
25Accepted6ms4016 KiB
26Accepted6ms4140 KiB
subtask525/25
27Accepted7ms4156 KiB
28Accepted4ms4252 KiB
29Accepted35ms4408 KiB
30Accepted28ms4556 KiB
31Accepted32ms4564 KiB
32Accepted26ms4460 KiB
33Accepted27ms4552 KiB
34Accepted28ms4716 KiB
subtask642/42
35Accepted116ms5876 KiB
36Accepted41ms5844 KiB
37Accepted767ms10128 KiB
38Accepted782ms8364 KiB
39Accepted769ms10756 KiB
40Accepted629ms5840 KiB
41Accepted628ms5836 KiB
42Accepted625ms5832 KiB
43Accepted751ms11684 KiB