105782024-04-05 22:40:40999Branch Cuttingcpp17Wrong answer 0/100908ms15644 KiB
// Source: https://usaco.guide/general/io

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

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){
	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(l+1),r);
			}
			else if(r==X){
				ansu(l,cir(r-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;
	vector<int> v(n);
	for(int i = 0;i<n;i++){
		cin>>v[i];
	}
	for(int i = 0;i<n;i++){
		if(v[i]<v[cir(i+1)]){
			if((v[i]>v[cir(i-1)])&&s.size()){
				i++;
				belga(i,2);
			}
			else belga(i,1);
		}
	}
	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;
	}
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1808 KiB
2Wrong answer3ms2004 KiB
subtask20/8
3Accepted3ms2088 KiB
4Accepted3ms2212 KiB
5Wrong answer3ms2296 KiB
6Wrong answer3ms2432 KiB
7Wrong answer3ms2672 KiB
8Wrong answer3ms2728 KiB
9Wrong answer3ms2856 KiB
10Wrong answer3ms2988 KiB
11Wrong answer3ms3032 KiB
subtask30/15
12Wrong answer669ms8152 KiB
13Wrong answer555ms7728 KiB
14Wrong answer561ms7444 KiB
15Wrong answer493ms9008 KiB
16Wrong answer495ms9680 KiB
17Wrong answer400ms5088 KiB
18Wrong answer391ms5084 KiB
subtask40/10
19Wrong answer3ms3852 KiB
20Accepted3ms3772 KiB
21Wrong answer6ms3764 KiB
22Wrong answer6ms3788 KiB
23Wrong answer6ms3952 KiB
24Wrong answer6ms4188 KiB
25Wrong answer6ms4176 KiB
26Wrong answer6ms4224 KiB
subtask50/25
27Wrong answer7ms4800 KiB
28Accepted4ms4444 KiB
29Wrong answer28ms5052 KiB
30Wrong answer28ms5048 KiB
31Wrong answer28ms4896 KiB
32Wrong answer28ms5092 KiB
33Wrong answer32ms4920 KiB
34Wrong answer30ms4924 KiB
subtask60/42
35Wrong answer135ms15536 KiB
36Accepted37ms6176 KiB
37Wrong answer777ms13020 KiB
38Wrong answer908ms14080 KiB
39Wrong answer773ms12516 KiB
40Wrong answer847ms15508 KiB
41Wrong answer751ms15508 KiB
42Wrong answer764ms15644 KiB
43Wrong answer748ms15512 KiB