[BZOJ4071] eight adjacent bridges (line segment trees)

Problem surface

BZOJ permission questions, link to Luogu

Title Solution

Since K < = 2
So, the breakthrough is here

Classified discussion
①k=1
This... Isn't that the median....
Put all the starting points in order,
Just count the median
②k=2
It doesn't seem easy
orz ZSY Dalao
I'm too weak
I'm a Vegetable Chicken
ZSY can do it at a glance

First of all, let's take a look. If there are two bridges,
How can a person move?
If the bridge is in the transverse section he is moving
Then, it must cross the bridge, the distance is dis(Qi − Ti)+1
If no bridge is in his range
He's going to get to the bridge first, and then he's going to come from the bridge
At this time, the distance is abs(Qi − Brige)+abs(Ti − Brige)+1
This thing is combined with image simplification
Equal to 2abs(Qi+Ti2 − Brige)+1
Therefore, the bridge for this person must be the bridge close to Qi+Ti2

So, after ranking all the people according to Qi+Ti2
Start enumerating where to cut
And then all the ones on the left take the bridge on the left
Take the bridge on the right
It's k=1 when it's split on both sides
But because it's the median of the dynamic maintenance interval
So we need to find something to maintain it
Weight line tree and balance tree are all available

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define MAX 250000
#define lson (now<<1)
#define rson (now<<1|1)
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Node{int a,b;}w[MAX];
bool operator<(Node a,Node b){return a.a+a.b<b.a+b.b;}
int cnt;
long long ans;
int K,n,a[MAX],b[MAX],S[MAX],top;
struct SegMentTree
{
struct node
{
int size;ll sum;
}t[MAX<<2];
int Kth(int now,int l,int r,int k)
{
if(l==r)return l;
int mid=(l+r)>>1;
if(k<=t[lson].size)return Kth(lson,l,mid,k);
else return Kth(rson,mid+1,r,k-t[lson].size);
}
void Modify(int now,int l,int r,int pos,int ww)
{
t[now].size+=ww;t[now].sum+=1ll*ww*S[pos];
if(l==r)return;
int mid=(l+r)>>1;
if(pos<=mid)Modify(lson,l,mid,pos,ww);
else Modify(rson,mid+1,r,pos,ww);
}
ll QueryV(int now,int l,int r,int al,int ar)
{
if(al<=l&&r<=ar)return t[now].sum;
int mid=(l+r)>>1;ll ret=0;
if(al<=mid)ret+=QueryV(lson,l,mid,al,ar);
if(ar>mid)ret+=QueryV(rson,mid+1,r,al,ar);
return ret;
}
int QueryS(int now,int l,int r,int al,int ar)
{
if(al<=l&&r<=ar)return t[now].size;
int mid=(l+r)>>1,ret=0;
if(al<=mid)ret+=QueryS(lson,l,mid,al,ar);
if(ar>mid)ret+=QueryS(rson,mid+1,r,al,ar);
return ret;
}
}T[2];
int main()
{
char ch[2];
if(K==1)
{
int tot=0;
for(int i=1;i<=n;++i)
{
scanf("%s",ch);int p=ch[0]-'A';
scanf("%s",ch);int q=ch[0]-'A';
if(p==q){ans+=abs(s-t);continue;}
else if(p==1)swap(s,t);
++tot;a[tot]=s;b[tot]=t;S[++top]=s;S[++top]=t;
}
sort(&S[1],&S[top+1]);
int G=S[top/2];
for(int i=1;i<=top;++i)ans+=abs(G-S[i]);
printf("%lld\n",ans+tot);
return 0;
}
for(int i=1;i<=n;++i)
{
scanf("%s",ch);int p=ch[0]-'A';
scanf("%s",ch);int q=ch[0]-'A';
if(p==q){ans+=abs(s-t);continue;}
ans++;S[++top]=s;S[++top]=t;
if(s>t)swap(s,t);
w[++cnt]=(Node){s,t};
}
if(!cnt){printf("%lld\n",ans);return 0;}
sort(&w[1],&w[cnt+1]);
sort(&S[1],&S[top+1]);
top=unique(&S[1],&S[top+1])-S-1;
for(int i=1;i<=cnt;++i)
{
w[i].a=lower_bound(&S[1],&S[top+1],w[i].a)-S;
w[i].b=lower_bound(&S[1],&S[top+1],w[i].b)-S;
T[1].Modify(1,1,top,w[i].a,1);
T[1].Modify(1,1,top,w[i].b,1);
}
long long sum=1e18;
for(int i=1;i<=cnt;++i)
{
T[0].Modify(1,1,top,w[i].a,1);
T[0].Modify(1,1,top,w[i].b,1);
T[1].Modify(1,1,top,w[i].a,-1);
T[1].Modify(1,1,top,w[i].b,-1);
int p1=T[0].Kth(1,1,top,i);//Find the median
int p2=T[1].Kth(1,1,top,cnt-i);
long long D0=0;
D0+=1ll*T[0].QueryS(1,1,top,1,p1)*S[p1]-T[0].QueryV(1,1,top,1,p1);
D0+=T[0].QueryV(1,1,top,p1,top)-1ll*T[0].QueryS(1,1,top,p1,top)*S[p1];
long long D1=0;
D1+=1ll*T[1].QueryS(1,1,top,1,p2)*S[p2]-T[1].QueryV(1,1,top,1,p2);
D1+=T[1].QueryV(1,1,top,p2,top)-1ll*T[1].QueryS(1,1,top,p2,top)*S[p2];
sum=min(sum,D0+D1);
}
printf("%lld\n",ans+sum);
return 0;
}

Posted on Mon, 04 May 2020 05:58:29 -0400 by mrkumar