답안 #201599

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
201599 2020-02-11T11:25:27 Z Alexa2001 Fire (JOI20_ho_t5) C++17
100 / 100
412 ms 35904 KB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 2e5 + 5;
typedef long long ll;

int n, q;
int A[Nmax], Prev[Nmax], Next[Nmax], st[Nmax];

ll ans[Nmax];


struct event 
{
    int tip, t, p, w;

    bool operator < (const event &other) const 
    {
        return other.t - t < other.p - p;
    }
};
vector<event>  ev;


struct AIB 
{
    ll a[Nmax], b[Nmax];

    int ub(int x) { return (x&(-x)); }

    
    void upd(ll a[], int pos, ll add)
    {
        for(++pos; pos <= n+1; pos += ub(pos))
            a[pos] += add;
    }

    ll query(ll a[], int pos)
    { 
        ll ans = 0;
        assert(0 <= pos && pos <= n);
        for(++pos; pos; pos -= ub(pos)) ans += a[pos];
        return ans;
    }

public:
    void clear()
    {
        memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b));
    }

    void update(int pos, int w)
    {
        upd(a, pos, w);
        upd(b, pos, -(ll)pos*w + w);
    }

    ll query(int pos)
    {
        return query(a, pos) * pos + query(b, pos);
    }
} aib;

void precalc()
{
    int i, nr = 0;

    for(i=1; i<=n; ++i)
    {
        while(nr && A[st[nr]] < A[i]) --nr;
        
        Prev[i] = st[nr];
        st[++nr] = i;
    }

    nr = 0;
    for(i=n; i; --i)
    {
        while(nr && A[st[nr]] <= A[i]) --nr;

        Next[i] = st[nr];
        st[++nr] = i;
    }
}

void solve()
{
    int i;
    for(i=1; i<=n; ++i)
    {
        ev.push_back({0, 0, i, +A[i]});
        
        if(Prev[i])
            ev.push_back({0, i - Prev[i], i, -A[i]});

        if(Next[i])
            ev.push_back({0, Next[i] - i, Next[i], -A[i]});

        if(Prev[i] && Next[i])
            ev.push_back({0, Next[i] - Prev[i], Next[i], +A[i]});
    }
    
    for(i=1; i<=q; ++i)
    {
        int T, L, R;
        cin >> T >> L >> R;
        
        ev.push_back({1, T, R, i});
        ev.push_back({-1, T, L-1, i});
    }    

    sort(ev.begin(), ev.end());

    aib.clear();
    for(auto it : ev)
        if(it.tip == 0)
            aib.update(it.t, it.w);
        else if(it.tip == 1) 
            ans[it.w] += aib.query(it.t);
        else ans[it.w] -= aib.query(it.t);

    reverse(ev.begin(), ev.end());

    aib.clear();
    for(auto it : ev)
        if(it.tip == 0)
            aib.update(it.p, it.w);
        else if(it.tip == 1)
            ans[it.w] += aib.query(it.p);
        else ans[it.w] -= aib.query(it.p);
}

int main()
{
   // freopen("input", "r", stdin);
    cin.sync_with_stdio(false); cin.tie(0);

    cin >> n >> q;

    int i;
    for(i=1; i<=n; ++i) cin >> A[i];

    precalc();
    solve();

    for(i=1; i<=q; ++i)
        cout << ans[i] << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3448 KB Output is correct
2 Correct 7 ms 3576 KB Output is correct
3 Correct 7 ms 3448 KB Output is correct
4 Correct 7 ms 3576 KB Output is correct
5 Correct 8 ms 3576 KB Output is correct
6 Correct 7 ms 3576 KB Output is correct
7 Correct 7 ms 3576 KB Output is correct
8 Correct 7 ms 3580 KB Output is correct
9 Correct 7 ms 3576 KB Output is correct
10 Correct 7 ms 3576 KB Output is correct
11 Correct 7 ms 3576 KB Output is correct
12 Correct 7 ms 3576 KB Output is correct
13 Correct 7 ms 3576 KB Output is correct
14 Correct 7 ms 3576 KB Output is correct
15 Correct 7 ms 3576 KB Output is correct
16 Correct 7 ms 3576 KB Output is correct
17 Correct 7 ms 3576 KB Output is correct
18 Correct 7 ms 3576 KB Output is correct
19 Correct 7 ms 3576 KB Output is correct
20 Correct 7 ms 3576 KB Output is correct
21 Correct 7 ms 3576 KB Output is correct
22 Correct 7 ms 3576 KB Output is correct
23 Correct 7 ms 3576 KB Output is correct
24 Correct 7 ms 3576 KB Output is correct
25 Correct 7 ms 3576 KB Output is correct
26 Correct 7 ms 3576 KB Output is correct
27 Correct 7 ms 3576 KB Output is correct
28 Correct 7 ms 3576 KB Output is correct
29 Correct 7 ms 3580 KB Output is correct
30 Correct 7 ms 3576 KB Output is correct
31 Correct 7 ms 3576 KB Output is correct
32 Correct 7 ms 3580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3448 KB Output is correct
2 Correct 371 ms 35648 KB Output is correct
3 Correct 357 ms 35648 KB Output is correct
4 Correct 381 ms 35680 KB Output is correct
5 Correct 375 ms 35648 KB Output is correct
6 Correct 383 ms 35904 KB Output is correct
7 Correct 399 ms 35648 KB Output is correct
8 Correct 377 ms 35648 KB Output is correct
9 Correct 372 ms 35648 KB Output is correct
10 Correct 355 ms 35520 KB Output is correct
11 Correct 361 ms 35656 KB Output is correct
12 Correct 347 ms 35520 KB Output is correct
13 Correct 391 ms 35648 KB Output is correct
14 Correct 368 ms 35648 KB Output is correct
15 Correct 369 ms 35648 KB Output is correct
16 Correct 383 ms 35820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3448 KB Output is correct
2 Correct 380 ms 35648 KB Output is correct
3 Correct 361 ms 35648 KB Output is correct
4 Correct 374 ms 35648 KB Output is correct
5 Correct 371 ms 35648 KB Output is correct
6 Correct 381 ms 35648 KB Output is correct
7 Correct 393 ms 35648 KB Output is correct
8 Correct 379 ms 35648 KB Output is correct
9 Correct 376 ms 35520 KB Output is correct
10 Correct 378 ms 35520 KB Output is correct
11 Correct 384 ms 35648 KB Output is correct
12 Correct 364 ms 35648 KB Output is correct
13 Correct 378 ms 35648 KB Output is correct
14 Correct 373 ms 35648 KB Output is correct
15 Correct 377 ms 35648 KB Output is correct
16 Correct 374 ms 35648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 310 ms 24052 KB Output is correct
2 Correct 311 ms 24192 KB Output is correct
3 Correct 315 ms 24640 KB Output is correct
4 Correct 330 ms 24264 KB Output is correct
5 Correct 322 ms 24256 KB Output is correct
6 Correct 319 ms 24128 KB Output is correct
7 Correct 327 ms 24380 KB Output is correct
8 Correct 325 ms 24508 KB Output is correct
9 Correct 315 ms 24256 KB Output is correct
10 Correct 324 ms 24260 KB Output is correct
11 Correct 318 ms 24388 KB Output is correct
12 Correct 332 ms 24384 KB Output is correct
13 Correct 323 ms 24256 KB Output is correct
14 Correct 317 ms 24384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3448 KB Output is correct
2 Correct 7 ms 3576 KB Output is correct
3 Correct 7 ms 3448 KB Output is correct
4 Correct 7 ms 3576 KB Output is correct
5 Correct 8 ms 3576 KB Output is correct
6 Correct 7 ms 3576 KB Output is correct
7 Correct 7 ms 3576 KB Output is correct
8 Correct 7 ms 3580 KB Output is correct
9 Correct 7 ms 3576 KB Output is correct
10 Correct 7 ms 3576 KB Output is correct
11 Correct 7 ms 3576 KB Output is correct
12 Correct 7 ms 3576 KB Output is correct
13 Correct 7 ms 3576 KB Output is correct
14 Correct 7 ms 3576 KB Output is correct
15 Correct 7 ms 3576 KB Output is correct
16 Correct 7 ms 3576 KB Output is correct
17 Correct 7 ms 3576 KB Output is correct
18 Correct 7 ms 3576 KB Output is correct
19 Correct 7 ms 3576 KB Output is correct
20 Correct 7 ms 3576 KB Output is correct
21 Correct 7 ms 3576 KB Output is correct
22 Correct 7 ms 3576 KB Output is correct
23 Correct 7 ms 3576 KB Output is correct
24 Correct 7 ms 3576 KB Output is correct
25 Correct 7 ms 3576 KB Output is correct
26 Correct 7 ms 3576 KB Output is correct
27 Correct 7 ms 3576 KB Output is correct
28 Correct 7 ms 3576 KB Output is correct
29 Correct 7 ms 3580 KB Output is correct
30 Correct 7 ms 3576 KB Output is correct
31 Correct 7 ms 3576 KB Output is correct
32 Correct 7 ms 3580 KB Output is correct
33 Correct 379 ms 35648 KB Output is correct
34 Correct 412 ms 35648 KB Output is correct
35 Correct 398 ms 35652 KB Output is correct
36 Correct 381 ms 35648 KB Output is correct
37 Correct 405 ms 35648 KB Output is correct
38 Correct 392 ms 35648 KB Output is correct
39 Correct 390 ms 35648 KB Output is correct
40 Correct 392 ms 35520 KB Output is correct
41 Correct 375 ms 35652 KB Output is correct
42 Correct 398 ms 35652 KB Output is correct
43 Correct 363 ms 35648 KB Output is correct
44 Correct 360 ms 35648 KB Output is correct
45 Correct 390 ms 35520 KB Output is correct
46 Correct 362 ms 35648 KB Output is correct
47 Correct 370 ms 35648 KB Output is correct
48 Correct 354 ms 35648 KB Output is correct
49 Correct 352 ms 35648 KB Output is correct
50 Correct 364 ms 35652 KB Output is correct
51 Correct 366 ms 35648 KB Output is correct
52 Correct 355 ms 35648 KB Output is correct
53 Correct 354 ms 35648 KB Output is correct
54 Correct 381 ms 35648 KB Output is correct
55 Correct 384 ms 35644 KB Output is correct
56 Correct 398 ms 35648 KB Output is correct
57 Correct 382 ms 35648 KB Output is correct
58 Correct 398 ms 35648 KB Output is correct
59 Correct 371 ms 35648 KB Output is correct
60 Correct 357 ms 35648 KB Output is correct
61 Correct 381 ms 35680 KB Output is correct
62 Correct 375 ms 35648 KB Output is correct
63 Correct 383 ms 35904 KB Output is correct
64 Correct 399 ms 35648 KB Output is correct
65 Correct 377 ms 35648 KB Output is correct
66 Correct 372 ms 35648 KB Output is correct
67 Correct 355 ms 35520 KB Output is correct
68 Correct 361 ms 35656 KB Output is correct
69 Correct 347 ms 35520 KB Output is correct
70 Correct 391 ms 35648 KB Output is correct
71 Correct 368 ms 35648 KB Output is correct
72 Correct 369 ms 35648 KB Output is correct
73 Correct 383 ms 35820 KB Output is correct
74 Correct 380 ms 35648 KB Output is correct
75 Correct 361 ms 35648 KB Output is correct
76 Correct 374 ms 35648 KB Output is correct
77 Correct 371 ms 35648 KB Output is correct
78 Correct 381 ms 35648 KB Output is correct
79 Correct 393 ms 35648 KB Output is correct
80 Correct 379 ms 35648 KB Output is correct
81 Correct 376 ms 35520 KB Output is correct
82 Correct 378 ms 35520 KB Output is correct
83 Correct 384 ms 35648 KB Output is correct
84 Correct 364 ms 35648 KB Output is correct
85 Correct 378 ms 35648 KB Output is correct
86 Correct 373 ms 35648 KB Output is correct
87 Correct 377 ms 35648 KB Output is correct
88 Correct 374 ms 35648 KB Output is correct
89 Correct 310 ms 24052 KB Output is correct
90 Correct 311 ms 24192 KB Output is correct
91 Correct 315 ms 24640 KB Output is correct
92 Correct 330 ms 24264 KB Output is correct
93 Correct 322 ms 24256 KB Output is correct
94 Correct 319 ms 24128 KB Output is correct
95 Correct 327 ms 24380 KB Output is correct
96 Correct 325 ms 24508 KB Output is correct
97 Correct 315 ms 24256 KB Output is correct
98 Correct 324 ms 24260 KB Output is correct
99 Correct 318 ms 24388 KB Output is correct
100 Correct 332 ms 24384 KB Output is correct
101 Correct 323 ms 24256 KB Output is correct
102 Correct 317 ms 24384 KB Output is correct