답안 #561598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
561598 2022-05-13T08:36:14 Z two_sides Ants and Sugar (JOI22_sugar) C++17
100 / 100
1761 ms 115400 KB
#include <bits/stdc++.h>
 
using namespace std;
 
template <class X, class Y>
bool cmax(X &a, const Y &b) {
    return a < b ? a = b, 1 : 0;
}
 
using ll = long long;
 
#define il i * 2
#define ir i * 2 + 1
 
const int N = 1500005;
 
vector<int> val;
int lo, hi, t[N], x[N], a[N];
ll dp[N * 4][2][2];
ll tag1[N * 4], tag2[N * 4];
void build(int i, int l, int r) {
    dp[i][0][1] = -1e18;
    if (l < r) {
        int m = (l + r) / 2;
        build(il, l, m);
        build(ir, m + 1, r);
    }
}

void apply(int i, ll v1, ll v2) {
    dp[i][0][0] += v1 + v2;
    dp[i][1][1] -= v1;
    dp[i][0][1] += v2;
    dp[i][1][0] += v2;
    tag1[i] += v1;
    tag2[i] += v2;
}
 
void push(int i) {
    apply(il, tag1[i], tag2[i]);
    apply(ir, tag1[i], tag2[i]);
    tag1[i] = tag2[i] = 0;
}
 
void pull(int i) {
    for (int x = 0; x < 2; x++)
        for (int y = 0; y < 2; y++) {
            dp[i][x][y] = max(dp[il][x][y], dp[ir][x][y]);
            cmax(dp[i][x][y], dp[il][x][0] + dp[ir][1][y]);
            cmax(dp[i][x][y], dp[il][x][1] + dp[ir][0][y]);
        }
}
 
void add(int i, int l, int r, int v1, int v2) {
    if (l >= lo && r <= hi)
        return apply(i, v1, v2);
    push(i);
    int m = (l + r) / 2;
    if (m >= lo) add(il, l, m, v1, v2);
    if (m < hi) add(ir, m + 1, r, v1, v2);
    pull(i);
}
 
void add(int l, int r, int v1, int v2) {
    if (l > r) return;
    lo = l; hi = r;
    add(1, 1, val.size(), v1, v2);
}
 
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int q, m; cin >> q >> m;
    for (int i = 0; i < q; i++) {
        cin >> t[i] >> x[i] >> a[i];
        if (t[i] == 2) {
            val.push_back(x[i] - m);
            val.push_back(x[i] + m);
        } else val.push_back(x[i]);
    }
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    build(1, 1, val.size());
    ll sum = 0;
    for (int i = 0; i < q; i++) {
        if (t[i] == 1) {
            int l = upper_bound(val.begin(),
            val.end(), x[i]) - val.begin();
            add(l + 1, val.size(), a[i], 0);
            add(l, l, 0, a[i]); sum += a[i];
        } else {
            int l = upper_bound(val.begin(),
            val.end(), x[i] - m) - val.begin();
            int r = upper_bound(val.begin(),
            val.end(), x[i] + m) - val.begin();
            add(r + 1, val.size(), -a[i], 0);
            add(l, r, 0, -a[i]);
            
        }
        cout << sum - max(dp[1][1][0], 0ll) << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 5 ms 1236 KB Output is correct
11 Correct 6 ms 1236 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 5 ms 1236 KB Output is correct
14 Correct 4 ms 1236 KB Output is correct
15 Correct 4 ms 1236 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 4 ms 724 KB Output is correct
18 Correct 5 ms 724 KB Output is correct
19 Correct 5 ms 1236 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 6 ms 1236 KB Output is correct
22 Correct 4 ms 724 KB Output is correct
23 Correct 6 ms 1236 KB Output is correct
24 Correct 6 ms 816 KB Output is correct
25 Correct 7 ms 1236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 903 ms 36696 KB Output is correct
5 Correct 354 ms 20544 KB Output is correct
6 Correct 914 ms 38740 KB Output is correct
7 Correct 388 ms 20672 KB Output is correct
8 Correct 1132 ms 41204 KB Output is correct
9 Correct 823 ms 41712 KB Output is correct
10 Correct 1115 ms 41164 KB Output is correct
11 Correct 814 ms 41644 KB Output is correct
12 Correct 359 ms 18536 KB Output is correct
13 Correct 485 ms 33160 KB Output is correct
14 Correct 781 ms 38248 KB Output is correct
15 Correct 804 ms 38296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 330 ms 18632 KB Output is correct
3 Correct 474 ms 33084 KB Output is correct
4 Correct 758 ms 38260 KB Output is correct
5 Correct 851 ms 38308 KB Output is correct
6 Correct 894 ms 60200 KB Output is correct
7 Correct 64 ms 7376 KB Output is correct
8 Correct 550 ms 30240 KB Output is correct
9 Correct 730 ms 57632 KB Output is correct
10 Correct 1432 ms 111996 KB Output is correct
11 Correct 1534 ms 112048 KB Output is correct
12 Correct 1465 ms 111980 KB Output is correct
13 Correct 1334 ms 111980 KB Output is correct
14 Correct 1450 ms 112112 KB Output is correct
15 Correct 1409 ms 111748 KB Output is correct
16 Correct 1305 ms 112024 KB Output is correct
17 Correct 1501 ms 112108 KB Output is correct
18 Correct 1514 ms 112056 KB Output is correct
19 Correct 1563 ms 112012 KB Output is correct
20 Correct 1546 ms 112024 KB Output is correct
21 Correct 1619 ms 112008 KB Output is correct
22 Correct 1610 ms 112064 KB Output is correct
23 Correct 1632 ms 112016 KB Output is correct
24 Correct 1552 ms 112080 KB Output is correct
25 Correct 1761 ms 112056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 3 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 5 ms 1236 KB Output is correct
11 Correct 6 ms 1236 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 5 ms 1236 KB Output is correct
14 Correct 4 ms 1236 KB Output is correct
15 Correct 4 ms 1236 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 4 ms 724 KB Output is correct
18 Correct 5 ms 724 KB Output is correct
19 Correct 5 ms 1236 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 6 ms 1236 KB Output is correct
22 Correct 4 ms 724 KB Output is correct
23 Correct 6 ms 1236 KB Output is correct
24 Correct 6 ms 816 KB Output is correct
25 Correct 7 ms 1236 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 903 ms 36696 KB Output is correct
30 Correct 354 ms 20544 KB Output is correct
31 Correct 914 ms 38740 KB Output is correct
32 Correct 388 ms 20672 KB Output is correct
33 Correct 1132 ms 41204 KB Output is correct
34 Correct 823 ms 41712 KB Output is correct
35 Correct 1115 ms 41164 KB Output is correct
36 Correct 814 ms 41644 KB Output is correct
37 Correct 359 ms 18536 KB Output is correct
38 Correct 485 ms 33160 KB Output is correct
39 Correct 781 ms 38248 KB Output is correct
40 Correct 804 ms 38296 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 330 ms 18632 KB Output is correct
43 Correct 474 ms 33084 KB Output is correct
44 Correct 758 ms 38260 KB Output is correct
45 Correct 851 ms 38308 KB Output is correct
46 Correct 894 ms 60200 KB Output is correct
47 Correct 64 ms 7376 KB Output is correct
48 Correct 550 ms 30240 KB Output is correct
49 Correct 730 ms 57632 KB Output is correct
50 Correct 1432 ms 111996 KB Output is correct
51 Correct 1534 ms 112048 KB Output is correct
52 Correct 1465 ms 111980 KB Output is correct
53 Correct 1334 ms 111980 KB Output is correct
54 Correct 1450 ms 112112 KB Output is correct
55 Correct 1409 ms 111748 KB Output is correct
56 Correct 1305 ms 112024 KB Output is correct
57 Correct 1501 ms 112108 KB Output is correct
58 Correct 1514 ms 112056 KB Output is correct
59 Correct 1563 ms 112012 KB Output is correct
60 Correct 1546 ms 112024 KB Output is correct
61 Correct 1619 ms 112008 KB Output is correct
62 Correct 1610 ms 112064 KB Output is correct
63 Correct 1632 ms 112016 KB Output is correct
64 Correct 1552 ms 112080 KB Output is correct
65 Correct 1761 ms 112056 KB Output is correct
66 Correct 727 ms 58840 KB Output is correct
67 Correct 939 ms 60696 KB Output is correct
68 Correct 1226 ms 62852 KB Output is correct
69 Correct 1143 ms 61676 KB Output is correct
70 Correct 1697 ms 114808 KB Output is correct
71 Correct 1641 ms 115020 KB Output is correct
72 Correct 1686 ms 114988 KB Output is correct
73 Correct 1608 ms 115044 KB Output is correct
74 Correct 1592 ms 109764 KB Output is correct
75 Correct 1634 ms 115296 KB Output is correct
76 Correct 1170 ms 64136 KB Output is correct
77 Correct 1077 ms 58536 KB Output is correct
78 Correct 1033 ms 58560 KB Output is correct
79 Correct 1411 ms 115176 KB Output is correct
80 Correct 1030 ms 58580 KB Output is correct
81 Correct 1485 ms 115256 KB Output is correct
82 Correct 1014 ms 58588 KB Output is correct
83 Correct 1547 ms 115388 KB Output is correct
84 Correct 1086 ms 58800 KB Output is correct
85 Correct 1648 ms 115316 KB Output is correct
86 Correct 1080 ms 58480 KB Output is correct
87 Correct 1676 ms 115400 KB Output is correct
88 Correct 1088 ms 58492 KB Output is correct
89 Correct 1525 ms 114676 KB Output is correct