bzoj4237: 稻草人 cdq分治 單調棧
阿新 • • 發佈:2018-09-27
題目 while online https etc return print || getchar()
目錄
- 題目鏈接
- 題解
- 代碼
題目鏈接
bzoj4237: 稻草人
題解
暴力統計是n^2的
考慮統計一段區間對另一端的貢獻
對於y值cdq分治,降調一維
對於當前兩個分治區間統計上面那部分對下面那部分的貢獻
對當前兩區間x排序後,對上部分維護單增單調棧,得到距離當前點最近的比她低的點p
對於下面的區間維護一個上凸殼 ,直接在凸殼上二分p統計答案
代碼
#include<set> #include<cstdio> #include<cstring> #include<algorithm> #define gc getchar() #define pc putchar #define LL long long inline int read() { int x = 0,f = 1; char c = gc; while(c < '0' || c > '9') c = gc; while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc; return x * f; } void print(LL x) { if(x < 0) { pc('-'); x = -x; } if(x >= 10) print(x / 10); pc(x % 10 + '0'); } const int maxn = 200007; struct Point { int x,y; } po[maxn]; int n; bool cmpx(Point a,Point b) { return a.x < b.x; } bool cmpy(Point a,Point b) { return a.y < b.y; } int tp,tl; int sk[maxn],sk2[maxn]; LL ans = 0; void solve(int l = 1,int r = n) { if(l == r) return ; int mid = l + r >> 1; std::sort(po + l,po + r + 1,cmpy); std::sort(po + l,po + mid + 1,cmpx); //down std::sort(po + mid + 1,po + r + 1,cmpx); //up tp = tl = 0; int p = l,L,R,to,miid,lim; for(int i = mid + 1;i <= r;++ i) { while(tp && po[sk[tp]].y >= po[i].y) tp --; sk[++ tp] = i; for(;p <= mid && po[p].x < po[i].x;++ p) { while(tl && po[sk2[tl]].y <= po[p].y) tl --; sk2[++ tl] = p; } L = 1,R = tl;to = - 1;lim = po[sk[tp - 1]].x; while(L <= R) { miid = L + R >> 1; if(po[sk2[miid]].x > lim) to = miid,R = miid - 1; else L = miid + 1; } if(to != -1) ans += 1ll * tl - 1ll * to + 1; } solve(l,mid); solve(mid + 1,r); } int main() { n = read(); for(int i = 1;i <= n;++ i) { po[i].x = read(),po[i].y = read(); } po[0].x = po[0].y = -1; solve(); print(ans); pc('\n'); }
bzoj4237: 稻草人 cdq分治 單調棧