Submission #232659

#TimeUsernameProblemLanguageResultExecution timeMemory
232659dualityConstellation 3 (JOI20_constellation3)C++11
100 / 100
931 ms29428 KiB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int A[200000];
vpii stars[200000];
int l[200000],r[200000];
vi S;
bool comp(int a,int b) {
    return r[a]-l[a] < r[b]-l[b];
}
int order[200000];
LLI dp[200000];
int low[200000];
int tree1[1 << 19];
int build(int s,int e,int i) {
    if (s == e) {
        tree1[i] = s;
        return 0;
    }

    int mid = (s+e) / 2;
    build(s,mid,2*i+1),build(mid+1,e,2*i+2);
    tree1[i] = (low[tree1[2*i+1]] < low[tree1[2*i+2]]) ? tree1[2*i+1]:tree1[2*i+2];
    return 0;
}
int update(int s,int e,int ai,int i) {
    if ((s > ai) || (e < ai)) return 0;
    else if (s == e) return 0;

    int mid = (s+e) / 2;
    update(s,mid,ai,2*i+1),update(mid+1,e,ai,2*i+2);
    tree1[i] = (low[tree1[2*i+1]] < low[tree1[2*i+2]]) ? tree1[2*i+1]:tree1[2*i+2];
    return 0;
}
int query(int s,int e,int qs,int qe,int i) {
    if ((s > qe) || (e < qs)) return qs;
    else if ((s >= qs) && (e <= qe)) return tree1[i];

    int mid = (s+e) / 2;
    int l = query(s,mid,qs,qe,2*i+1),r = query(mid+1,e,qs,qe,2*i+2);
    return (low[l] < low[r]) ? l:r;
}
LLI tree2[1 << 19],lazy[1 << 19];
int prop(int s,int e,int i) {
    tree2[i] += lazy[i];
    if (s != e) lazy[2*i+1] += lazy[i],lazy[2*i+2] += lazy[i];
    lazy[i] = 0;
    return 0;
}
int update2(int s,int e,int as,int ae,int i,LLI num) {
    prop(s,e,i);
    if ((s > ae) || (e < as)) return 0;
    else if ((s >= as) && (e <= ae)) {
        lazy[i] += num;
        prop(s,e,i);
        return 0;
    }

    int mid = (s+e) / 2;
    update2(s,mid,as,ae,2*i+1,num),update2(mid+1,e,as,ae,2*i+2,num);
    tree2[i] = max(tree2[2*i+1],tree2[2*i+2]);
    return 0;
}
LLI query2(int s,int e,int qs,int qe,int i) {
    prop(s,e,i);
    if ((s > qe) || (e < qs)) return 0;
    else if ((s >= qs) && (e <= qe)) return tree2[i];

    int mid = (s+e) / 2;
    return max(query2(s,mid,qs,qe,2*i+1),query2(mid+1,e,qs,qe,2*i+2));
}
int main() {
    int i;
    int N,M,X,Y,C;
    LLI sum = 0;
    scanf("%d",&N);
    for (i = 0; i < N; i++) scanf("%d",&A[i]);
    scanf("%d",&M);
    for (i = 0; i < M; i++) scanf("%d %d %d",&X,&Y,&C),stars[X-1].pb(mp(Y,C)),sum += C;

    for (i = 0; i < N; i++) {
        while (!S.empty() && (A[S.back()] <= A[i])) S.pop_back();
        if (!S.empty()) l[i] = S.back();
        else l[i] = -1;
        S.pb(i);
    }
    S.clear();
    for (i = N-1; i >= 0; i--) {
        while (!S.empty() && (A[S.back()] < A[i])) S.pop_back();
        if (!S.empty()) r[i] = S.back();
        else r[i] = N;
        S.pb(i);
    }
    for (i = 0; i < N; i++) {
        order[i] = i,sort(stars[i].begin(),stars[i].end(),greater<pii>());
        low[i] = stars[i].empty() ? 1e9:stars[i].back().first;
    }
    sort(order,order+N,comp);
    build(0,N-1,0);
    LLI ans = 0;
    for (i = 0; i < N; i++) {
        int u = order[i],v;
        LLI maxl = query2(0,N-1,l[u]+1,u-1,0),maxr = query2(0,N-1,u+1,r[u]-1,0);
        if (u-1 >= l[u]+1) {
            v = query(0,N-1,l[u]+1,u-1,0);
            while (low[v] <= A[u]) {
                maxl = max(maxl,query2(0,N-1,v,v,0)+stars[v].back().second);
                stars[v].pop_back(),low[v] = stars[v].empty() ? 1e9:stars[v].back().first;
                update(0,N-1,v,0),v = query(0,N-1,l[u]+1,u-1,0);
            }
        }
        if (r[u]-1 >= u+1) {
            v = query(0,N-1,u+1,r[u]-1,0);
            while (low[v] <= A[u]) {
                maxr = max(maxr,query2(0,N-1,v,v,0)+stars[v].back().second);
                stars[v].pop_back(),low[v] = stars[v].empty() ? 1e9:stars[v].back().first;
                update(0,N-1,v,0),v = query(0,N-1,u+1,r[u]-1,0);
            }
        }
        update2(0,N-1,l[u]+1,u,0,maxr),update2(0,N-1,u,r[u]-1,0,maxl);
        ans = max(ans,query2(0,N-1,u,u,0));
    }
    for (i = 0; i < N; i++) {
        LLI x = query2(0,N-1,i,i,0);
        while (!stars[i].empty()) ans = max(ans,stars[i].back().second+x),stars[i].pop_back();
    }
    printf("%lld\n",sum-ans);

    return 0;
}

Compilation message (stderr)

constellation3.cpp: In function 'int main()':
constellation3.cpp:130:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&N);
     ~~~~~^~~~~~~~~
constellation3.cpp:131:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&A[i]);
                             ~~~~~^~~~~~~~~~~~
constellation3.cpp:132:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&M);
     ~~~~~^~~~~~~~~
constellation3.cpp:133:78: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < M; i++) scanf("%d %d %d",&X,&Y,&C),stars[X-1].pb(mp(Y,C)),sum += C;
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...