답안 #836349

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
836349 2023-08-24T10:26:59 Z green_gold_dog Fire (JOI20_ho_t5) C++17
100 / 100
462 ms 106340 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;

constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;

random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());

template<typename T>
bool assign_max(T& a, T b) {
        if (b > a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
bool assign_min(T& a, T b) {
        if (b < a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
T square(T a) {
        return a * a;
}

template<>
struct std::hash<pair<ll, ll>> {
        ll operator() (pair<ll, ll> p) const {
                return ((__int128)p.first * MOD + p.second) % INF64;
        }
};

struct sparce_table {
        vector<vector<pair<ll, ll>>> arr;
        vector<ll> lg;
        sparce_table(vector<ll> a) {
                lg.push_back(0);
                while (lg.size() <= a.size()) {
                        lg.push_back(lg.back());
                        if ((1 << (lg.back() + 1)) == lg.size() - 1) {
                                lg.back()++;
                        }
                }
                arr.resize(lg.back() + 1, vector<pair<ll, ll>>(a.size()));
                for (ll i = 0; i < a.size(); i++) {
                        arr[0][i] = make_pair(a[i], i);
                }
                for (ll i = 1; i <= lg.back(); i++) {
                        for (ll j = 0; j + (1 << (i - 1)) < a.size(); j++) {
                                arr[i][j] = max(arr[i - 1][j], arr[i - 1][j + (1 << (i - 1))]);
                        }
                }
        }
        pair<ll, ll> get(ll l, ll r) {
                return max(arr[lg[r - l]][l], arr[lg[r - l]][r - (1 << lg[r - l])]);
        }
};

struct BIT {
        vector<ll> tree;
        BIT(ll n) {
                tree.resize(n + 1, 0);
        }
        void add(ll x, ll y) {
                x++;
                for (ll i = x; i < tree.size(); i += i & -i) {
                        tree[i] += y;
                }
        }
        ll get(ll x) {
                ll ans = 0;
                for (ll i = x; i > 0; i -= i & -i) {
                        ans += tree[i];
                }
                return ans;
        }
        ll get(ll l, ll r) {
                return get(r) - get(l);
        }
};

void solve() {
        ll n, q;
        cin >> n >> q;
        vector<ll> arr(n);
        for (ll i = 0; i < n; i++) {
                cin >> arr[i];
        }
        sparce_table st(arr);
        BIT b(n), a(n);
        for (ll i = 0; i < n; i++) {
                b.add(i, arr[i]);
        }
        vector<vector<pair<ll, ll>>> s(n + 1);
        for (ll i = 0; i < n; i++) {
                s[0].emplace_back(i, arr[i]);
                ll l = i, r = n;
                while (r - l > 1) {
                        ll mid = (l + r) / 2;
                        if (st.get(i, mid + 1).second == i) {
                                l = mid;
                        } else {
                                r = mid;
                        }
                }
                s[l - i].emplace_back(i, -arr[i]);
                if (st.get(0, i + 1).second != i) {
                        ll nl = 0, nr = i;
                        while (nr - nl > 1) {
                                ll mid = (nl + nr) / 2;
                                if (st.get(mid, i + 1).second == i) {
                                        nr = mid;
                                } else {
                                        nl = mid;
                                }
                        }
                        s[i - nr].emplace_back(i, -arr[i]);
                        s[l - nl].emplace_back(i, arr[i]);
                }
        }
        vector<vector<tuple<ll, ll, ll>>> qs(n + 1);
        for (ll i = 0; i < q; i++) {
                ll t, l, r;
                cin >> t >> l >> r;
                l--;
                qs[t].emplace_back(l, r, i);
        }
        vector<ll> ans(q);
        for (ll i = 0; i <= n; i++) {
                for (auto[j, k] : s[i]) {
                        b.add(j, -k*i);
                        a.add(j, k);
                }
                for (auto[l, r, j] : qs[i]) {
                        ll nr = st.get(max(0ll, r - i - 1), r).second;
                        ll sl = -1, sr = r - 1;
                        while (sr - sl > 1) {
                                ll mid = (sl + sr) / 2;
                                if (st.get(max(0ll, mid - i), mid + 1).second == nr) {
                                        sr = mid;
                                } else {
                                        sl = mid;
                                }
                        }
                        ll nl = st.get(max(0ll, l - i), l + 1).second;
                        ll fl = -1, fr = l;
                        while (fr - fl > 1) {
                                ll mid = (fl + fr) / 2;
                                if (st.get(max(0ll, mid - i), mid + 1).second == nl) {
                                        fr = mid;
                                } else {
                                        fl = mid;
                                }
                        }
                        ans[j] = b.get(nl, nr) + a.get(nl, nr) * i + arr[nr] * (r - sr) - arr[nl] * (l - fr);
                }
        }
        for (auto i : ans) {
                cout << i << '\n';
        }
}

int main() {
        if (IS_FILE) {
                freopen("", "r", stdin);
                freopen("", "w", stdout);
        }
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        ll t = 1;
        if (IS_TEST_CASES) {
                cin >> t;
        }
        for (ll i = 0; i < t; i++) {
                solve();
        }
}

Compilation message

ho_t5.cpp: In constructor 'sparce_table::sparce_table(std::vector<long long int>)':
ho_t5.cpp:57:52: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |                         if ((1 << (lg.back() + 1)) == lg.size() - 1) {
      |                             ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:62:34: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |                 for (ll i = 0; i < a.size(); i++) {
      |                                ~~^~~~~~~~~~
ho_t5.cpp:66:59: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |                         for (ll j = 0; j + (1 << (i - 1)) < a.size(); j++) {
      |                                        ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
ho_t5.cpp: In member function 'void BIT::add(ll, ll)':
ho_t5.cpp:83:34: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |                 for (ll i = x; i < tree.size(); i += i & -i) {
      |                                ~~^~~~~~~~~~~~~
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:182:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  182 |                 freopen("", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~
ho_t5.cpp:183:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  183 |                 freopen("", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 404 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 376 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 370 ms 98092 KB Output is correct
3 Correct 378 ms 102180 KB Output is correct
4 Correct 368 ms 103216 KB Output is correct
5 Correct 406 ms 106340 KB Output is correct
6 Correct 347 ms 103800 KB Output is correct
7 Correct 383 ms 104240 KB Output is correct
8 Correct 391 ms 105856 KB Output is correct
9 Correct 363 ms 105136 KB Output is correct
10 Correct 383 ms 101732 KB Output is correct
11 Correct 426 ms 105564 KB Output is correct
12 Correct 359 ms 102388 KB Output is correct
13 Correct 385 ms 105900 KB Output is correct
14 Correct 408 ms 106088 KB Output is correct
15 Correct 425 ms 105968 KB Output is correct
16 Correct 387 ms 103740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 417 ms 94320 KB Output is correct
3 Correct 397 ms 92668 KB Output is correct
4 Correct 428 ms 97960 KB Output is correct
5 Correct 414 ms 95444 KB Output is correct
6 Correct 423 ms 96896 KB Output is correct
7 Correct 408 ms 97460 KB Output is correct
8 Correct 462 ms 96388 KB Output is correct
9 Correct 423 ms 96284 KB Output is correct
10 Correct 420 ms 95432 KB Output is correct
11 Correct 428 ms 99900 KB Output is correct
12 Correct 411 ms 100316 KB Output is correct
13 Correct 426 ms 99868 KB Output is correct
14 Correct 403 ms 97060 KB Output is correct
15 Correct 390 ms 100332 KB Output is correct
16 Correct 380 ms 99784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 411 ms 92216 KB Output is correct
2 Correct 391 ms 92780 KB Output is correct
3 Correct 418 ms 94972 KB Output is correct
4 Correct 406 ms 92408 KB Output is correct
5 Correct 420 ms 92360 KB Output is correct
6 Correct 420 ms 93020 KB Output is correct
7 Correct 412 ms 94612 KB Output is correct
8 Correct 404 ms 93528 KB Output is correct
9 Correct 399 ms 92408 KB Output is correct
10 Correct 431 ms 94384 KB Output is correct
11 Correct 442 ms 92812 KB Output is correct
12 Correct 427 ms 93208 KB Output is correct
13 Correct 415 ms 93292 KB Output is correct
14 Correct 419 ms 93012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 404 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 376 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 370 ms 98092 KB Output is correct
34 Correct 378 ms 102180 KB Output is correct
35 Correct 368 ms 103216 KB Output is correct
36 Correct 406 ms 106340 KB Output is correct
37 Correct 347 ms 103800 KB Output is correct
38 Correct 383 ms 104240 KB Output is correct
39 Correct 391 ms 105856 KB Output is correct
40 Correct 363 ms 105136 KB Output is correct
41 Correct 383 ms 101732 KB Output is correct
42 Correct 426 ms 105564 KB Output is correct
43 Correct 359 ms 102388 KB Output is correct
44 Correct 385 ms 105900 KB Output is correct
45 Correct 408 ms 106088 KB Output is correct
46 Correct 425 ms 105968 KB Output is correct
47 Correct 387 ms 103740 KB Output is correct
48 Correct 417 ms 94320 KB Output is correct
49 Correct 397 ms 92668 KB Output is correct
50 Correct 428 ms 97960 KB Output is correct
51 Correct 414 ms 95444 KB Output is correct
52 Correct 423 ms 96896 KB Output is correct
53 Correct 408 ms 97460 KB Output is correct
54 Correct 462 ms 96388 KB Output is correct
55 Correct 423 ms 96284 KB Output is correct
56 Correct 420 ms 95432 KB Output is correct
57 Correct 428 ms 99900 KB Output is correct
58 Correct 411 ms 100316 KB Output is correct
59 Correct 426 ms 99868 KB Output is correct
60 Correct 403 ms 97060 KB Output is correct
61 Correct 390 ms 100332 KB Output is correct
62 Correct 380 ms 99784 KB Output is correct
63 Correct 411 ms 92216 KB Output is correct
64 Correct 391 ms 92780 KB Output is correct
65 Correct 418 ms 94972 KB Output is correct
66 Correct 406 ms 92408 KB Output is correct
67 Correct 420 ms 92360 KB Output is correct
68 Correct 420 ms 93020 KB Output is correct
69 Correct 412 ms 94612 KB Output is correct
70 Correct 404 ms 93528 KB Output is correct
71 Correct 399 ms 92408 KB Output is correct
72 Correct 431 ms 94384 KB Output is correct
73 Correct 442 ms 92812 KB Output is correct
74 Correct 427 ms 93208 KB Output is correct
75 Correct 415 ms 93292 KB Output is correct
76 Correct 419 ms 93012 KB Output is correct
77 Correct 420 ms 100204 KB Output is correct
78 Correct 415 ms 100776 KB Output is correct
79 Correct 402 ms 101884 KB Output is correct
80 Correct 402 ms 99832 KB Output is correct
81 Correct 383 ms 99212 KB Output is correct
82 Correct 449 ms 101012 KB Output is correct
83 Correct 406 ms 100080 KB Output is correct
84 Correct 408 ms 99148 KB Output is correct
85 Correct 408 ms 102324 KB Output is correct
86 Correct 403 ms 99688 KB Output is correct
87 Correct 385 ms 103376 KB Output is correct
88 Correct 395 ms 103840 KB Output is correct
89 Correct 389 ms 100028 KB Output is correct
90 Correct 370 ms 103792 KB Output is correct
91 Correct 364 ms 101216 KB Output is correct
92 Correct 359 ms 99792 KB Output is correct
93 Correct 347 ms 101724 KB Output is correct
94 Correct 388 ms 104536 KB Output is correct
95 Correct 389 ms 103872 KB Output is correct
96 Correct 376 ms 101688 KB Output is correct
97 Correct 385 ms 101624 KB Output is correct
98 Correct 377 ms 100376 KB Output is correct
99 Correct 385 ms 101696 KB Output is correct
100 Correct 384 ms 102116 KB Output is correct
101 Correct 379 ms 100580 KB Output is correct
102 Correct 394 ms 103192 KB Output is correct