답안 #963812

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963812 2024-04-15T18:23:34 Z EJIC_B_KEDAX Fire (JOI20_ho_t5) C++17
1 / 100
145 ms 262144 KB
#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>

#ifndef LOCAL
    // #pragma GCC optimize("O3")
    // #pragma GCC optimize("Ofast")
    #pragma GCC optimize("unroll-loops")
    #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

mt19937 mt(123);

void solve();
void init();

int32_t main() {
#ifndef LOCAL
    cin.tie(nullptr)->sync_with_stdio(false);
#endif
    cout << fixed << setprecision(30);
    init();
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

void init() {}

const int N = 200200, K = 400;
int a[N];
ll sm[K][N];

void solve() {
    int n, q;
    cin >> n >> q;
    vector<pair<int, pair<int, int>>> st;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        sm[0][i + 1] = a[i];
        if (st.empty() || a[i] > st.back().x) {
            st.emplace_back(a[i], make_pair(i, 0));
        }
    }
    vector<pair<ll, int>> good(1, {0, -1});
    vector<pair<int, int>> bad;
    for (int i = 0; i < st.size(); i++) {
        if (i + 1 < st.size()) {
            st[i].y.y = st[i + 1].y.x - st[i].y.x;
        } else {
            st[i].y.y = n - st[i].y.x;
        }
        if (st[i].y.y < K) {
            good.emplace_back(1ll * st[i].y.y * st[i].x, st[i].y.x);
        } else {
            bad.push_back(st[i].y);
        }
    }
    for (int i = 1; i < good.size(); i++) {
        good[i].x += good[i - 1].x;
    }
    for (int l = 1; l < K; l++) {
        for (int i = 1; i <= n; i++) {
            sm[l][i] = max(sm[l - 1][i], sm[l - 1][i - 1]);
        }
    }
    for (int l = 0; l < K; l++) {
        for (int i = 1; i <= n; i++) {
            sm[l][i] += sm[l][i - 1];
        }
    }
    while (q--) {
        int t, l, r;
        cin >> t >> l >> r; l--; r--;
        if (t < K) {
            cout << sm[t][r + 1] - sm[t][l] << '\n';
            continue;
        }
        ll res = 0;
        int left = 0, right = good.size();
        while (right - left > 1) {
            int mid = (right + left) / 2;
            if (good[mid].y >= l) {
                right = mid;
            } else {
                left = mid;
            }
        }
        int lg = right - 1;
        left = 0, right = good.size();
        while (right - left > 1) {
            int mid = (right + left) / 2;
            if (good[mid].y > r) {
                right = mid;
            } else {
                left = mid;
            }
        }
        int rg = left - 1;
        if (lg < rg) {
            res += good[rg].x - good[lg].x;
        }
        for (int i = 0; i < bad.size(); i++) {
            lg = bad[i].x;
            rg = bad[i].x + bad[i].y - 1;
            int mg = bad[i].x + t;
            if (mg >= rg) {
                lg = max(lg, l);
                rg = min(rg, r);
                if (lg <= rg) {
                    res += 1ll * (rg - lg + 1) * a[bad[i].x];
                }
            } else {
                int trg = rg;
                lg = max(lg, l);
                rg = min(mg, r);
                if (lg <= rg) {
                    res += 1ll * (rg - lg + 1) * a[bad[i].x];
                }
                lg = max(mg + 1, l);
                rg = min(trg, r);
                if (lg <= rg) {
                    res += sm[0][rg + 1] - sm[0][lg];
                }
            }
        }
        cout << res << '\n';
    }
}

Compilation message

ho_t5.cpp: In function 'void solve()':
ho_t5.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int i = 0; i < st.size(); i++) {
      |                     ~~^~~~~~~~~~~
ho_t5.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         if (i + 1 < st.size()) {
      |             ~~~~~~^~~~~~~~~~~
ho_t5.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |     for (int i = 1; i < good.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
ho_t5.cpp:113:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |         for (int i = 0; i < bad.size(); i++) {
      |                         ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 89028 KB Output is correct
2 Correct 12 ms 87388 KB Output is correct
3 Correct 12 ms 87384 KB Output is correct
4 Correct 11 ms 89436 KB Output is correct
5 Correct 11 ms 87332 KB Output is correct
6 Correct 11 ms 89436 KB Output is correct
7 Correct 11 ms 89436 KB Output is correct
8 Correct 11 ms 87388 KB Output is correct
9 Correct 11 ms 89436 KB Output is correct
10 Correct 11 ms 89436 KB Output is correct
11 Correct 11 ms 87412 KB Output is correct
12 Correct 11 ms 87512 KB Output is correct
13 Correct 11 ms 87396 KB Output is correct
14 Correct 11 ms 87384 KB Output is correct
15 Correct 11 ms 89436 KB Output is correct
16 Correct 11 ms 89432 KB Output is correct
17 Correct 11 ms 87332 KB Output is correct
18 Correct 12 ms 87452 KB Output is correct
19 Correct 12 ms 87392 KB Output is correct
20 Correct 11 ms 87388 KB Output is correct
21 Correct 10 ms 87388 KB Output is correct
22 Correct 11 ms 87388 KB Output is correct
23 Correct 10 ms 87352 KB Output is correct
24 Correct 11 ms 89432 KB Output is correct
25 Correct 12 ms 87308 KB Output is correct
26 Correct 11 ms 87388 KB Output is correct
27 Correct 12 ms 87388 KB Output is correct
28 Correct 11 ms 87424 KB Output is correct
29 Correct 11 ms 89388 KB Output is correct
30 Correct 11 ms 87388 KB Output is correct
31 Correct 11 ms 89692 KB Output is correct
32 Correct 11 ms 87388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 89028 KB Output is correct
2 Runtime error 145 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 89028 KB Output is correct
2 Runtime error 120 ms 262144 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 113 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 89028 KB Output is correct
2 Correct 12 ms 87388 KB Output is correct
3 Correct 12 ms 87384 KB Output is correct
4 Correct 11 ms 89436 KB Output is correct
5 Correct 11 ms 87332 KB Output is correct
6 Correct 11 ms 89436 KB Output is correct
7 Correct 11 ms 89436 KB Output is correct
8 Correct 11 ms 87388 KB Output is correct
9 Correct 11 ms 89436 KB Output is correct
10 Correct 11 ms 89436 KB Output is correct
11 Correct 11 ms 87412 KB Output is correct
12 Correct 11 ms 87512 KB Output is correct
13 Correct 11 ms 87396 KB Output is correct
14 Correct 11 ms 87384 KB Output is correct
15 Correct 11 ms 89436 KB Output is correct
16 Correct 11 ms 89432 KB Output is correct
17 Correct 11 ms 87332 KB Output is correct
18 Correct 12 ms 87452 KB Output is correct
19 Correct 12 ms 87392 KB Output is correct
20 Correct 11 ms 87388 KB Output is correct
21 Correct 10 ms 87388 KB Output is correct
22 Correct 11 ms 87388 KB Output is correct
23 Correct 10 ms 87352 KB Output is correct
24 Correct 11 ms 89432 KB Output is correct
25 Correct 12 ms 87308 KB Output is correct
26 Correct 11 ms 87388 KB Output is correct
27 Correct 12 ms 87388 KB Output is correct
28 Correct 11 ms 87424 KB Output is correct
29 Correct 11 ms 89388 KB Output is correct
30 Correct 11 ms 87388 KB Output is correct
31 Correct 11 ms 89692 KB Output is correct
32 Correct 11 ms 87388 KB Output is correct
33 Runtime error 145 ms 262144 KB Execution killed with signal 9
34 Halted 0 ms 0 KB -