题解:P3136 [USACO16JAN] Mowing the Field P

· · 题解

题解:P3136 [USACO16JAN] Mowing the Field P

闲话

十年 OI 一场空,开了 long long 见祖宗。
这里提供一种卡常解法。

思路

按顺序扫描,维护距离 \ge T 的另一类线段。

那就可以分块:
对 x 坐标分块,块内存储竖直线段 (x, y_1, y_2)。
对 y 坐标分块,块内存储水平线段 (y, x_1, x_2)。

然后扫描两次: 处理水平线段:查询 x 区间内竖直线段中 y_1 < y_0 < y_2 的数量。
竖直线段也是类似的。

最后卡常:坐标离散化,即可通过。

暴力代码(80pts)

#include<bits/stdc++.h>
//Things will always be better, just let time play
using namespace std;
#define HACKILLER_FAST ios::sync_with_stdio(false),cin.tie(0)
#define int long long
#define rint register int
#define db double
#define pii pair<int,int>
#define vc vector
#define pb push_back
#define ls x<<1
#define rs x<<1|1
struct node{
    int xi,yi;
}a[100050];
int n,T,ans;

inline int calc(int x){
    if(a[x].xi == a[x+1].xi) return 1;
    return 0;
}
inline bool ok(int l,int r){
    if(calc(l) == calc(r)) return false;
    if(calc(r) == 1) swap(l,r); //l是竖着的,r是横着的
    if(((a[l].yi < a[r].yi && a[r].yi < a[l+1].yi) || (a[l+1].yi < a[r].yi && a[r].yi < a[l].yi)) && ((a[r].xi < a[l].xi && a[l].xi < a[r+1].xi) || (a[r+1].xi < a[l].xi && a[l].xi < a[r].xi))) return true;
    return false;
}

signed main(){
//  freopen("data.in","r",stdin);
//  freopen("task.out","w",stdout);
    HACKILLER_FAST;
    cin>>n>>T;
    for(int i = 1;i <= n;++i) cin>>a[i].xi>>a[i].yi;
    for(int i = T+1;i < n;++i){
        for(int j = 1;j <= i-T;++j){
            if(ok(j,i)) ++ans;
        }
    }
    cout<<ans;
    return 0;
}

分块代码(100pts)

#include<bits/stdc++.h>
//Things will always be better, just let time play
using namespace std;
#define HACKILLER_FAST ios::sync_with_stdio(false),cin.tie(0)
//#define int long long 开了只有90pts,别问我怎么知道的
#define rint register int
#define db double
#define pii pair<int,int>
#define vc vector
#define pb push_back
#define ls x<<1
#define rs x<<1|1
const int B = 350;
struct node{
    int xi,yi;
}a[100050];
struct Seg{
    int id,x1,x2,y1,y2;
}hor[100050],ver[100050],hh[100050],vv[100050];
int n,T,ans,hcnt,vcnt,xs[200050],ys[200050],nx,ny,pv = 1,qh = 1,blk[200050],tp[100050];
vc<pair<int,pii> > bver[600],bhor[600];

inline int calc(int x){
    if(a[x].xi == a[x+1].xi) return 1;
    return 0;
}
inline void add_ver(int x,int y1,int y2){
    int b = blk[x];
    bver[b].pb(make_pair(x,make_pair(y1,y2)));
}
inline int query_ver(int l,int r,int y0){
    int bl = blk[l],br = blk[r],res = 0;
    if(bl == br){
        for(int i = 0;i < bver[bl].size();++i){
            int x = bver[bl][i].first;
            int y1 = bver[bl][i].second.first,y2 = bver[bl][i].second.second;
            if(l <= x && x <= r && y1 < y0 && y0 < y2) ++res;
        }
    }
    else{
        for(int i = 0;i < bver[bl].size();++i){
            int x = bver[bl][i].first;
            int y1 = bver[bl][i].second.first,y2 = bver[bl][i].second.second;
            if(l <= x && x <= r && y1 < y0 && y0 < y2) ++res;
        }
        for(int b = bl+1;b < br;++b){
            for(int i = 0;i < bver[b].size();++i){
                int y1 = bver[b][i].second.first,y2 = bver[b][i].second.second;
                if(y1 < y0 && y0 < y2) ++res;
            }
        }
        for(int i = 0;i < bver[br].size();++i){
            int x = bver[br][i].first;
            int y1 = bver[br][i].second.first,y2 = bver[br][i].second.second;
            if(l <= x && x <= r && y1 < y0 && y0 < y2) ++res;
        }
    }
    return res;
}
inline void add_hor(int y,int x1,int x2){
    int b = blk[y];
    bhor[b].pb(make_pair(y,make_pair(x1,x2)));
}
inline int query_hor(int l,int r,int x0){
    int bl = blk[l],br = blk[r],res = 0;
    if(bl == br){
        for(int i = 0;i < bhor[bl].size();++i){
            int y = bhor[bl][i].first;
            int x1 = bhor[bl][i].second.first,x2 = bhor[bl][i].second.second;
            if(l <= y && y <= r && x1 < x0 && x0 < x2) ++res;
        }
    }
    else{
        for(int i = 0;i < bhor[bl].size();++i){
            int y = bhor[bl][i].first;
            int x1 = bhor[bl][i].second.first,x2 = bhor[bl][i].second.second;
            if(l <= y && y <= r && x1 < x0 && x0 < x2) ++res;
        }
        for(int b = bl+1;b < br;++b){
            for(int i = 0;i < bhor[b].size();++i){
                int x1 = bhor[b][i].second.first,x2 = bhor[b][i].second.second;
                if(x1 < x0 && x0 < x2) ++res;
            }
        }
        for(int i = 0;i < bhor[br].size();++i){
            int y = bhor[br][i].first;
            int x1 = bhor[br][i].second.first,x2 = bhor[br][i].second.second;
            if(l <= y && y <= r && x1 < x0 && x0 < x2) ++res;
        }
    }
    return res;
}

signed main(){
//  freopen("data.in","r",stdin);
//  freopen("task.out","w",stdout);
    HACKILLER_FAST;
    cin>>n>>T;
    for(rint i = 1;i <= n;++i) cin>>a[i].xi>>a[i].yi; //祝99
    for(rint i = 1;i < n;++i){
        if(calc(i) == 0){
            tp[i] = 0;
            int x1 = min(a[i].xi,a[i+1].xi),x2 = max(a[i].xi,a[i+1].xi),y = a[i].yi;
            hh[i] = {i,x1,x2,y,0};
            hor[++hcnt] = hh[i];
        }
        else{
            tp[i] = 1;
            int y1 = min(a[i].yi,a[i+1].yi),y2 = max(a[i].yi,a[i+1].yi),x = a[i].xi;
            vv[i] = {i,x,x,y1,y2};
            ver[++vcnt] = vv[i];
        }
    }
    for(rint i = 1;i <= hcnt;++i){
        xs[++nx] = hor[i].x1,xs[++nx] = hor[i].x2;
        ys[++ny] = hor[i].y1;
    }
    for(rint i = 1;i <= vcnt;++i){
        xs[++nx] = ver[i].x1;
        ys[++ny] = ver[i].y1,ys[++ny] = ver[i].y2;
    }
    sort(xs+1,xs+nx+1);
    nx = unique(xs+1,xs+nx+1)-xs-1;
    sort(ys+1,ys+ny+1);
    ny = unique(ys+1,ys+ny+1)-ys-1;
    for(int i = 1;i <= hcnt;++i){
        hor[i].x1 = lower_bound(xs+1,xs+nx+1,hor[i].x1)-xs;
        hor[i].x2 = lower_bound(xs+1,xs+nx+1,hor[i].x2)-xs;
        hor[i].y1 = lower_bound(ys+1,ys+ny+1,hor[i].y1)-ys;
    }
    for(int i = 1;i <= vcnt;++i){
        ver[i].x1 = lower_bound(xs+1,xs+nx+1,ver[i].x1)-xs;
        ver[i].y1 = lower_bound(ys+1,ys+ny+1,ver[i].y1)-ys;
        ver[i].y2 = lower_bound(ys+1,ys+ny+1,ver[i].y2)-ys;
    }
    for(int i = 1;i <= hcnt;++i) hh[hor[i].id] = hor[i];
    for(int i = 1;i <= vcnt;++i) vv[ver[i].id] = ver[i];
    int maxn = max(nx,ny);
    for(int i = 1;i <= maxn;++i) blk[i] = (i-1)/B;
    pv = 1;
    for(int i = 1;i < n;++i){
        while(pv <= vcnt && ver[pv].id <= i-T){
            add_ver(ver[pv].x1,ver[pv].y1,ver[pv].y2);
            ++pv;
        }
        if(tp[i] == 0){
            if(hh[i].x1+1 <= hh[i].x2-1) ans+=query_ver(hh[i].x1+1,hh[i].x2-1,hh[i].y1);
        }
    }
    for(int i = 0;i < 600;++i) bhor[i].clear();
    qh = 1;
    for(int i = 1;i < n;++i){
        while(qh <= hcnt && hor[qh].id <= i-T){
            add_hor(hor[qh].y1,hor[qh].x1,hor[qh].x2);
            ++qh;
        }
        if(tp[i] == 1){
            if(vv[i].y1+1 <= vv[i].y2-1) ans+=query_hor(vv[i].y1+1,vv[i].y2-1,vv[i].x1);
        }
    }
    cout<<ans;
    return 0;
}