BZOJ 2850 巧克力王国

题目链接http://www.lydsy.com/JudgeOnline/problem.php?id=2850

题意:略。

思路:用KDTreeYY出了一种类似线段树区间查询的方法,跟那个摆棋子差不多,这里的限制条件就是ax+by<=c的时候可以吃这个巧克力,那么建好树以后我们改改估价函数啊,求出max(ax+by)min(ax+by),如果最大的也比c小的话我们就不用往下找了直接把这个节点管辖的所有巧克力的美味值加到答案上就可以了,不然的话如果最小的比c小的话我们就继续往下找,如此即可,单次查询的时间复杂度期望是O(\sqrt n)的,总感觉很玄学很暴力。。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define F first
#define S second
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
template <class T1, class T2>inline void gmax(T1 &a,T2 b){if (b>a) a=b;}
template <class T1, class T2>inline void gmin(T1 &a,T2 b){if (b<a) a=b;}
int n,m,i,j,root,A,B,C,D,p[maxn];
ll ans;
struct A{
    int d[2];ll w;
    bool operator<(const A&rhs)const{
        return d[D]<rhs.d[D];
    }
}a[maxn];
struct T{int d[2],s[2],x[2],y[2];ll sum,w;}t[maxn];
#define ls t[root].s[0]
#define rs t[root].s[1]
#define oo 2000000000000000001LL
void umin(int&a,int b){if(a>b)a=b;}
void umax(int&a,int b){if(a<b)a=b;}
void maintain(int f,int x){
    umin(t[f].x[0],t[x].x[0]),umax(t[f].x[1],t[x].x[1]);
    umin(t[f].y[0],t[x].y[0]),umax(t[f].y[1],t[x].y[1]);
    t[f].sum+=t[x].sum;
}
int build(int l,int r,int d){
    D=d;int root=l+((r-l)>>1);
    nth_element(a+l,a+root,a+r+1);
    t[root].x[0]=t[root].x[1]=t[root].d[0]=a[root].d[0];
    t[root].y[0]=t[root].y[1]=t[root].d[1]=a[root].d[1];
    t[root].sum=t[root].w=a[root].w;
    if (l<root) ls=build(l,root-1,d^1),maintain(root,ls);
    if (root<r) rs=build(root+1,r,d^1),maintain(root,rs);
    return root;
}
ll cnt(int X,int Y){return A*X+B*Y;}
ll getmx(int p){return max(max(cnt(t[p].x[0],t[p].y[0]),cnt(t[p].x[0],t[p].y[1])),max(cnt(t[p].x[1],t[p].y[0]),cnt(t[p].x[1],t[p].y[1])));}
ll getmn(int p){return min(min(cnt(t[p].x[0],t[p].y[0]),cnt(t[p].x[0],t[p].y[1])),min(cnt(t[p].x[1],t[p].y[0]),cnt(t[p].x[1],t[p].y[1])));}
void query(int root){
    ll tmp=cnt(t[root].d[0],t[root].d[1]);
    if (tmp<C) ans+=t[root].w;
    if (ls){
        ll mx=getmx(ls),mn=getmn(ls);
        if (mx<C) ans+=t[ls].sum;
        else if (mn<C) query(ls);
    }
    if (rs){
        ll mx=getmx(rs),mn=getmn(rs);
        if (mx<C) ans+=t[rs].sum;
        else if (mn<C) query(rs);
    }
}
int main(){
    read(n),read(m);
    for (i=1;i<=n;i++){
        read(a[i].d[0]),read(a[i].d[1]),read(a[i].w);
        p[i]=i;
    }
    root=build(1,n,0);
    for (i=1;i<=m;i++){
        read(A),read(B),read(C);
        ans=0,query(root);
        printf("%lld\n",ans);
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注