HDU 5140 Hun Gui Wei Company KDTree裸题

Hun Gui Wei Company


 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 491    Accepted Submission(s): 126


Problem Description
My friend Hun Gui Wei (HGW) has started a company which is named Hun Gui Wei Company (HGWC). In HGWC, there are many staffs. Every staff has a certain salary, level and working age. Sometimes, HGW wants to do some queries. He wants to know the sum of salary of the staffs fit some conditions. For the large amount of staffs, artificial query is time-consuming. So he wants to hire you to write a program to help him. If you do a good job, he will pay you a generous remuneration.
 

Input
Multi test cases (about 10).
The first line contains an integer n indicates there are n staffs in HGWC.
In the next n lines, each line describes the attributes of one staff.
Each line contains S, L, A indicate the salary, level and working age of one staff.
Next line an integer m comes which indicates there will be m queries.
Then next m lines, query dates will come
LL0LL1LLm1HL0HL1HLm1LA0LA1LAm1HA0HA1HAm1
HGW sets a variable k. In the beginning of each test case, k = 0. For the i-th query, he sets 
LLi=LLi+k,HLi=HLik,LAi=LAi+k,HAi=HAik , then make k the answer of this query.

[Technical Specification]
All numbers in the input are integers.
1n,m105
0S,L,A109
1017LLi,HLi,LAi,HAi1017
 

Output
For i-th query,output the sum of salary of the staffs whose level are between  LLi  and  HLi  while working age are between  LAi  and  HAi  in a single line.
 

Sample Input
  
  
2 1 2 3 4 5 6 2 3 2 3 3 2 6 2 7
 

Sample Output
  
  
1 4
Hint
For this case, first we clear k i.e. set k to 0. For the first query we should find the staff whose level is between 2 and 3 while working age is exactly 3. There is only one staff fitting the conditions whose salary is 1. So the answer for the first query is 1. Then we set k to 1. For the second query we should update query parameters according to the statement. Thus after updating, the query parameters become 3 5 3 6. There is also only one staff fitting the condition whose salary is 4. So the answer for the second case is 4.
 

KD树裸题,把问题抽象为二维平面求矩形范围内数字和即可,因为强制在线,所以没法用离线树状数组,只能用KD树。

前几发都T了,不停的尝试,从不重构KD树到每10次重构,每100次重构,每1000次重构,直到每10000次才没超时。后来试了试,每50000次重构更快。

注:不重构也行,因为数据根查询是分开的,将数据全部输入然后一次build即可。

#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
#define inf 0x7fffffffffffffff
using namespace std;
int n, m, root, cmpd, cnt;
struct data
{
    int l, r;
    long long d[2], mx[2], mn[2];
    long long sum, w; // sum:以此为根节点数目和  w:节点上数字
    data() {}
    data(long long a, long long b, long long c)
    {
        l = r = 0;
        d[0] = mx[0] = mn[0] = a;
        d[1] = mx[1] = mn[1] = b;
        w = sum = c;
    }
} tr[maxn];
void update(int x)
{
    int l = tr[x].l, r = tr[x].r;
    if (l) // 维护以此为根的子树各个维度范围与节点数目和
    {
        tr[x].mx[0] = max(tr[x].mx[0], tr[l].mx[0]);
        tr[x].mn[0] = min(tr[x].mn[0], tr[l].mn[0]);
        tr[x].mx[1] = max(tr[x].mx[1], tr[l].mx[1]);
        tr[x].mn[1] = min(tr[x].mn[1], tr[l].mn[1]);
        tr[x].sum += tr[l].sum;
    }
    if (r)
    {
        tr[x].mx[0] = max(tr[x].mx[0], tr[r].mx[0]);
        tr[x].mn[0] = min(tr[x].mn[0], tr[r].mn[0]);
        tr[x].mx[1] = max(tr[x].mx[1], tr[r].mx[1]);
        tr[x].mn[1] = min(tr[x].mn[1], tr[r].mn[1]);
        tr[x].sum += tr[r].sum;
    }
}
int cmp(data a, data b)
{
    if (a.d[cmpd] == b.d[cmpd])
    {
        return a.d[cmpd ^ 1] < b.d[cmpd ^ 1];
    }
    return a.d[cmpd] < b.d[cmpd];
}
int build(int l, int r, int d)
{
    int mid = (l + r) >> 1;
    cmpd = d;
    //不对整体排序,只求第n小的元素并放在其位置上
    nth_element(tr + l, tr + mid, tr + r + 1, cmp);
    tr[mid].mx[0] = tr[mid].mn[0] = tr[mid].d[0];
    tr[mid].mx[1] = tr[mid].mn[1] = tr[mid].d[1];
    tr[mid].sum = tr[mid].w;
    tr[mid].l = 0;
    tr[mid].r = 0;
    if (l < mid)
    {
        tr[mid].l = build(l, mid - 1, d ^ 1);
    }
    if (r > mid)
    {
        tr[mid].r = build(mid + 1, r, d ^ 1);
    }
    update(mid);
    return mid;
}
void kdinsert(int now)
{
    if (now == root)
    {
        return;
    }
    int D, p;
    D = 0;
    p = root;
    while (true)
    {
        //插入到P中,维护信息
        if (tr[now].mx[0] > tr[p].mx[0])
        {
            tr[p].mx[0] = tr[now].mx[0];
        }
        if (tr[now].mx[1] > tr[p].mx[1])
        {
            tr[p].mx[1] = tr[now].mx[1];
        }
        if (tr[now].mn[0] < tr[p].mn[0])
        {
            tr[p].mn[0] = tr[now].mn[0];
        }
        if (tr[now].mn[1] < tr[p].mn[1])
        {
            tr[p].mn[1] = tr[now].mn[1];
        }
        tr[p].sum += tr[now].sum;
        if (tr[p].d[0] == tr[now].d[0] && tr[p].d[1] == tr[now].d[1]) //相等时只更新不插入
        {
            tr[p].w += tr[now].w;
            cnt--;
            return;
        }
        if (tr[now].d[D] >= tr[p].d[D])
        {
            if (tr[p].r == 0)
            {
                tr[p].r = now;
                return;
            }
            else
            {
                p = tr[p].r;
            }
        }
        else
        {
            if (tr[p].l == 0)
            {
                tr[p].l = now;
                return;
            }
            else
            {
                p = tr[p].l;
            }
        }
        D = !D;
    }
}
long long query(int now, long long x1, long long y1, long long x2, long long y2)
{
    // 当前节点范围与查询矩形不相交
    if (!now || tr[now].mx[0] < x1 || tr[now].mn[0] > x2 || tr[now].mx[1] < y1 || tr[now].mn[1] > y2)
    {
        return 0;
    }
    // 当前节点范围包含查询矩形
    if (tr[now].mx[0] <= x2 && tr[now].mn[0] >= x1 && tr[now].mx[1] <= y2 && tr[now].mn[1] >= y1)
    {
        return tr[now].sum;
    }
    long long ret = 0;
    // 查询矩形包含当前节点
    if (tr[now].d[0] >= x1 && tr[now].d[0] <= x2 && tr[now].d[1] >= y1 && tr[now].d[1] <= y2)
    {
        ret += tr[now].w;
    }
    ret += query(tr[now].l, x1, y1, x2, y2) + query(tr[now].r, x1, y1, x2, y2);
    return ret;
}
int main()
{
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif
    while (scanf("%d", &n) != EOF)
    {
        long long a, b, c, d;
        root = 1;
        int mod = 10000;
        cnt = 0;
        long long ans = 0;
        for (int i = 0; i < n; i++)
        {
            scanf("%lld %lld %lld", &a, &b, &c);
            tr[++cnt] = data(b, c, a);
            kdinsert(cnt);
            if (cnt % mod == 0)
            {
                root = build(1, cnt, 0);
            }
        }
        scanf("%d", &m);
        for (int i = 0; i < m; i++)
        {
            scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
            long long qx1 = min(a + ans, b - ans), qy1 = min(c + ans, d - ans);
            long long qx2 = max(a + ans, b - ans), qy2 = max(c + ans, d - ans);
            ans = query(root, qx1, qy1, qx2, qy2);
            printf("%lld\n", ans);
        }
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值