답안 #256450

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256450 2020-08-02T17:35:34 Z doowey Fire (JOI20_ho_t5) C++14
100 / 100
587 ms 78032 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)2e5 + 10;

int m;
int A[N];
int L[N];
int R[N];

const int M = (int)1e6 + 100;
ll tree[M];

void innit(){
    for(int i = 0 ; i < M; i ++ ){
        tree[i] = 0;
    }
}

void upd(int id, ll v){
    id += m;
    tree[id] += v;
    id /= 2;
    while(id > 0){
        tree[id] = tree[id * 2] + tree[id * 2 + 1];
        id /= 2;
    }
}

ll qry(int l, int r){
    l += m;
    r += m;
    ll res = 0;
    while(l <= r){
        if(l % 2 == 1) res += tree[l ++ ];
        if(r % 2 == 0) res += tree[r -- ];
        l /= 2;
        r /= 2;
    }
    return res;
}

struct Q{
    int lef;
    int rig;
    int idx;
};

vector<Q> typ[N];
ll ans[N];
vector<Q> eve[N];
vector<pii> lin[N];

int main(){
    fastIO;
    int n, q;
    cin >> n >> q;
    m = n * 2 + 30;
    for(int i = 1; i <= n; i ++ ){
        cin >> A[i];
    }
    int tt, li, ri;
    for(int qq = 0; qq < q; qq ++ ){
        cin >> tt >> li >> ri;
        typ[tt].push_back({li, ri, qq});
    }
    vector<int> id;
    for(int i = 1; i <= n; i ++ ){
        while(!id.empty() && A[id.back()] <= A[i]){
            R[id.back()] = i;
            id.pop_back();
        }
        if(id.empty()){
            L[i] = i-n-1;
        }
        else{
            L[i] = id.back();
        }
        id.push_back(i);
    }
    for(int i = 1; i <= n; i ++ ){
        if(R[i] == 0)
            R[i] = i+n+1;
    }
    int len;
    for(int i = 1; i <= n; i ++ ){
        if(i - L[i] < R[i] - i){
            len = R[i] - i;
            for(int j = 0; j < i - L[i]; j ++ ){
                eve[j].push_back({i, j, A[i]});
                if(j + len <= n)
                    eve[j + len].push_back({i + len, j + len, -A[i]});
            }
        }
        else{
            len = i - L[i];
            for(int j = 0 ; j < R[i] - i; j ++){
                lin[j].push_back(mp(i + j, A[i]));
                if(j + len <= n)
                    lin[j + len].push_back(mp(i + j, -A[i]));
            }
        }
    }
    innit();
    for(int t = 0; t <= n; t ++ ){
        for(auto q : eve[t]){
            upd(q.lef - t + n, q.idx);
        }
        for(auto q : typ[t]){
            ans[q.idx] += qry(q.lef - t + n, q.rig - t + n);
        }
    }
    innit();
    for(int t = 0; t <= n; t ++ ){
        for(auto q : lin[t]){
            upd(q.fi, q.se);
        }
        for(auto q : typ[t]){
            ans[q.idx] += qry(q.lef, q.rig);
        }
    }
    for(int t =0 ; t < q ;t ++ )
        cout << ans[t] << "\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 22272 KB Output is correct
2 Correct 14 ms 22272 KB Output is correct
3 Correct 15 ms 22272 KB Output is correct
4 Correct 14 ms 22272 KB Output is correct
5 Correct 15 ms 22272 KB Output is correct
6 Correct 14 ms 22272 KB Output is correct
7 Correct 14 ms 22272 KB Output is correct
8 Correct 14 ms 22272 KB Output is correct
9 Correct 14 ms 22376 KB Output is correct
10 Correct 16 ms 22272 KB Output is correct
11 Correct 14 ms 22272 KB Output is correct
12 Correct 17 ms 22336 KB Output is correct
13 Correct 15 ms 22272 KB Output is correct
14 Correct 14 ms 22272 KB Output is correct
15 Correct 14 ms 22272 KB Output is correct
16 Correct 14 ms 22272 KB Output is correct
17 Correct 14 ms 22272 KB Output is correct
18 Correct 14 ms 22272 KB Output is correct
19 Correct 14 ms 22272 KB Output is correct
20 Correct 14 ms 22272 KB Output is correct
21 Correct 15 ms 22272 KB Output is correct
22 Correct 14 ms 22272 KB Output is correct
23 Correct 14 ms 22272 KB Output is correct
24 Correct 14 ms 22272 KB Output is correct
25 Correct 14 ms 22272 KB Output is correct
26 Correct 14 ms 22272 KB Output is correct
27 Correct 15 ms 22272 KB Output is correct
28 Correct 15 ms 22272 KB Output is correct
29 Correct 14 ms 22272 KB Output is correct
30 Correct 15 ms 22272 KB Output is correct
31 Correct 15 ms 22272 KB Output is correct
32 Correct 14 ms 22392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 22272 KB Output is correct
2 Correct 377 ms 71780 KB Output is correct
3 Correct 384 ms 73696 KB Output is correct
4 Correct 373 ms 71660 KB Output is correct
5 Correct 389 ms 72860 KB Output is correct
6 Correct 378 ms 73576 KB Output is correct
7 Correct 398 ms 72036 KB Output is correct
8 Correct 433 ms 73952 KB Output is correct
9 Correct 385 ms 73060 KB Output is correct
10 Correct 378 ms 71240 KB Output is correct
11 Correct 385 ms 71268 KB Output is correct
12 Correct 375 ms 71652 KB Output is correct
13 Correct 379 ms 72208 KB Output is correct
14 Correct 375 ms 72956 KB Output is correct
15 Correct 388 ms 73700 KB Output is correct
16 Correct 387 ms 73572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 22272 KB Output is correct
2 Correct 398 ms 75252 KB Output is correct
3 Correct 369 ms 70856 KB Output is correct
4 Correct 441 ms 78032 KB Output is correct
5 Correct 389 ms 72140 KB Output is correct
6 Correct 364 ms 72592 KB Output is correct
7 Correct 378 ms 73416 KB Output is correct
8 Correct 375 ms 73472 KB Output is correct
9 Correct 375 ms 72140 KB Output is correct
10 Correct 379 ms 72648 KB Output is correct
11 Correct 407 ms 77080 KB Output is correct
12 Correct 383 ms 74124 KB Output is correct
13 Correct 390 ms 74568 KB Output is correct
14 Correct 370 ms 71116 KB Output is correct
15 Correct 379 ms 73292 KB Output is correct
16 Correct 407 ms 73552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 312 ms 42120 KB Output is correct
2 Correct 277 ms 41956 KB Output is correct
3 Correct 301 ms 45416 KB Output is correct
4 Correct 304 ms 45052 KB Output is correct
5 Correct 300 ms 45156 KB Output is correct
6 Correct 294 ms 45032 KB Output is correct
7 Correct 348 ms 45224 KB Output is correct
8 Correct 365 ms 45304 KB Output is correct
9 Correct 335 ms 45228 KB Output is correct
10 Correct 322 ms 45120 KB Output is correct
11 Correct 324 ms 45116 KB Output is correct
12 Correct 333 ms 45264 KB Output is correct
13 Correct 312 ms 45032 KB Output is correct
14 Correct 317 ms 45116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 22272 KB Output is correct
2 Correct 14 ms 22272 KB Output is correct
3 Correct 15 ms 22272 KB Output is correct
4 Correct 14 ms 22272 KB Output is correct
5 Correct 15 ms 22272 KB Output is correct
6 Correct 14 ms 22272 KB Output is correct
7 Correct 14 ms 22272 KB Output is correct
8 Correct 14 ms 22272 KB Output is correct
9 Correct 14 ms 22376 KB Output is correct
10 Correct 16 ms 22272 KB Output is correct
11 Correct 14 ms 22272 KB Output is correct
12 Correct 17 ms 22336 KB Output is correct
13 Correct 15 ms 22272 KB Output is correct
14 Correct 14 ms 22272 KB Output is correct
15 Correct 14 ms 22272 KB Output is correct
16 Correct 14 ms 22272 KB Output is correct
17 Correct 14 ms 22272 KB Output is correct
18 Correct 14 ms 22272 KB Output is correct
19 Correct 14 ms 22272 KB Output is correct
20 Correct 14 ms 22272 KB Output is correct
21 Correct 15 ms 22272 KB Output is correct
22 Correct 14 ms 22272 KB Output is correct
23 Correct 14 ms 22272 KB Output is correct
24 Correct 14 ms 22272 KB Output is correct
25 Correct 14 ms 22272 KB Output is correct
26 Correct 14 ms 22272 KB Output is correct
27 Correct 15 ms 22272 KB Output is correct
28 Correct 15 ms 22272 KB Output is correct
29 Correct 14 ms 22272 KB Output is correct
30 Correct 15 ms 22272 KB Output is correct
31 Correct 15 ms 22272 KB Output is correct
32 Correct 14 ms 22392 KB Output is correct
33 Correct 504 ms 73560 KB Output is correct
34 Correct 477 ms 73644 KB Output is correct
35 Correct 504 ms 74260 KB Output is correct
36 Correct 533 ms 74764 KB Output is correct
37 Correct 553 ms 73888 KB Output is correct
38 Correct 587 ms 75988 KB Output is correct
39 Correct 531 ms 74900 KB Output is correct
40 Correct 496 ms 72468 KB Output is correct
41 Correct 457 ms 74196 KB Output is correct
42 Correct 451 ms 73420 KB Output is correct
43 Correct 441 ms 74256 KB Output is correct
44 Correct 404 ms 72008 KB Output is correct
45 Correct 414 ms 70160 KB Output is correct
46 Correct 432 ms 73780 KB Output is correct
47 Correct 431 ms 74004 KB Output is correct
48 Correct 408 ms 70736 KB Output is correct
49 Correct 490 ms 74588 KB Output is correct
50 Correct 419 ms 73760 KB Output is correct
51 Correct 447 ms 74164 KB Output is correct
52 Correct 465 ms 74440 KB Output is correct
53 Correct 438 ms 74388 KB Output is correct
54 Correct 414 ms 72336 KB Output is correct
55 Correct 407 ms 71504 KB Output is correct
56 Correct 421 ms 74704 KB Output is correct
57 Correct 398 ms 71316 KB Output is correct
58 Correct 429 ms 72580 KB Output is correct
59 Correct 377 ms 71780 KB Output is correct
60 Correct 384 ms 73696 KB Output is correct
61 Correct 373 ms 71660 KB Output is correct
62 Correct 389 ms 72860 KB Output is correct
63 Correct 378 ms 73576 KB Output is correct
64 Correct 398 ms 72036 KB Output is correct
65 Correct 433 ms 73952 KB Output is correct
66 Correct 385 ms 73060 KB Output is correct
67 Correct 378 ms 71240 KB Output is correct
68 Correct 385 ms 71268 KB Output is correct
69 Correct 375 ms 71652 KB Output is correct
70 Correct 379 ms 72208 KB Output is correct
71 Correct 375 ms 72956 KB Output is correct
72 Correct 388 ms 73700 KB Output is correct
73 Correct 387 ms 73572 KB Output is correct
74 Correct 398 ms 75252 KB Output is correct
75 Correct 369 ms 70856 KB Output is correct
76 Correct 441 ms 78032 KB Output is correct
77 Correct 389 ms 72140 KB Output is correct
78 Correct 364 ms 72592 KB Output is correct
79 Correct 378 ms 73416 KB Output is correct
80 Correct 375 ms 73472 KB Output is correct
81 Correct 375 ms 72140 KB Output is correct
82 Correct 379 ms 72648 KB Output is correct
83 Correct 407 ms 77080 KB Output is correct
84 Correct 383 ms 74124 KB Output is correct
85 Correct 390 ms 74568 KB Output is correct
86 Correct 370 ms 71116 KB Output is correct
87 Correct 379 ms 73292 KB Output is correct
88 Correct 407 ms 73552 KB Output is correct
89 Correct 312 ms 42120 KB Output is correct
90 Correct 277 ms 41956 KB Output is correct
91 Correct 301 ms 45416 KB Output is correct
92 Correct 304 ms 45052 KB Output is correct
93 Correct 300 ms 45156 KB Output is correct
94 Correct 294 ms 45032 KB Output is correct
95 Correct 348 ms 45224 KB Output is correct
96 Correct 365 ms 45304 KB Output is correct
97 Correct 335 ms 45228 KB Output is correct
98 Correct 322 ms 45120 KB Output is correct
99 Correct 324 ms 45116 KB Output is correct
100 Correct 333 ms 45264 KB Output is correct
101 Correct 312 ms 45032 KB Output is correct
102 Correct 317 ms 45116 KB Output is correct