Submission #847798

# Submission time Handle Problem Language Result Execution time Memory
847798 2023-09-10T12:30:57 Z Urvuk3 Catfish Farm (IOI22_fish) C++17
100 / 100
611 ms 162640 KB
#include "fish.h"

#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int INF=1e9;
const ll LINF=1e18;
#define fi first
#define se second
#define pii pair<int,int>
#define mid ((l+r)/2)
#define sz(a) (int((a).size()))
#define all(a) a.begin(),a.end()
#define endl "\n"
#define pb push_back

void PRINT(int x) {cerr << x;}
void PRINT(ll x) {cerr << x;}
void PRINT(double x) {cerr << x;}
void PRINT(char x) {cerr << '\'' << x << '\'';}
void PRINT(string x) {cerr << '\"' << x << '\"';}
void PRINT(bool x) {cerr << (x ? "true" : "false");}

template<typename T,typename V>
void PRINT(pair<T,V>& x){
    cerr<<"{";
    PRINT(x.fi);
    cerr<<",";
    PRINT(x.se);
    cerr<<"}";
}
template<typename T>
void PRINT(T &x){
    int id=0;
    cerr<<"{";
    for(auto _i:x){
        cerr<<(id++ ? "," : "");
        PRINT(_i);
    }
    cerr<<"}";
}
void _PRINT(){
    cerr<<"]\n";
}
template<typename Head,typename... Tail>
void _PRINT(Head h,Tail... t){
    PRINT(h);
    if(sizeof...(t)) cerr<<", ";
    _PRINT(t...);
}

#define Debug(x...) cerr<<"["<<#x<<"]=["; _PRINT(x)

const int mxM=3e5+1;
const int mxN=1e5+1;
const int mxC=6e6;

int N,M;
ll seg[mxC],_l[mxC],_r[mxC],roots[mxC];
int idxx;

void Update(int node,int l,int r,int idx,ll val){
    if(l==r){
        seg[node]+=val;
        return;
    }
    if(idx<=mid){
        if(!_l[node]) _l[node]=++idxx;
        Update(_l[node],l,mid,idx,val);
    }
    else{
        if(!_r[node]) _r[node]=++idxx;
        Update(_r[node],mid+1,r,idx,val);
    }
    seg[node]=(_l[node] ? seg[_l[node]] : 0)+(_r[node] ? seg[_r[node]] : 0);
}

ll Query(int node,int l,int r,int L,int R){
    if(L>R) return 0;
    if(L<=l && r<=R) return seg[node];
    ll ret=0;
    if(L<=mid && _l[node]) ret+=Query(_l[node],l,mid,L,R);
    if(R>mid && _r[node]) ret+=Query(_r[node],mid+1,r,L,R);
    return ret;
}

ll Query(int col,int L,int R){
    return Query(roots[col],0,N-1,L,R);
}

vector<int> piers[mxN];
vector<pair<int,ll>> w[mxN];
vector<ll> dp[2][mxN];
vector<ll> mx(mxN);

void MaxSelf(ll& x,ll v){
    x=max(x,v);
}

long long max_weights(int N1,int M1,vector<int> X,vector<int> Y,vector<int> W){
    N=N1; M=M1;
    ll sum0=0,sum1=0;
    for(int i=0;i<M;i++){
        if(X[i]-1>=0) piers[X[i]-1].pb(Y[i]+1);
        if(X[i]+1<N) piers[X[i]+1].pb(Y[i]+1);
        w[X[i]].pb({Y[i],W[i]});
        if(X[i]==0) sum0+=W[i];
        else sum1+=W[i];
    }
    for(int i=0;i<N;i++){
        sort(all(piers[i]));
        piers[i].resize(distance(piers[i].begin(),unique(all(piers[i]))));
        sort(all(w[i]));
        roots[i]=++idxx;
        for(auto p:w[i]){
            Update(roots[i],0,N-1,p.fi,p.se);
        }
        dp[0][i].resize(sz(piers[i]));
        dp[1][i].resize(sz(piers[i]));
    }
    for(int i=0;i<N;i++){
        if(i){
            if(i>=3){
                for(int j=0;j<sz(piers[i]);j++){
                    int p=piers[i][j];
                    MaxSelf(dp[0][i][j],mx[i-3]+Query(i-1,0,p-1));
                }
            }
            if(i>=2){
                int j1=-1;
                ll pref=0;
                for(int j=0;j<sz(piers[i]);j++){
                    int p=piers[i][j];
                    while(j1+1<sz(piers[i-2]) && piers[i-2][j1+1]<=p){
                        MaxSelf(pref,max(dp[0][i-2][j1+1],dp[1][i-2][j1+1]));
                        j1++;
                    }
                    MaxSelf(dp[0][i][j],pref+Query(i-1,0,p-1));
                }
                j1=sz(piers[i-2]);
                pref=0;
                for(int j=sz(piers[i])-1;j>=0;j--){
                    int p=piers[i][j];
                    while(j1-1>=0 && piers[i-2][j1-1]>p){
                        MaxSelf(pref,max(dp[0][i-2][j1-1],dp[1][i-2][j1-1])+Query(i-1,0,piers[i-2][j1-1]-1));
                        j1--;
                    }
                    MaxSelf(dp[0][i][j],pref);
                }
            }
            for(int j=0;j<sz(piers[i]);j++){
                int p=piers[i][j];
                MaxSelf(dp[0][i][j],Query(i-1,0,p-1));
            }
            int j1=-1;
            ll pref=0;
            for(int j=0;j<sz(piers[i]);j++){
                int p=piers[i][j];
                while(j1+1<sz(piers[i-1]) && piers[i-1][j1+1]<=p){
                    MaxSelf(pref,dp[0][i-1][j1+1]+Query(i-1,piers[i-1][j1+1],N-1));
                    j1++;
                }
                MaxSelf(dp[0][i][j],pref-Query(i-1,p,N-1));
            }
            j1=sz(piers[i-1]);
            pref=0;
            for(int j=sz(piers[i])-1;j>=0;j--){
                int p=piers[i][j];
                while(j1-1>=0 && piers[i-1][j1-1]>p){
                    MaxSelf(pref,max(dp[0][i-1][j1-1],dp[1][i-1][j1-1])+Query(i,0,piers[i-1][j1-1]-1));
                    j1--;
                }
                MaxSelf(dp[1][i][j],pref-Query(i,0,p-1));
            }
        }
        if(i+1<N){
            for(int j=0;j<sz(piers[i]);j++){
                int p=piers[i][j];
                MaxSelf(mx[i],max(dp[0][i][j],dp[1][i][j])+Query(i+1,0,p-1));
            }
            if(i) MaxSelf(mx[i],mx[i-1]);
        }
    }
    ll res=mx[N-2];
    for(int j=0;j<sz(piers[N-1]);j++){
        res=max(res,max(dp[0][N-1][j],dp[1][N-1][j]));
    }
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 57 ms 22020 KB Output is correct
2 Correct 70 ms 23760 KB Output is correct
3 Correct 4 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 357 ms 53392 KB Output is correct
6 Correct 528 ms 150932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 166 ms 33640 KB Output is correct
3 Correct 202 ms 40048 KB Output is correct
4 Correct 58 ms 23048 KB Output is correct
5 Correct 68 ms 24768 KB Output is correct
6 Correct 3 ms 13656 KB Output is correct
7 Correct 3 ms 13656 KB Output is correct
8 Correct 3 ms 13656 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 4 ms 13660 KB Output is correct
11 Correct 4 ms 13912 KB Output is correct
12 Correct 86 ms 24512 KB Output is correct
13 Correct 104 ms 26816 KB Output is correct
14 Correct 71 ms 23572 KB Output is correct
15 Correct 92 ms 24508 KB Output is correct
16 Correct 76 ms 23620 KB Output is correct
17 Correct 89 ms 24704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13656 KB Output is correct
2 Correct 4 ms 13660 KB Output is correct
3 Correct 81 ms 36440 KB Output is correct
4 Correct 51 ms 28752 KB Output is correct
5 Correct 150 ms 57068 KB Output is correct
6 Correct 139 ms 56940 KB Output is correct
7 Correct 147 ms 57000 KB Output is correct
8 Correct 141 ms 57072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13912 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 4 ms 13916 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 4 ms 13916 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13912 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 4 ms 13916 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 4 ms 13916 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13656 KB Output is correct
15 Correct 4 ms 13720 KB Output is correct
16 Correct 4 ms 13912 KB Output is correct
17 Correct 49 ms 19280 KB Output is correct
18 Correct 45 ms 19908 KB Output is correct
19 Correct 41 ms 19544 KB Output is correct
20 Correct 40 ms 19544 KB Output is correct
21 Correct 39 ms 19360 KB Output is correct
22 Correct 81 ms 25168 KB Output is correct
23 Correct 16 ms 15192 KB Output is correct
24 Correct 42 ms 18008 KB Output is correct
25 Correct 4 ms 13912 KB Output is correct
26 Correct 14 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13912 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13660 KB Output is correct
10 Correct 4 ms 13916 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 4 ms 13916 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13656 KB Output is correct
15 Correct 4 ms 13720 KB Output is correct
16 Correct 4 ms 13912 KB Output is correct
17 Correct 49 ms 19280 KB Output is correct
18 Correct 45 ms 19908 KB Output is correct
19 Correct 41 ms 19544 KB Output is correct
20 Correct 40 ms 19544 KB Output is correct
21 Correct 39 ms 19360 KB Output is correct
22 Correct 81 ms 25168 KB Output is correct
23 Correct 16 ms 15192 KB Output is correct
24 Correct 42 ms 18008 KB Output is correct
25 Correct 4 ms 13912 KB Output is correct
26 Correct 14 ms 14936 KB Output is correct
27 Correct 7 ms 14680 KB Output is correct
28 Correct 240 ms 44652 KB Output is correct
29 Correct 360 ms 58044 KB Output is correct
30 Correct 508 ms 82452 KB Output is correct
31 Correct 532 ms 82280 KB Output is correct
32 Correct 297 ms 56404 KB Output is correct
33 Correct 550 ms 88776 KB Output is correct
34 Correct 548 ms 88748 KB Output is correct
35 Correct 180 ms 34272 KB Output is correct
36 Correct 488 ms 73040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 13656 KB Output is correct
2 Correct 4 ms 13660 KB Output is correct
3 Correct 81 ms 36440 KB Output is correct
4 Correct 51 ms 28752 KB Output is correct
5 Correct 150 ms 57068 KB Output is correct
6 Correct 139 ms 56940 KB Output is correct
7 Correct 147 ms 57000 KB Output is correct
8 Correct 141 ms 57072 KB Output is correct
9 Correct 194 ms 68516 KB Output is correct
10 Correct 118 ms 43404 KB Output is correct
11 Correct 263 ms 80208 KB Output is correct
12 Correct 3 ms 13656 KB Output is correct
13 Correct 3 ms 13656 KB Output is correct
14 Correct 3 ms 13656 KB Output is correct
15 Correct 3 ms 13656 KB Output is correct
16 Correct 4 ms 13656 KB Output is correct
17 Correct 3 ms 13656 KB Output is correct
18 Correct 4 ms 13656 KB Output is correct
19 Correct 4 ms 13488 KB Output is correct
20 Correct 4 ms 13656 KB Output is correct
21 Correct 4 ms 13656 KB Output is correct
22 Correct 187 ms 68944 KB Output is correct
23 Correct 364 ms 82848 KB Output is correct
24 Correct 374 ms 84048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 22020 KB Output is correct
2 Correct 70 ms 23760 KB Output is correct
3 Correct 4 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 357 ms 53392 KB Output is correct
6 Correct 528 ms 150932 KB Output is correct
7 Correct 3 ms 13656 KB Output is correct
8 Correct 166 ms 33640 KB Output is correct
9 Correct 202 ms 40048 KB Output is correct
10 Correct 58 ms 23048 KB Output is correct
11 Correct 68 ms 24768 KB Output is correct
12 Correct 3 ms 13656 KB Output is correct
13 Correct 3 ms 13656 KB Output is correct
14 Correct 3 ms 13656 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 4 ms 13660 KB Output is correct
17 Correct 4 ms 13912 KB Output is correct
18 Correct 86 ms 24512 KB Output is correct
19 Correct 104 ms 26816 KB Output is correct
20 Correct 71 ms 23572 KB Output is correct
21 Correct 92 ms 24508 KB Output is correct
22 Correct 76 ms 23620 KB Output is correct
23 Correct 89 ms 24704 KB Output is correct
24 Correct 4 ms 13656 KB Output is correct
25 Correct 4 ms 13660 KB Output is correct
26 Correct 81 ms 36440 KB Output is correct
27 Correct 51 ms 28752 KB Output is correct
28 Correct 150 ms 57068 KB Output is correct
29 Correct 139 ms 56940 KB Output is correct
30 Correct 147 ms 57000 KB Output is correct
31 Correct 141 ms 57072 KB Output is correct
32 Correct 2 ms 13660 KB Output is correct
33 Correct 3 ms 13660 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13660 KB Output is correct
36 Correct 3 ms 13660 KB Output is correct
37 Correct 3 ms 13912 KB Output is correct
38 Correct 3 ms 13660 KB Output is correct
39 Correct 3 ms 13660 KB Output is correct
40 Correct 3 ms 13660 KB Output is correct
41 Correct 4 ms 13916 KB Output is correct
42 Correct 3 ms 13656 KB Output is correct
43 Correct 4 ms 13916 KB Output is correct
44 Correct 3 ms 13660 KB Output is correct
45 Correct 4 ms 13656 KB Output is correct
46 Correct 4 ms 13720 KB Output is correct
47 Correct 4 ms 13912 KB Output is correct
48 Correct 49 ms 19280 KB Output is correct
49 Correct 45 ms 19908 KB Output is correct
50 Correct 41 ms 19544 KB Output is correct
51 Correct 40 ms 19544 KB Output is correct
52 Correct 39 ms 19360 KB Output is correct
53 Correct 81 ms 25168 KB Output is correct
54 Correct 16 ms 15192 KB Output is correct
55 Correct 42 ms 18008 KB Output is correct
56 Correct 4 ms 13912 KB Output is correct
57 Correct 14 ms 14936 KB Output is correct
58 Correct 7 ms 14680 KB Output is correct
59 Correct 240 ms 44652 KB Output is correct
60 Correct 360 ms 58044 KB Output is correct
61 Correct 508 ms 82452 KB Output is correct
62 Correct 532 ms 82280 KB Output is correct
63 Correct 297 ms 56404 KB Output is correct
64 Correct 550 ms 88776 KB Output is correct
65 Correct 548 ms 88748 KB Output is correct
66 Correct 180 ms 34272 KB Output is correct
67 Correct 488 ms 73040 KB Output is correct
68 Correct 194 ms 68516 KB Output is correct
69 Correct 118 ms 43404 KB Output is correct
70 Correct 263 ms 80208 KB Output is correct
71 Correct 3 ms 13656 KB Output is correct
72 Correct 3 ms 13656 KB Output is correct
73 Correct 3 ms 13656 KB Output is correct
74 Correct 3 ms 13656 KB Output is correct
75 Correct 4 ms 13656 KB Output is correct
76 Correct 3 ms 13656 KB Output is correct
77 Correct 4 ms 13656 KB Output is correct
78 Correct 4 ms 13488 KB Output is correct
79 Correct 4 ms 13656 KB Output is correct
80 Correct 4 ms 13656 KB Output is correct
81 Correct 187 ms 68944 KB Output is correct
82 Correct 364 ms 82848 KB Output is correct
83 Correct 374 ms 84048 KB Output is correct
84 Correct 537 ms 70088 KB Output is correct
85 Correct 559 ms 69324 KB Output is correct
86 Correct 596 ms 154704 KB Output is correct
87 Correct 611 ms 157520 KB Output is correct
88 Correct 3 ms 13656 KB Output is correct
89 Correct 589 ms 162640 KB Output is correct
90 Correct 568 ms 83316 KB Output is correct
91 Correct 404 ms 66896 KB Output is correct