题解:P12969 [CCO 2025] Restaurant Recommendation Rescue
困难题。
题意:
有
思路:
记
不妨令循环移位了
因为
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) begin(x),end(x)
#define pii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
void IOS(){ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);}
const int kN=5e5+7;
const int kQ=3e5+7;
const int mod=998244353;
int n,Q,x,y,a[kN],b[kN];
struct SGT{
pii tree[kN*4];
int tag[kN*4];
SGT(){fill(all(tree),mp(1e9,1e9));}
void build(int x,int l,int r){
if(l==r){tree[x]=mp(b[l],l);return ;}
int mid=(l+r)>>1;
build(x<<1,l,mid);build(x<<1|1,mid+1,r);
tree[x]=min(tree[x<<1],tree[x<<1|1]);
}
void update(int x,int l,int r,int L,int R,int val){
if(L>R)return ;
if(L<=l && r<=R){tree[x].first+=val,tag[x]+=val;return ;}
int mid=(l+r)>>1;
if(L<=mid)update(x<<1,l,mid,L,R,val);
if(mid<R)update(x<<1|1,mid+1,r,L,R,val);
tree[x]=min(tree[x<<1],tree[x<<1|1]);tree[x].first+=tag[x];
}
}T;
signed main(){
IOS();
cin>>n>>Q;
for(int i=1;i<=n;i++)cin>>a[i],a[i]--,b[i]=b[i-1]+a[i];
if(b[n]!=-1){
++Q;
while(Q--)cout<<0<<' '<<0<<'\n';
return 0;
}
T.build(1,1,n);
cout<<1<<' '<<T.tree[1].second%n<<'\n';
while(Q--){
cin>>x>>y;
++x,++y;
if(x>y)swap(x,y);
T.update(1,1,n,x,y-1,-a[x]+a[y]);
swap(a[x],a[y]);
cout<<1<<' '<<T.tree[1].second%n<<'\n';
}
return 0;
}