답안 #522060

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
522060 2022-02-03T17:09:52 Z Mahdi 별자리 3 (JOI20_constellation3) C++17
100 / 100
843 ms 56140 KB
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef long long ll;
typedef pair<int, int>pii;
typedef pair<ll, ll> pl;
typedef pair<ll, pl> pll;
const int N=2e5+5;
int n, m, sz, a[N], cl[N], cr[N], x[N], y[N], c[N], mx[4*N], la[4*N], p[N], td[N];
ll dp[N], pd[N], mm[N], ans[4*N], fuck;
vector<pii>u[N];

int max(int x, int lx, int rx, int l, int r){
    if(lx>=r || rx<=l)
        return n;
    if(lx>=l && rx<=r)
        return mx[x];
    int mid=(lx+rx)/2;
    int l1=max(2*x+1, lx, mid, l, r);
    int r1=max(2*x+2, mid, rx, l, r);
    if(a[l1]<=a[r1])
        return r1;
    return l1;
}

void max(int x, int lx, int rx, int l, int r, ll v){
    if(rx<=l || lx>=r)
        return;
    if(lx>=l && rx<=r){
        ans[x]=max(ans[x], v);
        return;
    }
    int mid=(lx+rx)/2;
    if(la[x]){
        ans[2*x+1]=ans[2*x+2]=0;
        la[2*x+1]=la[2*x+2]=1;
        la[x]=0;
    }
    ans[2*x+1]=max(ans[x], ans[2*x+1]);
    ans[2*x+2]=max(ans[x], ans[2*x+2]);
    max(2*x+1, lx, mid, l, r, v);
    max(2*x+2, mid, rx, l, r, v);
}

ll num(int x, int l, int r, int y){
    if(r-l==1)
        return ans[x]+fuck;
    if(la[x]){
        la[2*x+1]=la[2*x+2]=1;
        ans[2*x+1]=ans[2*x+2]=0;
        la[x]=0;
    }
    ans[2*x+1]=max(ans[2*x+1], ans[x]);
    ans[2*x+2]=max(ans[2*x+2], ans[x]);
    int mid=(l+r)/2;
    if(y<mid)
        return num(2*x+1, l, mid, y);
    return num(2*x+2, mid, r, y);
}

void dsu(int l1, int r1, int l2, int r2){
    int w1=p[l1];
    int w2=p[l2];
    if(u[w1].size()>u[w2].size()){
        swap(w1, w2);
        swap(l1, l2);
        swap(r1, r2);
    }
    for(pii ww: u[w1]){
        dp[ww.S]+=pd[w1]-pd[w2];
        u[w2].push_back(ww);
        mm[w2]=max(mm[w2], dp[ww.S]+pd[w2]);
    }
    for(int i=l1;i<r1;++i)
        p[i]=w2;
}

ll sol(int l, int r){
    if(l==r)
        return 0;
    int id=max(0, 0, sz, l, r);
    int s1=td[l]-td[id], s2=td[id+1]-td[r];
    ll lr=0, zz=0, rl=0;
    if(s1<s2){
        ll z=sol(l, id);
        if(a[id]<n-1)
            lr=num(0, 0, sz, a[id]+1);
        else
            lr=z;
        la[0]=1;
        ans[0]=0;
        fuck=0;
        zz=sol(id+1, r);
        if(a[id]<n-1)
            rl=num(0, 0, sz, a[id]+1);
        else
            rl=zz;
        if(id>l){
            pd[p[l]]+=rl;
            mm[p[l]]+=rl;
            fuck+=lr;
            int v=p[l];
            for(pii w: u[v])
                max(0, 0, sz, w.F+1, n, dp[w.S]+pd[v]-fuck);
        }
        if(id+1<r){
            pd[p[id+1]]+=lr;
            mm[p[id+1]]+=lr;
        }
    }
    else{
        ll z=sol(id+1, r);
        if(a[id]<n-1)
            lr=num(0, 0, sz, a[id]+1);
        else
            lr=z;
        la[0]=1;
        ans[0]=0;
        fuck=0;
        zz=sol(l, id);
        if(a[id]<n-1)
            rl=num(0, 0, sz, a[id]+1);
        else
            rl=zz;
        if(id+1<r){
            pd[p[id+1]]+=rl;
            mm[p[id+1]]+=rl;
            fuck+=lr;
            int v=p[id+1];
            for(pii w: u[v])
                max(0, 0, sz, w.F+1, n, dp[w.S]+pd[v]-fuck);
        }
        if(id>l){
            pd[p[l]]+=lr;
            mm[p[l]]+=lr;
        }
    }
    for(pii w: u[id]){
        dp[w.S]=c[w.S]+lr+rl;
        mm[id]=max(mm[id], dp[w.S]);
        max(0, 0, sz, w.F+1, n, dp[w.S]-fuck);
    }
    if(id+1<r)
        dsu(id, id+1, id+1, r);
    if(id>l)
        dsu(l, id, id, r);
    return max(rl+lr, mm[p[l]]);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>a[i];
        --a[i];
    }
    a[n]=-1;
    cin>>m;
    for(int i=0;i<m;++i){
        cin>>x[i]>>y[i]>>c[i];
        u[--x[i]].push_back({--y[i], i});
    }
    for(int i=n-1;i>=0;--i){
        sort(all(u[i]));
        td[i]=td[i+1]+u[i].size();
    }
    sz=1;
    while(sz<n)
        sz*=2;
    iota(mx+sz-1, mx+n+sz-1, 0);
    for(int i=sz-2;i>=0;--i){
        if(a[mx[2*i+1]]<a[mx[2*i+2]])
            mx[i]=mx[2*i+2];
        else
            mx[i]=mx[2*i+1];
    }
    iota(p, p+n, 0);
    ll z=0;
    for(int i=0;i<m;++i)
        z+=c[i];
    z-=sol(0, n);
    cout<<z<<'\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5008 KB Output is correct
2 Correct 4 ms 5036 KB Output is correct
3 Correct 4 ms 5028 KB Output is correct
4 Correct 5 ms 5068 KB Output is correct
5 Correct 5 ms 5028 KB Output is correct
6 Correct 3 ms 5028 KB Output is correct
7 Correct 3 ms 5028 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5156 KB Output is correct
13 Correct 3 ms 5024 KB Output is correct
14 Correct 3 ms 5068 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
16 Correct 3 ms 5068 KB Output is correct
17 Correct 5 ms 5060 KB Output is correct
18 Correct 5 ms 5156 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5008 KB Output is correct
2 Correct 4 ms 5036 KB Output is correct
3 Correct 4 ms 5028 KB Output is correct
4 Correct 5 ms 5068 KB Output is correct
5 Correct 5 ms 5028 KB Output is correct
6 Correct 3 ms 5028 KB Output is correct
7 Correct 3 ms 5028 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5156 KB Output is correct
13 Correct 3 ms 5024 KB Output is correct
14 Correct 3 ms 5068 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
16 Correct 3 ms 5068 KB Output is correct
17 Correct 5 ms 5060 KB Output is correct
18 Correct 5 ms 5156 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 6 ms 5288 KB Output is correct
24 Correct 6 ms 5420 KB Output is correct
25 Correct 6 ms 5424 KB Output is correct
26 Correct 6 ms 5324 KB Output is correct
27 Correct 5 ms 5324 KB Output is correct
28 Correct 6 ms 5324 KB Output is correct
29 Correct 6 ms 5300 KB Output is correct
30 Correct 6 ms 5324 KB Output is correct
31 Correct 5 ms 5420 KB Output is correct
32 Correct 5 ms 5452 KB Output is correct
33 Correct 4 ms 5468 KB Output is correct
34 Correct 5 ms 5460 KB Output is correct
35 Correct 5 ms 5452 KB Output is correct
36 Correct 6 ms 5324 KB Output is correct
37 Correct 6 ms 5324 KB Output is correct
38 Correct 4 ms 5580 KB Output is correct
39 Correct 5 ms 5288 KB Output is correct
40 Correct 4 ms 5468 KB Output is correct
41 Correct 6 ms 5324 KB Output is correct
42 Correct 5 ms 5324 KB Output is correct
43 Correct 5 ms 5560 KB Output is correct
44 Correct 5 ms 5280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5008 KB Output is correct
2 Correct 4 ms 5036 KB Output is correct
3 Correct 4 ms 5028 KB Output is correct
4 Correct 5 ms 5068 KB Output is correct
5 Correct 5 ms 5028 KB Output is correct
6 Correct 3 ms 5028 KB Output is correct
7 Correct 3 ms 5028 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5156 KB Output is correct
13 Correct 3 ms 5024 KB Output is correct
14 Correct 3 ms 5068 KB Output is correct
15 Correct 3 ms 5068 KB Output is correct
16 Correct 3 ms 5068 KB Output is correct
17 Correct 5 ms 5060 KB Output is correct
18 Correct 5 ms 5156 KB Output is correct
19 Correct 3 ms 5024 KB Output is correct
20 Correct 3 ms 5068 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 6 ms 5288 KB Output is correct
24 Correct 6 ms 5420 KB Output is correct
25 Correct 6 ms 5424 KB Output is correct
26 Correct 6 ms 5324 KB Output is correct
27 Correct 5 ms 5324 KB Output is correct
28 Correct 6 ms 5324 KB Output is correct
29 Correct 6 ms 5300 KB Output is correct
30 Correct 6 ms 5324 KB Output is correct
31 Correct 5 ms 5420 KB Output is correct
32 Correct 5 ms 5452 KB Output is correct
33 Correct 4 ms 5468 KB Output is correct
34 Correct 5 ms 5460 KB Output is correct
35 Correct 5 ms 5452 KB Output is correct
36 Correct 6 ms 5324 KB Output is correct
37 Correct 6 ms 5324 KB Output is correct
38 Correct 4 ms 5580 KB Output is correct
39 Correct 5 ms 5288 KB Output is correct
40 Correct 4 ms 5468 KB Output is correct
41 Correct 6 ms 5324 KB Output is correct
42 Correct 5 ms 5324 KB Output is correct
43 Correct 5 ms 5560 KB Output is correct
44 Correct 5 ms 5280 KB Output is correct
45 Correct 724 ms 42700 KB Output is correct
46 Correct 759 ms 42096 KB Output is correct
47 Correct 757 ms 42672 KB Output is correct
48 Correct 843 ms 41968 KB Output is correct
49 Correct 794 ms 41788 KB Output is correct
50 Correct 735 ms 41616 KB Output is correct
51 Correct 711 ms 41928 KB Output is correct
52 Correct 681 ms 42960 KB Output is correct
53 Correct 683 ms 41940 KB Output is correct
54 Correct 345 ms 51216 KB Output is correct
55 Correct 338 ms 46708 KB Output is correct
56 Correct 420 ms 43884 KB Output is correct
57 Correct 415 ms 42184 KB Output is correct
58 Correct 411 ms 37356 KB Output is correct
59 Correct 444 ms 37440 KB Output is correct
60 Correct 199 ms 56140 KB Output is correct
61 Correct 405 ms 37104 KB Output is correct
62 Correct 314 ms 52448 KB Output is correct
63 Correct 364 ms 36524 KB Output is correct
64 Correct 406 ms 36416 KB Output is correct
65 Correct 345 ms 52524 KB Output is correct
66 Correct 436 ms 36688 KB Output is correct