HDU 5140 Hun Gui Wei Company KDTree裸题

本文介绍使用KD树解决二维平面上的矩形范围查询问题,通过不断尝试重构KD树的策略来优化运行效率,最终实现在线处理大量数据点及查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持多种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个项目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值