結果

問題 No.1826 Fruits Collecting
ユーザー pockynypockyny
提出日時 2022-01-28 22:57:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,508 ms / 2,000 ms
コード長 6,246 bytes
コンパイル時間 3,754 ms
コンパイル使用メモリ 231,908 KB
実行使用メモリ 152,044 KB
最終ジャッジ日時 2023-08-28 21:35:36
合計ジャッジ時間 31,579 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,872 KB
testcase_01 AC 5 ms
6,044 KB
testcase_02 AC 6 ms
6,036 KB
testcase_03 AC 7 ms
6,312 KB
testcase_04 AC 3 ms
5,916 KB
testcase_05 AC 461 ms
67,172 KB
testcase_06 AC 871 ms
118,040 KB
testcase_07 AC 540 ms
73,624 KB
testcase_08 AC 39 ms
12,372 KB
testcase_09 AC 782 ms
89,784 KB
testcase_10 AC 507 ms
71,132 KB
testcase_11 AC 488 ms
70,148 KB
testcase_12 AC 172 ms
33,420 KB
testcase_13 AC 74 ms
18,844 KB
testcase_14 AC 126 ms
23,352 KB
testcase_15 AC 1,387 ms
151,956 KB
testcase_16 AC 1,399 ms
151,876 KB
testcase_17 AC 1,376 ms
151,900 KB
testcase_18 AC 1,378 ms
151,900 KB
testcase_19 AC 1,388 ms
151,884 KB
testcase_20 AC 1,377 ms
151,952 KB
testcase_21 AC 1,385 ms
152,044 KB
testcase_22 AC 1,410 ms
151,736 KB
testcase_23 AC 1,401 ms
151,956 KB
testcase_24 AC 1,384 ms
151,756 KB
testcase_25 AC 2 ms
5,532 KB
testcase_26 AC 2 ms
5,660 KB
testcase_27 AC 2 ms
5,508 KB
testcase_28 AC 2 ms
5,444 KB
testcase_29 AC 2 ms
5,576 KB
testcase_30 AC 169 ms
37,532 KB
testcase_31 AC 382 ms
70,680 KB
testcase_32 AC 329 ms
36,632 KB
testcase_33 AC 1,508 ms
120,164 KB
testcase_34 AC 1,140 ms
83,728 KB
testcase_35 AC 188 ms
23,424 KB
testcase_36 AC 1,471 ms
118,960 KB
testcase_37 AC 467 ms
80,448 KB
testcase_38 AC 328 ms
65,704 KB
testcase_39 AC 534 ms
104,340 KB
testcase_40 AC 666 ms
118,808 KB
testcase_41 AC 130 ms
30,180 KB
testcase_42 AC 17 ms
8,024 KB
testcase_43 AC 2 ms
5,384 KB
testcase_44 AC 2 ms
5,588 KB
testcase_45 AC 2 ms
5,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
typedef long long ll;
#define def 0
using V = ll;
V comp(V& l, V& r) { return max(l,r); };
struct SegTree { //[l,r)
    int NV;
    vector<V> val;
    void init(int n) {
        NV = 1;
        while (NV < n) NV *= 2;
        val = vector<V>(NV * 2, def);
    }
    V get(int x, int y, int l, int r, int k) {
        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);
    }
    V get(int x, int y) { return get(x, y, 0, NV, 1); }
    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); }
};

struct Healthy2DSegTree {
    int NV;
    vector<SegTree> st;
    vector<vector<int>> index;
    
    void init(vector<vector<int>> &v) {
        int n = v.size();
        NV = 1; while (NV < n) NV *= 2;
        index.resize(2 * NV);
        rep(i, 0, n) fore(j, v[i]) index[i + NV].push_back(j);
        rrep(i, NV * 2 - 1, 1) {
            sort(index[i].begin(), index[i].end());
            index[i].erase(unique(index[i].begin(), index[i].end()), index[i].end());
            fore(j, index[i]) index[i / 2].push_back(j);
        }
        st.resize(2 * NV);
        rep(i, 1, NV * 2) st[i].init(index[i].size());
    }
    void update(int x, int y, V v) {
        assert(x < NV);
        x += NV;
        while (x) {
            int yy = lower_bound(index[x].begin(), index[x].end(), y) - index[x].begin();
            assert(yy != index[x].size());
            assert(y == index[x][yy]);
            st[x].update(yy, v);
            x >>= 1;
        }
    }
    void add(int x, int y, V v) {
        assert(x < NV);
        x += NV;
        while (x) {
            int yy = lower_bound(index[x].begin(), index[x].end(), y) - index[x].begin();
            assert(yy != index[x].size());
            assert(y == index[x][yy]);
            st[x].add(yy, v);
            x >>= 1;
        }
    }
    V get(int sx, int tx, int sy, int ty, int k, int l, int r) {
        assert(k < NV * 2);
        assert(l < r);
        if (r <= sx or tx <= l) return def;
        if (sx <= l and r <= tx) {
            int syy = lower_bound(index[k].begin(), index[k].end(), sy) - index[k].begin();
            int tyy = lower_bound(index[k].begin(), index[k].end(), ty) - index[k].begin();
            return st[k].get(syy, tyy);
        }
        int md = (l + r) / 2;
        V le = get(sx, tx, sy, ty, k * 2, l, md);
        V ri = get(sx, tx, sy, ty, k * 2 + 1, md, r);
        return comp(le, ri);
    }
    V get(int sx, int tx, int sy, int ty) {
        return get(sx, tx, sy, ty, 1, 0, NV);
    }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/



//int H, W, Q;
//vector<vector<int>> query;
//---------------------------------------------------------------------------------------------------
ll t[200010],x[200010],v[200010];
void _main(){
    int i,n; cin >> n;
    for(i=0;i<n;i++) cin >> t[i] >> x[i] >> v[i];
    vector<ll> a(n),b(n);
    for(i=0;i<n;i++){
        a[i] = t[i] + x[i]; b[i] = t[i] - x[i];
    }
    sort(a.begin(),a.end()); sort(b.begin(),b.end());
    vector<pair<ll,pair<ll,ll>>> w(n);
    vector<vector<int>> index(n);
    for(i=0;i<n;i++){
        w[i] = {t[i],{x[i],v[i]}};
        int xx = lower_bound(a.begin(),a.end(),t[i] + x[i]) - a.begin();
        int yy = lower_bound(b.begin(),b.end(),t[i] - x[i]) - b.begin();
        index[xx].push_back(yy);
    }
    //index[0].push_back(0);
    sort(w.begin(),w.end());
    Healthy2DSegTree st;
    st.init(index);
    //ll inf = 1000000000000000;
    //st.update(0,0,inf);
    for(i=0;i<n;i++){
        if(w[i].first<abs(w[i].second.first)) continue;
        ll aa = w[i].first + w[i].second.first,bb = w[i].first - w[i].second.first;
        ll xx = lower_bound(a.begin(),a.end(),aa) - a.begin();
        ll yy = lower_bound(b.begin(),b.end(),bb) - b.begin();
        ll mx = st.get(0,xx + 1,0,yy + 1) + w[i].second.second;
        ll mx2 = st.get(xx,xx + 1,yy,yy + 1);
        if(mx2<=mx){
            //cout << mx << " " << mx2 << endl;
            //st.add(xx,yy,-mx2);
            st.update(xx,yy,mx);
        }
    }
    cout << st.get(0,n,0,n) << endl;   
}

/*void _main() {
    cin >> H >> W >> Q;
    rep(i, 0, Q) {
        int t; cin >> t;
        if (t == 1) {
            int x, y, v; cin >> x >> y >> v;
            query.push_back({ t, x, y, v });
        }
        else {
            int sx, sy, tx, ty; cin >> sx >> sy >> tx >> ty;
            query.push_back({ t, sx, sy, tx, ty });
        }
    }

    Healthy2DSegTree st;
    vector<vector<int>> index(W);
    rep(i, 0, Q) {
        if (query[i][0] == 1) {
            int x = query[i][1];
            int y = query[i][2];
            index[x].push_back(y);
        }
    }
    st.init(index);

    rep(i, 0, Q) {
        if (query[i][0] == 1) {
            int x = query[i][1];
            int y = query[i][2];
            int v = query[i][3];
            st.add(x, y, v);
        } else {
            int sx = query[i][1];
            int sy = query[i][2];
            int tx = query[i][3];
            int ty = query[i][4];
            ll ans = st.get(sx, tx + 1, sy, ty + 1);
            printf("%lld\n", ans);
        }
    }
}*/
0