結果

問題 No.1826 Fruits Collecting
ユーザー SSRSSSRS
提出日時 2022-01-28 21:39:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 1,972 bytes
コンパイル時間 4,164 ms
コンパイル使用メモリ 218,336 KB
実行使用メモリ 23,612 KB
最終ジャッジ日時 2023-08-28 19:25:02
合計ジャッジ時間 11,953 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 142 ms
11,752 KB
testcase_06 AC 237 ms
18,132 KB
testcase_07 AC 168 ms
12,808 KB
testcase_08 AC 19 ms
4,376 KB
testcase_09 AC 232 ms
15,612 KB
testcase_10 AC 156 ms
12,540 KB
testcase_11 AC 152 ms
12,384 KB
testcase_12 AC 60 ms
7,072 KB
testcase_13 AC 30 ms
4,828 KB
testcase_14 AC 48 ms
5,900 KB
testcase_15 AC 362 ms
23,464 KB
testcase_16 AC 367 ms
23,612 KB
testcase_17 AC 366 ms
23,564 KB
testcase_18 AC 365 ms
23,452 KB
testcase_19 AC 365 ms
23,568 KB
testcase_20 AC 364 ms
23,420 KB
testcase_21 AC 364 ms
23,400 KB
testcase_22 AC 364 ms
23,552 KB
testcase_23 AC 367 ms
23,432 KB
testcase_24 AC 365 ms
23,488 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 75 ms
7,760 KB
testcase_31 AC 147 ms
11,832 KB
testcase_32 AC 79 ms
8,396 KB
testcase_33 AC 265 ms
20,284 KB
testcase_34 AC 222 ms
16,540 KB
testcase_35 AC 53 ms
6,336 KB
testcase_36 AC 263 ms
20,076 KB
testcase_37 AC 175 ms
15,548 KB
testcase_38 AC 121 ms
12,488 KB
testcase_39 AC 189 ms
15,060 KB
testcase_40 AC 238 ms
17,716 KB
testcase_41 AC 53 ms
6,288 KB
testcase_42 AC 8 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct segment_tree{
	int N;
	vector<T> ST;
	segment_tree(int n){
		N = 1;
		while (N < n){
			N *= 2;
		}
		ST = vector<T>(N * 2 - 1, 0);
	}
	T operator [](int k){
		return ST[N - 1 + k];
	}
	void update(int k, T x){
		k += N - 1;
		ST[k] = x;
		while (k > 0){
			k = (k - 1) / 2;
			ST[k] = max(ST[k * 2 + 1], ST[k * 2 + 2]);
		}
	}
	T range_max(int L, int R, int i, int l, int r){
		if (R <= l || r <= L){
			return 0;
		} else if (L <= l && r <= R){
			return ST[i];
		} else {
			int m = (l + r) / 2;
			return max(range_max(L, R, i * 2 + 1, l, m), range_max(L, R, i * 2 + 2, m, r));
		}
	}
	T range_max(int L, int R){
		return range_max(L, R, 0, 0, N);
	}
	T all(){
		return ST[0];
	}
};
int main(){
  int N;
  cin >> N;
  vector<int> T(N), X(N), V(N);
  for (int i = 0; i < N; i++){
    cin >> T[i] >> X[i] >> V[i];
  }
  vector<long long> A(N), B(N);
  for (int i = 0; i < N; i++){
    A[i] = T[i] + X[i];
    B[i] = T[i] - X[i];
  }
  vector<long long> A2 = A;
  sort(A2.begin(), A2.end());
  A2.erase(unique(A2.begin(), A2.end()), A2.end());
  for (int i = 0; i < N; i++){
    A[i] = lower_bound(A2.begin(), A2.end(), A[i]) - A2.begin();
  }
  vector<long long> B2 = B;
  sort(B2.begin(), B2.end());
  B2.erase(unique(B2.begin(), B2.end()), B2.end());
  for (int i = 0; i < N; i++){
    B[i] = lower_bound(B2.begin(), B2.end(), B[i]) - B2.begin();
  }
  int cnt1 = A2.size(), cnt2 = B2.size();
  vector<vector<pair<int, int>>> P(cnt1);
  for (int i = 0; i < N; i++){
    if (A2[A[i]] >= 0 && B2[B[i]] >= 0){
      P[A[i]].push_back(make_pair(B[i], i));
    }
  }
  segment_tree<long long> dp(cnt2);
  for (int i = 0; i < cnt1; i++){
    sort(P[i].begin(), P[i].end());
    int cnt = P[i].size();
    for (int j = 0; j < cnt; j++){
      int pos = P[i][j].first;
      int value = V[P[i][j].second];
      dp.update(pos, dp.range_max(0, pos + 1) + value);
    }
  }
  cout << dp.all() << endl;
}
0