【LOJ#3085】【GXOI / GZOI2019】—特技飞行(扫描线/逆序对)
第二问把切比雪夫距离转成曼哈顿距离扫描线即可
至于交点分析一下就可以发现是要找逆序对
至于交换序列
如果是要尽量交换多
可以发现所有交点都可以交换
因为考虑交错时交换恰好不会改变两个之间相对关系
这样最后所有相互关系都不会改变
否则要交换次数最少
考虑一个排名的变化可以形成若干个环
显然每个环都可以只交换次
这样恰好最少
#include<bits/stdc++.h>
using namespace std;
const int RLEN=(1<<20)|5;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define pb push_back
#define re register
#define fi first
#define se second
#define pii pair<int,int>
#define cs const
#define bg begin
#define ll long long
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=100005,M=2000005;
char xxx;
vector<int>tr[N<<1];
struct pt{
double x,y;
pt(double _x=0,double _y=0):x(_x),y(_y){}
friend inline pt operator +(cs pt &a,cs pt &b){
return pt(a.x+b.x,a.y+b.y);
}
friend inline pt operator -(cs pt &a,cs pt &b){
return pt(a.x-b.x,a.y-b.y);
}
friend inline double operator *(cs pt &a,cs pt &b){
return a.x*b.y-a.y*b.x;
}
friend inline pt operator *(cs pt &a,cs double &b){
return pt(a.x*b,a.y*b);
}
friend inline pt operator /(cs pt &a,cs double &b){
return pt(a.x/b,a.y/b);
}
}p[M],beg[N],ed[N];
int tot,n,m,a,b,c,st,des;
int ly[N],ry[N],ty[N],sy[N];
int k,x[N],y[N],r[N];
int v[N*2],cnt,num,rk[N];
int cir,vis[N];
double al[M];
void dfs(int u){
vis[u]=1;
if(vis[rk[u]])return;
dfs(rk[u]);
}
inline void getnode(int a,int b){
double f1=(ed[b]-beg[a])*(beg[b]-beg[a]),f2=(ed[b]-beg[b])*(ed[a]-beg[b]);
p[++tot]=beg[a]+(ed[a]-beg[a])*(f1/(f1+f2));
}
#define lb(x) (x&(-x))
void update(int p,int node){
for(;p;p-=lb(p))tr[p].pb(node);
}
int qry(int p,int node){
int res=0;
for(;p<=cnt;p+=lb(p)){
res+=tr[p].size();
for(int &x:tr[p]){
getnode(x,node);
}
}
return res;
}
#undef lb
ll ans,ans1,ans2;
struct opt{
int l,r,v;
};
namespace Seg{
int tag[M<<2];
#define lc (u<<1)
#define rc ((u<<1)|1)
#define mid ((l+r)>>1)
void update(int u,int l,int r,int st,int des,int k){
if(st<=l&&r<=des){tag[u]+=k;return;}
if(st<=mid)update(lc,l,mid,st,des,k);
if(mid<des)update(rc,mid+1,r,st,des,k);
}
int query(int u,int l,int r,int p){
if(tag[u])return 1;
if(l==r)return 0;
if(p<=mid)return query(lc,l,mid,p);
else return query(rc,mid+1,r,p);
}
}
vector<opt> pp[M];
vector<int> po[M];
char yyy;
int main(){
n=read(),a=read(),b=read(),c=read(),st=read(),des=read();
for(int i=1;i<=n;i++)sy[i]=read(),v[++cnt]=sy[i],beg[i]=pt(st,sy[i]);
for(int i=1;i<=n;i++)ty[i]=read(),v[++cnt]=ty[i],ed[i]=pt(des,ty[i]);
sort(v+1,v+cnt+1),cnt=unique(v+1,v+cnt+1)-v-1;
for(int i=1;i<=n;i++)ly[i]=lower_bound(v+1,v+cnt+1,sy[i])-v;
for(int i=1;i<=n;i++)ry[i]=lower_bound(v+1,v+cnt+1,ty[i])-v;
for(int i=1;i<=n;i++)num+=qry(ry[i],i),update(ry[i],i);
ans1=1ll*num*a;
for(int i=1;i<=n;i++)v[i]=ry[i];
sort(v+1,v+n+1);cir=n;
for(int i=1;i<=n;i++)rk[i]=lower_bound(v+1,v+n+1,ry[i])-v;
for(int i=1;i<=n;i++)if(!vis[i])cir--,dfs(i);
ans2=1ll*cir*a+1ll*(tot-cir)*b;
// cout<<num<<" "<<cir<<" "<<ans1<<" "<<ans2<<'\n';
m=read();cnt=0;
for(int i=1;i<=m;i++){
int a=read(),b=read();r[i]=read();
x[i]=a+b,y[i]=a-b;
al[++cnt]=x[i]+r[i],al[++cnt]=x[i]-r[i],al[++cnt]=y[i]+r[i],al[++cnt]=y[i]-r[i];
}
for(int i=1;i<=tot;i++){
double a=p[i].x,b=p[i].y;
p[i].x=a+b,p[i].y=a-b;
al[++cnt]=p[i].x,al[++cnt]=p[i].y;
}
sort(al+1,al+cnt+1),cnt=unique(al+1,al+cnt+1)-al;
for(int i=1;i<=m;i++){
int l1=lower_bound(al+1,al+cnt+1,x[i]-r[i])-al,l2=lower_bound(al+1,al+cnt+1,x[i]+r[i])-al;
int r1=lower_bound(al+1,al+cnt+1,y[i]-r[i])-al,r2=lower_bound(al+1,al+cnt+1,y[i]+r[i])-al;
//cerr<<"Rec "<<l1-6<<" "<<l2-6<<" "<<r1<<" "<<r2<<'\n';
pp[l1].pb(opt{r1,r2,1}),pp[l2].pb(opt{r1,r2,-1});
}
for(int i=1;i<=tot;i++){
int x=lower_bound(al+1,al+cnt+1,p[i].x)-al,y=lower_bound(al+1,al+cnt+1,p[i].y)-al;
po[x].pb(y);
// cerr<<"Pt "<<x-6<<" "<<y<<'\n';
}
for(int i=1;i<=cnt;i++){
for(opt &x:pp[i]){
Seg::update(1,1,cnt,x.l,x.r,x.v);
}
for(int &x:po[i]){
if(Seg::query(1,1,cnt,x))ans+=c;
}
}
cout<<min(ans1,ans2)+ans<<" "<<max(ans1,ans2)+ans<<'\n';
}