QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#109369#68. Designated Citiesbashkort#0 1ms3884kbC++204.3kb2023-05-28 18:59:102024-05-31 13:46:33

Judging History

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

  • [2024-05-31 13:46:33]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3884kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-28 18:59:10]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int inf = 1e9 + 7;

struct SegmentTree {
    vector<pair<int, int>> t;
    vector<ll> ansL;

    int sz = 1;

    int cost(int x, int d, ll &ans) {
        if (t[x].first == -inf) {
            return d;
        }
        if (t[x].first <= t[x].second) {
            ans += max(0, d - t[x].second);
            return min(t[x].second, max(d, t[x].first));
        } else if (t[x << 1].second == -inf) {
            ans += ansL[x];
            cost(x << 1, d, ans);
            return t[x].first;
        } else {
            ans += max(0, d - t[x << 1].second);
            return cost(x << 1 | 1, min(t[x << 1].second, max(d, t[x << 1].first)), ans);
        }
    }

    void pull(int x) {
        ansL[x] = 0;
        if (t[x << 1 | 1].first == -inf) {
            t[x] = t[x << 1];
            return;
        }
        if (t[x << 1].second == -inf) {
            t[x].first = cost(x << 1 | 1, t[x << 1].first, ansL[x]);
            t[x].second = -inf;
        } else if (t[x << 1 | 1].second == -inf) {
            t[x] = t[x << 1 | 1];
        } else {
            if (t[x << 1].first > t[x << 1 | 1].second) {
                t[x].first = t[x << 1 | 1].second;
                t[x].second = -inf;
            } else if (t[x << 1].second < t[x << 1 | 1].first) {
                t[x].first = t[x << 1 | 1].first;
                t[x].second = -inf;
            } else {
                t[x].first = max(t[x << 1].first, t[x << 1 | 1].first);
                t[x].second = min(t[x << 1].second, t[x << 1 | 1].second);
            }
        }
    }

    void init(int n, vector<int> L, vector<int> R) {
        sz = 1 << __lg(n) + !!(n & (n - 1));
        t.assign(sz << 1, {-inf, -inf}), ansL.resize(sz << 1);
        for (int i = 0; i < n; ++i) {
            t[i + sz].first = L[i], t[i + sz].second = R[i];
        }
        for (int i = sz - 1; i > 0; --i) {
            pull(i);
        }
    }

    void rangeQuery(int l, int r, int &d, ll &ans, int x, int lx, int rx) {
        if (l >= rx || lx >= r) {
            return;
        }
        if (l <= lx && rx <= r) {
            d = cost(x, d, ans);
            return;
        }
        int mid = lx + rx >> 1;
        rangeQuery(l, r, d, ans, x << 1, lx, mid);
        rangeQuery(l, r, d, ans, x << 1 | 1, mid, rx);
    }

    ll rangeQuery(int l, int r, int &d) {
        ll ans = 0;
        rangeQuery(l, r, d, ans, 1, 0, sz);
        return ans;
    }

    void modify(int x, int L, int R) {
        x += sz;
        t[x].first = L, t[x].second = R;
        while (x >>= 1) {
            pull(x);
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;

    vector<int> L(n), R(n), AL(n), AR(n), BL(n), BR(n);

    for (int i = 0; i < n - 1; ++i) {
        cin >> L[i] >> R[i];
        AL[i] = L[i] - i;
        AR[i] = R[i] - i - 1;
        BL[n - i - 2] = L[i] - (n - i - 2);
        BR[n - i - 2] = R[i] - (n - i - 2) - 1;
    }

    SegmentTree A, B;
    if (n > 1) {
        A.init(n - 1, AL, AR);
        B.init(n - 1, BL, BR);
    }

    auto modify = [&](int i, int lx, int rx) {
        L[i] = lx, R[i] = rx;
        AL[i] = L[i] - i;
        AR[i] = R[i] - i - 1;
        BL[n - i - 2] = L[i] - (n - i - 2);
        BR[n - i - 2] = R[i] - (n - i - 2) - 1;
        A.modify(i, AL[i], AR[i]);
        B.modify(n - i - 2, BL[n - i - 2], BR[n - i - 2]);
    };
    
    auto query = [&](int x, int a, int y, int b) -> ll {
        if (x == y) {
            return max(0, a - b);
        }

        ll ans = 0;

        if (x <= y) {
            a -= x;
            b -= y;
            ans = A.rangeQuery(x, y, a);
        } else {
            a -= n - x - 1;
            b -= n - y - 1;
            ans = B.rangeQuery(n - x - 1, n - y - 1, a);
        }

        ans += max(0, a - b);

        return ans;
    };

    while (q--) {
        int t;
        cin >> t;

        if (t == 1) {
            int p, s, e;
            cin >> p >> s >> e;
            modify(p - 1, s, e);
        } else {
            int a, b, c, d;
            cin >> a >> b >> c >> d;
            cout << query(a - 1, b, c - 1, d) << '\n';
        }
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3596kb

input:

2
1 2 781089648 283888890
2
1
2

output:

1

result:

wrong answer 1st lines differ - expected: '283888890', found: '1'

Subtask #2:

score: 0
Wrong Answer

Test #12:

score: 0
Wrong Answer
time: 1ms
memory: 3656kb

input:

2
1 2 683402985 526289818
1
1

output:

2

result:

wrong answer 1st lines differ - expected: '526289818', found: '2'

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 1ms
memory: 3884kb

input:

2
2 1 92722556 873785501
1
2

output:

3
3

result:

wrong answer 1st lines differ - expected: '0', found: '3'

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Wrong Answer

Test #45:

score: 0
Wrong Answer
time: 0ms
memory: 3880kb

input:

2
1 2 543195986 144983073
1
1

output:

2

result:

wrong answer 1st lines differ - expected: '144983073', found: '2'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%