Submission #380819

#TimeUsernameProblemLanguageResultExecution timeMemory
380819ponytailSplit the sequence (APIO14_sequence)C++17
100 / 100
1540 ms82584 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...