Submission #654519

# Submission time Handle Problem Language Result Execution time Memory
654519 2022-10-31T14:58:46 Z ayalla Divide and conquer (IZhO14_divide) C++14
100 / 100
46 ms 10576 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;
  }
  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;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 340 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 2 ms 352 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 340 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 2 ms 352 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
25 Correct 2 ms 844 KB Output is correct
26 Correct 4 ms 1236 KB Output is correct
27 Correct 5 ms 1236 KB Output is correct
28 Correct 20 ms 5340 KB Output is correct
29 Correct 22 ms 5352 KB Output is correct
30 Correct 45 ms 10484 KB Output is correct
31 Correct 40 ms 10492 KB Output is correct
32 Correct 41 ms 10576 KB Output is correct
33 Correct 40 ms 10496 KB Output is correct
34 Correct 40 ms 10480 KB Output is correct
35 Correct 44 ms 10452 KB Output is correct
36 Correct 46 ms 10496 KB Output is correct