답안 #654518

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
654518 2022-10-31T14:57:04 Z ayalla 금 캐기 (IZhO14_divide) C++14
100 / 100
52 ms 10780 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long int
#define endl '\n'
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define MAXN 100005
#define mod 1000000007

struct item
{
  int pref, sum;
};

vector<item> seg;
vector<int> aux;

item single(int x)
{
  return {x, x};
}
item neutral()
{
  return {0, 0};
}
item merge(item a, item b)
{
  item ret;
  ret.pref = max(a.pref, a.sum + b.pref);
  ret.sum = a.sum + b.sum;
  return ret;
}
void update(int i, int l, int r, int q, int x)
{
  if (l == r)
  {
    seg[i] = single(x);
    return;
  }
  int mid = (l + r) >> 1;
  if (q <= mid)
    update(i << 1, l, mid, q, x);
  else
    update((i << 1) | 1, mid + 1, r, q, x);
  seg[i] = merge(seg[i << 1], seg[(i << 1) | 1]);
}
int query(int l, int r, int i, int x)
{
  if (l == r)
    return l;
  int mid = (l + r) >> 1;
  int curr = seg[i << 1].sum + seg[(i << 1) | 1].pref;
  if (curr >= x)
    return query(mid + 1, r, (i << 1) | 1, x - seg[i << 1].sum);
  if (seg[i << 1].pref < x)
    return -1;
  return query(l, mid, (i << 1), x);
}
void build(int l, int r, int i)
{
  if (l == r)
  {
    seg[i] = single(aux[l]);
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, i << 1);
  build(mid + 1, r, (i << 1) | 1);
  seg[i] = merge(seg[i << 1], seg[(i << 1) | 1]);
}
signed main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<pii> v(n);
  int ans = 0;
  for (int i = 0; i < n; i++)
  {
    cin >> v[i].fir >> v[i].sec.fir >> v[i].sec.sec;
    ans = max(ans, v[i].sec.fir);
  }
  vector<int> pref(n + 1, 0);
  for (int i = 0; i < n; i++)
  {
    pref[i + 1] = pref[i] + v[i].sec.fir;
  }
  sort(v.begin(), v.end());
  aux.assign(n, 0);
  for (int i = 1; i < n; i++)
  {
    aux[i] = v[i].sec.sec - (v[i].fir - v[i - 1].fir);
  }
  seg.resize(4 * n);
  build(0, n - 1, 1);
  for (int i = 0; i < n; i++)
  {
    int r = query(0, n - 1, 1, -v[i].sec.sec);
    if (r != -1)
    {
      int curr = pref[r + 1] - pref[i];
      ans = max(ans, curr);
    }
    if (i + 1 < n)
      update(1, 0, n - 1, i + 1, 0);
  }
  cout << ans << endl;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 3 ms 852 KB Output is correct
24 Correct 3 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 3 ms 852 KB Output is correct
24 Correct 3 ms 852 KB Output is correct
25 Correct 3 ms 852 KB Output is correct
26 Correct 5 ms 1364 KB Output is correct
27 Correct 5 ms 1364 KB Output is correct
28 Correct 26 ms 5504 KB Output is correct
29 Correct 29 ms 5552 KB Output is correct
30 Correct 47 ms 10624 KB Output is correct
31 Correct 48 ms 10756 KB Output is correct
32 Correct 52 ms 10768 KB Output is correct
33 Correct 44 ms 10676 KB Output is correct
34 Correct 47 ms 10748 KB Output is correct
35 Correct 49 ms 10780 KB Output is correct
36 Correct 50 ms 10700 KB Output is correct