# bzoj 4071 [Apio2015]巴邻旁之桥

http://www.elijahqi.win/archives/3379
Description

Input

Output

Sample Input
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
Sample Output
24
HINT

K=1
1≤N≤1000

K=1
1≤N≤100000

K=2
1≤N≤100

K=2
1≤N≤1000

K=2
1≤N≤100000

#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
#define lc (x<<1)
#define rc (x<<1|1)
#define ll long long
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
return *S++;
}
int x=0,f=1;char ch=gc();
while (!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while (isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
inline char getc(){
char ch=gc();
while(ch!='A'&&ch!='B') ch=gc();
return ch;
}
const int N=2e5+10;
struct node1{
int l,r;
}qr[N>>1];
struct node{
ll sum;int nm;
}tree[2][N<<2];
ll ans;int q[N],tot,nn,lnm,rnm,n,k;ll ls,rs;
inline bool cmp(const node1 &a,const node1 &b){return a.l+a.r<b.l+b.r;}
inline void insert1(int x,int l,int r,int p,int id,int op){
tree[id][x].nm+=op;tree[id][x].sum+=q[p]*op;
if (l==r) return;int mid=l+r>>1;
if (p<=mid) insert1(lc,l,mid,p,id,op);
if (p>mid) insert1(rc,mid+1,r,p,id,op);
}
inline int query(int x,int l,int r,int id,int p){
if (l==r) {lnm+=tree[id][x].nm;ls+=tree[id][x].sum;return q[l];}int mid=l+r>>1;
if (p<=tree[id][lc].nm) {rnm+=tree[id][rc].nm;rs+=tree[id][rc].sum;return query(lc,l,mid,id,p);}
lnm+=tree[id][lc].nm;ls+=tree[id][lc].sum;return query(rc,mid+1,r,id,p-tree[id][lc].nm);
}
int main(){
//freopen("bzoj4071.in","r",stdin);
for (int i=1;i<=n;++i){
if (cha==chb){ans+=abs(y-x);continue;}
++ans;qr[++tot]=(node1){x,y};q[++nn]=x;q[++nn]=y;
}sort(q+1,q+nn+1);
if (k==1){
for (int i=1,j=nn;i<j;++i,--j) ans+=q[j]-q[i];
printf("%lld\n",ans);return 0;
}nn=unique(q+1,q+nn+1)-q-1;sort(qr+1,qr+tot+1,cmp);
if (!tot) {printf("%lld\n",ans);return 0;}
for (int i=1;i<=tot;++i){
qr[i].l=lower_bound(q+1,q+nn+1,qr[i].l)-q;
qr[i].r=lower_bound(q+1,q+nn+1,qr[i].r)-q;
insert1(1,1,nn,qr[i].l,1,1);
insert1(1,1,nn,qr[i].r,1,1);
}ll ans1;
ll t=query(1,1,nn,1,tot);ans1=t*lnm-ls+rs-rnm*t;
for (int i=1;i<tot;++i){
insert1(1,1,nn,qr[i].l,0,1);insert1(1,1,nn,qr[i].r,0,1);
insert1(1,1,nn,qr[i].l,1,-1);insert1(1,1,nn,qr[i].r,1,-1);
ll tmp1,tmp2;lnm=ls=rnm=rs=0;
t=query(1,1,nn,0,i);tmp1=t*lnm-ls+rs-rnm*t;
lnm=ls=rnm=rs=0;
t=query(1,1,nn,1,tot-i);tmp2=t*lnm-ls+rs-rnm*t;
ans1=min(ans1,tmp1+tmp2);
}printf("%lld\n",ans1+ans);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120