Submission #233755

# Submission time Handle Problem Language Result Execution time Memory
233755 2020-05-21T16:33:30 Z duality Fire (JOI20_ho_t5) C++11
100 / 100
341 ms 49536 KB
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef long long int LLI;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

int S[200000];
LLI pre[200001],pre2[200001];
struct query { int T,L,R,i; };
bool comp(query a,query b) {
    return a.T < b.T;
}
query queries[200000];
LLI ans[200000];
int l[200000],depth[200000];
vi SS;
vpii events[200005];
LLI bit[200001];
int main() {
    int i;
    int N,Q,T,L,R;
    scanf("%d %d",&N,&Q);
    for (i = 0; i < N; i++) scanf("%d",&S[i]);
    for (i = 0; i < Q; i++) {
        scanf("%d %d %d",&T,&L,&R);
        queries[i] = (query){T+1,L-1,R-1,i};
    }
    sort(queries,queries+Q,comp);

    LLI sum = 0;
    for (i = 0; i < N; i++) {
        while (!SS.empty() && (S[SS.back()] <= S[i])) SS.pop_back();
        if (SS.empty()) l[i] = -1,depth[i] = 0;
        else l[i] = SS.back(),depth[i] = depth[l[i]]+1;
        SS.pb(i),sum += depth[i];
    }
    if (sum <= 1e7) {
        for (i = 0; i < N; i++) {
            int u = i,p = 0;
            while (u != -1) {
                events[i-u+1].pb(mp(i,S[u]-p));
                p = S[u],u = l[u];
            }
        }
        int j = 0,k,l;
        for (i = 0; i < Q; i++) {
            while (j <= queries[i].T) {
                for (k = 0; k < events[j].size(); k++) {
                    for (l = events[j][k].first+1; l <= N; l += l & (-l)) bit[l] += events[j][k].second;
                }
                j++;
            }
            LLI a = 0;
            for (l = queries[i].R+1; l > 0; l -= l & (-l)) a += bit[l];
            for (l = queries[i].L; l > 0; l -= l & (-l)) a -= bit[l];
            ans[queries[i].i] = a;
        }
        for (i = 0; i < Q; i++) printf("%lld\n",ans[i]);
        return 0;
    }

    int j,c = 1;
    int k = 0,f = 0;
    for (i = 0; i < Q; i++) {
        if ((i > 0) && (queries[i].T != queries[i-1].T)) f = 0;
        while (queries[i].T >= 2*c) {
            for (j = N-1; j >= c; j--) S[j] = max(S[j],S[j-c]);
            for (j = 0; j < N; j++) pre[j+1] = pre[j]+S[j];
            c *= 2;
        }
        int d = queries[i].T-c;
        while ((k < Q) && (queries[k].T == queries[i].T)) k++;
        if (!f && (k-i > 10)) {
            f = 1;
            for (j = 0; j < N; j++) pre2[j+1] = pre2[j]+max(S[j],(j >= d) ? S[j-d]:0);
        }
        if (f) ans[queries[i].i] = pre2[queries[i].R+1]-pre2[queries[i].L];
        else {
            LLI a = 0;
            if (d > queries[i].L) a += pre[min(d,queries[i].R+1)]-pre[queries[i].L];
            int e = max(d,queries[i].L);
            if (e <= queries[i].R) {
                int *p = S+e,*q = S+(e-d);
                int t = queries[i].R-e+1;
                int tt = (t/4)*4;
                for (j = 0; j < tt; j += 4) {
                    a += (p[j] > q[j]) ? p[j]:q[j];
                    a += (p[j+1] > q[j+1]) ? p[j+1]:q[j+1];
                    a += (p[j+2] > q[j+2]) ? p[j+2]:q[j+2];
                    a += (p[j+3] > q[j+3]) ? p[j+3]:q[j+3];
                }
                for (; j < t; j++) a += (p[j] > q[j]) ? p[j]:q[j];
            }
            ans[queries[i].i] = a;
        }
    }
    for (i = 0; i < Q; i++) printf("%lld\n",ans[i]);

    return 0;
}

Compilation message

ho_t5.cpp: In function 'int main()':
ho_t5.cpp:53:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 for (k = 0; k < events[j].size(); k++) {
                             ~~^~~~~~~~~~~~~~~~~~
ho_t5.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&N,&Q);
     ~~~~~^~~~~~~~~~~~~~~
ho_t5.cpp:28:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&S[i]);
                             ~~~~~^~~~~~~~~~~~
ho_t5.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d",&T,&L,&R);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 8 ms 5164 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 8 ms 5152 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 8 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 7 ms 5120 KB Output is correct
26 Correct 9 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 8 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 7 ms 5120 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 282 ms 41316 KB Output is correct
3 Correct 281 ms 41700 KB Output is correct
4 Correct 278 ms 40536 KB Output is correct
5 Correct 283 ms 42720 KB Output is correct
6 Correct 289 ms 41948 KB Output is correct
7 Correct 274 ms 40536 KB Output is correct
8 Correct 276 ms 40932 KB Output is correct
9 Correct 300 ms 43620 KB Output is correct
10 Correct 282 ms 40800 KB Output is correct
11 Correct 293 ms 42700 KB Output is correct
12 Correct 289 ms 42076 KB Output is correct
13 Correct 269 ms 40296 KB Output is correct
14 Correct 304 ms 44140 KB Output is correct
15 Correct 289 ms 43588 KB Output is correct
16 Correct 292 ms 45156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 336 ms 43364 KB Output is correct
3 Correct 304 ms 41176 KB Output is correct
4 Correct 341 ms 45672 KB Output is correct
5 Correct 292 ms 39648 KB Output is correct
6 Correct 315 ms 41048 KB Output is correct
7 Correct 287 ms 39268 KB Output is correct
8 Correct 294 ms 39640 KB Output is correct
9 Correct 280 ms 38492 KB Output is correct
10 Correct 285 ms 38500 KB Output is correct
11 Correct 300 ms 41840 KB Output is correct
12 Correct 318 ms 42444 KB Output is correct
13 Correct 314 ms 42148 KB Output is correct
14 Correct 280 ms 38108 KB Output is correct
15 Correct 297 ms 41432 KB Output is correct
16 Correct 296 ms 41188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 17636 KB Output is correct
2 Correct 187 ms 21568 KB Output is correct
3 Correct 179 ms 21864 KB Output is correct
4 Correct 177 ms 21604 KB Output is correct
5 Correct 180 ms 21732 KB Output is correct
6 Correct 173 ms 21220 KB Output is correct
7 Correct 174 ms 21732 KB Output is correct
8 Correct 174 ms 21604 KB Output is correct
9 Correct 177 ms 21732 KB Output is correct
10 Correct 175 ms 21348 KB Output is correct
11 Correct 180 ms 21784 KB Output is correct
12 Correct 177 ms 21860 KB Output is correct
13 Correct 176 ms 21604 KB Output is correct
14 Correct 176 ms 21852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 5120 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 8 ms 5164 KB Output is correct
6 Correct 7 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 7 ms 5120 KB Output is correct
12 Correct 7 ms 5120 KB Output is correct
13 Correct 8 ms 5152 KB Output is correct
14 Correct 7 ms 5120 KB Output is correct
15 Correct 8 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 7 ms 5120 KB Output is correct
26 Correct 9 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 8 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 7 ms 5120 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 297 ms 40388 KB Output is correct
34 Correct 312 ms 47972 KB Output is correct
35 Correct 310 ms 46656 KB Output is correct
36 Correct 304 ms 45156 KB Output is correct
37 Correct 341 ms 48976 KB Output is correct
38 Correct 312 ms 48476 KB Output is correct
39 Correct 320 ms 46936 KB Output is correct
40 Correct 294 ms 44632 KB Output is correct
41 Correct 313 ms 47324 KB Output is correct
42 Correct 312 ms 47208 KB Output is correct
43 Correct 305 ms 46836 KB Output is correct
44 Correct 290 ms 44888 KB Output is correct
45 Correct 298 ms 44188 KB Output is correct
46 Correct 319 ms 49248 KB Output is correct
47 Correct 311 ms 46292 KB Output is correct
48 Correct 303 ms 46948 KB Output is correct
49 Correct 306 ms 48128 KB Output is correct
50 Correct 298 ms 46728 KB Output is correct
51 Correct 304 ms 47196 KB Output is correct
52 Correct 317 ms 47736 KB Output is correct
53 Correct 301 ms 46684 KB Output is correct
54 Correct 307 ms 46224 KB Output is correct
55 Correct 293 ms 47200 KB Output is correct
56 Correct 304 ms 49536 KB Output is correct
57 Correct 281 ms 44256 KB Output is correct
58 Correct 294 ms 45856 KB Output is correct
59 Correct 282 ms 41316 KB Output is correct
60 Correct 281 ms 41700 KB Output is correct
61 Correct 278 ms 40536 KB Output is correct
62 Correct 283 ms 42720 KB Output is correct
63 Correct 289 ms 41948 KB Output is correct
64 Correct 274 ms 40536 KB Output is correct
65 Correct 276 ms 40932 KB Output is correct
66 Correct 300 ms 43620 KB Output is correct
67 Correct 282 ms 40800 KB Output is correct
68 Correct 293 ms 42700 KB Output is correct
69 Correct 289 ms 42076 KB Output is correct
70 Correct 269 ms 40296 KB Output is correct
71 Correct 304 ms 44140 KB Output is correct
72 Correct 289 ms 43588 KB Output is correct
73 Correct 292 ms 45156 KB Output is correct
74 Correct 336 ms 43364 KB Output is correct
75 Correct 304 ms 41176 KB Output is correct
76 Correct 341 ms 45672 KB Output is correct
77 Correct 292 ms 39648 KB Output is correct
78 Correct 315 ms 41048 KB Output is correct
79 Correct 287 ms 39268 KB Output is correct
80 Correct 294 ms 39640 KB Output is correct
81 Correct 280 ms 38492 KB Output is correct
82 Correct 285 ms 38500 KB Output is correct
83 Correct 300 ms 41840 KB Output is correct
84 Correct 318 ms 42444 KB Output is correct
85 Correct 314 ms 42148 KB Output is correct
86 Correct 280 ms 38108 KB Output is correct
87 Correct 297 ms 41432 KB Output is correct
88 Correct 296 ms 41188 KB Output is correct
89 Correct 173 ms 17636 KB Output is correct
90 Correct 187 ms 21568 KB Output is correct
91 Correct 179 ms 21864 KB Output is correct
92 Correct 177 ms 21604 KB Output is correct
93 Correct 180 ms 21732 KB Output is correct
94 Correct 173 ms 21220 KB Output is correct
95 Correct 174 ms 21732 KB Output is correct
96 Correct 174 ms 21604 KB Output is correct
97 Correct 177 ms 21732 KB Output is correct
98 Correct 175 ms 21348 KB Output is correct
99 Correct 180 ms 21784 KB Output is correct
100 Correct 177 ms 21860 KB Output is correct
101 Correct 176 ms 21604 KB Output is correct
102 Correct 176 ms 21852 KB Output is correct