Codeforces Round #555 (Div. 3)E. Minimum Array线段树写法

mac2025-11-06  17

思路比较简单,就是优先让C数组左边的数更小,

令now = n-a[i].那么  b尽量等于now,如果没有b等于now就找第一个大于now的数,还没有的话就找1-now之间最小的数。

上述操作用线段树、mulset、并查集都可以维护。

 

//Codeforces Round #555 (Div. 3)E. Minimum Array #include <bits/stdc++.h> using namespace std; typedef long long ll; //typedef __int128 LL; //typedef unsigned long long ull; //#define F first //#define S second typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<ld,ld> pdd; const ld PI=acos(-1); const ld eps=1e-9; //unordered_map<int,int>mp; #define ls (o<<1) #define rs (o<<1|1) #define pb push_back const int seed=131; const int M = 2e5+7; int a[M],b[M],p[M]; int sz; int tr[M<<2]; void up(int o,int l,int r,int x,int d) { if(l==r) { tr[o]+=d; return ; } int m=(l+r)/2; if(x<=m)up(ls,l,m,x,d); else up(rs,m+1,r,x,d); tr[o]=tr[ls]+tr[rs]; } int id; void qu(int o,int l,int r,int x,int y)//最左边的数 { if(id>0)return ; if(l==r) { if(tr[o]>=1){ id=l; return ; } } int m=(l+r)/2; if(x<=m&&tr[ls]) qu(ls,l,m,x,y); if(y>m&&tr[rs]) qu(rs,m+1,r,x,y); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; sz=n+1; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) cin>>b[i],up(1,1,sz,b[i]+1,1); for(int i=1;i<=n;i++) { int now=n-a[i]+1,l=-1,r=-1; id=-1;qu(1,1,sz,now,sz); // l=id; if(id==-1)qu(1,1,sz,1,now); //r=id; up(1,1,sz,id,-1); //cout<<"-----"<<l<<" "<<r<<endl; cout<<(id+a[i]-1+n)%n<<" "; //<<id<<" "<<now<<endl; } return 0; }

 

最新回复(0)