Submission #7443801


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
#define def -1
template<class V, int NV> struct SegTree { //[l,r)
    V comp(V& l, V& r) { return max(l, r); };

    vector<V> val; SegTree() { val = vector<V>(NV * 2, def); }
    V get(int x, int y, int l = 0, int r = NV, int k = 1) {
        if (r <= x || y <= l)return def; if (x <= l && r <= y)return val[k];
        auto a = get(x, y, l, (l + r) / 2, k * 2); 
        auto b = get(x, y, (l + r) / 2, r, k * 2 + 1);
        return comp(a, b);
    }
    void update(int i, V v) {
        i += NV; val[i] = v;
        while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]);
    }
    void add(int i, V v) { update(i, val[i + NV] + v); }
    V operator[](int x) { return get(x, x + 1); }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     @hamayanhamayan
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/

int N, P[101010];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N) cin >> P[i];

    SegTree<int,1<<17> st;
    rep(i, 0, N) st.update(i, P[i]);

    ll ans = 0;
    rep(i, 0, N) {
        int a = -1, b = -1, c = -1, d = N, ok, ng;
        ll comb = 0;

        if(P[i] < st.get(0, i)) {
            ok = 0;
            ng = i;
            while(ok + 1 != ng) {
                int md = (ok + ng) / 2;
                if(P[i] < st.get(md, i))
                    ok = md;
                else
                    ng = md;
            }
            b = ok;

            if(P[i] < st.get(0, b)) {
                ok = 0, ng = b;
                while(ok + 1 != ng) {
                    int md = (ok + ng) / 2;
                    if(P[i] < st.get(md, b))
                        ok = md;
                    else
                        ng = md;
                }
                a = ok;
            }
        } else
            b = -1;

        if(P[i] < st.get(i, N)) {
            ok = N;
            ng = i;
            while(ng + 1 != ok) {
                int md = (ok + ng) / 2;
                if(P[i] < st.get(i, md))
                    ok = md;
                else
                    ng = md;
            }
            c = ok-1;

            if (P[i] < st.get(c + 1, N)) {
                ok = N, ng = c + 1;
                while(ng + 1 != ok) {
                    int md = (ok + ng) / 2;
                    if(P[i] < st.get(c+1, md))
                        ok = md;
                    else
                        ng = md;
                }
                d = ok - 1;
            }
        }
        else c = N;

        //printf("%d %d %d %d\n", a, b, c, d);

        if (0 <= b) {
            comb += 1LL * (b - a) * (c - i);
        }
        if (c < N) {
            comb += 1LL * (d - c) * (i - b);
        }

        ans += comb * P[i];
    }
    cout << ans << endl;
}

Submission Info

Submission Time
Task E - Second Sum
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 500
Code Size 3924 Byte
Status
Exec Time 945 ms
Memory 1664 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt
All 500 / 500 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt, 01-small-01.txt, 01-small-02.txt, 01-small-03.txt, 01-small-04.txt, 01-small-05.txt, 02-large-01.txt, 02-large-02.txt, 02-large-03.txt, 02-large-04.txt, 02-large-05.txt, 03-max-01.txt, 03-max-02.txt, 04-min-01.txt, 05-sorted-01.txt, 05-sorted-02.txt, 06-almost-sorted-01.txt, 06-almost-sorted-02.txt, 06-almost-sorted-03.txt, 06-almost-sorted-04.txt
Case Name Status Exec Time Memory
00-sample-01.txt 2 ms 1280 KB
00-sample-02.txt 2 ms 1280 KB
00-sample-03.txt 2 ms 1280 KB
01-small-01.txt 2 ms 1280 KB
01-small-02.txt 2 ms 1280 KB
01-small-03.txt 2 ms 1280 KB
01-small-04.txt 2 ms 1280 KB
01-small-05.txt 2 ms 1280 KB
02-large-01.txt 824 ms 1664 KB
02-large-02.txt 491 ms 1536 KB
02-large-03.txt 864 ms 1664 KB
02-large-04.txt 877 ms 1664 KB
02-large-05.txt 520 ms 1536 KB
03-max-01.txt 945 ms 1664 KB
03-max-02.txt 944 ms 1664 KB
04-min-01.txt 2 ms 1280 KB
05-sorted-01.txt 441 ms 1664 KB
05-sorted-02.txt 488 ms 1664 KB
06-almost-sorted-01.txt 425 ms 1664 KB
06-almost-sorted-02.txt 451 ms 1664 KB
06-almost-sorted-03.txt 443 ms 1664 KB
06-almost-sorted-04.txt 448 ms 1664 KB