답안 #701535

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
701535 2023-02-21T12:30:20 Z jiahng Fire (JOI20_ho_t5) C++14
100 / 100
547 ms 70920 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 400010
#define INF (ll)1e9
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
#define DEBUG 0

int S[maxn];
int N,Q;
int t,l,r;
vpi queries[maxn];
int ans[maxn];

struct FW{
    int fw[maxn],N;
    FW(int _N){
        N = _N;
        FOR(i,1,N) fw[i] = 0;
    }
    void upd(int p,int v){
        assert(p != 0);
        for (int i=p;i<=N;i+=i&(-i)) fw[i] += v;
    }

    int pref_qry(int p){
        int ans = 0;
        for (int i=p;i>0;i-=i&(-i)) ans += fw[i];
        return ans;
    }
    int suff_qry(int p){
        return pref_qry(N) - pref_qry(p-1);
    }
    void print(){
        FOR(i,1,N) cout << pref_qry(i) - pref_qry(i-1) << ' ';
        cout << '\n';
    }

};

int A[maxn],B[maxn],C[maxn],W[maxn];

int32_t main(){
    fast;
    
    // Let an edge a->b be such that a is the greatest index < b such that S[a] >= S[b]
    // Edges are indexed by b
    // Let c be the maximum value of a node in b's subtree
    // Let w be S[a] - S[b]

    cin >> N >> Q;
    FOR(i,1,N) cin >> S[i];
    FW normalfw = FW(N);
    FOR(i,1,N) normalfw.upd(i,S[i]);
    FOR(i,1,Q){
        cin >> t >> l >> r;
        ans[i] += normalfw.pref_qry(r) - normalfw.pref_qry(l-1);
        queries[r].pb({t,i});
        queries[l-1].pb({t,-i});
    }




    FW fixfw_wcb = FW(2*N), fixfw_wab = FW(2*N), fixfw_w = FW(2*N), fixfw_wab2 = FW(2*N), fixfw_w2 = FW(2*N);
    FW stackfw_w = FW(2*N), stackfw_wb = FW(2*N), stackfw_wab = FW(2*N), stackfw_wab2 = FW(2*N), stackfw_w2 = FW(2*N);

    // Fixed fenwicks indexed on c-a
    // Stack fenwicks indexed on a
    //
    // fixfw_wcb stores sum of w(c-b)
    // fixfw_wab stores sum of w(a-b)
    // fixfw_w stores sum of w
    // stackfw_w stores sum of w
    // stackfw_wb stores sum of wb
    // stackfw_wab stores sum of w(a-b)

    stack <int> st;
    int shift = 0;
    FOR(i,1,N){
        while (!st.empty() && S[st.top()] < S[i]){
            int x = st.top();
            if (A[x] != -1){
                stackfw_w.upd(A[x], -W[x]);
                stackfw_wb.upd(A[x], -W[x]*x);
                stackfw_wab.upd(A[x], -W[x]*(A[x]-x));
                stackfw_w2.upd(x-A[x], -W[x]);
                stackfw_wab2.upd(x-A[x], -W[x]*(A[x]-x));

                C[x] += shift; // transfer updates from stack

                fixfw_wcb.upd(C[x] - A[x], W[x]*(C[x]-x));
                fixfw_wab.upd(C[x] - A[x], W[x]*(A[x]-x));
                fixfw_w.upd(C[x] - A[x], W[x]);
                fixfw_w2.upd(x - A[x], W[x]);
                fixfw_wab2.upd(x - A[x], W[x]*(A[x]-x));
            }

            st.pop();
        }


        shift++; // increase all C by 1 in the stack

        if (!st.empty()){
            A[i] = st.top();
            C[i] = i - shift; // have to take into account stack updates
            W[i] = S[A[i]] - S[i];
            
            stackfw_w.upd(A[i], W[i]);
            stackfw_wb.upd(A[i], W[i]*i);
            stackfw_wab.upd(A[i], W[i]*(A[i]-i));
            stackfw_w2.upd(i-A[i], W[i]);
            stackfw_wab2.upd(i-A[i], W[i]*(A[i]-i));
        }else A[i] = -1;
        st.push(i);
        
        if (DEBUG && i == N){
            cout << "FIX\n";

            cout << "wcb "; fixfw_wcb.print();
            cout << "w "; fixfw_w.print();
            cout << "wab "; fixfw_wab.print();
            cout << "w2 "; fixfw_w2.print();
            cout << "wab2 ";  fixfw_wab2.print();
            
            cout << "STACK\n";

            cout << "w "; stackfw_w.print();
            cout << "wb "; stackfw_wb.print();
            cout << "wab "; stackfw_wab.print();
            cout << "w2 "; stackfw_w2.print();
            cout << "wab2 "; stackfw_wab2.print();
        }
        aFOR(q, queries[i]){
            int t = q.f;

            int fix = fixfw_wcb.pref_qry(t) + fixfw_w.suff_qry(t+1)*t + fixfw_wab.suff_qry(t+1) - fixfw_w2.suff_qry(t+1)*t - fixfw_wab2.suff_qry(t+1) + fixfw_w2.pref_qry(t);
            int sta = stackfw_w.suff_qry(i-t) * i - stackfw_wb.suff_qry(i-t) + stackfw_w.pref_qry(i-t-1)*t + stackfw_wab.pref_qry(i-t-1) - stackfw_w2.suff_qry(t+1)*t - stackfw_wab2.suff_qry(t+1) + stackfw_w2.pref_qry(t);
            assert(fix >= 0 && sta >= 0);

            if (q.s < 0) ans[abs(q.s)] -= fix + sta;
            else ans[abs(q.s)] += fix + sta;
        }
    }
    while (!st.empty()){
        C[st.top()] += shift; st.pop(); 
    }
    if (DEBUG) FOR(i,1,N) cout << A[i] << ' ' << C[i] << ' ' << W[i] << '\n';

    FOR(i,1,Q) cout << ans[i] << '\n';

}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 44176 KB Output is correct
2 Correct 20 ms 44176 KB Output is correct
3 Correct 19 ms 44116 KB Output is correct
4 Correct 21 ms 44132 KB Output is correct
5 Correct 19 ms 44168 KB Output is correct
6 Correct 20 ms 44152 KB Output is correct
7 Correct 22 ms 44160 KB Output is correct
8 Correct 20 ms 44168 KB Output is correct
9 Correct 19 ms 44184 KB Output is correct
10 Correct 19 ms 44116 KB Output is correct
11 Correct 19 ms 44152 KB Output is correct
12 Correct 19 ms 44144 KB Output is correct
13 Correct 19 ms 44168 KB Output is correct
14 Correct 19 ms 44212 KB Output is correct
15 Correct 19 ms 44116 KB Output is correct
16 Correct 19 ms 44124 KB Output is correct
17 Correct 19 ms 44116 KB Output is correct
18 Correct 20 ms 44164 KB Output is correct
19 Correct 20 ms 44164 KB Output is correct
20 Correct 19 ms 44116 KB Output is correct
21 Correct 24 ms 44108 KB Output is correct
22 Correct 19 ms 44164 KB Output is correct
23 Correct 20 ms 44140 KB Output is correct
24 Correct 21 ms 44292 KB Output is correct
25 Correct 20 ms 44172 KB Output is correct
26 Correct 20 ms 44116 KB Output is correct
27 Correct 20 ms 44164 KB Output is correct
28 Correct 20 ms 44164 KB Output is correct
29 Correct 19 ms 44156 KB Output is correct
30 Correct 20 ms 44112 KB Output is correct
31 Correct 18 ms 44132 KB Output is correct
32 Correct 21 ms 44208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 44176 KB Output is correct
2 Correct 353 ms 70260 KB Output is correct
3 Correct 315 ms 70220 KB Output is correct
4 Correct 339 ms 70464 KB Output is correct
5 Correct 316 ms 70336 KB Output is correct
6 Correct 349 ms 69960 KB Output is correct
7 Correct 333 ms 70600 KB Output is correct
8 Correct 334 ms 70340 KB Output is correct
9 Correct 355 ms 70652 KB Output is correct
10 Correct 316 ms 70248 KB Output is correct
11 Correct 377 ms 70776 KB Output is correct
12 Correct 336 ms 70036 KB Output is correct
13 Correct 325 ms 70156 KB Output is correct
14 Correct 336 ms 69920 KB Output is correct
15 Correct 299 ms 70140 KB Output is correct
16 Correct 370 ms 70024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 44176 KB Output is correct
2 Correct 450 ms 69752 KB Output is correct
3 Correct 380 ms 69264 KB Output is correct
4 Correct 380 ms 69840 KB Output is correct
5 Correct 422 ms 69052 KB Output is correct
6 Correct 400 ms 69432 KB Output is correct
7 Correct 381 ms 69436 KB Output is correct
8 Correct 401 ms 69708 KB Output is correct
9 Correct 385 ms 69544 KB Output is correct
10 Correct 414 ms 69032 KB Output is correct
11 Correct 391 ms 69836 KB Output is correct
12 Correct 387 ms 69292 KB Output is correct
13 Correct 439 ms 69892 KB Output is correct
14 Correct 410 ms 69352 KB Output is correct
15 Correct 398 ms 69464 KB Output is correct
16 Correct 362 ms 69088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 449 ms 67740 KB Output is correct
2 Correct 426 ms 67544 KB Output is correct
3 Correct 409 ms 68328 KB Output is correct
4 Correct 428 ms 68072 KB Output is correct
5 Correct 547 ms 68036 KB Output is correct
6 Correct 444 ms 67532 KB Output is correct
7 Correct 513 ms 67916 KB Output is correct
8 Correct 516 ms 68052 KB Output is correct
9 Correct 451 ms 67896 KB Output is correct
10 Correct 489 ms 67688 KB Output is correct
11 Correct 422 ms 68228 KB Output is correct
12 Correct 465 ms 68260 KB Output is correct
13 Correct 420 ms 67940 KB Output is correct
14 Correct 420 ms 68044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 44176 KB Output is correct
2 Correct 20 ms 44176 KB Output is correct
3 Correct 19 ms 44116 KB Output is correct
4 Correct 21 ms 44132 KB Output is correct
5 Correct 19 ms 44168 KB Output is correct
6 Correct 20 ms 44152 KB Output is correct
7 Correct 22 ms 44160 KB Output is correct
8 Correct 20 ms 44168 KB Output is correct
9 Correct 19 ms 44184 KB Output is correct
10 Correct 19 ms 44116 KB Output is correct
11 Correct 19 ms 44152 KB Output is correct
12 Correct 19 ms 44144 KB Output is correct
13 Correct 19 ms 44168 KB Output is correct
14 Correct 19 ms 44212 KB Output is correct
15 Correct 19 ms 44116 KB Output is correct
16 Correct 19 ms 44124 KB Output is correct
17 Correct 19 ms 44116 KB Output is correct
18 Correct 20 ms 44164 KB Output is correct
19 Correct 20 ms 44164 KB Output is correct
20 Correct 19 ms 44116 KB Output is correct
21 Correct 24 ms 44108 KB Output is correct
22 Correct 19 ms 44164 KB Output is correct
23 Correct 20 ms 44140 KB Output is correct
24 Correct 21 ms 44292 KB Output is correct
25 Correct 20 ms 44172 KB Output is correct
26 Correct 20 ms 44116 KB Output is correct
27 Correct 20 ms 44164 KB Output is correct
28 Correct 20 ms 44164 KB Output is correct
29 Correct 19 ms 44156 KB Output is correct
30 Correct 20 ms 44112 KB Output is correct
31 Correct 18 ms 44132 KB Output is correct
32 Correct 21 ms 44208 KB Output is correct
33 Correct 353 ms 70260 KB Output is correct
34 Correct 315 ms 70220 KB Output is correct
35 Correct 339 ms 70464 KB Output is correct
36 Correct 316 ms 70336 KB Output is correct
37 Correct 349 ms 69960 KB Output is correct
38 Correct 333 ms 70600 KB Output is correct
39 Correct 334 ms 70340 KB Output is correct
40 Correct 355 ms 70652 KB Output is correct
41 Correct 316 ms 70248 KB Output is correct
42 Correct 377 ms 70776 KB Output is correct
43 Correct 336 ms 70036 KB Output is correct
44 Correct 325 ms 70156 KB Output is correct
45 Correct 336 ms 69920 KB Output is correct
46 Correct 299 ms 70140 KB Output is correct
47 Correct 370 ms 70024 KB Output is correct
48 Correct 450 ms 69752 KB Output is correct
49 Correct 380 ms 69264 KB Output is correct
50 Correct 380 ms 69840 KB Output is correct
51 Correct 422 ms 69052 KB Output is correct
52 Correct 400 ms 69432 KB Output is correct
53 Correct 381 ms 69436 KB Output is correct
54 Correct 401 ms 69708 KB Output is correct
55 Correct 385 ms 69544 KB Output is correct
56 Correct 414 ms 69032 KB Output is correct
57 Correct 391 ms 69836 KB Output is correct
58 Correct 387 ms 69292 KB Output is correct
59 Correct 439 ms 69892 KB Output is correct
60 Correct 410 ms 69352 KB Output is correct
61 Correct 398 ms 69464 KB Output is correct
62 Correct 362 ms 69088 KB Output is correct
63 Correct 449 ms 67740 KB Output is correct
64 Correct 426 ms 67544 KB Output is correct
65 Correct 409 ms 68328 KB Output is correct
66 Correct 428 ms 68072 KB Output is correct
67 Correct 547 ms 68036 KB Output is correct
68 Correct 444 ms 67532 KB Output is correct
69 Correct 513 ms 67916 KB Output is correct
70 Correct 516 ms 68052 KB Output is correct
71 Correct 451 ms 67896 KB Output is correct
72 Correct 489 ms 67688 KB Output is correct
73 Correct 422 ms 68228 KB Output is correct
74 Correct 465 ms 68260 KB Output is correct
75 Correct 420 ms 67940 KB Output is correct
76 Correct 420 ms 68044 KB Output is correct
77 Correct 513 ms 70308 KB Output is correct
78 Correct 505 ms 70920 KB Output is correct
79 Correct 462 ms 70316 KB Output is correct
80 Correct 445 ms 70324 KB Output is correct
81 Correct 475 ms 70156 KB Output is correct
82 Correct 504 ms 70348 KB Output is correct
83 Correct 453 ms 70212 KB Output is correct
84 Correct 457 ms 70440 KB Output is correct
85 Correct 469 ms 70080 KB Output is correct
86 Correct 464 ms 70476 KB Output is correct
87 Correct 335 ms 70840 KB Output is correct
88 Correct 334 ms 70496 KB Output is correct
89 Correct 348 ms 70104 KB Output is correct
90 Correct 352 ms 70464 KB Output is correct
91 Correct 358 ms 69956 KB Output is correct
92 Correct 378 ms 69992 KB Output is correct
93 Correct 414 ms 70040 KB Output is correct
94 Correct 362 ms 70832 KB Output is correct
95 Correct 365 ms 70648 KB Output is correct
96 Correct 367 ms 70492 KB Output is correct
97 Correct 369 ms 70644 KB Output is correct
98 Correct 401 ms 69780 KB Output is correct
99 Correct 369 ms 69880 KB Output is correct
100 Correct 388 ms 70216 KB Output is correct
101 Correct 334 ms 69840 KB Output is correct
102 Correct 386 ms 70456 KB Output is correct