QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#257963#7582. Snowy Smileddl_VS_pigeon#AC ✓1678ms3868kbC++173.3kb2023-11-19 14:02:242023-11-19 14:02:24

Judging History

你现在查看的是最新测评结果

  • [2023-11-19 14:02:24]
  • 评测
  • 测评结果:AC
  • 用时:1678ms
  • 内存:3868kb
  • [2023-11-19 14:02:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll lnf = 1e18;

struct Segt {
    struct T {
        ll tag, mx, mn;
        T() : tag(0), mx(0), mn(0) {}
        void add(int val) { tag += val, mx += val, mn += val; }
    };
    int n;
    vector<T> t;
    Segt(int _n) : n(_n), t(n << 2 | 1) {}
    void add(int al, int ar, int val) { add(al, ar, val, 1, 0, n - 1); }
    ll getmx(int ql, int qr) { return getmx(ql, qr, 1, 0, n - 1); }
    ll getmn(int ql, int qr) { return getmn(ql, qr, 1, 0, n - 1); }
#define mid ((l + r) >> 1)
#define ls (v << 1)
#define rs (v << 1 | 1)
    void add(int al, int ar, int val, int v, int l, int r) {
        if (ar < l || al > r) {
            return;
        }
        if (al <= l && ar >= r) {
            t[v].add(val);
        } else {
            add(al, ar, val, ls, l, mid);
            add(al, ar, val, rs, mid + 1, r);
            t[v].mx = max(t[ls].mx, t[rs].mx) + t[v].tag;
            t[v].mn = min(t[ls].mn, t[rs].mn) + t[v].tag;
        }
    }
    ll getmx(int ql, int qr, int v, int l, int r) {
        if (qr < l || ql > r) {
            return -lnf;
        }
        if (ql <= l && qr >= r) {
            return t[v].mx;
        } else {
            return max(getmx(ql, qr, ls, l, mid),
                       getmx(ql, qr, rs, mid + 1, r)) +
                   t[v].tag;
        }
    }
    ll getmn(int ql, int qr, int v, int l, int r) {
        if (qr < l || ql > r) {
            return lnf;
        }
        if (ql <= l && qr >= r) {
            return t[v].mn;
        } else {
            return min(getmn(ql, qr, ls, l, mid),
                       getmn(ql, qr, rs, mid + 1, r)) +
                   t[v].tag;
        }
    }
#undef rs
#undef ls
#undef mid
};

void solve() {
    int n;
    cin >> n;
    struct P {
        int x, y, w;
    };
    vector<P> p(n);
    vector<int> vx, vy;
    for (auto& [x, y, w] : p) {
        cin >> x >> y >> w;
        vx.emplace_back(x);
        vy.emplace_back(y);
    }
    sort(vx.begin(), vx.end());
    vx.erase(unique(vx.begin(), vx.end()), vx.end());
    sort(vy.begin(), vy.end());
    vy.erase(unique(vy.begin(), vy.end()), vy.end());
    for (auto& [x, y, w] : p) {
        x = lower_bound(vx.begin(), vx.end(), x) - vx.begin() + 1;
        y = lower_bound(vy.begin(), vy.end(), y) - vy.begin() + 1;
    }
    sort(p.begin(), p.end(), [](const P& a, const P& b) { return a.x < b.x; });
    ll ans = 0;
    for (int l = 0; l < n; l++) {
        if (l == 0 || p[l - 1].x < p[l].x) {
            Segt tr(vy.size() + 1);
            for (int i = l; i < n;) {
                int j = i;
                while (j < n && p[j].x == p[i].x) {
                    ++j;
                }
                for (int k = i; k < j; k++) {
                    tr.add(p[k].y, vy.size(), p[k].w);
                }
                for (int k = i; k < j; k++) {
                    ans = max(ans, tr.getmx(p[k].y, vy.size()) -
                                       tr.getmn(0, p[k].y - 1));
                }
                i = j;
            }
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3632kb

input:

2
4
1 1 50
2 1 50
1 2 50
2 2 -500
2
-1 1 5
-1 1 1

output:

100
6

result:

ok 2 number(s): "100 6"

Test #2:

score: 0
Accepted
time: 1678ms
memory: 3868kb

input:

52
10
30 -1 -40065867
-15 -74 -695603735
-14 3 -847010045
33 -92 -458180374
-88 -86 -488393753
50 -91 -949931576
39 -99 -168684248
-29 64 783067879
14 -5 -104144786
-67 90 492127865
10
-29 -70 151800541
54 41 -677722362
-85 -72 766300892
-47 -90 -74384884
-12 -56 679506148
0 -41 -30038154
-4 -6 -254...

output:

1275195744
2084179225
1734353870
1018976777
2591083202
1309403622
2358891488
2143566532
1602649268
2112317422
1470851645
2423431804
2209082718
1571719735
1313345276
2039708041
1526772930
1368470878
866267413
1826546180
1878414495
1706604892
1822460963
1942258759
2943744963
2090539403
666502909
14660...

result:

ok 52 numbers