結果

問題 No.1965 Heavier
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-05-15 22:46:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,016 bytes
コンパイル時間 1,734 ms
コンパイル使用メモリ 137,788 KB
実行使用メモリ 55,140 KB
最終ジャッジ日時 2023-08-20 21:36:32
合計ジャッジ時間 16,512 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 473 ms
51,688 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 346 ms
51,832 KB
testcase_26 AC 346 ms
51,684 KB
testcase_27 AC 848 ms
55,140 KB
testcase_28 AC 848 ms
54,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>

using namespace std;

template<class S, S (*op)(S, S), S (*e)()>struct SegTree {
    vector<S> seg;
    long long N = 1;

    SegTree (long long n) : SegTree(vector<S>(n, e())) {}
    SegTree (const vector<S> &v){
        long long n = v.size();
        while (N < n) N *= 2;
        seg.resize(N*2-1, e());
        for (long long i=0; i<n; i++) seg[i+N-1] = v[i];
        for (long long i=N-2; i>=0; i--){
            seg[i] = op(seg[i*2+1], seg[i*2+2]);
        }
    }

    void set(long long loc, S val){
        loc += N-1;
        seg[loc] = val;
        while (loc != 0){
            loc = (loc-1)/2;
            seg[loc] = op(seg[loc*2+1], seg[loc*2+2]);
        }
    }

    //op(a[l], ..., a[r])
    S prod (long long l, long long r) const{
        return _prod(l, r, 0, 0, N-1);
    }

    S all_prod() const{
        return seg[0];
    }

    S _prod (long long l, long long r, long long idx, long long bitl, long long bitr) const{
        if (r < bitl || l > bitr) return e();
        if (l <= bitl && bitr <= r) return seg[idx];

        long long bitm = (bitl+bitr)/2;
        return op(_prod(l, r, idx*2+1, bitl, bitm), _prod(l, r, idx*2+2, bitm+1, bitr));
    }

    S get (long long i) const{
        return seg[i+N-1];
    }

    void show() const{
        for (int i=N-1; i<N*2-1; i++) cout << seg[i] << " ";
        cout << endl;
    }
};

long long op(long long a, long long b){
    return max(a, b);
}

const long long INF = (1LL<<62) - 1;

long long e(){
    return -INF;
}

template <typename T>
map<T, T> compress(vector<T> &A){

    map<T, T> comp;
    int N = A.size(), i=0;
    for (int i=0; i<N; i++) comp[A[i]];

    for (auto &e : comp){
        e.second = i;
        i++;
    }

    return comp;
}

using ll = long long;
 
int main(){

    ll N, l=0, ans=0;
    cin >> N;
    vector<ll> A(N), B(N);
    for (int i=0; i<N; i++) cin >> A[i];
    for (int i=0; i<N; i++) cin >> B[i];

    vector<set<ll>> v(N), w(N);
    map<ll, ll> mp = compress(B);
    SegTree<long long, op, e> mi(N+1), pl(N+1);

    auto judge = [&](auto a, auto b){
        return (pl.prod(mp[b], N) < a+b) && (mi.prod(0, mp[b]-1) < a-b);
    };

    for (int r=0; r<N; r++){
        while(l<r && !judge(A[r], B[r])){
            v[mp[B[l]]].erase(A[l]-B[l]);
            w[mp[B[l]]].erase(A[r]-B[r]);
            if (v[mp[B[l]]].size() == 0) mi.set(mp[B[l]], e());
            else mi.set(mp[B[l]], *v[mp[B[l]]].rbegin());
            if (w[mp[B[l]]].size() == 0) pl.set(mp[B[l]], e());
            else pl.set(mp[B[l]], *w[mp[B[l]]].rbegin());
            l++;
        }
        if (judge(A[r], B[r])) ans += r-l;
        mi.set(mp[B[r]], A[r]-B[r]);
        pl.set(mp[B[r]], A[r]+B[r]);
        v[mp[B[r]]].insert(A[r]-B[r]);
        w[mp[B[r]]].insert(A[r]+B[r]);
    }

    cout << ans << endl;
    return 0;
}
0