Submission #944731

# Submission time Handle Problem Language Result Execution time Memory
944731 2024-03-13T04:12:24 Z guagua0407 Constellation 3 (JOI20_constellation3) C++17
100 / 100
321 ms 77908 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=2e5+5;
const int inf=1e9;
const ll inf2=1e18;
int a[mxn];
pair<int,int> table[mxn][18];
int up[mxn][18];
vector<pair<int,int>> st[mxn],en[mxn];
int l[mxn],r[mxn];
ll dp[mxn];
int ordl[mxn],ordr[mxn];
ll bit[mxn];
int cnt=0;

void update(int pos,ll val){
    for(;pos<mxn;pos+=(pos&-pos)){
        bit[pos]+=val;
    }
}

ll query(int pos){
    ll ans=0;
    for(;pos>0;pos-=(pos&-pos)){
        ans+=bit[pos];
    }
    return ans;
}

int get(int l,int r){
    int b=__lg(r-l+1);
    return max(table[l][b],table[r-(1<<b)+1][b]).s;
}

void go(int L,int R){
    if(L==R) return;
    int mid=get(L,R);
    if(mid>L){
        l[mid]=get(L,mid-1);
        up[l[mid]][0]=mid;
        go(L,mid-1);
    }
    if(mid<R){
        r[mid]=get(mid+1,R);
        up[r[mid]][0]=mid;
        go(mid+1,R);
    }
}

void dfs(int v){
    //cout<<v<<' '<<l[v]<<' '<<r[v]<<'\n';
    ordl[v]=++cnt;
    if(l[v]!=0){
        dfs(l[v]);
    }
    if(r[v]!=0){
        dfs(r[v]);
    }
    ordr[v]=cnt;
    if(l[v]!=0){
        update(ordl[v],dp[l[v]]);
        update(ordr[v]+1,-dp[l[v]]);
        update(ordl[l[v]],-dp[l[v]]);
        update(ordr[l[v]]+1,dp[l[v]]);
    }
    if(r[v]!=0){
        update(ordl[v],dp[r[v]]);
        update(ordr[v]+1,-dp[r[v]]);
        update(ordl[r[v]],-dp[r[v]]);
        update(ordr[r[v]]+1,dp[r[v]]);
    }
    dp[v]=dp[l[v]]+dp[r[v]];
    for(auto u:en[v]){
        int x=u.f;
        dp[v]=max(dp[v],query(ordl[x])+u.s);
    }
}

signed main() {_
    int n;
    cin>>n;
    a[0]=inf;
    //dp[0]=-inf2;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        table[i][0]={a[i],i};
    }
    for(int j=1;j<18;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            table[i][j]=max(table[i][j-1],table[i+(1<<(j-1))][j-1]);
        }
    }
    int root=get(1,n);
    go(1,n);
    for(int j=1;j<18;j++){
        for(int i=1;i<=n;i++){
            up[i][j]=up[up[i][j-1]][j-1];
        }
    }
    int m;
    cin>>m;
    ll sum=0;
    for(int i=1;i<=m;i++){
        int x,y,c;
        cin>>x>>y>>c;
        sum+=c;
        int node=x;
        for(int j=17;j>=0;j--){
            int v=up[node][j];
            if(a[v]<y) node=v;
        }
        //cout<<st<<'\n';
        en[node].push_back({x,c});
        st[x].push_back({node,c});
    }
    dfs(root);
    cout<<sum-dp[root]<<'\n';
    return 0;
}
//maybe its multiset not set
//yeeorz
//laborz

Compilation message

constellation3.cpp: In function 'void setIO(std::string)':
constellation3.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
constellation3.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18948 KB Output is correct
2 Correct 4 ms 18780 KB Output is correct
3 Correct 4 ms 18976 KB Output is correct
4 Correct 4 ms 18976 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18776 KB Output is correct
7 Correct 4 ms 19032 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18780 KB Output is correct
10 Correct 4 ms 18776 KB Output is correct
11 Correct 5 ms 18776 KB Output is correct
12 Correct 4 ms 18780 KB Output is correct
13 Correct 4 ms 18780 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 5 ms 18776 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 4 ms 18780 KB Output is correct
18 Correct 4 ms 18780 KB Output is correct
19 Correct 4 ms 18976 KB Output is correct
20 Correct 5 ms 18780 KB Output is correct
21 Correct 4 ms 18780 KB Output is correct
22 Correct 4 ms 18780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18948 KB Output is correct
2 Correct 4 ms 18780 KB Output is correct
3 Correct 4 ms 18976 KB Output is correct
4 Correct 4 ms 18976 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18776 KB Output is correct
7 Correct 4 ms 19032 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18780 KB Output is correct
10 Correct 4 ms 18776 KB Output is correct
11 Correct 5 ms 18776 KB Output is correct
12 Correct 4 ms 18780 KB Output is correct
13 Correct 4 ms 18780 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 5 ms 18776 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 4 ms 18780 KB Output is correct
18 Correct 4 ms 18780 KB Output is correct
19 Correct 4 ms 18976 KB Output is correct
20 Correct 5 ms 18780 KB Output is correct
21 Correct 4 ms 18780 KB Output is correct
22 Correct 4 ms 18780 KB Output is correct
23 Correct 5 ms 19036 KB Output is correct
24 Correct 5 ms 19036 KB Output is correct
25 Correct 5 ms 19036 KB Output is correct
26 Correct 5 ms 19036 KB Output is correct
27 Correct 5 ms 19036 KB Output is correct
28 Correct 5 ms 19036 KB Output is correct
29 Correct 5 ms 19036 KB Output is correct
30 Correct 7 ms 19036 KB Output is correct
31 Correct 5 ms 19036 KB Output is correct
32 Correct 5 ms 18872 KB Output is correct
33 Correct 5 ms 18872 KB Output is correct
34 Correct 5 ms 19036 KB Output is correct
35 Correct 5 ms 19036 KB Output is correct
36 Correct 5 ms 19036 KB Output is correct
37 Correct 6 ms 19212 KB Output is correct
38 Correct 5 ms 19036 KB Output is correct
39 Correct 5 ms 19036 KB Output is correct
40 Correct 5 ms 19032 KB Output is correct
41 Correct 5 ms 19032 KB Output is correct
42 Correct 5 ms 19032 KB Output is correct
43 Correct 5 ms 19036 KB Output is correct
44 Correct 5 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 18948 KB Output is correct
2 Correct 4 ms 18780 KB Output is correct
3 Correct 4 ms 18976 KB Output is correct
4 Correct 4 ms 18976 KB Output is correct
5 Correct 4 ms 18780 KB Output is correct
6 Correct 4 ms 18776 KB Output is correct
7 Correct 4 ms 19032 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 5 ms 18780 KB Output is correct
10 Correct 4 ms 18776 KB Output is correct
11 Correct 5 ms 18776 KB Output is correct
12 Correct 4 ms 18780 KB Output is correct
13 Correct 4 ms 18780 KB Output is correct
14 Correct 4 ms 18780 KB Output is correct
15 Correct 5 ms 18776 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 4 ms 18780 KB Output is correct
18 Correct 4 ms 18780 KB Output is correct
19 Correct 4 ms 18976 KB Output is correct
20 Correct 5 ms 18780 KB Output is correct
21 Correct 4 ms 18780 KB Output is correct
22 Correct 4 ms 18780 KB Output is correct
23 Correct 5 ms 19036 KB Output is correct
24 Correct 5 ms 19036 KB Output is correct
25 Correct 5 ms 19036 KB Output is correct
26 Correct 5 ms 19036 KB Output is correct
27 Correct 5 ms 19036 KB Output is correct
28 Correct 5 ms 19036 KB Output is correct
29 Correct 5 ms 19036 KB Output is correct
30 Correct 7 ms 19036 KB Output is correct
31 Correct 5 ms 19036 KB Output is correct
32 Correct 5 ms 18872 KB Output is correct
33 Correct 5 ms 18872 KB Output is correct
34 Correct 5 ms 19036 KB Output is correct
35 Correct 5 ms 19036 KB Output is correct
36 Correct 5 ms 19036 KB Output is correct
37 Correct 6 ms 19212 KB Output is correct
38 Correct 5 ms 19036 KB Output is correct
39 Correct 5 ms 19036 KB Output is correct
40 Correct 5 ms 19032 KB Output is correct
41 Correct 5 ms 19032 KB Output is correct
42 Correct 5 ms 19032 KB Output is correct
43 Correct 5 ms 19036 KB Output is correct
44 Correct 5 ms 19036 KB Output is correct
45 Correct 261 ms 66816 KB Output is correct
46 Correct 231 ms 66388 KB Output is correct
47 Correct 225 ms 66580 KB Output is correct
48 Correct 236 ms 66576 KB Output is correct
49 Correct 246 ms 66324 KB Output is correct
50 Correct 212 ms 66128 KB Output is correct
51 Correct 228 ms 66252 KB Output is correct
52 Correct 237 ms 66588 KB Output is correct
53 Correct 225 ms 66388 KB Output is correct
54 Correct 301 ms 72628 KB Output is correct
55 Correct 304 ms 70592 KB Output is correct
56 Correct 286 ms 69212 KB Output is correct
57 Correct 265 ms 68692 KB Output is correct
58 Correct 219 ms 70860 KB Output is correct
59 Correct 223 ms 70852 KB Output is correct
60 Correct 139 ms 77908 KB Output is correct
61 Correct 213 ms 65872 KB Output is correct
62 Correct 321 ms 73216 KB Output is correct
63 Correct 208 ms 65484 KB Output is correct
64 Correct 217 ms 65488 KB Output is correct
65 Correct 296 ms 73036 KB Output is correct
66 Correct 224 ms 65392 KB Output is correct