답안 #944723

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944723 2024-03-13T04:11:02 Z guagua0407 별자리 3 (JOI20_constellation3) C++17
100 / 100
432 ms 81952 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][20];
int up[mxn][20];
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<20;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<20;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=19;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);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 4 ms 18944 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 18916 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 4 ms 18780 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 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 4 ms 18780 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 3 ms 18780 KB Output is correct
19 Correct 3 ms 18780 KB Output is correct
20 Correct 3 ms 18780 KB Output is correct
21 Correct 3 ms 18880 KB Output is correct
22 Correct 3 ms 18776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 4 ms 18944 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 18916 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 4 ms 18780 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 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 4 ms 18780 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 3 ms 18780 KB Output is correct
19 Correct 3 ms 18780 KB Output is correct
20 Correct 3 ms 18780 KB Output is correct
21 Correct 3 ms 18880 KB Output is correct
22 Correct 3 ms 18776 KB Output is correct
23 Correct 4 ms 19036 KB Output is correct
24 Correct 5 ms 18936 KB Output is correct
25 Correct 4 ms 19036 KB Output is correct
26 Correct 5 ms 18852 KB Output is correct
27 Correct 4 ms 19036 KB Output is correct
28 Correct 4 ms 19036 KB Output is correct
29 Correct 5 ms 19036 KB Output is correct
30 Correct 5 ms 19036 KB Output is correct
31 Correct 5 ms 19036 KB Output is correct
32 Correct 5 ms 19024 KB Output is correct
33 Correct 5 ms 19036 KB Output is correct
34 Correct 5 ms 19044 KB Output is correct
35 Correct 5 ms 19036 KB Output is correct
36 Correct 6 ms 19040 KB Output is correct
37 Correct 5 ms 19036 KB Output is correct
38 Correct 4 ms 19044 KB Output is correct
39 Correct 5 ms 19044 KB Output is correct
40 Correct 6 ms 19032 KB Output is correct
41 Correct 5 ms 19036 KB Output is correct
42 Correct 4 ms 19036 KB Output is correct
43 Correct 4 ms 19036 KB Output is correct
44 Correct 5 ms 19032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 4 ms 18944 KB Output is correct
3 Correct 3 ms 18780 KB Output is correct
4 Correct 3 ms 18780 KB Output is correct
5 Correct 4 ms 18916 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18780 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 4 ms 18780 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
11 Correct 3 ms 18776 KB Output is correct
12 Correct 3 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 4 ms 18780 KB Output is correct
16 Correct 4 ms 18780 KB Output is correct
17 Correct 3 ms 18780 KB Output is correct
18 Correct 3 ms 18780 KB Output is correct
19 Correct 3 ms 18780 KB Output is correct
20 Correct 3 ms 18780 KB Output is correct
21 Correct 3 ms 18880 KB Output is correct
22 Correct 3 ms 18776 KB Output is correct
23 Correct 4 ms 19036 KB Output is correct
24 Correct 5 ms 18936 KB Output is correct
25 Correct 4 ms 19036 KB Output is correct
26 Correct 5 ms 18852 KB Output is correct
27 Correct 4 ms 19036 KB Output is correct
28 Correct 4 ms 19036 KB Output is correct
29 Correct 5 ms 19036 KB Output is correct
30 Correct 5 ms 19036 KB Output is correct
31 Correct 5 ms 19036 KB Output is correct
32 Correct 5 ms 19024 KB Output is correct
33 Correct 5 ms 19036 KB Output is correct
34 Correct 5 ms 19044 KB Output is correct
35 Correct 5 ms 19036 KB Output is correct
36 Correct 6 ms 19040 KB Output is correct
37 Correct 5 ms 19036 KB Output is correct
38 Correct 4 ms 19044 KB Output is correct
39 Correct 5 ms 19044 KB Output is correct
40 Correct 6 ms 19032 KB Output is correct
41 Correct 5 ms 19036 KB Output is correct
42 Correct 4 ms 19036 KB Output is correct
43 Correct 4 ms 19036 KB Output is correct
44 Correct 5 ms 19032 KB Output is correct
45 Correct 296 ms 74308 KB Output is correct
46 Correct 280 ms 73952 KB Output is correct
47 Correct 298 ms 73792 KB Output is correct
48 Correct 251 ms 74420 KB Output is correct
49 Correct 279 ms 73396 KB Output is correct
50 Correct 259 ms 73500 KB Output is correct
51 Correct 277 ms 73556 KB Output is correct
52 Correct 305 ms 74116 KB Output is correct
53 Correct 278 ms 73920 KB Output is correct
54 Correct 432 ms 80300 KB Output is correct
55 Correct 423 ms 77628 KB Output is correct
56 Correct 365 ms 76404 KB Output is correct
57 Correct 374 ms 75656 KB Output is correct
58 Correct 288 ms 76640 KB Output is correct
59 Correct 307 ms 76512 KB Output is correct
60 Correct 168 ms 81952 KB Output is correct
61 Correct 239 ms 73532 KB Output is correct
62 Correct 381 ms 80184 KB Output is correct
63 Correct 243 ms 72884 KB Output is correct
64 Correct 218 ms 72272 KB Output is correct
65 Correct 320 ms 80208 KB Output is correct
66 Correct 226 ms 72308 KB Output is correct