Submission #1050127

# Submission time Handle Problem Language Result Execution time Memory
1050127 2024-08-09T07:36:31 Z 변재우(#11050) Constellation 3 (JOI20_constellation3) C++17
100 / 100
304 ms 119420 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;


const int Nmax=200010;
int N, M, K, all, G[Nmax], A[Nmax], L[Nmax], R[Nmax], Lo[Nmax], Hi[Nmax], D[Nmax], E[Nmax], S[Nmax];;
int P[Nmax], p, I[Nmax], Siz[Nmax], Top[Nmax];
vector<pair<int, int>> T[Nmax], TT[Nmax], tV, X[Nmax];
vector<int> adj[Nmax];
stack<pair<int, int>> Stk;

class Fenwick {
public:
    int Tree[Nmax];
    void Update(int k, int v) {
        for(int i=k; i<=N; i+=i&-i) Tree[i]+=v;
    }
    int Query(int k) {
        int ret=0;
        for(int i=k; i>=1; i-=i&-i) ret+=Tree[i];
        return ret;
    }
    int Query(int l, int r) {return Query(r)-Query(l-1);}
}H;

int Find(int x) {return G[x]?(G[x]=Find(G[x])):x;}
void Union(int u, int v) {
    u=Find(u), v=Find(v);
    if(u!=v) G[u]=v;
}

set<pair<pair<int, int>, int>> DFS1(int curr) {
    set<pair<pair<int, int>, int>> ret;
    for(auto x:T[curr]) ret.insert({{x.first, curr}, x.second});
    for(int next:adj[curr]) {
        P[next]=curr;
        auto tmp=DFS1(next);
        if(ret.size()<tmp.size()) swap(ret, tmp);
        for(auto x:tmp) ret.insert(x);
    }
    while(true) {
        auto p=ret.lower_bound(make_pair(make_pair(Lo[curr], 0ll), 0ll));
        if(p!=ret.end() && (*p).first.first<=Hi[curr]) X[curr].push_back({(*p).first.second, (*p).second}), ret.erase(p);
        else break;
    }
    return ret;
}

void DFSH1(int curr) {
    Siz[curr]=1;
    for(int next:adj[curr]) {
        DFSH1(next);
        Siz[curr]+=Siz[next];
    }
}

void DFSH2(int curr) {
    I[curr]=++p;
    for(int &next:adj[curr]) if(Siz[next]>Siz[adj[curr][0]]) swap(adj[curr][0], next);
    for(int next:adj[curr]) {
        if(next==adj[curr][0]) Top[next]=Top[curr];
        else Top[next]=next;
        DFSH2(next);
    }
}

int Query(int u, int v) {
    if(u==v) return 0;
    int ret=0;
    while(Top[u]!=Top[v]) {
        ret+=H.Query(I[Top[u]], I[u]);
        u=P[Top[u]];
    }
    ret+=H.Query(I[v], I[u]);
    return ret;
}

void DFS(int curr) {
    for(int next:adj[curr]) DFS(next), D[curr]+=D[next];
    S[curr]=D[curr];
    for(int next:adj[curr]) E[next]=D[curr]-D[next], H.Update(I[next], E[next]);
    for(auto [x, v]:X[curr]) {
        int tmp=S[Find(x)]+Query(Find(x), curr);
        D[curr]=max(D[curr], tmp+v);
    }
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>N;
    for(int i=1; i<=N; i++) cin>>A[i], A[i]=N-A[i]+1;
    cin>>M;
    for(int i=1; i<=M; i++) {
        int x, y, v; cin>>x>>y>>v;
        y=N-y+2, all+=v;
        TT[x].push_back({y, v});
    }
    Stk.push({0, 0});
    for(int i=1; i<=N; i++) {
        while(Stk.top().first>=A[i]) Stk.pop();
        L[i]=Stk.top().second;
        Stk.push({A[i], i});
    }
    while(!Stk.empty()) Stk.pop();
    Stk.push({0, N+1});
    for(int i=N; i>=1; i--) {
        while(Stk.top().first>=A[i]) Stk.pop();
        R[i]=Stk.top().second;
        Stk.push({A[i], i});
    }
    for(int i=1; i<N; i++) if(A[i]==A[i+1]) Union(i, i+1);
    for(int i=1; i<=N; i++) if(A[L[i]]==A[R[i]] && A[L[i]]) Union(L[i], R[i]);
    for(int i=1; i<=N; i++) if(i==Find(i)) {
        Lo[i]=max(A[L[i]], A[R[i]])+1, Hi[i]=A[i];
        tV.push_back({A[i], i});
    }
    sort(tV.begin(), tV.end());
    for(auto [h, k]:tV) {
        if(A[L[k]]==0 && A[R[k]]==0) adj[N+1].push_back(k);
        else if(A[L[k]]>A[R[k]]) adj[Find(L[k])].push_back(k);
        else adj[Find(R[k])].push_back(k);
    }
    for(int i=1; i<=N; i++) for(auto x:TT[i]) T[Find(i)].push_back(x);
    DFS1(N+1);
    DFSH1(N+1), DFSH2(N+1);
    DFS(N+1);
    cout<<all-D[N+1];
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37432 KB Output is correct
2 Correct 6 ms 37212 KB Output is correct
3 Correct 4 ms 37212 KB Output is correct
4 Correct 5 ms 37212 KB Output is correct
5 Correct 4 ms 37208 KB Output is correct
6 Correct 4 ms 37212 KB Output is correct
7 Correct 4 ms 37212 KB Output is correct
8 Correct 4 ms 37212 KB Output is correct
9 Correct 4 ms 37212 KB Output is correct
10 Correct 5 ms 37468 KB Output is correct
11 Correct 4 ms 37464 KB Output is correct
12 Correct 6 ms 37464 KB Output is correct
13 Correct 4 ms 37468 KB Output is correct
14 Correct 5 ms 37212 KB Output is correct
15 Correct 4 ms 37212 KB Output is correct
16 Correct 4 ms 37528 KB Output is correct
17 Correct 4 ms 37212 KB Output is correct
18 Correct 5 ms 37468 KB Output is correct
19 Correct 5 ms 37212 KB Output is correct
20 Correct 4 ms 37212 KB Output is correct
21 Correct 4 ms 37468 KB Output is correct
22 Correct 4 ms 37212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37432 KB Output is correct
2 Correct 6 ms 37212 KB Output is correct
3 Correct 4 ms 37212 KB Output is correct
4 Correct 5 ms 37212 KB Output is correct
5 Correct 4 ms 37208 KB Output is correct
6 Correct 4 ms 37212 KB Output is correct
7 Correct 4 ms 37212 KB Output is correct
8 Correct 4 ms 37212 KB Output is correct
9 Correct 4 ms 37212 KB Output is correct
10 Correct 5 ms 37468 KB Output is correct
11 Correct 4 ms 37464 KB Output is correct
12 Correct 6 ms 37464 KB Output is correct
13 Correct 4 ms 37468 KB Output is correct
14 Correct 5 ms 37212 KB Output is correct
15 Correct 4 ms 37212 KB Output is correct
16 Correct 4 ms 37528 KB Output is correct
17 Correct 4 ms 37212 KB Output is correct
18 Correct 5 ms 37468 KB Output is correct
19 Correct 5 ms 37212 KB Output is correct
20 Correct 4 ms 37212 KB Output is correct
21 Correct 4 ms 37468 KB Output is correct
22 Correct 4 ms 37212 KB Output is correct
23 Correct 7 ms 37464 KB Output is correct
24 Correct 6 ms 37428 KB Output is correct
25 Correct 5 ms 37576 KB Output is correct
26 Correct 5 ms 37464 KB Output is correct
27 Correct 5 ms 37468 KB Output is correct
28 Correct 5 ms 37572 KB Output is correct
29 Correct 5 ms 37468 KB Output is correct
30 Correct 6 ms 37660 KB Output is correct
31 Correct 7 ms 37844 KB Output is correct
32 Correct 6 ms 37980 KB Output is correct
33 Correct 5 ms 37724 KB Output is correct
34 Correct 6 ms 37724 KB Output is correct
35 Correct 5 ms 37824 KB Output is correct
36 Correct 7 ms 37464 KB Output is correct
37 Correct 7 ms 37468 KB Output is correct
38 Correct 7 ms 37980 KB Output is correct
39 Correct 6 ms 37464 KB Output is correct
40 Correct 5 ms 37980 KB Output is correct
41 Correct 5 ms 37468 KB Output is correct
42 Correct 6 ms 37468 KB Output is correct
43 Correct 6 ms 37980 KB Output is correct
44 Correct 6 ms 37468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37432 KB Output is correct
2 Correct 6 ms 37212 KB Output is correct
3 Correct 4 ms 37212 KB Output is correct
4 Correct 5 ms 37212 KB Output is correct
5 Correct 4 ms 37208 KB Output is correct
6 Correct 4 ms 37212 KB Output is correct
7 Correct 4 ms 37212 KB Output is correct
8 Correct 4 ms 37212 KB Output is correct
9 Correct 4 ms 37212 KB Output is correct
10 Correct 5 ms 37468 KB Output is correct
11 Correct 4 ms 37464 KB Output is correct
12 Correct 6 ms 37464 KB Output is correct
13 Correct 4 ms 37468 KB Output is correct
14 Correct 5 ms 37212 KB Output is correct
15 Correct 4 ms 37212 KB Output is correct
16 Correct 4 ms 37528 KB Output is correct
17 Correct 4 ms 37212 KB Output is correct
18 Correct 5 ms 37468 KB Output is correct
19 Correct 5 ms 37212 KB Output is correct
20 Correct 4 ms 37212 KB Output is correct
21 Correct 4 ms 37468 KB Output is correct
22 Correct 4 ms 37212 KB Output is correct
23 Correct 7 ms 37464 KB Output is correct
24 Correct 6 ms 37428 KB Output is correct
25 Correct 5 ms 37576 KB Output is correct
26 Correct 5 ms 37464 KB Output is correct
27 Correct 5 ms 37468 KB Output is correct
28 Correct 5 ms 37572 KB Output is correct
29 Correct 5 ms 37468 KB Output is correct
30 Correct 6 ms 37660 KB Output is correct
31 Correct 7 ms 37844 KB Output is correct
32 Correct 6 ms 37980 KB Output is correct
33 Correct 5 ms 37724 KB Output is correct
34 Correct 6 ms 37724 KB Output is correct
35 Correct 5 ms 37824 KB Output is correct
36 Correct 7 ms 37464 KB Output is correct
37 Correct 7 ms 37468 KB Output is correct
38 Correct 7 ms 37980 KB Output is correct
39 Correct 6 ms 37464 KB Output is correct
40 Correct 5 ms 37980 KB Output is correct
41 Correct 5 ms 37468 KB Output is correct
42 Correct 6 ms 37468 KB Output is correct
43 Correct 6 ms 37980 KB Output is correct
44 Correct 6 ms 37468 KB Output is correct
45 Correct 201 ms 63156 KB Output is correct
46 Correct 158 ms 62468 KB Output is correct
47 Correct 232 ms 63160 KB Output is correct
48 Correct 171 ms 62388 KB Output is correct
49 Correct 177 ms 61836 KB Output is correct
50 Correct 181 ms 62648 KB Output is correct
51 Correct 179 ms 62176 KB Output is correct
52 Correct 203 ms 62396 KB Output is correct
53 Correct 189 ms 62392 KB Output is correct
54 Correct 279 ms 95256 KB Output is correct
55 Correct 284 ms 92524 KB Output is correct
56 Correct 268 ms 88588 KB Output is correct
57 Correct 269 ms 86076 KB Output is correct
58 Correct 149 ms 68436 KB Output is correct
59 Correct 163 ms 68440 KB Output is correct
60 Correct 128 ms 119420 KB Output is correct
61 Correct 238 ms 67716 KB Output is correct
62 Correct 304 ms 101752 KB Output is correct
63 Correct 176 ms 66240 KB Output is correct
64 Correct 195 ms 66240 KB Output is correct
65 Correct 234 ms 101212 KB Output is correct
66 Correct 187 ms 66236 KB Output is correct