题解:P3136 [USACO16JAN] Mowing the Field P

· · 题解

P3136 [USACO16JAN] Mowing the Field P

part0 前言

  1. 这是一篇 cdq 分治题解。
  2. 这题真的很毒瘤,机房已经有 O(2) 个人品尝过,请谨慎食用
  3. 这篇代码码风可能不太良好,但注释和变量名仍有可读性,欢迎提出建议!

part1 思路分析

题意: 给出 n 个点,其中第 i 个点和第 i+1 个点相连必定构成一条水平或竖直的线段,记为线段 i。保证线段 i 必定与线段 i+1 垂直。对于某条线段 d,统计线段 1⋯d−T 中有多少条与线段 d 相交且垂直。

part2 AC 代码

这题需要注意以下几个细节:

  1. 端点不能取,左右都是开区间
  2. 跑 BIT 前需要离散化
  3. 线段有查询和贡献及横竖两种分类,注意区分,以及结构体要开 4 倍空间,离散化数组和树状数组要开 8 倍空间

具体实现及细节部分参考注释。

#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);i++)
#define dFor(i,j,k) for(int i=(j);i>=(k);i--)
#define FIO(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout);
#define int long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define fi first
#define se second
#define mk make_pair
#define mkt make_tuple
#define eb emplace_back
#define lwb lower_bound
#define upb upper_bound
#define all(ve) (ve).begin(),(ve).end()
using namespace std;
const int _=100005,inf=1e18,mod=998244353,base=20280031;
typedef double db;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef tuple<int,int,int> piii;
typedef set<int>::iterator iter;
template<class T> inline void cmax(T &x,T y){if(y>x) x=y;}
template<class T> inline void cmin(T &x,T y){if(y<x) x=y;}
inline void madd(int &x,int y){x=(x+y)%mod;}
inline void mmul(int &x,int y){x=(x*y)%mod;}
namespace DEBUG{
    template<class T> void _debug(const char *s, T x){cerr<<s<<'='<<x<<endl;}
    template<class F, class... Nxt> void _debug(const char *s, F x, Nxt... nxt){
        int d=0; while(*s!=','||d) d+=*s=='(',d-=*s==')',cerr<<*s++;
        cerr<<'='<<x<<',',_debug(s+1, nxt...);
    }
    #define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
}
bool M_st; using namespace DEBUG;
int n,T,ans[_],c[_<<3],v[_<<3]; struct Node{int t,op,d,w,l,r;}a[_<<2],b[_<<2];
inline int lowbit(int x){return x&-x;}
inline void A(int x,int w){while(x<=*v) c[x]+=w,x+=lowbit(x);}
inline int Q(int x){int res=0; while(x) res+=c[x],x-=lowbit(x); return res;}
inline void solve(int N){   
    sort(b+1,b+N+1,[&](Node x,Node y){return (x.op?x.w:x.r)^(y.op?y.w:y.r)?(x.op?x.w:x.r)>(y.op?y.w:y.r):x.op<y.op;});  //排序
    For(i,1,N) !b[i].op?A(b[i].w,b[i].l):(ans[b[i].t]+=b[i].l*Q(b[i].r),void());    //计算贡献
    For(i,1,N) if(!b[i].op) A(b[i].w,-b[i].l);  //清空树状数组
}
void CDQ(int l,int r){  //CDQ分治
    if(l>=r) return ;
    int mid=l+r>>1,N=0; CDQ(l,mid),CDQ(mid+1,r);    //递归处理
    For(i,l,mid) if(!a[i].d&&!a[i].op) b[++N]=a[i]; For(i,mid+1,r) if(a[i].d&&a[i].op) b[++N]=a[i]; solve(N),N=0;   //处理竖线段贡献
    For(i,l,mid) if(a[i].d&&!a[i].op) b[++N]=a[i]; For(i,mid+1,r) if(!a[i].d&&a[i].op) b[++N]=a[i]; solve(N);   //处理横线段贡献
}
bool M_ed; signed main(){
    cerr<<fixed<<setprecision(3)<<fabs(&M_ed-&M_st)/1048576.0<<"MB\n";
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    //FIO("P3136");
    cin>>n>>T; int N=0,ANS=0,op,px,py,xx,yy,x,y; cin>>px>>py;
    For(i,1,n-1){
        cin>>x>>y,op=(y==py),xx=px,yy=py,px=x,py=y; if(xx>x) swap(x,xx); if(yy>y) swap(y,yy); ++xx,--x,++yy,--y;    //由于是开区间,所以要变一下
        if(!op) a[++N]={i+T,0,0,px,1,y},a[++N]={i+T,0,0,px,-1,yy-1},a[++N]={i,1,0,px,1,y},a[++N]={i,1,0,px,-1,yy-1};
        else a[++N]={i+T,0,1,py,1,x},a[++N]={i+T,0,1,py,-1,xx-1},a[++N]={i,1,1,py,1,x},a[++N]={i,1,1,py,-1,xx-1};
    } 
    For(i,1,N) v[++*v]=a[i].w,v[++*v]=a[i].r; sort(v+1,v+*v+1),*v=unique(v+1,v+*v+1)-v-1;   //离散化
    For(i,1,N) a[i].w=lwb(v+1,v+*v+1,a[i].w)-v,a[i].r=lwb(v+1,v+*v+1,a[i].r)-v;
    sort(a+1,a+N+1,[&](Node x,Node y){return x.t^y.t?x.t<y.t:x.op<y.op;});  //按时间维排序
    CDQ(1,N); For(i,1,n-1) ANS+=ans[i];
    return cout<<ANS,0;
}

完结撒花~(樱花庄万岁)如果有写的不好的地方,欢迎私信作者或在下方留言,谢谢各位阅读与支持!