답안 #515928

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
515928 2022-01-20T06:42:31 Z Jarif_Rahman Fire (JOI20_ho_t5) C++17
100 / 100
563 ms 45760 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

struct BIT{
    int n;
    vector<ll> sm;
    BIT(int _n){
        n = _n;
        sm.resize(n);
    }
    void add(int in, ll x){
        in++;
        while(in <= n) sm[in-1]+=x, in+=in&-in;
    }
    ll sum(int in){
        in++;
        ll s = 0;
        while(in >= 1) s+=sm[in-1], in-=in&-in;
        return s;
    }
    ll sum(int l, int r){
        return sum(r)-(l == 0? 0:sum(l-1));
    }
    void assign(int in, ll x){
        add(in, x-sum(in, in));
    }
};

struct segtree{
    struct node{
        ll sm, lazy_sm;
        node(){
            sm = 0, lazy_sm = 0;
        }
        node(ll x){
            sm = x, lazy_sm = 0;
        }
        node operator+(node a){
            node rt;
            rt.sm = sm+a.sm;
            return rt;
        }
        void pushdown_from(node a, int sz){
            sm+=a.lazy_sm*sz;
            lazy_sm+=a.lazy_sm;
        }
    };
    int k;
    vector<node> v;
    segtree(int n){
        k = 1;
        while(k < n) k*=2;
        k*=2;
        v.resize(k, node());
    }
    void add(int l, int r, int nd, int a, int b, ll x){
        if(a > r || b < l) return;
        if(a >= l && b <= r){
            v[nd].sm+=(b-a+1)*x;
            v[nd].lazy_sm+=x;
            return;
        }
        int md = (a+b)/2;
        v[2*nd].pushdown_from(v[nd], md-a+1);
        v[2*nd+1].pushdown_from(v[nd], b-md);
        v[nd].lazy_sm = 0;
        add(l, r, 2*nd, a, md, x);
        add(l, r, 2*nd+1, md+1, b, x);
        v[nd] = v[2*nd]+v[2*nd+1];
    }
    void add(int l, int r, int x){
        add(l, r, 1, 0, k/2-1, x);
    }
    ll sum(int l, int r, int nd, int a, int b){
        if(a > r || b < l) return 0;
        if(a >= l && b <= r) return v[nd].sm;
        int md = (a+b)/2;
        v[2*nd].pushdown_from(v[nd], md-a+1);
        v[2*nd+1].pushdown_from(v[nd], b-md);
        v[nd].lazy_sm = 0;
        ll rt = sum(l, r, 2*nd, a, md) + sum(l, r, 2*nd+1, md+1, b);
        v[nd] = v[2*nd]+v[2*nd+1];
        return rt;
    }
    ll sum(int l, int r){
        return sum(l, r, 1, 0, k/2-1);
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, q; cin >> n >> q;
    vector<ll> s(n);
    for(ll &x: s) cin >> x;

    vector<int> prev(n, -1);
    stack<int> st;
    for(int i = n-1; i >= 0; i--){
        while(!st.empty() && s[i] > s[st.top()]) prev[st.top()] = i, st.pop();
        st.push(i);
    }
    while(!st.empty()) st.pop();

    vector<vector<int>> end(n+1);
    vector<vector<pair<int, int>>> chng(n+1);
    for(int i = 0; i < n; i++){
        while(!st.empty() && s[st.top()] <= s[i]){
            int x = st.top();
            st.pop();
            end[i-x-1].pb(x);
            if(prev[x] == -1) continue;
            if(s[prev[x]] <= s[i]) continue;
            chng[i-prev[x]-1].pb({prev[x], i});
        }
        st.push(i);
    }
    while(!st.empty()) end[n-st.top()-1].pb(st.top()), st.pop();

    vector<vector<array<int, 3>>> queries(n);
    for(int i = 0; i < q; i++){
        int t, l, r; cin >> t >> l >> r; t--, l--, r--;
        queries[t].pb({l, r, i});
    }

    vector<ll> ans(q, 0);
    segtree ss(n);
    BIT s1(n), s2(n), s3(n);
    for(int i = 0; i < n; i++) ss.add(i, i, s[i]);

    for(int i = 0; i+1 < n; i++){
        s1.assign(i, max(0LL, s[i]-s[i+1]));
        s2.assign(i, max(0LL, s[i]-s[i+1])*i);
        s3.assign(i, max(0LL, s[i]-s[i+1])*(n-1-i));
    }

    for(int t = 1; t <= n; t++){
        for(int in: end[t-1]){
            if(in != n-1) ss.add(in+1, in+t-1, s1.sum(in, in));
            s1.assign(in, 0);
            s2.assign(in, 0);
            s3.assign(in, 0);
        }
        for(auto [in, nxt]: chng[t-1]){
            ll x = s1.sum(in, in)-(s[in]-s[nxt]);
            ss.add(in+1, nxt-1, x);
            s1.assign(in, s[in]-s[nxt]);
            s2.assign(in, (s[in]-s[nxt])*in);
            s3.assign(in, (s[in]-s[nxt])*(n-1-in));
        }
        for(auto [l, r, in]: queries[t-1]){
            ans[in]+=ss.sum(l, r);

            int mx, a, b;
            if(t <= r-l+1){
                mx = t;
                a = l, b = r-mx+1;
                a--, b--;
                a = max(a, 0);
            }
            else{
                mx = r-l+1;
                a = r-t+1, b = l;
                a--, b--;
                a = max(a, 0);
            }
            ans[in]+=s1.sum(a, b)*mx;

            if(mx != 1){
                int aa = max(0, a-mx+1);
                ll c = s2.sum(aa, a-1)+(s1.sum(aa, a-1)*(mx-a));
                ans[in]+=c;

                int bb = min(n-1, b+mx-1);
                c = s3.sum(b+1, bb)+(s1.sum(b+1, bb)*(mx-(n-1-b)));
                ans[in]+=c;
            }
        }
    }

    for(ll x: ans) cout << x << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 502 ms 37724 KB Output is correct
3 Correct 480 ms 43008 KB Output is correct
4 Correct 502 ms 43236 KB Output is correct
5 Correct 519 ms 43880 KB Output is correct
6 Correct 472 ms 43408 KB Output is correct
7 Correct 483 ms 43544 KB Output is correct
8 Correct 466 ms 44004 KB Output is correct
9 Correct 506 ms 43692 KB Output is correct
10 Correct 474 ms 42880 KB Output is correct
11 Correct 508 ms 43944 KB Output is correct
12 Correct 543 ms 42840 KB Output is correct
13 Correct 563 ms 43616 KB Output is correct
14 Correct 547 ms 43512 KB Output is correct
15 Correct 547 ms 43636 KB Output is correct
16 Correct 494 ms 43252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 425 ms 38704 KB Output is correct
3 Correct 496 ms 38240 KB Output is correct
4 Correct 486 ms 39396 KB Output is correct
5 Correct 434 ms 38496 KB Output is correct
6 Correct 417 ms 38784 KB Output is correct
7 Correct 423 ms 38972 KB Output is correct
8 Correct 457 ms 38620 KB Output is correct
9 Correct 468 ms 38336 KB Output is correct
10 Correct 407 ms 38016 KB Output is correct
11 Correct 426 ms 39240 KB Output is correct
12 Correct 442 ms 38984 KB Output is correct
13 Correct 439 ms 39088 KB Output is correct
14 Correct 446 ms 38520 KB Output is correct
15 Correct 478 ms 39184 KB Output is correct
16 Correct 498 ms 38820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 446 ms 37080 KB Output is correct
2 Correct 447 ms 41272 KB Output is correct
3 Correct 454 ms 42092 KB Output is correct
4 Correct 464 ms 41216 KB Output is correct
5 Correct 484 ms 41332 KB Output is correct
6 Correct 484 ms 41492 KB Output is correct
7 Correct 461 ms 41880 KB Output is correct
8 Correct 481 ms 41784 KB Output is correct
9 Correct 542 ms 41344 KB Output is correct
10 Correct 520 ms 41620 KB Output is correct
11 Correct 481 ms 41532 KB Output is correct
12 Correct 471 ms 41604 KB Output is correct
13 Correct 450 ms 41524 KB Output is correct
14 Correct 469 ms 41548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 502 ms 37724 KB Output is correct
34 Correct 480 ms 43008 KB Output is correct
35 Correct 502 ms 43236 KB Output is correct
36 Correct 519 ms 43880 KB Output is correct
37 Correct 472 ms 43408 KB Output is correct
38 Correct 483 ms 43544 KB Output is correct
39 Correct 466 ms 44004 KB Output is correct
40 Correct 506 ms 43692 KB Output is correct
41 Correct 474 ms 42880 KB Output is correct
42 Correct 508 ms 43944 KB Output is correct
43 Correct 543 ms 42840 KB Output is correct
44 Correct 563 ms 43616 KB Output is correct
45 Correct 547 ms 43512 KB Output is correct
46 Correct 547 ms 43636 KB Output is correct
47 Correct 494 ms 43252 KB Output is correct
48 Correct 425 ms 38704 KB Output is correct
49 Correct 496 ms 38240 KB Output is correct
50 Correct 486 ms 39396 KB Output is correct
51 Correct 434 ms 38496 KB Output is correct
52 Correct 417 ms 38784 KB Output is correct
53 Correct 423 ms 38972 KB Output is correct
54 Correct 457 ms 38620 KB Output is correct
55 Correct 468 ms 38336 KB Output is correct
56 Correct 407 ms 38016 KB Output is correct
57 Correct 426 ms 39240 KB Output is correct
58 Correct 442 ms 38984 KB Output is correct
59 Correct 439 ms 39088 KB Output is correct
60 Correct 446 ms 38520 KB Output is correct
61 Correct 478 ms 39184 KB Output is correct
62 Correct 498 ms 38820 KB Output is correct
63 Correct 446 ms 37080 KB Output is correct
64 Correct 447 ms 41272 KB Output is correct
65 Correct 454 ms 42092 KB Output is correct
66 Correct 464 ms 41216 KB Output is correct
67 Correct 484 ms 41332 KB Output is correct
68 Correct 484 ms 41492 KB Output is correct
69 Correct 461 ms 41880 KB Output is correct
70 Correct 481 ms 41784 KB Output is correct
71 Correct 542 ms 41344 KB Output is correct
72 Correct 520 ms 41620 KB Output is correct
73 Correct 481 ms 41532 KB Output is correct
74 Correct 471 ms 41604 KB Output is correct
75 Correct 450 ms 41524 KB Output is correct
76 Correct 469 ms 41548 KB Output is correct
77 Correct 509 ms 45140 KB Output is correct
78 Correct 502 ms 45752 KB Output is correct
79 Correct 514 ms 45648 KB Output is correct
80 Correct 528 ms 45044 KB Output is correct
81 Correct 494 ms 45012 KB Output is correct
82 Correct 537 ms 45360 KB Output is correct
83 Correct 494 ms 45232 KB Output is correct
84 Correct 495 ms 44996 KB Output is correct
85 Correct 513 ms 45620 KB Output is correct
86 Correct 505 ms 45168 KB Output is correct
87 Correct 497 ms 45476 KB Output is correct
88 Correct 458 ms 45348 KB Output is correct
89 Correct 497 ms 44448 KB Output is correct
90 Correct 478 ms 45248 KB Output is correct
91 Correct 479 ms 44684 KB Output is correct
92 Correct 454 ms 44220 KB Output is correct
93 Correct 465 ms 44804 KB Output is correct
94 Correct 519 ms 45760 KB Output is correct
95 Correct 458 ms 45512 KB Output is correct
96 Correct 463 ms 44944 KB Output is correct
97 Correct 445 ms 44912 KB Output is correct
98 Correct 504 ms 44228 KB Output is correct
99 Correct 480 ms 44572 KB Output is correct
100 Correct 542 ms 44780 KB Output is correct
101 Correct 473 ms 44344 KB Output is correct
102 Correct 534 ms 45136 KB Output is correct