답안 #653101

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
653101 2022-10-25T17:21:44 Z someone Fire (JOI20_ho_t5) C++14
100 / 100
587 ms 84312 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

struct Query {
    bool isModif;
    int temps, deb, fin, id, modif;
};

const int M = 1 << 18, N = 2 * M, T = 18, INF = 1e18 + 42;

vector<Query> req;
vector<int> getId[M];
int n, q, print[M], pre[M], aft[M], val[M], a[N], b[N], id[M][2];

void update(int i, int upd, int t) {
    i += M;
    a[i] += upd;
    b[i] -= (t-1) * upd;
    while(i) {
        i >>= 1;
        a[i] = (a[i * 2] + a[i * 2 + 1]);
        b[i] = (b[i * 2] + b[i * 2 + 1]);
    }
}

int getSum(int i, int deb, int fin, int l, int r, int t) {
    if(fin <= l || r <= deb)
        return 0;
    if(l <= deb && fin <= r)
        return a[i] * t + b[i];
    int mid = (deb + fin) / 2;
    return getSum(i*2, deb, mid, l, r, t) + getSum(i*2+1, mid, fin, l, r, t);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n >> q;
    for(int i = 0; i < n; i++)
        cin >> val[i];
    for(int i = M; i < N; i++)
        a[i] = b[i] = val[i - M];
    for(int i = M-1; i > 0; i--)
        a[i] = b[i] = (a[i * 2] + a[i * 2 + 1]);
    for(int i = 0; i < q; i++) {
        int deb, fin, t;
        cin >> t >> deb >> fin;
        deb--, fin--;
        getId[deb].push_back(i);
        getId[fin].push_back(i);
        id[i][0] = id[i][1] = -1;
        req.push_back({false, t, deb, fin, i, -1});
    }
    deque<int> imax;
    for(int i = 0; i < n; i++) {
        while(!imax.empty() && val[imax[0]] < val[i])
            imax.pop_front();
        if(imax.empty())
            pre[i] = -INF;
        else
            pre[i] = imax[0];
        imax.push_front(i);
        for(int j : getId[i]) {
            int t = req[j].temps, ans = -1;
            for(int k = T-1; k > -1; k--) {
                int x = ans + (1 << k);
                if(x < (int)imax.size() && i - pre[imax[x]] <= t)
                    ans = x;
            }
            ans = imax[ans+1];
            if(id[j][0] == -1)
                id[j][0] = ans;
            else
                id[j][1] = ans;
        }
    }
    imax.clear();
    for(int i = n-1; i > -1; i--) {
        while(!imax.empty() && val[imax.back()] <= val[i])
            imax.pop_back();
        if(imax.empty())
            aft[i] = INF;
        else
            aft[i] = imax.back();
        imax.push_back(i);
    }
    for(int i = 0; i < n; i++) {
        req.push_back({true, i - pre[i], -1, -1, i, -val[i]});
        req.push_back({true, aft[i] - i, -1, -1, i, -val[i]});
        req.push_back({true, aft[i] - pre[i], -1, -1, i, val[i]});
    }
    sort(req.begin(), req.end(),
    [](Query a, Query b) {
        if(a.temps == b.temps)
            return a.isModif && !b.isModif;
        return a.temps < b.temps;
    });
    for(Query q : req) {
        if(q.isModif) {
            update(q.id, q.modif, q.temps);
        } else {
            int deb = id[q.id][0],
                fin = id[q.id][1],
                ans = getSum(1, 0, M, deb+1, fin, q.temps);
            
            q.fin++;
            
            int l = max(q.deb, max(pre[deb] + q.temps + 1, deb)),
                r = min(deb + q.temps + 1, min(aft[deb], q.fin));
            if(l < r)
                ans += val[deb] * (r - l);
            if(deb != fin) {
                l = max(q.deb, max(pre[fin] + q.temps + 1, fin)),
                r = min(fin + q.temps + 1, min(aft[fin], q.fin));
                if(l < r)
                    ans += val[fin] * (r - l);
            }
            print[q.id] = ans;
        }
    }
    for(int i = 0; i < q; i++)
        cout << print[i] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14676 KB Output is correct
2 Correct 10 ms 14808 KB Output is correct
3 Correct 9 ms 14804 KB Output is correct
4 Correct 8 ms 14804 KB Output is correct
5 Correct 8 ms 14804 KB Output is correct
6 Correct 9 ms 14764 KB Output is correct
7 Correct 8 ms 14804 KB Output is correct
8 Correct 11 ms 14804 KB Output is correct
9 Correct 8 ms 14804 KB Output is correct
10 Correct 10 ms 14804 KB Output is correct
11 Correct 8 ms 14804 KB Output is correct
12 Correct 8 ms 14804 KB Output is correct
13 Correct 8 ms 14804 KB Output is correct
14 Correct 8 ms 14804 KB Output is correct
15 Correct 8 ms 14804 KB Output is correct
16 Correct 9 ms 14708 KB Output is correct
17 Correct 8 ms 14804 KB Output is correct
18 Correct 10 ms 14804 KB Output is correct
19 Correct 8 ms 14716 KB Output is correct
20 Correct 10 ms 14804 KB Output is correct
21 Correct 8 ms 14788 KB Output is correct
22 Correct 9 ms 14804 KB Output is correct
23 Correct 8 ms 14804 KB Output is correct
24 Correct 8 ms 14724 KB Output is correct
25 Correct 8 ms 14804 KB Output is correct
26 Correct 10 ms 14804 KB Output is correct
27 Correct 8 ms 14808 KB Output is correct
28 Correct 8 ms 14804 KB Output is correct
29 Correct 8 ms 14804 KB Output is correct
30 Correct 8 ms 14800 KB Output is correct
31 Correct 8 ms 14804 KB Output is correct
32 Correct 9 ms 14804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14676 KB Output is correct
2 Correct 412 ms 83864 KB Output is correct
3 Correct 417 ms 84024 KB Output is correct
4 Correct 450 ms 83928 KB Output is correct
5 Correct 405 ms 83876 KB Output is correct
6 Correct 428 ms 83520 KB Output is correct
7 Correct 415 ms 84056 KB Output is correct
8 Correct 397 ms 83940 KB Output is correct
9 Correct 415 ms 84056 KB Output is correct
10 Correct 386 ms 83880 KB Output is correct
11 Correct 428 ms 84296 KB Output is correct
12 Correct 409 ms 83672 KB Output is correct
13 Correct 463 ms 83740 KB Output is correct
14 Correct 431 ms 83564 KB Output is correct
15 Correct 420 ms 83776 KB Output is correct
16 Correct 436 ms 83844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14676 KB Output is correct
2 Correct 405 ms 82640 KB Output is correct
3 Correct 357 ms 82136 KB Output is correct
4 Correct 412 ms 82696 KB Output is correct
5 Correct 383 ms 82132 KB Output is correct
6 Correct 396 ms 82296 KB Output is correct
7 Correct 378 ms 82400 KB Output is correct
8 Correct 389 ms 82628 KB Output is correct
9 Correct 374 ms 82332 KB Output is correct
10 Correct 403 ms 82008 KB Output is correct
11 Correct 381 ms 82688 KB Output is correct
12 Correct 387 ms 82188 KB Output is correct
13 Correct 403 ms 82584 KB Output is correct
14 Correct 375 ms 82300 KB Output is correct
15 Correct 380 ms 82448 KB Output is correct
16 Correct 430 ms 82304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 576 ms 82864 KB Output is correct
2 Correct 547 ms 82936 KB Output is correct
3 Correct 587 ms 83420 KB Output is correct
4 Correct 549 ms 83176 KB Output is correct
5 Correct 553 ms 83188 KB Output is correct
6 Correct 554 ms 82908 KB Output is correct
7 Correct 587 ms 83180 KB Output is correct
8 Correct 546 ms 83244 KB Output is correct
9 Correct 582 ms 83248 KB Output is correct
10 Correct 532 ms 83032 KB Output is correct
11 Correct 566 ms 83340 KB Output is correct
12 Correct 530 ms 83264 KB Output is correct
13 Correct 550 ms 83248 KB Output is correct
14 Correct 518 ms 83324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14676 KB Output is correct
2 Correct 10 ms 14808 KB Output is correct
3 Correct 9 ms 14804 KB Output is correct
4 Correct 8 ms 14804 KB Output is correct
5 Correct 8 ms 14804 KB Output is correct
6 Correct 9 ms 14764 KB Output is correct
7 Correct 8 ms 14804 KB Output is correct
8 Correct 11 ms 14804 KB Output is correct
9 Correct 8 ms 14804 KB Output is correct
10 Correct 10 ms 14804 KB Output is correct
11 Correct 8 ms 14804 KB Output is correct
12 Correct 8 ms 14804 KB Output is correct
13 Correct 8 ms 14804 KB Output is correct
14 Correct 8 ms 14804 KB Output is correct
15 Correct 8 ms 14804 KB Output is correct
16 Correct 9 ms 14708 KB Output is correct
17 Correct 8 ms 14804 KB Output is correct
18 Correct 10 ms 14804 KB Output is correct
19 Correct 8 ms 14716 KB Output is correct
20 Correct 10 ms 14804 KB Output is correct
21 Correct 8 ms 14788 KB Output is correct
22 Correct 9 ms 14804 KB Output is correct
23 Correct 8 ms 14804 KB Output is correct
24 Correct 8 ms 14724 KB Output is correct
25 Correct 8 ms 14804 KB Output is correct
26 Correct 10 ms 14804 KB Output is correct
27 Correct 8 ms 14808 KB Output is correct
28 Correct 8 ms 14804 KB Output is correct
29 Correct 8 ms 14804 KB Output is correct
30 Correct 8 ms 14800 KB Output is correct
31 Correct 8 ms 14804 KB Output is correct
32 Correct 9 ms 14804 KB Output is correct
33 Correct 412 ms 83864 KB Output is correct
34 Correct 417 ms 84024 KB Output is correct
35 Correct 450 ms 83928 KB Output is correct
36 Correct 405 ms 83876 KB Output is correct
37 Correct 428 ms 83520 KB Output is correct
38 Correct 415 ms 84056 KB Output is correct
39 Correct 397 ms 83940 KB Output is correct
40 Correct 415 ms 84056 KB Output is correct
41 Correct 386 ms 83880 KB Output is correct
42 Correct 428 ms 84296 KB Output is correct
43 Correct 409 ms 83672 KB Output is correct
44 Correct 463 ms 83740 KB Output is correct
45 Correct 431 ms 83564 KB Output is correct
46 Correct 420 ms 83776 KB Output is correct
47 Correct 436 ms 83844 KB Output is correct
48 Correct 405 ms 82640 KB Output is correct
49 Correct 357 ms 82136 KB Output is correct
50 Correct 412 ms 82696 KB Output is correct
51 Correct 383 ms 82132 KB Output is correct
52 Correct 396 ms 82296 KB Output is correct
53 Correct 378 ms 82400 KB Output is correct
54 Correct 389 ms 82628 KB Output is correct
55 Correct 374 ms 82332 KB Output is correct
56 Correct 403 ms 82008 KB Output is correct
57 Correct 381 ms 82688 KB Output is correct
58 Correct 387 ms 82188 KB Output is correct
59 Correct 403 ms 82584 KB Output is correct
60 Correct 375 ms 82300 KB Output is correct
61 Correct 380 ms 82448 KB Output is correct
62 Correct 430 ms 82304 KB Output is correct
63 Correct 576 ms 82864 KB Output is correct
64 Correct 547 ms 82936 KB Output is correct
65 Correct 587 ms 83420 KB Output is correct
66 Correct 549 ms 83176 KB Output is correct
67 Correct 553 ms 83188 KB Output is correct
68 Correct 554 ms 82908 KB Output is correct
69 Correct 587 ms 83180 KB Output is correct
70 Correct 546 ms 83244 KB Output is correct
71 Correct 582 ms 83248 KB Output is correct
72 Correct 532 ms 83032 KB Output is correct
73 Correct 566 ms 83340 KB Output is correct
74 Correct 530 ms 83264 KB Output is correct
75 Correct 550 ms 83248 KB Output is correct
76 Correct 518 ms 83324 KB Output is correct
77 Correct 486 ms 83776 KB Output is correct
78 Correct 457 ms 84244 KB Output is correct
79 Correct 399 ms 83828 KB Output is correct
80 Correct 395 ms 83896 KB Output is correct
81 Correct 467 ms 83836 KB Output is correct
82 Correct 421 ms 83928 KB Output is correct
83 Correct 492 ms 83756 KB Output is correct
84 Correct 547 ms 83936 KB Output is correct
85 Correct 532 ms 83760 KB Output is correct
86 Correct 542 ms 83928 KB Output is correct
87 Correct 410 ms 84312 KB Output is correct
88 Correct 418 ms 84020 KB Output is correct
89 Correct 422 ms 83700 KB Output is correct
90 Correct 439 ms 83988 KB Output is correct
91 Correct 417 ms 83668 KB Output is correct
92 Correct 397 ms 83656 KB Output is correct
93 Correct 462 ms 83748 KB Output is correct
94 Correct 434 ms 84292 KB Output is correct
95 Correct 396 ms 84236 KB Output is correct
96 Correct 449 ms 83928 KB Output is correct
97 Correct 422 ms 83940 KB Output is correct
98 Correct 461 ms 83344 KB Output is correct
99 Correct 462 ms 83440 KB Output is correct
100 Correct 469 ms 83692 KB Output is correct
101 Correct 433 ms 83416 KB Output is correct
102 Correct 458 ms 83928 KB Output is correct