10493 | 2024-04-03 13:50:39 | 111 | Branch Cutting | cpp17 | Time limit exceeded 8/100 | 2.099s | 18452 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2100 KiB | ||||
2 | Accepted | 3ms | 2316 KiB | ||||
subtask2 | 8/8 | ||||||
3 | Accepted | 3ms | 2424 KiB | ||||
4 | Accepted | 3ms | 2496 KiB | ||||
5 | Accepted | 3ms | 2688 KiB | ||||
6 | Accepted | 3ms | 2908 KiB | ||||
7 | Accepted | 3ms | 3132 KiB | ||||
8 | Accepted | 3ms | 3244 KiB | ||||
9 | Accepted | 3ms | 3208 KiB | ||||
10 | Accepted | 3ms | 3212 KiB | ||||
11 | Accepted | 3ms | 3280 KiB | ||||
subtask3 | 0/15 | ||||||
12 | Time limit exceeded | 2.099s | 9560 KiB | ||||
13 | Time limit exceeded | 2.055s | 9316 KiB | ||||
14 | Runtime error | 59ms | 16216 KiB | ||||
15 | Runtime error | 1.679s | 18452 KiB | ||||
16 | Time limit exceeded | 2.045s | 10712 KiB | ||||
17 | Runtime error | 54ms | 13260 KiB | ||||
18 | Runtime error | 569ms | 13512 KiB | ||||
subtask4 | 0/10 | ||||||
19 | Accepted | 3ms | 4232 KiB | ||||
20 | Accepted | 3ms | 4320 KiB | ||||
21 | Accepted | 27ms | 4284 KiB | ||||
22 | Runtime error | 18ms | 4284 KiB | ||||
23 | Runtime error | 6ms | 4372 KiB | ||||
24 | Accepted | 27ms | 4296 KiB | ||||
25 | Accepted | 27ms | 4380 KiB | ||||
26 | Runtime error | 18ms | 4384 KiB | ||||
subtask5 | 0/25 | ||||||
27 | Accepted | 4ms | 4660 KiB | ||||
28 | Accepted | 3ms | 4588 KiB | ||||
29 | Runtime error | 41ms | 4740 KiB | ||||
30 | Runtime error | 264ms | 4872 KiB | ||||
31 | Runtime error | 230ms | 4792 KiB | ||||
32 | Accepted | 1.511s | 4560 KiB | ||||
33 | Accepted | 1.514s | 4640 KiB | ||||
34 | Runtime error | 569ms | 4776 KiB | ||||
subtask6 | 0/42 | ||||||
35 | Accepted | 35ms | 13464 KiB | ||||
36 | Accepted | 20ms | 13588 KiB | ||||
37 | Time limit exceeded | 2.078s | 11100 KiB | ||||
38 | Time limit exceeded | 2.075s | 9676 KiB | ||||
39 | Time limit exceeded | 2.069s | 11720 KiB | ||||
40 | Time limit exceeded | 2.062s | 8480 KiB | ||||
41 | Time limit exceeded | 2.073s | 8472 KiB | ||||
42 | Time limit exceeded | 2.078s | 8348 KiB | ||||
43 | Time limit exceeded | 2.069s | 8348 KiB |