Submission #451941

# Submission time Handle Problem Language Result Execution time Memory
451941 2021-08-03T13:37:13 Z mjhmjh1104 Fire (JOI20_ho_t5) C++17
1 / 100
1000 ms 101836 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")

#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;
        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;
        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:153:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |     scanf("%d%d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:154:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  154 |     for (int i = 0; i < n; i++) scanf("%d", a + i);
      |                                 ~~~~~^~~~~~~~~~~~~
ho_t5.cpp:176:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  176 |         scanf("%d%d%d", &t, &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 460 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 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 2 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 1 ms 600 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 1 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 596 KB Output is correct
31 Correct 1 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 460 KB Output is correct
2 Execution timed out 1042 ms 101300 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 460 KB Output is correct
2 Execution timed out 1048 ms 100248 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1002 ms 101836 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 460 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 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 2 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 1 ms 600 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 1 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 596 KB Output is correct
31 Correct 1 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
33 Execution timed out 1042 ms 101300 KB Time limit exceeded
34 Halted 0 ms 0 KB -