답안 #376272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376272 2021-03-11T06:40:33 Z valerikk 별자리 3 (JOI20_constellation3) C++17
100 / 100
310 ms 23032 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = (int)2e5 + 7;

ll f[N];

void add(int pos, ll delt) {
  for (int i = pos + 1; i < N; i += i & -i) f[i] += delt;
}

void addseg(int l, int r, ll delt) {
  add(l, delt);
  add(r, -delt);
}

ll get(int pos) {
  ll sum = 0;
  for (int i = pos; i > 0; i -= i & -i) sum += f[i];
  return sum;
}

int n, a[N];
int m;
int x[N], y[N];
ll c[N];
int l[N], r[N];
vector<int> who[N];
int ord[N];

bool cmp(int i, int j) {
  return make_pair(r[i] - l[i], make_pair(l[i], i)) < make_pair(r[j] - l[j], make_pair(l[j], j));
}

signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  cin >> m;
  for (int i = 0; i < m; ++i) {
    cin >> x[i] >> y[i] >> c[i];
    --x[i];
    --y[i];
    who[x[i]].push_back(i);
  }
  vector<pair<int, int>> st = {{(int)1e9, -1}};
  for (int i = 0; i < n; ++i) {
    while (!st.empty() && st.back().first <= a[i]) st.pop_back();
    st.push_back({a[i], i});
    for (int ind : who[i]) l[ind] = lower_bound(st.rbegin(), st.rend(), make_pair(y[ind], -1))->second + 1;
  }
  st = {{(int)1e9, n}};
  for (int i = n - 1; i >= 0; --i) {
    while (!st.empty() && st.back().first <= a[i]) st.pop_back();
    st.push_back({a[i], i});
    for (int ind : who[i]) r[ind] = lower_bound(st.rbegin(), st.rend(), make_pair(y[ind], -1))->second;
  }
  for (int i = 0; i < m; ++i) ord[i] = i;
  sort(ord, ord + m, cmp);
  ll sum = 0;
  for (int i = 0; i < m; ++i) sum += c[i];
  for (int i = 0; i < m; ++i) {
    int ind = ord[i];
    ll curr = c[ind] - get(x[ind] + 1);
    if (curr > 0) {
      sum -= curr;
      addseg(l[ind], r[ind], curr);
    }
  }
  cout << sum << endl;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5228 KB Output is correct
2 Correct 5 ms 5244 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 5 ms 5100 KB Output is correct
5 Correct 5 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 5 ms 5100 KB Output is correct
9 Correct 5 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 5 ms 5248 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 5 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 4 ms 5100 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5228 KB Output is correct
2 Correct 5 ms 5244 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 5 ms 5100 KB Output is correct
5 Correct 5 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 5 ms 5100 KB Output is correct
9 Correct 5 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 5 ms 5248 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 5 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 4 ms 5100 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 5 ms 5228 KB Output is correct
24 Correct 7 ms 5228 KB Output is correct
25 Correct 7 ms 5228 KB Output is correct
26 Correct 5 ms 5228 KB Output is correct
27 Correct 6 ms 5228 KB Output is correct
28 Correct 5 ms 5228 KB Output is correct
29 Correct 7 ms 5228 KB Output is correct
30 Correct 5 ms 5228 KB Output is correct
31 Correct 6 ms 5228 KB Output is correct
32 Correct 6 ms 5228 KB Output is correct
33 Correct 6 ms 5228 KB Output is correct
34 Correct 5 ms 5228 KB Output is correct
35 Correct 5 ms 5228 KB Output is correct
36 Correct 5 ms 5228 KB Output is correct
37 Correct 6 ms 5228 KB Output is correct
38 Correct 5 ms 5228 KB Output is correct
39 Correct 6 ms 5228 KB Output is correct
40 Correct 6 ms 5228 KB Output is correct
41 Correct 6 ms 5228 KB Output is correct
42 Correct 6 ms 5228 KB Output is correct
43 Correct 6 ms 5376 KB Output is correct
44 Correct 5 ms 5228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5228 KB Output is correct
2 Correct 5 ms 5244 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 5 ms 5100 KB Output is correct
5 Correct 5 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 5 ms 5100 KB Output is correct
9 Correct 5 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 5 ms 5248 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 5 ms 5100 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 4 ms 5100 KB Output is correct
22 Correct 4 ms 5100 KB Output is correct
23 Correct 5 ms 5228 KB Output is correct
24 Correct 7 ms 5228 KB Output is correct
25 Correct 7 ms 5228 KB Output is correct
26 Correct 5 ms 5228 KB Output is correct
27 Correct 6 ms 5228 KB Output is correct
28 Correct 5 ms 5228 KB Output is correct
29 Correct 7 ms 5228 KB Output is correct
30 Correct 5 ms 5228 KB Output is correct
31 Correct 6 ms 5228 KB Output is correct
32 Correct 6 ms 5228 KB Output is correct
33 Correct 6 ms 5228 KB Output is correct
34 Correct 5 ms 5228 KB Output is correct
35 Correct 5 ms 5228 KB Output is correct
36 Correct 5 ms 5228 KB Output is correct
37 Correct 6 ms 5228 KB Output is correct
38 Correct 5 ms 5228 KB Output is correct
39 Correct 6 ms 5228 KB Output is correct
40 Correct 6 ms 5228 KB Output is correct
41 Correct 6 ms 5228 KB Output is correct
42 Correct 6 ms 5228 KB Output is correct
43 Correct 6 ms 5376 KB Output is correct
44 Correct 5 ms 5228 KB Output is correct
45 Correct 243 ms 22268 KB Output is correct
46 Correct 249 ms 22124 KB Output is correct
47 Correct 250 ms 22380 KB Output is correct
48 Correct 229 ms 22124 KB Output is correct
49 Correct 253 ms 22124 KB Output is correct
50 Correct 229 ms 21868 KB Output is correct
51 Correct 287 ms 22104 KB Output is correct
52 Correct 264 ms 22508 KB Output is correct
53 Correct 230 ms 21996 KB Output is correct
54 Correct 310 ms 23032 KB Output is correct
55 Correct 277 ms 22912 KB Output is correct
56 Correct 285 ms 22632 KB Output is correct
57 Correct 269 ms 22636 KB Output is correct
58 Correct 236 ms 21868 KB Output is correct
59 Correct 247 ms 22124 KB Output is correct
60 Correct 138 ms 21468 KB Output is correct
61 Correct 231 ms 21892 KB Output is correct
62 Correct 309 ms 22888 KB Output is correct
63 Correct 234 ms 21868 KB Output is correct
64 Correct 235 ms 21484 KB Output is correct
65 Correct 276 ms 22632 KB Output is correct
66 Correct 231 ms 21996 KB Output is correct