bzoj 2850 巧克力王國
阿新 • • 發佈:2019-02-20
() true nth rhs bit pri span bits 矩形
bzoj 2850 巧克力王國
- 錢限題.題面可以看這裏.
- 顯然 \(x\) \(y\) 可以看成坐標平面上的兩維,蛋糕可以在坐標平面上表示為 \((x,y)\) ,權值為 \(h\) .用 \(kd-tree\) 維護這些點.
- 查詢時,類似於線段樹,若當前節點管轄範圍完全在查詢範圍內,直接返回當前節點記錄的總和;若完全在查詢範圍外,返回 \(0\) ;否則進入兩顆子樹,遞歸處理.
- 如何判斷當前節點管轄範圍與查詢範圍的關系?註意到查詢範圍是一個限制 \(ax+by<c\) ,即一個半平面.而用記錄的 \(x,y\) 最大最小值組合成 \(4\) 個點,管轄範圍是被包含在這個矩形中的.
- 由於矩形是一個凸包,根據半平面的性質,若這個矩形的 \(4\)
- 實現時註意一個小細節, \(kd-tree\) 節點的左右子樹是不包含自身這個節點的,與線段樹不同.所以進入遞歸前要判一下自身這個節點是否在範圍中而統計貢獻.
#include<bits/stdc++.h> using namespace std; #define ll long long #define mp make_pair #define pii pair<int,int> inline ll read() { ll x=0; bool pos=1; char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') pos=0; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; return pos?x:-x; } const int MAXN=5e4+10; int n,m,Dimen,rt; struct node{ ll sum,h; int v[2]; int mi[2],ma[2]; int ls,rs; bool operator < (const node &rhs) const { return v[Dimen]<rhs.v[Dimen]; } node() { h=0; sum=0; } }Tree[MAXN],A[MAXN]; #define root Tree[o] #define lson Tree[root.ls] #define rson Tree[root.rs] #define inf 0x7fffffff void init() { for(int i=0;i<2;++i) { Tree[0].mi[i]=inf; Tree[0].ma[i]=-inf; } Tree[0].sum=0; } inline void pushup(int o) { for(int i=0;i<2;++i) { root.mi[i]=min(root.mi[i],min(lson.mi[i],rson.mi[i])); root.ma[i]=max(root.ma[i],max(lson.ma[i],rson.ma[i])); } root.sum=lson.sum+rson.sum+root.h; } int BuildTree(int l,int r,int dimen) { Dimen=dimen; int mid=(l+r)>>1; int o=mid; nth_element(A+l,A+mid,A+r+1); root.h+=A[mid].h;//位置可能重復? for(int i=0;i<2;++i) { root.v[i]=A[mid].v[i]; root.mi[i]=root.ma[i]=root.v[i]; } root.ls=root.rs=0; if(l<=mid-1) root.ls=BuildTree(l,mid-1,dimen^1); if(mid+1<=r) root.rs=BuildTree(mid+1,r,dimen^1); pushup(o); return o; } ll ans,a,b,c; ll calc(int x,int y) { return 1LL*a*x+1LL*b*y; } bool totally_out(int o) { if(calc(root.mi[0],root.mi[1])<c) return false; if(calc(root.ma[0],root.ma[1])<c) return false; if(calc(root.mi[0],root.ma[1])<c) return false; if(calc(root.ma[0],root.mi[1])<c) return false; return true; } bool totally_in(int o) { if(calc(root.mi[0],root.mi[1])>=c) return false; if(calc(root.ma[0],root.ma[1])>=c) return false; if(calc(root.mi[0],root.ma[1])>=c) return false; if(calc(root.ma[0],root.mi[1])>=c) return false; return true; } void query(int o) { if(!o) return; if(totally_in(o)) { ans+=root.sum; return; } if(totally_out(o)) return; if(1LL*a*root.v[0]+1LL*b*root.v[1]<c) ans+=root.h; query(root.ls); query(root.rs); } int main() { n=read(),m=read(); for(int i=1;i<=n;++i) { A[i].v[0]=read(); A[i].v[1]=read(); A[i].h=read(); } init(); rt=BuildTree(1,n,0); while(m--) { a=read(),b=read(),c=read(); ans=0; query(rt); printf("%lld\n",ans); } return 0; }
bzoj 2850 巧克力王國