답안 #451945

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
451945 2021-08-03T13:47:39 Z mjhmjh1104 Fire (JOI20_ho_t5) C++17
100 / 100
851 ms 108408 KB
#include <tuple>
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

long long mul_triangles[200006], add_triangles[200006];
long long mul_squares[200006], add_squares[200006];
long long mul_parallels[400006], add_parallels[400006];

long long query_triangles(int x) {
    x++;
    int st = x;
    long long mul = 0, add = 0;
    while (x > 0) {
        mul += mul_triangles[x];
        add += add_triangles[x];
        x -= x & -x;
    }
    return st * mul + add;
}

long long query_triangles(int l, int r) {
    if (l > r) return 0;
    return query_triangles(r) - query_triangles(l - 1);
}

void update_triangles_int(int x, long long mul, long long add) {
    while (x < 200005) {
        mul_triangles[x] += mul;
        add_triangles[x] += add;
        x += x & -x;
    }
}

void update_triangles(int l, int r, long long v) {
    l++, r++;
    if (l > r) return;
    if (l <= 0) l = 1;
    update_triangles_int(l, v, -v * (l - 1));
    update_triangles_int(r + 1, -v, v * r);
}

long long query_squares(int x) {
    x++;
    int st = x;
    long long mul = 0, add = 0;
    while (x > 0) {
        mul += mul_squares[x];
        add += add_squares[x];
        x -= x & -x;
    }
    return st * mul + add;
}

long long query_squares(int l, int r) {
    if (l > r) return 0;
    return query_squares(r) - query_squares(l - 1);
}

void update_squares_int(int x, long long mul, long long add) {
    while (x < 200005) {
        mul_squares[x] += mul;
        add_squares[x] += add;
        x += x & -x;
    }
}

void update_squares(int l, int r, long long v) {
    l++, r++;
    if (l > r) return;
    if (l <= 0) l = 1;
    update_squares_int(l, v, -v * (l - 1));
    update_squares_int(r + 1, -v, v * r);
}

long long query_parallels(int x) {
    x++;
    int st = x;
    long long mul = 0, add = 0;
    while (x > 0) {
        mul += mul_parallels[x];
        add += add_parallels[x];
        x -= x & -x;
    }
    return st * mul + add;
}

long long query_parallels(int l, int r) {
    if (l > r) return 0;
    return query_parallels(r) - query_parallels(l - 1);
}

void update_parallels_int(int x, long long mul, long long add) {
    while (x < 400005) {
        mul_parallels[x] += mul;
        add_parallels[x] += add;
        x += x & -x;
    }
}

void update_parallels(int l, int r, long long v) {
    l++, r++;
    if (l > r) return;
    if (l <= 0) l = 1;
    update_parallels_int(l, v, -v * (l - 1));
    update_parallels_int(r + 1, -v, v * r);
}

int n, q, a[200006], lt[200006], rt[200006];
vector<int> st;
vector<pair<int, int>> triangles;
vector<tuple<int, int, int, int>> queries, part_squares, part_parallels;
vector<tuple<int, int, int, int, int>> squares, parallels;
long long res[200006];

void triangle(int x, int y, int l, int v) {
    if (x == y) {
        if (!x) triangles.push_back({ l, v });
        else {
            triangles.push_back({ x + l, v });
            triangles.push_back({ x - 1, -v });
            squares.push_back({ 0, x, x - 1, x, -v });
        }
    } else if (x < y) {
        if (!x) {
            triangles.push_back({ l - 1, v });
            squares.push_back({ 0, l - 1, l, y + 1, v });
            parallels.push_back({ 0, 0, l, y, -v });
        } else {
            triangles.push_back({ x + l - 1, v });
            triangles.push_back({ x - 1, -v });
            squares.push_back({ 0, x + l - 1, x + l, y - x + 1, v });
            squares.push_back({ 0, x - 1, x, y - x + l + 1, -v });
            parallels.push_back({ x, x, l, y - x, -v });
        }
    } else {
         triangles.push_back({ x + l, v });
         triangles.push_back({ x, -v });
         squares.push_back({ 0, x, x, l, -v });
         squares.push_back({ x, y + l, l, x - y, -v });
         parallels.push_back({ x, y, l, x - y, v });
    }
}

int main() {
    scanf("%d%d", &n, &q);
    for (int i = 0; i < n; i++) scanf("%d", a + i);
    for (int i = 0; i < n; i++) {
        while (!st.empty() && a[st.back()] < a[i]) st.pop_back();
        if (st.empty()) lt[i] = -1;
        else lt[i] = st.back();
        st.push_back(i);
    } st.clear();
    for (int i = n - 1; i >= 0; i--) {
        while (!st.empty() && a[st.back()] <= a[i]) st.pop_back();
        if (st.empty()) rt[i] = n;
        else rt[i] = st.back();
        st.push_back(i);
    } st.clear();
    for (int i = 0; i < n; i++) {
        int height = n + 1, width = rt[i] - i;
        if (lt[i] != -1) height = i - lt[i];
        squares.push_back({ 0, i, height, width, a[i] });
        triangle(0, i + 1, width - 1, -a[i]);
        triangle(height, i + 1, width - 1, a[i]);
    }
    for (int i = 0; i < q; i++) {
        int t, l, r;
        scanf("%d%d%d", &t, &l, &r);
        l--, r--;
        queries.push_back({ t, l, r, i });
    }
    sort(queries.begin(), queries.end());
    for (auto &i: squares) {
        auto [ a, b, c, d, e ] = i;
        if (!d) continue;
        part_squares.push_back({ a, b, b + d - 1, e });
        part_squares.push_back({ a + c, b, b + d - 1, -e });
    }
    for (auto &i: parallels) {
        auto [ a, b, c, d, e ] = i;
        if (!d) continue;
        part_parallels.push_back({ a, b - a, b - a + d - 1, e });
        part_parallels.push_back({ a + c, b - a, b - a + d - 1, -e });
    }
    sort(part_squares.begin(), part_squares.end());
    sort(part_parallels.begin(), part_parallels.end());
    for (auto &i: triangles) {
        auto [ a, b ] = i;
        update_triangles(0, a - 1, b);
    }
    int k = 0, l = 0;
    for (int i = 0; i < q; i++) {
        auto [ a, b, c, d ] = queries[i];
        while (k < (int)part_squares.size() && get<0>(part_squares[k]) <= a) {
            auto [ a, b, c, d ] = part_squares[k];
            update_squares(b, c, d);
            k++;
        }
        while (l < (int)part_parallels.size() && get<0>(part_parallels[l]) <= a) {
            auto [ a, b, c, d ] = part_parallels[l];
            update_parallels(200000 + b, 200000 + c, d);
            l++;
        }
        res[d] = query_triangles(max(a, b), c) + query_squares(b, c) + query_parallels(200000 + b - a, 200000 + c - a);
    }
    for (int i = 0; i < q; i++) printf("%lld\n", res[i]);
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:148:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:149:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 |     for (int i = 0; i < n; i++) scanf("%d", a + i);
      |                                 ~~~~~^~~~~~~~~~~~~
ho_t5.cpp:171:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         scanf("%d%d%d", &t, &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 460 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 560 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 2 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 2 ms 588 KB Output is correct
27 Correct 1 ms 588 KB Output is correct
28 Correct 1 ms 588 KB Output is correct
29 Correct 1 ms 588 KB Output is correct
30 Correct 1 ms 588 KB Output is correct
31 Correct 1 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 460 KB Output is correct
2 Correct 764 ms 101364 KB Output is correct
3 Correct 750 ms 106332 KB Output is correct
4 Correct 785 ms 106628 KB Output is correct
5 Correct 798 ms 108044 KB Output is correct
6 Correct 793 ms 107056 KB Output is correct
7 Correct 780 ms 107600 KB Output is correct
8 Correct 792 ms 108408 KB Output is correct
9 Correct 805 ms 107944 KB Output is correct
10 Correct 769 ms 105844 KB Output is correct
11 Correct 780 ms 107948 KB Output is correct
12 Correct 755 ms 105852 KB Output is correct
13 Correct 805 ms 107656 KB Output is correct
14 Correct 782 ms 107616 KB Output is correct
15 Correct 815 ms 107824 KB Output is correct
16 Correct 768 ms 106952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 460 KB Output is correct
2 Correct 783 ms 100240 KB Output is correct
3 Correct 766 ms 99588 KB Output is correct
4 Correct 780 ms 102604 KB Output is correct
5 Correct 763 ms 100420 KB Output is correct
6 Correct 790 ms 100988 KB Output is correct
7 Correct 778 ms 101468 KB Output is correct
8 Correct 780 ms 100404 KB Output is correct
9 Correct 784 ms 99604 KB Output is correct
10 Correct 771 ms 99064 KB Output is correct
11 Correct 806 ms 102348 KB Output is correct
12 Correct 851 ms 101732 KB Output is correct
13 Correct 846 ms 101904 KB Output is correct
14 Correct 779 ms 100012 KB Output is correct
15 Correct 807 ms 102204 KB Output is correct
16 Correct 802 ms 101636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 697 ms 101844 KB Output is correct
2 Correct 713 ms 102840 KB Output is correct
3 Correct 734 ms 104780 KB Output is correct
4 Correct 749 ms 101700 KB Output is correct
5 Correct 733 ms 102404 KB Output is correct
6 Correct 718 ms 103208 KB Output is correct
7 Correct 728 ms 104492 KB Output is correct
8 Correct 771 ms 103508 KB Output is correct
9 Correct 768 ms 102176 KB Output is correct
10 Correct 714 ms 103668 KB Output is correct
11 Correct 735 ms 102600 KB Output is correct
12 Correct 717 ms 103032 KB Output is correct
13 Correct 719 ms 102616 KB Output is correct
14 Correct 736 ms 102764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 460 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 2 ms 588 KB Output is correct
4 Correct 2 ms 560 KB Output is correct
5 Correct 1 ms 588 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 1 ms 588 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 2 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 2 ms 588 KB Output is correct
27 Correct 1 ms 588 KB Output is correct
28 Correct 1 ms 588 KB Output is correct
29 Correct 1 ms 588 KB Output is correct
30 Correct 1 ms 588 KB Output is correct
31 Correct 1 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
33 Correct 764 ms 101364 KB Output is correct
34 Correct 750 ms 106332 KB Output is correct
35 Correct 785 ms 106628 KB Output is correct
36 Correct 798 ms 108044 KB Output is correct
37 Correct 793 ms 107056 KB Output is correct
38 Correct 780 ms 107600 KB Output is correct
39 Correct 792 ms 108408 KB Output is correct
40 Correct 805 ms 107944 KB Output is correct
41 Correct 769 ms 105844 KB Output is correct
42 Correct 780 ms 107948 KB Output is correct
43 Correct 755 ms 105852 KB Output is correct
44 Correct 805 ms 107656 KB Output is correct
45 Correct 782 ms 107616 KB Output is correct
46 Correct 815 ms 107824 KB Output is correct
47 Correct 768 ms 106952 KB Output is correct
48 Correct 783 ms 100240 KB Output is correct
49 Correct 766 ms 99588 KB Output is correct
50 Correct 780 ms 102604 KB Output is correct
51 Correct 763 ms 100420 KB Output is correct
52 Correct 790 ms 100988 KB Output is correct
53 Correct 778 ms 101468 KB Output is correct
54 Correct 780 ms 100404 KB Output is correct
55 Correct 784 ms 99604 KB Output is correct
56 Correct 771 ms 99064 KB Output is correct
57 Correct 806 ms 102348 KB Output is correct
58 Correct 851 ms 101732 KB Output is correct
59 Correct 846 ms 101904 KB Output is correct
60 Correct 779 ms 100012 KB Output is correct
61 Correct 807 ms 102204 KB Output is correct
62 Correct 802 ms 101636 KB Output is correct
63 Correct 697 ms 101844 KB Output is correct
64 Correct 713 ms 102840 KB Output is correct
65 Correct 734 ms 104780 KB Output is correct
66 Correct 749 ms 101700 KB Output is correct
67 Correct 733 ms 102404 KB Output is correct
68 Correct 718 ms 103208 KB Output is correct
69 Correct 728 ms 104492 KB Output is correct
70 Correct 771 ms 103508 KB Output is correct
71 Correct 768 ms 102176 KB Output is correct
72 Correct 714 ms 103668 KB Output is correct
73 Correct 735 ms 102600 KB Output is correct
74 Correct 717 ms 103032 KB Output is correct
75 Correct 719 ms 102616 KB Output is correct
76 Correct 736 ms 102764 KB Output is correct
77 Correct 797 ms 105628 KB Output is correct
78 Correct 804 ms 106488 KB Output is correct
79 Correct 800 ms 106956 KB Output is correct
80 Correct 802 ms 105240 KB Output is correct
81 Correct 768 ms 105300 KB Output is correct
82 Correct 777 ms 106360 KB Output is correct
83 Correct 769 ms 105948 KB Output is correct
84 Correct 768 ms 104744 KB Output is correct
85 Correct 791 ms 107096 KB Output is correct
86 Correct 781 ms 105340 KB Output is correct
87 Correct 780 ms 106828 KB Output is correct
88 Correct 789 ms 107132 KB Output is correct
89 Correct 757 ms 104692 KB Output is correct
90 Correct 783 ms 106996 KB Output is correct
91 Correct 758 ms 105496 KB Output is correct
92 Correct 763 ms 104560 KB Output is correct
93 Correct 778 ms 106048 KB Output is correct
94 Correct 780 ms 107564 KB Output is correct
95 Correct 792 ms 107084 KB Output is correct
96 Correct 772 ms 105856 KB Output is correct
97 Correct 774 ms 105820 KB Output is correct
98 Correct 791 ms 105068 KB Output is correct
99 Correct 788 ms 106004 KB Output is correct
100 Correct 816 ms 106568 KB Output is correct
101 Correct 778 ms 105480 KB Output is correct
102 Correct 812 ms 106848 KB Output is correct