답안 #1060807

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1060807 2024-08-15T23:05:20 Z MilosMilutinovic Tricks of the Trade (CEOI23_trade) C++14
50 / 100
2279 ms 181272 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 250000;

const int MAX = (int) 1.00001e9;

int root[N * 60], ch[N * 60][2], cnt[N * 60], tsz;
long long sum[N * 60];

void Modify(int& x, int y, int l, int r, int i) {
  x = ++tsz;
  ch[x][0] = ch[y][0];
  ch[x][1] = ch[y][1];
  sum[x] = sum[y] + i;
  cnt[x] = cnt[y] + 1;
  if (l == r) {
    return;
  }
  int mid = (l + r) >> 1;
  if (i <= mid) {
    Modify(ch[x][0], ch[y][0], l, mid, i);
  } else {
    Modify(ch[x][1], ch[y][1], mid + 1, r, i);
  }
}

int Walk(int x, int y, int l, int r, int k) {
  if (l == r) {
    return l;
  }
  int mid = (l + r) >> 1;
  if (cnt[ch[x][1]] - cnt[ch[y][1]] >= k) {
    return Walk(ch[x][1], ch[y][1], mid + 1, r, k);
  } else {
    return Walk(ch[x][0], ch[y][0], l, mid, k - (cnt[ch[x][1]] - cnt[ch[y][1]]));
  }
}

int QueryCnt(int x, int y, int l, int r, int ll, int rr) {
  if (ll <= l && r <= rr) {
    return cnt[x] - cnt[y];
  }
  int mid = (l + r) >> 1;
  if (rr <= mid) {
    return QueryCnt(ch[x][0], ch[y][0], l, mid, ll, rr);
  } else if (ll > mid) {
    return QueryCnt(ch[x][1], ch[y][1], mid + 1, r, ll, rr);
  } else {
    return QueryCnt(ch[x][0], ch[y][0], l, mid, ll, rr) + QueryCnt(ch[x][1], ch[y][1], mid + 1, r, ll, rr);
  }
}

long long QuerySum(int x, int y, int l, int r, int ll, int rr) {
  if (ll <= l && r <= rr) {
    return sum[x] - sum[y];
  }
  int mid = (l + r) >> 1;
  if (rr <= mid) {
    return QuerySum(ch[x][0], ch[y][0], l, mid, ll, rr);
  } else if (ll > mid) {
    return QuerySum(ch[x][1], ch[y][1], mid + 1, r, ll, rr);
  } else {
    return QuerySum(ch[x][0], ch[y][0], l, mid, ll, rr) + QuerySum(ch[x][1], ch[y][1], mid + 1, r, ll, rr);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  vector<int> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  vector<long long> pref(n);
  for (int i = 0; i < n; i++) {
    pref[i] = (i == 0 ? 0LL : pref[i - 1]) + c[i];
  }
  for (int i = 0; i < n; i++) {
    if (i > 0) {
      root[i] = root[i - 1];
    }
    Modify(root[i], root[i], 0, MAX, s[i]);
  }
  const long long inf = (long long) -1e18;
  long long res = inf;
  int from = -1, to = -1;
  auto Calc = [&](int l, int r) {
    if (r - l + 1 < k) {
      return inf;
    }
    long long ret = -(pref[r] - (l == 0 ? 0LL : pref[l - 1]));
    int el = Walk(root[r], l == 0 ? 0 : root[l - 1], 0, MAX, k);
    int c = QueryCnt(root[r], l == 0 ? 0 : root[l - 1], 0, MAX, el + 1, MAX);
    ret += QuerySum(root[r], l == 0 ? 0 : root[l - 1], 0, MAX, el + 1, MAX);
    ret += el * 1LL * (k - c);
    return ret;
  };
  vector<pair<int, int>> segs;
  vector<int> idx(n);
  function<void(int, int, int, int)> Solve = [&](int l, int r, int optl, int optr) {
    if (l > r) {
      return;
    }
    int mid = (l + r) >> 1;
    int opt = min(optl, mid);
    long long best = Calc(opt, mid);
    for (int i = max(0, optl); i <= min(mid, optr); i++) {
      long long v = Calc(i, mid);
      if (v >= best && v != inf) {
        best = v;
        opt = i;
      }
    }
    res = max(res, best);
    idx[mid] = opt;
    Solve(l, mid - 1, optl, opt);
    Solve(mid + 1, r, opt, optr);
  };
  Solve(0, n - 1, 0, n - 1);
  cout << res << '\n';
  vector<int> ids;
  for (int i = 0; i < n; i++) {
    if (Calc(idx[i], i) == res) {
      ids.push_back(i);
    }
  }
  int ptr = 0;
  for (int b = 0; b < (int) ids.size(); b++) {
    int i = ids[b];
    while (ptr <= idx[i]) {
      segs.emplace_back(ptr, i);
      if (b > 0) {
        segs.emplace_back(ptr, ids[b - 1]);
      }
      if (b + 1 < (int) ids.size()) {
        segs.emplace_back(ptr, ids[b + 1]);
      }
      ptr += 1;
    }
  }
  vector<int> seq(n, 0);
  vector<vector<int>> qs(n + 1);
  for (auto& p : segs) {
    int l = p.first;
    int r = p.second;
    if (l > r || Calc(l, r) != res) {
      continue;
    }
    int el = Walk(root[r], l == 0 ? 0 : root[l - 1], 0, MAX, k);
    qs[l].push_back(el);
    qs[r + 1].push_back(~el);
  }
  multiset<int> st;
  for (int i = 0; i < n; i++) {
    for (int j : qs[i]) {
      if (j >= 0) {
        st.insert(j);
      } else {
        st.erase(st.find(~j));
      }
    }
    if (!st.empty() && s[i] >= *st.begin()) {
      seq[i] = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    cout << seq[i];
  }
  cout << '\n';
  return 0;
}

Compilation message

trade.cpp: In function 'int main()':
trade.cpp:94:7: warning: unused variable 'from' [-Wunused-variable]
   94 |   int from = -1, to = -1;
      |       ^~~~
trade.cpp:94:18: warning: unused variable 'to' [-Wunused-variable]
   94 |   int from = -1, to = -1;
      |                  ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Partially correct 1 ms 604 KB Partially correct
7 Partially correct 1 ms 604 KB Partially correct
8 Partially correct 1 ms 604 KB Partially correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Partially correct 1 ms 604 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Partially correct 1 ms 604 KB Partially correct
7 Partially correct 1 ms 604 KB Partially correct
8 Partially correct 1 ms 604 KB Partially correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Partially correct 1 ms 604 KB Partially correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Partially correct 1 ms 604 KB Partially correct
18 Partially correct 1 ms 604 KB Partially correct
19 Partially correct 1 ms 604 KB Partially correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Partially correct 1 ms 604 KB Partially correct
23 Correct 3 ms 6108 KB Output is correct
24 Partially correct 38 ms 6392 KB Partially correct
25 Partially correct 29 ms 6400 KB Partially correct
26 Partially correct 35 ms 6236 KB Partially correct
27 Partially correct 17 ms 6744 KB Partially correct
28 Correct 28 ms 6236 KB Output is correct
29 Correct 13 ms 6144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1376 ms 180624 KB Output is correct
3 Correct 1144 ms 166356 KB Output is correct
4 Correct 1949 ms 167884 KB Output is correct
5 Partially correct 2173 ms 173880 KB Partially correct
6 Partially correct 1979 ms 174208 KB Partially correct
7 Correct 2162 ms 178152 KB Output is correct
8 Correct 1127 ms 167408 KB Output is correct
9 Correct 1199 ms 167120 KB Output is correct
10 Correct 1313 ms 177720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1376 ms 180624 KB Output is correct
3 Correct 1144 ms 166356 KB Output is correct
4 Correct 1949 ms 167884 KB Output is correct
5 Partially correct 2173 ms 173880 KB Partially correct
6 Partially correct 1979 ms 174208 KB Partially correct
7 Correct 2162 ms 178152 KB Output is correct
8 Correct 1127 ms 167408 KB Output is correct
9 Correct 1199 ms 167120 KB Output is correct
10 Correct 1313 ms 177720 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1373 ms 180528 KB Output is correct
13 Correct 1119 ms 166364 KB Output is correct
14 Correct 1906 ms 167752 KB Output is correct
15 Partially correct 2181 ms 174108 KB Partially correct
16 Partially correct 1975 ms 174364 KB Partially correct
17 Correct 2211 ms 178036 KB Output is correct
18 Correct 1097 ms 167640 KB Output is correct
19 Correct 1189 ms 167080 KB Output is correct
20 Correct 1331 ms 177604 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 0 ms 604 KB Output is correct
25 Partially correct 1 ms 604 KB Partially correct
26 Partially correct 1 ms 604 KB Partially correct
27 Partially correct 1 ms 852 KB Partially correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Partially correct 1 ms 604 KB Partially correct
31 Correct 1190 ms 167692 KB Output is correct
32 Partially correct 2279 ms 173084 KB Partially correct
33 Partially correct 2084 ms 171336 KB Partially correct
34 Partially correct 1942 ms 173356 KB Partially correct
35 Partially correct 1819 ms 172768 KB Partially correct
36 Correct 1692 ms 167840 KB Output is correct
37 Partially correct 1567 ms 176440 KB Partially correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Partially correct 1 ms 604 KB Partially correct
9 Partially correct 1 ms 604 KB Partially correct
10 Partially correct 1 ms 604 KB Partially correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Partially correct 1 ms 604 KB Partially correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Partially correct 1 ms 604 KB Partially correct
20 Partially correct 1 ms 604 KB Partially correct
21 Partially correct 1 ms 604 KB Partially correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Partially correct 1 ms 604 KB Partially correct
25 Correct 3 ms 6108 KB Output is correct
26 Partially correct 38 ms 6392 KB Partially correct
27 Partially correct 29 ms 6400 KB Partially correct
28 Partially correct 35 ms 6236 KB Partially correct
29 Partially correct 17 ms 6744 KB Partially correct
30 Correct 28 ms 6236 KB Output is correct
31 Correct 13 ms 6144 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1376 ms 180624 KB Output is correct
34 Correct 1144 ms 166356 KB Output is correct
35 Correct 1949 ms 167884 KB Output is correct
36 Partially correct 2173 ms 173880 KB Partially correct
37 Partially correct 1979 ms 174208 KB Partially correct
38 Correct 2162 ms 178152 KB Output is correct
39 Correct 1127 ms 167408 KB Output is correct
40 Correct 1199 ms 167120 KB Output is correct
41 Correct 1313 ms 177720 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 1373 ms 180528 KB Output is correct
44 Correct 1119 ms 166364 KB Output is correct
45 Correct 1906 ms 167752 KB Output is correct
46 Partially correct 2181 ms 174108 KB Partially correct
47 Partially correct 1975 ms 174364 KB Partially correct
48 Correct 2211 ms 178036 KB Output is correct
49 Correct 1097 ms 167640 KB Output is correct
50 Correct 1189 ms 167080 KB Output is correct
51 Correct 1331 ms 177604 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 0 ms 604 KB Output is correct
56 Partially correct 1 ms 604 KB Partially correct
57 Partially correct 1 ms 604 KB Partially correct
58 Partially correct 1 ms 852 KB Partially correct
59 Correct 1 ms 604 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Partially correct 1 ms 604 KB Partially correct
62 Correct 1190 ms 167692 KB Output is correct
63 Partially correct 2279 ms 173084 KB Partially correct
64 Partially correct 2084 ms 171336 KB Partially correct
65 Partially correct 1942 ms 173356 KB Partially correct
66 Partially correct 1819 ms 172768 KB Partially correct
67 Correct 1692 ms 167840 KB Output is correct
68 Partially correct 1567 ms 176440 KB Partially correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 1379 ms 181272 KB Output is correct
71 Correct 1123 ms 168360 KB Output is correct
72 Correct 1912 ms 169672 KB Output is correct
73 Partially correct 2187 ms 175668 KB Partially correct
74 Partially correct 2041 ms 176176 KB Partially correct
75 Correct 2171 ms 179660 KB Output is correct
76 Correct 1075 ms 169204 KB Output is correct
77 Correct 1190 ms 168356 KB Output is correct
78 Correct 1296 ms 179036 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 1 ms 604 KB Output is correct
82 Correct 1 ms 604 KB Output is correct
83 Partially correct 1 ms 448 KB Partially correct
84 Partially correct 1 ms 600 KB Partially correct
85 Partially correct 1 ms 604 KB Partially correct
86 Correct 1 ms 604 KB Output is correct
87 Correct 1 ms 604 KB Output is correct
88 Partially correct 1 ms 604 KB Partially correct
89 Correct 1194 ms 169736 KB Output is correct
90 Partially correct 2257 ms 174488 KB Partially correct
91 Partially correct 2095 ms 173136 KB Partially correct
92 Partially correct 2009 ms 175352 KB Partially correct
93 Partially correct 1883 ms 174252 KB Partially correct
94 Correct 1780 ms 167888 KB Output is correct
95 Partially correct 1607 ms 176448 KB Partially correct
96 Correct 3 ms 6232 KB Output is correct
97 Partially correct 36 ms 6452 KB Partially correct
98 Partially correct 30 ms 6228 KB Partially correct
99 Partially correct 33 ms 6480 KB Partially correct
100 Partially correct 18 ms 6488 KB Partially correct
101 Correct 25 ms 6236 KB Output is correct
102 Correct 13 ms 6200 KB Output is correct
103 Correct 1200 ms 169764 KB Output is correct
104 Partially correct 2015 ms 173948 KB Partially correct
105 Partially correct 1985 ms 175136 KB Partially correct
106 Partially correct 1996 ms 174120 KB Partially correct
107 Partially correct 148 ms 167764 KB Partially correct
108 Partially correct 986 ms 170152 KB Partially correct
109 Partially correct 946 ms 178004 KB Partially correct
110 Correct 1057 ms 174784 KB Output is correct
111 Correct 625 ms 168024 KB Output is correct
112 Partially correct 1456 ms 175420 KB Partially correct