10494 | 2024-04-03 13:56:34 | 111 | Branch Cutting | cpp17 | Accepted 100/100 | 314ms | 21720 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;
}
auto ins=[&](int s,int e){
if(s==e)return;
ans+=((e-s+N)%N+1)/2;
z.emplace(s,e);
};
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];
ins(s,e);
}
else if(x==e){
e=p[e];
ins(s,e);
}
else{
ins(s,p[x]);
ins(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()){
ins(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;
}
}
ins(s,e);
}
cout<<ans<<' ';
}
cout<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2108 KiB | ||||
2 | Accepted | 3ms | 2112 KiB | ||||
subtask2 | 8/8 | ||||||
3 | Accepted | 3ms | 2244 KiB | ||||
4 | Accepted | 3ms | 2376 KiB | ||||
5 | Accepted | 3ms | 2448 KiB | ||||
6 | Accepted | 3ms | 2568 KiB | ||||
7 | Accepted | 3ms | 2676 KiB | ||||
8 | Accepted | 3ms | 2936 KiB | ||||
9 | Accepted | 3ms | 2936 KiB | ||||
10 | Accepted | 3ms | 2944 KiB | ||||
11 | Accepted | 3ms | 3084 KiB | ||||
subtask3 | 15/15 | ||||||
12 | Accepted | 215ms | 17612 KiB | ||||
13 | Accepted | 202ms | 16828 KiB | ||||
14 | Accepted | 190ms | 16040 KiB | ||||
15 | Accepted | 217ms | 18412 KiB | ||||
16 | Accepted | 214ms | 19064 KiB | ||||
17 | Accepted | 115ms | 13208 KiB | ||||
18 | Accepted | 115ms | 13116 KiB | ||||
subtask4 | 10/10 | ||||||
19 | Accepted | 3ms | 3944 KiB | ||||
20 | Accepted | 3ms | 4020 KiB | ||||
21 | Accepted | 4ms | 4044 KiB | ||||
22 | Accepted | 4ms | 3972 KiB | ||||
23 | Accepted | 4ms | 3988 KiB | ||||
24 | Accepted | 3ms | 3964 KiB | ||||
25 | Accepted | 3ms | 3968 KiB | ||||
26 | Accepted | 4ms | 4312 KiB | ||||
subtask5 | 25/25 | ||||||
27 | Accepted | 4ms | 4564 KiB | ||||
28 | Accepted | 4ms | 4784 KiB | ||||
29 | Accepted | 12ms | 5252 KiB | ||||
30 | Accepted | 12ms | 5236 KiB | ||||
31 | Accepted | 12ms | 5164 KiB | ||||
32 | Accepted | 9ms | 4912 KiB | ||||
33 | Accepted | 10ms | 4916 KiB | ||||
34 | Accepted | 10ms | 5288 KiB | ||||
subtask6 | 42/42 | ||||||
35 | Accepted | 35ms | 13704 KiB | ||||
36 | Accepted | 20ms | 13836 KiB | ||||
37 | Accepted | 312ms | 19636 KiB | ||||
38 | Accepted | 296ms | 17072 KiB | ||||
39 | Accepted | 314ms | 20496 KiB | ||||
40 | Accepted | 216ms | 13880 KiB | ||||
41 | Accepted | 215ms | 13864 KiB | ||||
42 | Accepted | 216ms | 13864 KiB | ||||
43 | Accepted | 293ms | 21720 KiB |