Submission #380819

# Submission time Handle Problem Language Result Execution time Memory
380819 2021-03-23T06:13:31 Z ponytail Split the sequence (APIO14_sequence) C++17
100 / 100
1540 ms 82584 KB
#include "bits/stdc++.h"
using namespace std;
#ifdef ONLINE_JUDGE
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> OST;
#endif
#define int long long
#define double long double
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
const int MOD = 1000000007;
const int MOD2 = 998244353;
const int BIG = 1197423052705914509LL;
mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 1e5 + 10;
const int NO_OPERATION = 69420420420LL;
const int is_query = -BIG;
struct line {
    int m, b;
    mutable function<const line*()> succ;
    bool operator<(const line& rhs) const {
        if (rhs.b != is_query) return m < rhs.m;
        const line* s = succ();
        if (!s) return 0;
        int x = rhs.m;
        return b - s->b < (s->m - m) * x;
    }
};
struct dynamic_hull : public multiset<line> { 
    const int inf = BIG;
    bool bad(iterator y) {
        auto z = next(y);
        if (y == begin()) {
            if (z == end()) return 0;
            return y->m == z->m && y->b <= z->b;
        }
        auto x = prev(y);
        if (z == end()) return y->m == x->m && y->b <= x->b;
        int v1 = (x->b - y->b);
        if (y->m == x->m) v1 = x->b > y->b ? inf : -inf;
        else v1 /= (y->m - x->m);
        int v2 = (y->b - z->b);
        if (z->m == y->m) v2 = y->b > z->b ? inf : -inf;
        else v2 /= (z->m - y->m);
        return v1 >= v2;
    }
    void insert_line(int m, int b) {
        auto y = insert({m,b});
        y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
        if (bad(y)) { erase(y); return; }
        while (next(y) != end() && bad(next(y))) erase(next(y));
        while (y != begin() && bad(prev(y))) erase(prev(y));
    }
    int eval(int x) {
        auto l = *lower_bound((line) { x, is_query });
        return l.m * x + l.b;
    }
};
struct auxiliary_tree{
    int n;
    vector<int>ori[MAXN];
    vector<int>storelatest;
    int tin[MAXN], tout[MAXN], dep[MAXN], cor[MAXN];
    bool imp[MAXN];
    int run=0;
    vector<int>euler;
    vector<vector<pair<int,int> > >lca_table;
    int lef[MAXN], rig[MAXN];
    void dfs(int node,int prev,int de){
        dep[node]=de;
        tin[node]=++run;
        cor[tin[node]]=node;
        euler.pb(node);
        for(int i=0;i<ori[node].size();i++){
            if(ori[node][i]!=prev){
                dfs(ori[node][i],node,de+1);
                euler.pb(node);
            }
        }
        tout[node]=++run;
    }
    void find_all_lcas(){
        int k=log2(2*n-1);
        lca_table.resize(k+1);
        for(int i=0;i<k+1;i++){
            lca_table[i].resize(2*n-1);
        }
        for(int i=0;i<2*n-1;i++){
            lca_table[0][i]=mp(dep[euler[i]],euler[i]);
        }
        for(int i=1;i<=k;i++){
            for(int j=0;j<2*n-1;j++){
                if(j+(1<<(i-1))>=2*n-1)continue;
                if(lca_table[i-1][j].fi<lca_table[i-1][j+(1<<(i-1))].fi){
                    lca_table[i][j].fi=lca_table[i-1][j].fi;
                    lca_table[i][j].se=lca_table[i-1][j].se;
                }
                else{
                    lca_table[i][j].fi=lca_table[i-1][j+(1<<(i-1))].fi;
                    lca_table[i][j].se=lca_table[i-1][j+(1<<(i-1))].se;
                }
            }
        }
        for(int i=0;i<MAXN;i++) lef[i]=-1;
        for(int i=0;i<2*n-1;i++){
            if(lef[euler[i]]==-1){
                lef[euler[i]]=i;
            }
            rig[euler[i]]=i;
        }
    }
    bool isParent(int u,int v){
        return tin[u]<tin[v] && tout[v]<tout[u];
    }
    public:
    vector<int>adj[MAXN];
    int root;
    int lcadep(int x,int y){
        if(lef[x]>rig[y]){
            swap(x,y);
        }
        int k=log2(rig[y]-lef[x]+1);
        return min(lca_table[k][lef[x]].fi,lca_table[k][rig[y]-(1<<k)+1].fi);
    }
    int lcaidx(int x,int y){
        if(lef[x]>rig[y]){
            swap(x,y);
        }
        int k=log2(rig[y]-lef[x]+1);
        if(lca_table[k][lef[x]].fi<lca_table[k][rig[y]-(1<<k)+1].fi){
            return lca_table[k][lef[x]].se;
        }
        else{
            return lca_table[k][rig[y]-(1<<k)+1].se;
        }
    }
    void base(int x,int rt,vector<int>y[]){
        n=x;
        for(int i=1;i<=n;i++){
            ori[i]=y[i];
        }
        dfs(rt,-1,0);
        find_all_lcas();
    }
    void build(vector<int>g){
        for(int i=0;i<g.size();i++){
            g[i]=tin[g[i]];
        }
        sort(all(g));
        for(int i=0;i<g.size();i++){
            g[i]=cor[g[i]];
        }
        int k=g.size();
        for(int i=0;i<k-1;i++){
            g.pb(lcaidx(g[i],g[i+1]));
        }
        for(int i=0;i<g.size();i++){
            g[i]=tin[g[i]];
        }
        sort(all(g));
        for(int i=0;i<g.size();i++){
            g[i]=cor[g[i]];
        }
        g.erase(unique(all(g)),g.end());
        for(int i=0;i<g.size();i++){
            imp[g[i]]=1;
            storelatest.pb(g[i]);
        }
        stack<int>vert;
        vert.push(g[0]);
        for(int i=1;i<g.size();i++){
            int u=g[i];
            while(vert.size()>1 && isParent(vert.top(),u)==0){
                int sto=vert.top();
                vert.pop();
                adj[vert.top()].pb(sto);
            }
            vert.push(u);
        }
        while(vert.size()>1){
            int sto=vert.top();
            vert.pop();
            adj[vert.top()].pb(sto);
        }
        root=vert.top();
    }
    void clear(){
        for(int i=0;i<storelatest.size();i++){
            imp[storelatest[i]]=0;
            adj[storelatest[i]].clear();
        }
        storelatest.clear();
    }
};
struct custom_hash{
    static uint64_t splitmix64(uint64_t x){
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t a) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(a + FIXED_RANDOM);
    }
    template<class T> size_t operator()(T a) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        hash<T> x;
        return splitmix64(x(a) + FIXED_RANDOM);
    }
    template<class T, class H> size_t operator()(pair<T, H> a) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        hash<T> x;
        hash<H> y;
        return splitmix64(x(a.f) * 37 + y(a.s) + FIXED_RANDOM);
    }
};
template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
int a[MAXN], ps[MAXN], dp[2][MAXN];
signed bt[210][MAXN];
int n;
int s(int j,int k){
    return ps[k]-ps[j-1];
}
double m(int i,int k,int l){
    double numer=dp[(i+1)&1][l]-ps[n]*ps[l]-ps[l]*ps[l]-dp[(i+1)&1][k]+ps[n]*ps[k]+ps[k]*ps[k];
    double denom=ps[k]-ps[l];
    if(denom==0) denom=1e-10;
    return numer/denom;
}
void solve(int test_case){
    int k;
    cin >> n >> k;
    for(int i=1;i<=n;i++){
        cin >> a[i];
        ps[i]=ps[i-1]+a[i];
    }
    k++;
    for(int i=0;i<2;i++){
        for(int j=0;j<=n;j++){
            dp[i][j]=-1e9;
        }
    }
    for(int i=1;i<=n;i++){
        dp[1][i]=ps[i]*(ps[n]-ps[i]);
    }
    for(int i=2;i<=k;i++){
        deque<int>d;
        d.push_back(i-1);
        for(int j=i;j<=n;j++){
            while(d.size()>1 && m(i,d[0],d[1])<2*ps[j]){
                d.pop_front();
            }
            dp[i&1][j]=dp[(i+1)&1][d[0]]+2*ps[j]*ps[d[0]]-ps[n]*ps[d[0]]-ps[d[0]]*ps[d[0]]+ps[j]*ps[n]-ps[j]*ps[j];
            bt[i][j]=d[0];
            while(d.size()>1 && m(i,d[d.size()-2],d[d.size()-1])>m(i,d[d.size()-1],j)){
                d.pop_back();
            }
            d.push_back(j);
        }
    }
    cout << dp[k&1][n]/2 << "\n";
    int J=n;
    for(int i=k;i>=2;i--){
        cout << bt[i][J] << " ";
        J=bt[i][J];
    }cout << "\n";
}
int32_t main(){
    time_t t=clock();
    srand(time(NULL));
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T=1;//cin >> T;
    int test_case=1;
    while(T--){
        solve(test_case);
        test_case++;
    }
    cerr<<"Program terminated successfully\n";
    t=clock()-t;
    cerr<<"Time used: "<<fixed<<setprecision(3)<<(double)(t*1.0/CLOCKS_PER_SEC)<<" seconds\n";
}
/*
27 9
3 1 4 1 5 9 2 6 5 0 0 0 0 0 0 0 0 0 3 1 4 1 5 9 2 6 5
 
Me:
207 272 512 567 812 1127 1175 1271 1296 1296 1296 1296 1296 1296 1296 1296 1296 1296 1287 1280 1232 1215 1100 767 671 335 0 
-1000000000 278 544 607 902 1379 1463 1667 1782 1782 1782 1782 1782 1782 1782 1782 1782 1782 1827 1838 1862 1863 1838 1667 1607 1379 1134 
-1000000000 -1000000000 550 615 942 1469 1573 1859 2134 2134 2134 2134 2134 2134 2134 2134 2134 2134 2275 2318 2470 2503 2638 2755 2759 2723 2638 
-1000000000 -1000000000 -1000000000 621 950 1509 1613 1969 2244 2244 2244 2244 2244 2244 2244 2244 2244 2244 2385 2428 2590 2653 2938 3325 3389 3533 3598 
-1000000000 -1000000000 -1000000000 -1000000000 956 1517 1649 2021 2304 2304 2304 2304 2304 2304 2304 2304 2304 2304 2481 2536 2736 2781 2988 3505 3609 3873 4038 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1523 1657 2045 2356 2356 2356 2356 2356 2356 2356 2356 2356 2356 2533 2588 2808 2871 3156 3555 3659 4005 4280 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1663 2053 2380 2380 2380 2380 2380 2380 2380 2380 2380 2380 2563 2628 2868 2923 3206 3723 3827 4091 4340 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 2059 2388 2388 2388 2388 2388 2388 2388 2388 2388 2388 2587 2652 2900 2963 3258 3773 3877 4223 4498 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 2394 2394 2394 2394 2394 2394 2394 2394 2394 2394 2595 2660 2924 2987 3298 3825 3929 4273 4558 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 2394 2394 2394 2394 2394 2394 2394 2394 2394 2601 2666 2932 2995 3322 3865 3969 4325 4608
 
Model:
207 272 512 567 812 1127 1175 1271 1296 1296 1296 1296 1296 1296 1296 1296 1296 1296 1287 1280 1232 1215 1100 767 671 335 0 
-1000000000 278 544 607 [908] 1379 1483 1747 1912 1912 1912 1912 1912 1912 1912 1912 1912 1912 2023 2062 2198 2227 2350 2503 2531 2567 2592 
-1000000000 -1000000000 550 615 942 1475 1579 1891 2154 2154 2154 2154 2154 2154 2154 2154 2154 2154 2295 2338 2514 2559 2754 3069 3133 3327 3450 
-1000000000 -1000000000 -1000000000 621 950 1509 1615 1987 2250 2250 2250 2250 2250 2250 2250 2250 2250 2250 2403 2458 2666 2721 2966 3371 3455 3689 3854 
-1000000000 -1000000000 -1000000000 -1000000000 956 1517 1649 2021 2322 2322 2322 2322 2322 2322 2322 2322 2322 2322 2499 2554 2762 2817 3078 3533 3637 3901 4138 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1523 1657 2045 2356 2356 2356 2356 2356 2356 2356 2356 2356 2356 2533 2594 2834 2895 3174 3645 3749 4045 4308 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1663 2053 2380 2380 2380 2380 2380 2380 2380 2380 2380 2380 2563 2628 2868 2929 3230 3741 3845 4157 4420 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 2059 2388 2388 2388 2388 2388 2388 2388 2388 2388 2388 2587 2652 2900 2963 3264 3797 3901 4253 4516 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 2394 2394 2394 2394 2394 2394 2394 2394 2394 2394 2595 2660 2924 2987 3298 3831 3937 4309 4588 
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 2394 2394 2394 2394 2394 2394 2394 2394 2394 2601 2666 2932 2995 3322 3865 3971 4343 4644
*/

Compilation message

sequence.cpp: In member function 'void auxiliary_tree::dfs(long long int, long long int, long long int)':
sequence.cpp:79:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         for(int i=0;i<ori[node].size();i++){
      |                     ~^~~~~~~~~~~~~~~~~
sequence.cpp: In member function 'void auxiliary_tree::build(std::vector<long long int>)':
sequence.cpp:151:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |         for(int i=0;i<g.size();i++){
      |                     ~^~~~~~~~~
sequence.cpp:155:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |         for(int i=0;i<g.size();i++){
      |                     ~^~~~~~~~~
sequence.cpp:162:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |         for(int i=0;i<g.size();i++){
      |                     ~^~~~~~~~~
sequence.cpp:166:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |         for(int i=0;i<g.size();i++){
      |                     ~^~~~~~~~~
sequence.cpp:170:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  170 |         for(int i=0;i<g.size();i++){
      |                     ~^~~~~~~~~
sequence.cpp:176:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  176 |         for(int i=1;i<g.size();i++){
      |                     ~^~~~~~~~~
sequence.cpp: In member function 'void auxiliary_tree::clear()':
sequence.cpp:193:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  193 |         for(int i=0;i<storelatest.size();i++){
      |                     ~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 364 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 364 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 364 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 364 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 364 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 364 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 364 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 364 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 384 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 364 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 364 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 364 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 364 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 364 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 364 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 364 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 364 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 620 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 364 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 364 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 364 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 492 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 492 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 364 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 492 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 364 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 3 ms 1388 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 364 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 2 ms 1004 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 3 ms 1260 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 2 ms 1388 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 1 ms 748 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 492 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 1 ms 620 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 492 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 13 ms 2156 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 492 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 14 ms 2156 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 12 ms 1900 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 14 ms 2156 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 12 ms 2156 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 4 ms 748 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 6 ms 1132 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 4 ms 876 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 3 ms 876 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 133 ms 9452 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 4 ms 876 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 92 ms 5996 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 103 ms 6764 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 132 ms 7276 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 92 ms 5996 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 100 ms 6764 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 128 ms 8556 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 30 ms 4904 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 30 ms 4876 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 1344 ms 82584 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 31 ms 5420 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 1540 ms 82304 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 1093 ms 58792 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 1190 ms 63220 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 908 ms 51792 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 1002 ms 59120 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 1293 ms 74636 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845