답안 #331844

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331844 2020-11-30T12:10:15 Z quocnguyen1012 Colouring a rectangle (eJOI19_colouring) C++14
100 / 100
1668 ms 44548 KB
#include <bits/stdc++.h>


using namespace std;
using ll = long long;

const int maxn = 4e5 + 5;

struct segmenttree {
  vector<ll> st;
  vector<ll> lazy;
  int n;
  void init(int _n) {
    n = _n;
    st.assign(4 * n + 5, 0);
    lazy.assign(4 * n + 5, 0);
  }
#define lc id << 1
#define rc id << 1 | 1
  void push(int id, int l, int r) {
    st[id] += lazy[id];
    if (l != r) {
      lazy[lc] += lazy[id];
      lazy[rc] += lazy[id];
    }
    lazy[id] = 0;
  }
  void modify(int L, int R, ll v, int id, int l, int r) {
    push(id, l, r);
    if (R < l || r < L)
      return;
    if (L <= l && r <= R) {
      lazy[id] += v;
      push(id, l, r);
      return;
    }
    int mid = (l + r) / 2;
    modify(L, R, v, lc, l, mid);
    modify(L, R, v, rc, mid + 1, r);
    st[id] = min(st[lc], st[rc]);
  }
  void update(int pos, ll v, int id, int l, int r) {
    push(id, l, r);
    if (r < pos || pos < l)
      return;
    if (l == r) {
      st[id] = min(st[id], v);
      return;
    }
    int mid = (l + r) / 2;
    update(pos, v, lc, l, mid);
    update(pos, v, rc, mid + 1, r);
    st[id] = min(st[lc], st[rc]);
  }
  ll getmin(int L, int R, int id, int l, int r) {
    push(id, l, r);
    if (R < l || r < L)
      return 1e17;
    if (L <= l && r <= R)
      return st[id];
    int mid = (l + r) / 2;
    return min(getmin(L, R, lc, l, mid), getmin(L, R, rc, mid + 1, r));
  }
  void modify(int l, int r, ll v) {
    modify(l, r, v, 1, 0, n);
  }
  ll getmin(int l, int r) {
    return getmin(l, r, 1, 0, n);
  }
  void set(int pos, ll v) {
    update(pos, v, 1, 0, n);
  }
};

ll solve(vector<int> a, vector<int> b, vector<pair<int, int>> query) {
  int n = a.size(), m = b.size();
#ifdef LOCAL
  for (int i : a) cerr << i << ' '; cerr << '\n';
  for (int i : b) cerr << i << ' '; cerr << '\n';
  for (auto & it : query) cerr << it.first << ' ' << it.second << '\n';
#endif // LOCAL

  vector<int> order;
  for (int i = 1; i < n; ++i)
    order.push_back(i);
  sort(order.begin(), order.end(), [&](int x, int y) {
    return query[x - 1].first < query[y - 1].first;
  });

  vector<ll> sum(m + 5);
  for (int i = 1; i < m; ++i)
    sum[i] = sum[i - 1] + b[i];


  segmenttree st1;
  segmenttree st2;
  st1.init(m); st2.init(m);
  st1.modify(1, m, 1e15);
  st2.modify(1, m, 1e15);

  auto castdp = [&](int idx) {
    //cerr << idx << ' ';
    int l = query[idx - 1].first, r = query[idx - 1].second;

    ll v1 = st1.getmin(0, l - 1);
    ll v2 = st2.getmin(l, r);

    st1.modify(0, r, a[idx]);
    st2.modify(0, r, a[idx]);

    st1.set(r, v1 + sum[r] - sum[l - 1]);
    st2.set(r, v1 + sum[r] - sum[l - 1] - sum[r]);

    st1.set(r, v2 + sum[r]);
    st2.set(r, v2 + sum[r] - sum[r]);
  };

  for (int i : order) {
    castdp(i);
  }
  return st1.getmin(0, m);
}

int N, M;
int a[maxn], b[maxn];
int L[maxn], R[maxn];

signed main(void) {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
  freopen("A.INP", "r", stdin);
  freopen("A.OUT", "w", stdout);
#endif // LOCAL
  cin >> N >> M;
  int l = M , r = M;
  int v1 = 1, v2 = 1;
  for (int i = 1; i <= N + M - 1; ++i) {
    cin >> a[i];
    L[i] = l; R[i] = r;
    //cerr << l << ' ' << r << '\n';
    l -= v1; r += v2;
    if (l == 0) {
      l += 2;
      v1 = -v1;
    }
    if (r > N + M - 1) {
      r -= 2;
      v2 = -v2;
    }
  }
  for (int i = 1; i <= N + M - 1; ++i)
    cin >> b[i];

  ll res = 0;

  {
    vector<int> A, B;
    vector<pair<int, int>> query;
    bool ok = false;
    A.push_back(0); B.push_back(0);
    for (int i = 1; i <= N + M - 1; ++i) {
      if (i % 2 == 1) {
        A.push_back(a[i]);
        if (L[i] & 1) ok = true;
        query.push_back(make_pair((L[i] + 1) / 2, (R[i] + 1) / 2));
      }
    }
    for (int i = 1; i <= N + M - 1; ++i) {
      if (ok) {
        if (i % 2 == 1) {
          B.push_back(b[i]);
        }
      } else {
        if (i % 2 == 0) {
          B.push_back(b[i]);
        }
      }
    }
    res += solve(A, B, query);
  }
  {
    vector<int> A, B;
    vector<pair<int, int>> query;
    bool ok = false;
    A.push_back(0); B.push_back(0);
    for (int i = 1; i <= N + M - 1; ++i) {
      if (i % 2 == 0) {
        A.push_back(a[i]);
        if (L[i] & 1) ok = true;
        query.push_back(make_pair((L[i] + 1) / 2, (R[i] + 1) / 2));
      }
    }
    for (int i = 1; i <= N + M - 1; ++i) {
      if (ok) {
        if (i % 2 == 1) {
          B.push_back(b[i]);
        }
      } else {
        if (i % 2 == 0) {
          B.push_back(b[i]);
        }
      }
    }
    res += solve(A, B, query);
  }

  cout << res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 376 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 376 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 9 ms 876 KB Output is correct
31 Correct 10 ms 916 KB Output is correct
32 Correct 10 ms 876 KB Output is correct
33 Correct 7 ms 748 KB Output is correct
34 Correct 7 ms 748 KB Output is correct
35 Correct 5 ms 620 KB Output is correct
36 Correct 4 ms 620 KB Output is correct
37 Correct 5 ms 620 KB Output is correct
38 Correct 5 ms 620 KB Output is correct
39 Correct 5 ms 620 KB Output is correct
40 Correct 5 ms 620 KB Output is correct
41 Correct 8 ms 660 KB Output is correct
42 Correct 5 ms 672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 717 ms 21456 KB Output is correct
2 Correct 649 ms 23236 KB Output is correct
3 Correct 692 ms 23392 KB Output is correct
4 Correct 664 ms 23484 KB Output is correct
5 Correct 675 ms 23388 KB Output is correct
6 Correct 651 ms 23512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1668 ms 42388 KB Output is correct
2 Correct 1619 ms 44524 KB Output is correct
3 Correct 1666 ms 44548 KB Output is correct
4 Correct 1640 ms 44476 KB Output is correct
5 Correct 1617 ms 44488 KB Output is correct
6 Correct 1581 ms 44524 KB Output is correct
7 Correct 1609 ms 44368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 364 KB Output is correct
27 Correct 1 ms 376 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 9 ms 876 KB Output is correct
31 Correct 10 ms 916 KB Output is correct
32 Correct 10 ms 876 KB Output is correct
33 Correct 7 ms 748 KB Output is correct
34 Correct 7 ms 748 KB Output is correct
35 Correct 5 ms 620 KB Output is correct
36 Correct 4 ms 620 KB Output is correct
37 Correct 5 ms 620 KB Output is correct
38 Correct 5 ms 620 KB Output is correct
39 Correct 5 ms 620 KB Output is correct
40 Correct 5 ms 620 KB Output is correct
41 Correct 8 ms 660 KB Output is correct
42 Correct 5 ms 672 KB Output is correct
43 Correct 717 ms 21456 KB Output is correct
44 Correct 649 ms 23236 KB Output is correct
45 Correct 692 ms 23392 KB Output is correct
46 Correct 664 ms 23484 KB Output is correct
47 Correct 675 ms 23388 KB Output is correct
48 Correct 651 ms 23512 KB Output is correct
49 Correct 1668 ms 42388 KB Output is correct
50 Correct 1619 ms 44524 KB Output is correct
51 Correct 1666 ms 44548 KB Output is correct
52 Correct 1640 ms 44476 KB Output is correct
53 Correct 1617 ms 44488 KB Output is correct
54 Correct 1581 ms 44524 KB Output is correct
55 Correct 1609 ms 44368 KB Output is correct
56 Correct 675 ms 23388 KB Output is correct
57 Correct 667 ms 23484 KB Output is correct
58 Correct 674 ms 23388 KB Output is correct
59 Correct 701 ms 23236 KB Output is correct
60 Correct 668 ms 23384 KB Output is correct
61 Correct 679 ms 23484 KB Output is correct
62 Correct 664 ms 23236 KB Output is correct
63 Correct 718 ms 23444 KB Output is correct
64 Correct 693 ms 23532 KB Output is correct
65 Correct 754 ms 24272 KB Output is correct
66 Correct 849 ms 26592 KB Output is correct
67 Correct 944 ms 28748 KB Output is correct
68 Correct 1549 ms 44292 KB Output is correct
69 Correct 1593 ms 44416 KB Output is correct
70 Correct 1166 ms 34656 KB Output is correct