답안 #590174

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
590174 2022-07-05T15:42:59 Z Huy 3단 점프 (JOI19_jumps) C++17
100 / 100
1400 ms 83624 KB
/// no sound bitch
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,ll>
#define fi first
#define se second
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")*/
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
const ll N = (int)1e9 + 1;
const int maxN = (int)5e5 + 1;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const ll infty = 1e18 + 7;
const int base = (int)4e5;

void InputFile()
{
    //freopen("scrivener.inp","r",stdin);
    //freopen("scrivener.out","w",stdout);
    //freopen("test.out","r",stdin);
}

void FastInput()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

struct TQuery
{
    int lf,rt,id;
};
vector<TQuery> save_query;

bool FQ(TQuery x,TQuery y)
{
    return x.lf > y.lf;
}

int n;
int q;
int a[maxN];
vector<pii> req;
int res[maxN];

struct Node
{
    int max_a;
    int lazy;
    int val;
};

Node ST[4*maxN];

void Build(int id,int l,int r)
{
    if(l == r)
    {
        ST[id].max_a = ST[id].val = a[l];
        ST[id].lazy = 0;
        return;
    }
    int mid = (l + r) / 2;
    Build(id * 2,l,mid);
    Build(id * 2 + 1,mid + 1,r);
    ST[id].max_a = ST[id].val = max(ST[id*2].max_a,ST[id*2+1].max_a);
    ST[id].lazy = 0;
}

void Down(int id)
{
    if(!ST[id].lazy) return;
    ST[id*2].val = max(ST[id*2].val,ST[id*2].max_a + ST[id].lazy);
    ST[id*2].lazy = max(ST[id*2].lazy,ST[id].lazy);
    ST[id*2+1].val = max(ST[id*2+1].val,ST[id*2+1].max_a + ST[id].lazy);
    ST[id*2+1].lazy = max(ST[id*2+1].lazy,ST[id].lazy);
    ST[id].lazy = 0;
}

void Update(int id,int l,int r,int u,int v,int n_val)
{
    if(v < l || u > r) return;
    if(u <= l && r <= v)
    {
        ST[id].val = max(ST[id].val,ST[id].max_a + n_val);
        ST[id].lazy = max(ST[id].lazy,n_val);
        return;
    }
    Down(id);
    int mid = (l + r) / 2;
    Update(id * 2,l,mid,u,v,n_val);
    Update(id * 2 + 1,mid + 1,r,u,v,n_val);
    ST[id].val = max(ST[id*2].val,ST[id*2+1].val);
}

int Get(int id,int l,int r,int u,int v)
{
    if(v < l || u > r) return -infty;
    if(u <= l && r <= v)
    {
        return ST[id].val;
    }
    Down(id);
    int mid = (l + r) / 2;
    return max(Get(id * 2,l,mid,u,v),Get(id * 2 + 1,mid + 1,r,u,v));
}

void Read()
{
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
    }
    cin >> q;
    for(int i = 1;i <= q;i++)
    {
        int l,r;
        cin >> l >> r;
        save_query.push_back({l,r,i});
    }
}

void Solve()
{
    //cout <<"dark";return;
    vector<int> vc;
    for(int i = n;i >= 1;i--)
    {
        if(vc.empty()) vc.push_back(i);
        else
        {
            while(!vc.empty() && a[vc.back()] < a[i])
            {
                //req.push_back({i,vc.back()});
                vc.pop_back();
            }
            if(!vc.empty()) req.push_back({i,vc.back()});
            vc.push_back(i);
        }
    }
    vc.clear();
    for(int i = 1;i <= n;i++)
    {
        if(vc.empty()) vc.push_back(i);
        else
        {
            while(!vc.empty() && a[vc.back()] < a[i])
            {
                //req.push_back({vc.back()});
                vc.pop_back();
            }
            if(!vc.empty()) req.push_back({vc.back(),i});
            vc.push_back(i);
        }
    }
    sort(req.begin(),req.end(),greater<pii>());
    sort(save_query.begin(),save_query.end(),FQ);
    /*for(pii tmp : req)
    {
        cout <<'*'<<' '<< tmp.fi <<' '<< tmp.se <<'\n';
    }
    return;*/
    Build(1,1,n);
    int pt = 0;
    for(TQuery tmp : save_query)
    {
        while(pt < req.size() && req[pt].fi >= tmp.lf)
        {
            if(2 * req[pt].se - req[pt].fi <= n)
            {
                Update(1,1,n,2*req[pt].se-req[pt].fi,n,a[req[pt].fi]+a[req[pt].se]);
            }
            pt++;
        }
        res[tmp.id] = Get(1,1,n,tmp.lf+2,tmp.rt);
    }
    for(int i = 1;i <= q;i++)
    {
        cout << res[i] <<'\n';
    }
}

void Debug()
{

}


int32_t main()
{
    FastInput();
    //InputFile();
    //int sub_type;
    //cin >> sub_type;
    //Sieve();
    //Prepare();
    int test;
    //cin >> test;
    test = 1;
    while(test--)
        //for(int prc = 1; prc <= test; prc++)
    {
        Read();
        Solve();
        //Debug();
    }
}

Compilation message

jumps.cpp: In function 'void Solve()':
jumps.cpp:175:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  175 |         while(pt < req.size() && req[pt].fi >= tmp.lf)
      |               ~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 317 ms 26540 KB Output is correct
12 Correct 311 ms 26268 KB Output is correct
13 Correct 360 ms 26316 KB Output is correct
14 Correct 370 ms 26536 KB Output is correct
15 Correct 359 ms 26532 KB Output is correct
16 Correct 343 ms 25888 KB Output is correct
17 Correct 402 ms 25740 KB Output is correct
18 Correct 624 ms 25744 KB Output is correct
19 Correct 469 ms 26388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 22324 KB Output is correct
2 Correct 110 ms 20848 KB Output is correct
3 Correct 143 ms 22780 KB Output is correct
4 Correct 294 ms 22328 KB Output is correct
5 Correct 230 ms 22348 KB Output is correct
6 Correct 216 ms 21680 KB Output is correct
7 Correct 210 ms 21692 KB Output is correct
8 Correct 187 ms 21524 KB Output is correct
9 Correct 218 ms 21880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 317 ms 26540 KB Output is correct
12 Correct 311 ms 26268 KB Output is correct
13 Correct 360 ms 26316 KB Output is correct
14 Correct 370 ms 26536 KB Output is correct
15 Correct 359 ms 26532 KB Output is correct
16 Correct 343 ms 25888 KB Output is correct
17 Correct 402 ms 25740 KB Output is correct
18 Correct 624 ms 25744 KB Output is correct
19 Correct 469 ms 26388 KB Output is correct
20 Correct 206 ms 22324 KB Output is correct
21 Correct 110 ms 20848 KB Output is correct
22 Correct 143 ms 22780 KB Output is correct
23 Correct 294 ms 22328 KB Output is correct
24 Correct 230 ms 22348 KB Output is correct
25 Correct 216 ms 21680 KB Output is correct
26 Correct 210 ms 21692 KB Output is correct
27 Correct 187 ms 21524 KB Output is correct
28 Correct 218 ms 21880 KB Output is correct
29 Correct 1204 ms 83624 KB Output is correct
30 Correct 1375 ms 78808 KB Output is correct
31 Correct 931 ms 73488 KB Output is correct
32 Correct 1276 ms 81400 KB Output is correct
33 Correct 1322 ms 81412 KB Output is correct
34 Correct 1342 ms 80920 KB Output is correct
35 Correct 1400 ms 80804 KB Output is correct
36 Correct 1291 ms 80868 KB Output is correct
37 Correct 1367 ms 81420 KB Output is correct
38 Correct 1075 ms 81420 KB Output is correct
39 Correct 1293 ms 81408 KB Output is correct
40 Correct 1302 ms 79912 KB Output is correct
41 Correct 918 ms 79568 KB Output is correct
42 Correct 931 ms 79592 KB Output is correct
43 Correct 912 ms 80564 KB Output is correct
44 Correct 912 ms 81352 KB Output is correct
45 Correct 962 ms 81372 KB Output is correct
46 Correct 927 ms 80008 KB Output is correct
47 Correct 1124 ms 79912 KB Output is correct
48 Correct 842 ms 79916 KB Output is correct
49 Correct 1139 ms 81036 KB Output is correct
50 Correct 954 ms 81472 KB Output is correct
51 Correct 1012 ms 83096 KB Output is correct
52 Correct 880 ms 80704 KB Output is correct
53 Correct 935 ms 80616 KB Output is correct
54 Correct 981 ms 80680 KB Output is correct
55 Correct 934 ms 81700 KB Output is correct