Submission #200922

# Submission time Handle Problem Language Result Execution time Memory
200922 2020-02-08T15:06:40 Z mhy908 Triple Jump (JOI19_jumps) C++14
100 / 100
2733 ms 54356 KB
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortvec(x) sort(all(x))
#define compress(x) x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=1987654321987654321;
const int inf=2000000000;
int n, q;
LL arr[500010], ans[500010];
pair<pii, int> query[500010];
vector<pii> poss;
int stck[500010], re;
struct SEGMENT_TREE{
    int x;
    struct NODE{
        LL f, arr, ans;
    }tree[2000000];
    void update_arr(int point, int s, int e, int num, LL qu){
        if(s==e){
            tree[point].arr=qu;
            tree[point].ans=tree[point].arr+tree[point].f;
            return;
        }
        int mid=(s+e)>>1;
        if(num<=mid)update_arr(point*2, s, mid, num, qu);
        else update_arr(point*2+1, mid+1, e, num, qu);
        tree[point].ans=max(max(tree[point*2].ans, tree[point*2+1].ans), tree[point*2].f+tree[point*2+1].arr);
        tree[point].f=max(tree[point*2].f, tree[point*2+1].f);
        tree[point].arr=max(tree[point*2].arr, tree[point*2+1].arr);
    }
    void update_f(int point, int s, int e, int num, LL qu){
        if(s==e){
            tree[point].f=max(tree[point].f, qu);
            tree[point].ans=tree[point].arr+tree[point].f;
            return;
        }
        int mid=(s+e)>>1;
        if(num<=mid)update_f(point*2, s, mid, num, qu);
        else update_f(point*2+1, mid+1, e, num, qu);
        tree[point].ans=max(max(tree[point*2].ans, tree[point*2+1].ans), tree[point*2].f+tree[point*2+1].arr);
        tree[point].f=max(tree[point*2].f, tree[point*2+1].f);
        tree[point].arr=max(tree[point*2].arr, tree[point*2+1].arr);
    }
    LL query_arr(int point, int s, int e, int a, int b){
        if(s>=a&&e<=b)return tree[point].arr;
        if(s>b||e<a)return -llinf;
        int mid=(s+e)>>1;
        return max(query_arr(point*2, s, mid, a, b), query_arr(point*2+1, mid+1, e, a, b));
    }
    LL query_f(int point, int s, int e, int a, int b){
        if(s>=a&&e<=b)return tree[point].f;
        if(s>b||e<a)return -llinf;
        int mid=(s+e)>>1;
        return max(query_f(point*2, s, mid, a, b), query_f(point*2+1, mid+1, e, a, b));
    }
    LL query(int point, int s, int e, int a, int b){
        if(s>=a&&e<=b)return tree[point].ans;
        if(s>b||e<a)return -llinf;
        int mid=(s+e)>>1;
        return max(max(query(point*2, s, mid, a, b), query(point*2+1, mid+1, e, a, b)), query_f(point*2, s, mid, a, b)+query_arr(point*2+1, mid+1, e, a, b));
    }
}seg;

int main(){
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        scanf("%lld", &arr[i]);
        seg.update_arr(1, 1, n, i, arr[i]);
    }
    scanf("%d", &q);
    for(int i=1; i<=q; i++){
        scanf("%d %d", &query[i].F.F, &query[i].F.S);
        query[i].S=i;
    }
    for(int i=1; i<=n; i++){
        while(re&&arr[stck[re]]<=arr[i]){
            poss.pb(mp(stck[re--], i));
        }
        if(re){
            poss.pb(mp(stck[re], i));
        }
        stck[++re]=i;
    }
    sort(all(poss), greater<pii>());
    sort(query+1, query+q+1, greater<pair<pii, int> >());
    int pv=0;
    for(int i=1; i<=q; i++){
        while(pv<poss.size()&&query[i].F.F<=poss[pv].F){
            if(poss[pv].S*2-poss[pv].F<=n){
                seg.update_f(1, 1, n, poss[pv].S*2-poss[pv].F, arr[poss[pv].F]+arr[poss[pv].S]);
                //printf("inserted %d %d\n", poss[pv].F, poss[pv].S);
            }
            pv++;
        }
        //printf("answered %d %d\n", query[i].F.F, query[i].F.S);
        ans[query[i].S]=seg.query(1, 1, n, query[i].F.F, query[i].F.S);
    }
    for(int i=1; i<=q; i++)printf("%lld\n", ans[i]);
}

Compilation message

jumps.cpp: In function 'int main()':
jumps.cpp:100:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(pv<poss.size()&&query[i].F.F<=poss[pv].F){
               ~~^~~~~~~~~~~~
jumps.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
jumps.cpp:79:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", &arr[i]);
         ~~~~~^~~~~~~~~~~~~~~~~
jumps.cpp:82:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &q);
     ~~~~~^~~~~~~~~~
jumps.cpp:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &query[i].F.F, &query[i].F.S);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 930 ms 18424 KB Output is correct
12 Correct 941 ms 18168 KB Output is correct
13 Correct 910 ms 18168 KB Output is correct
14 Correct 932 ms 18312 KB Output is correct
15 Correct 916 ms 18296 KB Output is correct
16 Correct 925 ms 17784 KB Output is correct
17 Correct 939 ms 17656 KB Output is correct
18 Correct 931 ms 17912 KB Output is correct
19 Correct 929 ms 18296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 164 ms 20196 KB Output is correct
2 Correct 111 ms 18152 KB Output is correct
3 Correct 119 ms 18668 KB Output is correct
4 Correct 179 ms 20196 KB Output is correct
5 Correct 159 ms 20196 KB Output is correct
6 Correct 154 ms 19556 KB Output is correct
7 Correct 154 ms 19428 KB Output is correct
8 Correct 151 ms 19428 KB Output is correct
9 Correct 158 ms 19816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 5 ms 376 KB Output is correct
9 Correct 5 ms 376 KB Output is correct
10 Correct 5 ms 376 KB Output is correct
11 Correct 930 ms 18424 KB Output is correct
12 Correct 941 ms 18168 KB Output is correct
13 Correct 910 ms 18168 KB Output is correct
14 Correct 932 ms 18312 KB Output is correct
15 Correct 916 ms 18296 KB Output is correct
16 Correct 925 ms 17784 KB Output is correct
17 Correct 939 ms 17656 KB Output is correct
18 Correct 931 ms 17912 KB Output is correct
19 Correct 929 ms 18296 KB Output is correct
20 Correct 164 ms 20196 KB Output is correct
21 Correct 111 ms 18152 KB Output is correct
22 Correct 119 ms 18668 KB Output is correct
23 Correct 179 ms 20196 KB Output is correct
24 Correct 159 ms 20196 KB Output is correct
25 Correct 154 ms 19556 KB Output is correct
26 Correct 154 ms 19428 KB Output is correct
27 Correct 151 ms 19428 KB Output is correct
28 Correct 158 ms 19816 KB Output is correct
29 Correct 2733 ms 54352 KB Output is correct
30 Correct 2564 ms 50396 KB Output is correct
31 Correct 2595 ms 52312 KB Output is correct
32 Correct 2725 ms 54224 KB Output is correct
33 Correct 2699 ms 54356 KB Output is correct
34 Correct 2710 ms 53640 KB Output is correct
35 Correct 2717 ms 53372 KB Output is correct
36 Correct 2726 ms 53496 KB Output is correct
37 Correct 2720 ms 53840 KB Output is correct
38 Correct 2267 ms 54124 KB Output is correct
39 Correct 2256 ms 54096 KB Output is correct
40 Correct 2242 ms 52156 KB Output is correct
41 Correct 2299 ms 52048 KB Output is correct
42 Correct 2236 ms 51932 KB Output is correct
43 Correct 2240 ms 52584 KB Output is correct
44 Correct 2328 ms 53584 KB Output is correct
45 Correct 2335 ms 53712 KB Output is correct
46 Correct 2408 ms 51792 KB Output is correct
47 Correct 2335 ms 51776 KB Output is correct
48 Correct 2348 ms 51592 KB Output is correct
49 Correct 2349 ms 52560 KB Output is correct
50 Correct 2441 ms 52948 KB Output is correct
51 Correct 2442 ms 53108 KB Output is correct
52 Correct 2414 ms 52200 KB Output is correct
53 Correct 2406 ms 52028 KB Output is correct
54 Correct 2434 ms 51712 KB Output is correct
55 Correct 2607 ms 52428 KB Output is correct