Submission #372532

# Submission time Handle Problem Language Result Execution time Memory
372532 2021-02-28T17:32:10 Z erfanmir Triple Jump (JOI19_jumps) C++17
100 / 100
1388 ms 87992 KB
// In the name of god

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds; 

template < class T > using ordered_set = tree < T , null_type , less < T > , rb_tree_tag , tree_order_statistics_node_update >;
  
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define lc(x)                       (x) << 1
#define rc(x)                       (x) << 1 | 1
#define F                           first
#define S                           second
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define mp                          make_pair
ll poww(ll a, ll b, ll md) {
    ll ans = 1;
    for(; b; b >>= 1, a = a * a % md){
        if(b & 1){
            ans = ans * a % md;
        }
    }
    return ans % md;
}

struct node{
    int x;
    int y;
    int res;
};

node mrg(node A, node B){
    node C;
    C.res = max({A.res, B.res, A.x + B.y});
    C.x = max(A.x, B.x);
    C.y = max(A.y, B.y);
    return C;
}

const int MAXN = 5e5 + 10;
const int MAXLG = 18;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const ll INF = 8e18;
int a[MAXN], n, q, ans[MAXN];
node seg[MAXN * 4];
vector<int> pre[MAXN], st;
vector<pii> que[MAXN];

void upd(int ind, int val1, int val2, int s = 0, int e = n, int v = 1){
    if(e - s < 2){
        seg[v].x = max(seg[v].x, val1);
        seg[v].y = max(seg[v].y, val2);
        seg[v].res = max(seg[v].res, seg[v].x + seg[v].y);
        return;
    }
    int mid = (s + e) / 2;
    if(ind < mid){
        upd(ind, val1, val2, s, mid, lc(v));
    }
    else{
        upd(ind, val1, val2, mid, e, rc(v));
    }
    seg[v] = mrg(seg[lc(v)], seg[rc(v)]);
}

node get(int l, int r, int s = 0, int e = n, int v = 1){
    if(r <= s || e <= l){
        return {0, 0, 0};
    }
    if(l <= s && e <= r){
        return seg[v];
    }
    int mid = (s + e) / 2;
    return mrg(get(l, r, s, mid, lc(v)), get(l, r, mid, e, rc(v)));
}

int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", a + i);
    }
    for(int i = 0; i < n; i++){
        while((int)st.size() && a[i] > a[st.back()]){
            pre[st.back()].push_back(i);
            st.pop_back();
        }
        if((int)st.size()){
            pre[st.back()].push_back(i);
        }
        st.push_back(i);
    }
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int l, r;
        scanf("%d %d", &l, &r);
        l--;
        que[l].push_back(mp(r, i));
    }

    for(int i = n - 1; ~i; i--){
        upd(i, 0, a[i]);
        for(auto u : pre[i]){
            int p = 2 * u - i;
            if(p < n){
                upd(p, a[i] + a[u], 0);
            }
        }
        for(auto u : que[i]){
            node w = get(i, u.F);
            ans[u.S] = w.res;
        }
    }

    for(int i = 0; i < q; i++){
        printf("%d\n", ans[i]);
    }
}

Compilation message

jumps.cpp:3: warning: ignoring #pragma comment  [-Wunknown-pragmas]
    3 | #pragma comment(linker, "/stack:200000000")
      | 
jumps.cpp: In function 'int main()':
jumps.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   90 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
jumps.cpp:92:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   92 |         scanf("%d", a + i);
      |         ~~~~~^~~~~~~~~~~~~
jumps.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  104 |     scanf("%d", &q);
      |     ~~~~~^~~~~~~~~~
jumps.cpp:107:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  107 |         scanf("%d %d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23788 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 18 ms 23788 KB Output is correct
4 Correct 18 ms 23884 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23788 KB Output is correct
8 Correct 18 ms 23788 KB Output is correct
9 Correct 18 ms 23788 KB Output is correct
10 Correct 18 ms 23788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23788 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 18 ms 23788 KB Output is correct
4 Correct 18 ms 23884 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23788 KB Output is correct
8 Correct 18 ms 23788 KB Output is correct
9 Correct 18 ms 23788 KB Output is correct
10 Correct 18 ms 23788 KB Output is correct
11 Correct 414 ms 42808 KB Output is correct
12 Correct 434 ms 42732 KB Output is correct
13 Correct 425 ms 43096 KB Output is correct
14 Correct 413 ms 42900 KB Output is correct
15 Correct 421 ms 43008 KB Output is correct
16 Correct 443 ms 42220 KB Output is correct
17 Correct 414 ms 42220 KB Output is correct
18 Correct 430 ms 42092 KB Output is correct
19 Correct 418 ms 42868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 198 ms 39020 KB Output is correct
2 Correct 166 ms 38764 KB Output is correct
3 Correct 141 ms 39648 KB Output is correct
4 Correct 197 ms 39020 KB Output is correct
5 Correct 199 ms 39020 KB Output is correct
6 Correct 193 ms 38380 KB Output is correct
7 Correct 189 ms 38252 KB Output is correct
8 Correct 199 ms 38252 KB Output is correct
9 Correct 195 ms 38636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23788 KB Output is correct
2 Correct 17 ms 23808 KB Output is correct
3 Correct 18 ms 23788 KB Output is correct
4 Correct 18 ms 23884 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 18 ms 23788 KB Output is correct
8 Correct 18 ms 23788 KB Output is correct
9 Correct 18 ms 23788 KB Output is correct
10 Correct 18 ms 23788 KB Output is correct
11 Correct 414 ms 42808 KB Output is correct
12 Correct 434 ms 42732 KB Output is correct
13 Correct 425 ms 43096 KB Output is correct
14 Correct 413 ms 42900 KB Output is correct
15 Correct 421 ms 43008 KB Output is correct
16 Correct 443 ms 42220 KB Output is correct
17 Correct 414 ms 42220 KB Output is correct
18 Correct 430 ms 42092 KB Output is correct
19 Correct 418 ms 42868 KB Output is correct
20 Correct 198 ms 39020 KB Output is correct
21 Correct 166 ms 38764 KB Output is correct
22 Correct 141 ms 39648 KB Output is correct
23 Correct 197 ms 39020 KB Output is correct
24 Correct 199 ms 39020 KB Output is correct
25 Correct 193 ms 38380 KB Output is correct
26 Correct 189 ms 38252 KB Output is correct
27 Correct 199 ms 38252 KB Output is correct
28 Correct 195 ms 38636 KB Output is correct
29 Correct 1299 ms 82448 KB Output is correct
30 Correct 1144 ms 81772 KB Output is correct
31 Correct 1141 ms 83804 KB Output is correct
32 Correct 1388 ms 82376 KB Output is correct
33 Correct 1323 ms 82156 KB Output is correct
34 Correct 1272 ms 80240 KB Output is correct
35 Correct 1285 ms 79852 KB Output is correct
36 Correct 1281 ms 79852 KB Output is correct
37 Correct 1286 ms 81168 KB Output is correct
38 Correct 984 ms 87916 KB Output is correct
39 Correct 976 ms 87992 KB Output is correct
40 Correct 954 ms 84716 KB Output is correct
41 Correct 957 ms 84140 KB Output is correct
42 Correct 961 ms 84144 KB Output is correct
43 Correct 964 ms 85868 KB Output is correct
44 Correct 1020 ms 87404 KB Output is correct
45 Correct 1038 ms 87272 KB Output is correct
46 Correct 1006 ms 84200 KB Output is correct
47 Correct 1008 ms 83820 KB Output is correct
48 Correct 1010 ms 83820 KB Output is correct
49 Correct 1022 ms 85996 KB Output is correct
50 Correct 1107 ms 85512 KB Output is correct
51 Correct 1092 ms 85484 KB Output is correct
52 Correct 1084 ms 83040 KB Output is correct
53 Correct 1105 ms 82796 KB Output is correct
54 Correct 1080 ms 82540 KB Output is correct
55 Correct 1083 ms 84228 KB Output is correct