답안 #1005396

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1005396 2024-06-22T11:57:08 Z Unforgettablepl 별자리 3 (JOI20_constellation3) C++17
100 / 100
408 ms 158700 KB
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define int long long

struct segtree{
    vector<pair<int,int>> tree;
    segtree():tree(524288){}
    void update(int k,int x){
        k+=262144;
        tree[k] = {x,k-262144};
        while(k/=2){
            tree[k]=max(tree[2*k],tree[2*k+1]);
        }
    }
    int get(int l,int r){
        l+=262144;r+=262144;
        pair<int,int> ans = {0,0};
        while(l<=r){
            if(l&1)ans=max(ans,tree[l++]);
            if(r%2==0)ans=max(ans,tree[r--]);
            l/=2;r/=2;
        }
        return ans.second;
    }
};

pair<int,int> adj[200001];
int tree[200001];
vector<pair<int,int>> chains[200001];
int arr[200001];
int stidx[200001];
int lift[18][200001];
int idx[200001];
int DP[200001];
segtree seg;
int c;

void place(int x,int y,int c){
    int node = idx[x];
    for(int bit=17;bit>=0;bit--){
        if(tree[lift[bit][node]]<y)node = lift[bit][node];
    }
    chains[node].emplace_back(x,c);
}

void build(int x,int l,int r){
    int mid = seg.get(l,r);
    idx[mid] = x;
    tree[x] = arr[mid];
    stidx[x] = mid;
    if(mid!=l){
        adj[x].first = ++c;
        lift[0][c] = x;
        build(c,l,mid-1);
    }
    if(mid!=r){
        adj[x].second = ++c;
        lift[0][c] = x;
        build(c,mid+1,r);
    }
}

void merge(pair<int,map<int,int>> &a,pair<int,map<int,int>> &b){
    if(a.second.size()<b.second.size())swap(a,b);
    int offset = b.first-a.first;
    for(auto[x,y]:b.second){
        a.second[x] = y+offset; 
    }
}

pair<int,map<int,int>> calc(int x){
    pair<int,map<int,int>> curr = {0,{}};
    pair<int,map<int,int>> l = {0,{}},r = {0,{}};
    if(adj[x].first)l=calc(adj[x].first);
    if(adj[x].second)r=calc(adj[x].second);
    l.first+=DP[adj[x].second];
    r.first+=DP[adj[x].first];
    merge(curr,l);
    merge(curr,r);
    DP[x] = DP[adj[x].first]+DP[adj[x].second];
    curr.second[stidx[x]] = DP[x]-curr.first;
    for(auto[a,b]:chains[x]){
        DP[x] = max(DP[x],curr.second[a]+curr.first+b);
    }
    return move(curr);
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    for(int i=1;i<=n;i++){cin>>arr[i];seg.update(i,arr[i]);}
    build(++c,1,n);
    assert(c==n);
    for(int bit=1;bit<18;bit++){
        for(int i=1;i<=n;i++){
            lift[bit][i] = lift[bit-1][lift[bit-1][i]];
        }
    }
    tree[0] = 1e15;
    int m;
    cin >> m;
    int sum = 0;
    for(int i=1;i<=m;i++){
        int x,y,c;cin>>x>>y>>c;
        place(x,y,c);
        sum+=c;
    }
    calc(1);
    cout << sum-DP[1] << '\n';
}

Compilation message

constellation3.cpp: In function 'std::pair<long long int, std::map<long long int, long long int> > calc(long long int)':
constellation3.cpp:87:16: warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]
   87 |     return move(curr);
      |            ~~~~^~~~~~
constellation3.cpp:87:16: note: remove 'std::move' call
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 49752 KB Output is correct
2 Correct 9 ms 49728 KB Output is correct
3 Correct 9 ms 49752 KB Output is correct
4 Correct 6 ms 49756 KB Output is correct
5 Correct 6 ms 49752 KB Output is correct
6 Correct 6 ms 49756 KB Output is correct
7 Correct 6 ms 49680 KB Output is correct
8 Correct 6 ms 49756 KB Output is correct
9 Correct 6 ms 49756 KB Output is correct
10 Correct 6 ms 49756 KB Output is correct
11 Correct 8 ms 49756 KB Output is correct
12 Correct 9 ms 49756 KB Output is correct
13 Correct 8 ms 49752 KB Output is correct
14 Correct 6 ms 49756 KB Output is correct
15 Correct 8 ms 49756 KB Output is correct
16 Correct 8 ms 49912 KB Output is correct
17 Correct 7 ms 49756 KB Output is correct
18 Correct 6 ms 49756 KB Output is correct
19 Correct 6 ms 49756 KB Output is correct
20 Correct 6 ms 49756 KB Output is correct
21 Correct 9 ms 49752 KB Output is correct
22 Correct 7 ms 49752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 49752 KB Output is correct
2 Correct 9 ms 49728 KB Output is correct
3 Correct 9 ms 49752 KB Output is correct
4 Correct 6 ms 49756 KB Output is correct
5 Correct 6 ms 49752 KB Output is correct
6 Correct 6 ms 49756 KB Output is correct
7 Correct 6 ms 49680 KB Output is correct
8 Correct 6 ms 49756 KB Output is correct
9 Correct 6 ms 49756 KB Output is correct
10 Correct 6 ms 49756 KB Output is correct
11 Correct 8 ms 49756 KB Output is correct
12 Correct 9 ms 49756 KB Output is correct
13 Correct 8 ms 49752 KB Output is correct
14 Correct 6 ms 49756 KB Output is correct
15 Correct 8 ms 49756 KB Output is correct
16 Correct 8 ms 49912 KB Output is correct
17 Correct 7 ms 49756 KB Output is correct
18 Correct 6 ms 49756 KB Output is correct
19 Correct 6 ms 49756 KB Output is correct
20 Correct 6 ms 49756 KB Output is correct
21 Correct 9 ms 49752 KB Output is correct
22 Correct 7 ms 49752 KB Output is correct
23 Correct 10 ms 49756 KB Output is correct
24 Correct 8 ms 49852 KB Output is correct
25 Correct 10 ms 49996 KB Output is correct
26 Correct 7 ms 50012 KB Output is correct
27 Correct 11 ms 50012 KB Output is correct
28 Correct 10 ms 50012 KB Output is correct
29 Correct 11 ms 49756 KB Output is correct
30 Correct 8 ms 49756 KB Output is correct
31 Correct 7 ms 49984 KB Output is correct
32 Correct 7 ms 50268 KB Output is correct
33 Correct 10 ms 50268 KB Output is correct
34 Correct 8 ms 50268 KB Output is correct
35 Correct 8 ms 50104 KB Output is correct
36 Correct 8 ms 50264 KB Output is correct
37 Correct 8 ms 50196 KB Output is correct
38 Correct 7 ms 50696 KB Output is correct
39 Correct 7 ms 49992 KB Output is correct
40 Correct 8 ms 50328 KB Output is correct
41 Correct 9 ms 49752 KB Output is correct
42 Correct 10 ms 50012 KB Output is correct
43 Correct 8 ms 50520 KB Output is correct
44 Correct 8 ms 49756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 49752 KB Output is correct
2 Correct 9 ms 49728 KB Output is correct
3 Correct 9 ms 49752 KB Output is correct
4 Correct 6 ms 49756 KB Output is correct
5 Correct 6 ms 49752 KB Output is correct
6 Correct 6 ms 49756 KB Output is correct
7 Correct 6 ms 49680 KB Output is correct
8 Correct 6 ms 49756 KB Output is correct
9 Correct 6 ms 49756 KB Output is correct
10 Correct 6 ms 49756 KB Output is correct
11 Correct 8 ms 49756 KB Output is correct
12 Correct 9 ms 49756 KB Output is correct
13 Correct 8 ms 49752 KB Output is correct
14 Correct 6 ms 49756 KB Output is correct
15 Correct 8 ms 49756 KB Output is correct
16 Correct 8 ms 49912 KB Output is correct
17 Correct 7 ms 49756 KB Output is correct
18 Correct 6 ms 49756 KB Output is correct
19 Correct 6 ms 49756 KB Output is correct
20 Correct 6 ms 49756 KB Output is correct
21 Correct 9 ms 49752 KB Output is correct
22 Correct 7 ms 49752 KB Output is correct
23 Correct 10 ms 49756 KB Output is correct
24 Correct 8 ms 49852 KB Output is correct
25 Correct 10 ms 49996 KB Output is correct
26 Correct 7 ms 50012 KB Output is correct
27 Correct 11 ms 50012 KB Output is correct
28 Correct 10 ms 50012 KB Output is correct
29 Correct 11 ms 49756 KB Output is correct
30 Correct 8 ms 49756 KB Output is correct
31 Correct 7 ms 49984 KB Output is correct
32 Correct 7 ms 50268 KB Output is correct
33 Correct 10 ms 50268 KB Output is correct
34 Correct 8 ms 50268 KB Output is correct
35 Correct 8 ms 50104 KB Output is correct
36 Correct 8 ms 50264 KB Output is correct
37 Correct 8 ms 50196 KB Output is correct
38 Correct 7 ms 50696 KB Output is correct
39 Correct 7 ms 49992 KB Output is correct
40 Correct 8 ms 50328 KB Output is correct
41 Correct 9 ms 49752 KB Output is correct
42 Correct 10 ms 50012 KB Output is correct
43 Correct 8 ms 50520 KB Output is correct
44 Correct 8 ms 49756 KB Output is correct
45 Correct 360 ms 80464 KB Output is correct
46 Correct 331 ms 78928 KB Output is correct
47 Correct 299 ms 79556 KB Output is correct
48 Correct 305 ms 78512 KB Output is correct
49 Correct 284 ms 77028 KB Output is correct
50 Correct 292 ms 80720 KB Output is correct
51 Correct 304 ms 78228 KB Output is correct
52 Correct 327 ms 79184 KB Output is correct
53 Correct 279 ms 78932 KB Output is correct
54 Correct 396 ms 130816 KB Output is correct
55 Correct 408 ms 115748 KB Output is correct
56 Correct 328 ms 107112 KB Output is correct
57 Correct 307 ms 101200 KB Output is correct
58 Correct 282 ms 113624 KB Output is correct
59 Correct 282 ms 113488 KB Output is correct
60 Correct 223 ms 158700 KB Output is correct
61 Correct 272 ms 78152 KB Output is correct
62 Correct 363 ms 137812 KB Output is correct
63 Correct 304 ms 75352 KB Output is correct
64 Correct 316 ms 78540 KB Output is correct
65 Correct 402 ms 139088 KB Output is correct
66 Correct 274 ms 75596 KB Output is correct