답안 #515931

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
515931 2022-01-20T06:49:59 Z Jarif_Rahman Fire (JOI20_ho_t5) C++17
100 / 100
412 ms 33356 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));
    }
};

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);
    BIT ss(n);
    BIT s1(n), s2(n), s3(n);
    for(int i = 0; i < n; i++) ss.add(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) for(int i = in+1; i <= in+t-1; i++) ss.add(i, 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]);
            for(int i = in+1; i < nxt; i++) ss.add(i, 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 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 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 1 ms 332 KB Output is correct
10 Correct 1 ms 436 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 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 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 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 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 286 ms 31080 KB Output is correct
3 Correct 271 ms 30608 KB Output is correct
4 Correct 281 ms 30908 KB Output is correct
5 Correct 294 ms 31508 KB Output is correct
6 Correct 293 ms 31176 KB Output is correct
7 Correct 283 ms 31264 KB Output is correct
8 Correct 301 ms 31764 KB Output is correct
9 Correct 306 ms 31376 KB Output is correct
10 Correct 271 ms 30436 KB Output is correct
11 Correct 300 ms 31432 KB Output is correct
12 Correct 298 ms 30432 KB Output is correct
13 Correct 311 ms 31424 KB Output is correct
14 Correct 304 ms 31396 KB Output is correct
15 Correct 301 ms 31480 KB Output is correct
16 Correct 316 ms 31128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 310 ms 32056 KB Output is correct
3 Correct 289 ms 31504 KB Output is correct
4 Correct 325 ms 32724 KB Output is correct
5 Correct 300 ms 31804 KB Output is correct
6 Correct 342 ms 32132 KB Output is correct
7 Correct 304 ms 32212 KB Output is correct
8 Correct 325 ms 31928 KB Output is correct
9 Correct 337 ms 31676 KB Output is correct
10 Correct 310 ms 31348 KB Output is correct
11 Correct 333 ms 32600 KB Output is correct
12 Correct 318 ms 32436 KB Output is correct
13 Correct 333 ms 32416 KB Output is correct
14 Correct 344 ms 31664 KB Output is correct
15 Correct 333 ms 32536 KB Output is correct
16 Correct 368 ms 32216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 258 ms 30380 KB Output is correct
2 Correct 262 ms 30508 KB Output is correct
3 Correct 267 ms 31296 KB Output is correct
4 Correct 295 ms 30392 KB Output is correct
5 Correct 278 ms 30432 KB Output is correct
6 Correct 255 ms 30620 KB Output is correct
7 Correct 279 ms 31120 KB Output is correct
8 Correct 255 ms 30836 KB Output is correct
9 Correct 284 ms 30504 KB Output is correct
10 Correct 267 ms 30828 KB Output is correct
11 Correct 248 ms 30640 KB Output is correct
12 Correct 285 ms 30808 KB Output is correct
13 Correct 253 ms 30576 KB Output is correct
14 Correct 253 ms 30660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 0 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 1 ms 332 KB Output is correct
10 Correct 1 ms 436 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 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 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 0 ms 332 KB Output is correct
23 Correct 0 ms 332 KB Output is correct
24 Correct 0 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 0 ms 332 KB Output is correct
30 Correct 0 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 0 ms 332 KB Output is correct
33 Correct 286 ms 31080 KB Output is correct
34 Correct 271 ms 30608 KB Output is correct
35 Correct 281 ms 30908 KB Output is correct
36 Correct 294 ms 31508 KB Output is correct
37 Correct 293 ms 31176 KB Output is correct
38 Correct 283 ms 31264 KB Output is correct
39 Correct 301 ms 31764 KB Output is correct
40 Correct 306 ms 31376 KB Output is correct
41 Correct 271 ms 30436 KB Output is correct
42 Correct 300 ms 31432 KB Output is correct
43 Correct 298 ms 30432 KB Output is correct
44 Correct 311 ms 31424 KB Output is correct
45 Correct 304 ms 31396 KB Output is correct
46 Correct 301 ms 31480 KB Output is correct
47 Correct 316 ms 31128 KB Output is correct
48 Correct 310 ms 32056 KB Output is correct
49 Correct 289 ms 31504 KB Output is correct
50 Correct 325 ms 32724 KB Output is correct
51 Correct 300 ms 31804 KB Output is correct
52 Correct 342 ms 32132 KB Output is correct
53 Correct 304 ms 32212 KB Output is correct
54 Correct 325 ms 31928 KB Output is correct
55 Correct 337 ms 31676 KB Output is correct
56 Correct 310 ms 31348 KB Output is correct
57 Correct 333 ms 32600 KB Output is correct
58 Correct 318 ms 32436 KB Output is correct
59 Correct 333 ms 32416 KB Output is correct
60 Correct 344 ms 31664 KB Output is correct
61 Correct 333 ms 32536 KB Output is correct
62 Correct 368 ms 32216 KB Output is correct
63 Correct 258 ms 30380 KB Output is correct
64 Correct 262 ms 30508 KB Output is correct
65 Correct 267 ms 31296 KB Output is correct
66 Correct 295 ms 30392 KB Output is correct
67 Correct 278 ms 30432 KB Output is correct
68 Correct 255 ms 30620 KB Output is correct
69 Correct 279 ms 31120 KB Output is correct
70 Correct 255 ms 30836 KB Output is correct
71 Correct 284 ms 30504 KB Output is correct
72 Correct 267 ms 30828 KB Output is correct
73 Correct 248 ms 30640 KB Output is correct
74 Correct 285 ms 30808 KB Output is correct
75 Correct 253 ms 30576 KB Output is correct
76 Correct 253 ms 30660 KB Output is correct
77 Correct 338 ms 32840 KB Output is correct
78 Correct 389 ms 33320 KB Output is correct
79 Correct 392 ms 33356 KB Output is correct
80 Correct 391 ms 32768 KB Output is correct
81 Correct 353 ms 32664 KB Output is correct
82 Correct 392 ms 33032 KB Output is correct
83 Correct 370 ms 32932 KB Output is correct
84 Correct 374 ms 32620 KB Output is correct
85 Correct 388 ms 33348 KB Output is correct
86 Correct 412 ms 32728 KB Output is correct
87 Correct 339 ms 33000 KB Output is correct
88 Correct 301 ms 32932 KB Output is correct
89 Correct 322 ms 31964 KB Output is correct
90 Correct 326 ms 32820 KB Output is correct
91 Correct 299 ms 32188 KB Output is correct
92 Correct 324 ms 31904 KB Output is correct
93 Correct 316 ms 32420 KB Output is correct
94 Correct 325 ms 33268 KB Output is correct
95 Correct 360 ms 33128 KB Output is correct
96 Correct 324 ms 32432 KB Output is correct
97 Correct 353 ms 32412 KB Output is correct
98 Correct 341 ms 32084 KB Output is correct
99 Correct 337 ms 32512 KB Output is correct
100 Correct 366 ms 32640 KB Output is correct
101 Correct 306 ms 32300 KB Output is correct
102 Correct 350 ms 33080 KB Output is correct