线段树 + 扫描线 路过大佬帮忙看一下 qwq 代码比较短了
查看原帖
线段树 + 扫描线 路过大佬帮忙看一下 qwq 代码比较短了
519384
Link_Cut_Y楼主2021/10/1 16:49

不知为何 RE 9个点 qwq

code

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define int long long

using namespace std;

const int N = 2e4 + 10;

int n, w, h;
vector<int> pos;

struct Seg
{
    int l, r, len, lazy;
}tr[N << 2];

struct node
{
    int x, y1, y2, f;
}p[N];

int cmp(node a, node b)
{
    return a.x < b.x || (a.x == b.x && a.f < 0);
}

void pushup(int u)
{
    tr[u].len = max(tr[u << 1].len, tr[u << 1 | 1].len) + tr[u].lazy;
}

void build(int u, int l, int r)
{
    tr[u] = {l, r, 0, 0};
    
    if (r - l == 1) return;
    
    int mid = (l + r) >> 1;
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
    
    pushup(u);
}

void modify(int u, int l, int r, int k)
{
    if (tr[u].l >= l && tr[u].r <= r)
    {
        tr[u].lazy += k;
        tr[u].len += k;
        return;
    }
    
    if (l < tr[u << 1].r) modify(u << 1, l, min(r, tr[u << 1].r), k);
    if (r > tr[u << 1 | 1].l) modify(u << 1 | 1, max(l, tr[u << 1 | 1].l), r, k);
    
    pushup(u);
}

signed main()
{
    int t;
    scanf("%lld", &t);
    
    while (t -- )
    {
        scanf("%lld%lld%lld", &n, &w, &h);
        
        int cnt = 0;
        for (int i = 1; i <= n; i ++ )
        {
            int a, b, c;
            scanf("%lld%lld%lld", &a, &b, &c);
            p[cnt] = {a, b, b + h, c};
            cnt ++ ;
            p[cnt] = {a + w, b, b + h, -c};
            cnt ++ ;
            
            pos.push_back(b);
            pos.push_back(b + h); // 离散化
        }
        
        sort(pos.begin(), pos.end()); // 按照权值排序
        int num = unique(pos.begin(), pos.end()) - pos.begin(); // 去重
        
        sort(p, p + cnt, cmp);
        build(1, 1, num); // 建树
        
        int ans = 0;
        
        for (int i = 0; i < cnt; i ++ )
        {
            modify(1, p[i].y1, p[i].y2, p[i].f);
            if (p[i].f > 0) ans = max(ans, tr[1].len);
        }
        
        cout << ans << endl;
    }
    
    return 0;
}
2021/10/1 16:49
加载中...