답안 #455221

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
455221 2021-08-05T17:29:09 Z erray Bubble Sort 2 (JOI18_bubblesort2) C++17
100 / 100
4558 ms 183468 KB
#include "bubblesort2.h"
#include<bits/stdc++.h>

using namespace std; 
template<typename A, typename B> string to_string(const pair<A, B>& p);
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t);
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char& c) {
  return string("'") + c + "'";
}

string to_string(const char *c) {
  return to_string(string(c));
}

string to_string(const bool& b) {
  return (b ? "true" : "false");
}

string to_string(const vector<bool>& v) {
  string res = "{";
  for (int i = 0; i < (int) v.size(); ++i) {
    if (i > 0) {
      res += ", ";
    }
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template<size_t T> string to_string(const bitset<T>& bs) {
  return bs.to_string();
}

template<typename T> string to_string(queue<T> q) {
  string res = "{";
  size_t sz = q.size();
  while (sz--) {
    T cur = q.front();
    q.pop();
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(cur);
  }
  res += "}";
  return res;
}

template<typename T, class C> string to_string(priority_queue<T, vector<T>, C> pq) {
  string res = "{";
  while (!pq.empty()) {
    T cur = pq.top();
    pq.pop();
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(cur);
  }
  res += "}";
  return res;
}

template<typename T> string to_string(const T& v) {
  string res = "{";
  for (auto &el : v) {
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(el);
  }
  res += "}";
  return res;
}

template<typename A, typename B> string to_string(const pair<A, B>& p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ')';
}

void debug_out(int size, bool first, string name) {
  vector<string> tmp = {name};
  vector<int> tmp2 = {size, first};
  cerr << endl;
}

constexpr int buffer_size = 255;

template<typename Head, typename... Tail> void debug_out(int size, bool first, string name, Head H, Tail... T) {
  string tmp;
  int off = 0;
  while ((!name.empty() && name[0] != ',') || off != 0) {
    tmp += name[0];
    name.erase(name.begin());
    char c = tmp.back();
    if (c == '{' || c == '(') {
      ++off;
    } else if (c == '}' || c == ')'){
      --off;
    }
  }
  if (!name.empty()) {
    name.erase(name.begin());
  }
  if (tmp[0] == ' ') {
    tmp.erase(tmp.begin());
  }

  string buff = to_string(H);
  if ((int) buff.size() + size + (int) tmp.size() > buffer_size - 5 && !first) {
    cerr << '\n';
    size = 0;
  }
  cerr << '[' << tmp << ": " << buff << "] ";
  debug_out(((int) buff.size() + size + (int) tmp.size() + 5) % buffer_size, false, name, T...);
}

#ifdef DEBUG
#define debug(...) cerr << "-> ", debug_out(3, true, string(#__VA_ARGS__), __VA_ARGS__)
#define here cerr << "-> " << __LINE__ << endl
#else
#define debug(...) void(37)
#define here void(37)
#endif

template<typename node> struct SegTree {
  node emp;
  inline void push(int v, int l, int r) {
    if (l == r) return;
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    if (tree[v].tag != emp.tag) {
      tree[rv].modify(mid + 1, r, tree[v].tag, 0);
      tree[v + 1].modify(l, mid, tree[v].tag, 0);
    }
    tree[v].tag = emp.tag;
  }

  inline void pull(int v, int rv) {
    tree[v] = tree[v + 1] +  tree[rv];
  }

  int n;
  vector<node> tree;

  template<typename T> void build(int v, int l, int r, const vector<T> vv) {
    if (l == r) {
      tree[v].modify(l, r, vv[l]);
      return;
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    build(v + 1, l, mid, vv);
    build(rv, mid + 1, r, vv);
    pull(v, rv);
  }

  node get(int v, int l, int r, int ll, int rr) {
    if (l >= ll && rr >= r) {
      return tree[v];
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    push(v, l, r);

    if (mid >= ll && mid < rr) {
      return get(v + 1, l, mid, ll, rr) + get(rv, mid + 1, r, ll, rr);
    }
    if (mid >= ll) {
      return get(v + 1, l, mid, ll, rr);
    }
    return get(rv, mid + 1, r, ll, rr);
  }

  template<typename R, typename... T> R get(int v, int l, int r, int ll, int rr, function<R(const R&, const R&)> f, const T&... t) {
    if (l >= ll && rr >= r) {
      return tree[v].get(l, r, t...);
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    push(v, l, r);

    if (mid >= ll && mid < rr) {
      return f(get(v + 1, l, mid, ll, rr, t...), get(rv, mid + 1, r, ll, rr, t...));
    }
    if (mid >= ll) {
      return get(v + 1, l, mid, ll, rr, t...);
    }
    return get(rv, mid + 1, r, ll, rr, t...);

  }

  template<typename... T> void modify(int v, int l, int r, int ll, int rr, const T&... t) {
    if (l >= ll && rr >= r) {
      tree[v].modify(l, r, t...);
      return;
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    push(v, l, r);

    if (mid >= ll) {
      modify(v + 1, l, mid, ll, rr, t...);
    }
    if (mid < rr) {
      modify(rv, mid + 1, r, ll, rr, t...);
    }
    pull(v, rv);
  }

  template<typename... T> void set_modify(int v, int l, int r, int tar, const T&... t) {
    if (l == r) {
      tree[v] = emp;
      tree[v].modify(l, r, t...);
      return;
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    push(v, l, r);

    if (mid >= tar) {
      set_modify(v + 1, l, mid, tar, t...);
    }
    if (mid < tar) {
      set_modify(rv, mid + 1, r, tar, t...);
    }
    pull(v, rv);
  }

  int find_first_exact(int v, int l, int r, const function<bool(const node&)> f) {
    if (!f(tree[v])) return -1;
    if (l == r) {
      return l;
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    push(v, l, r);

    int res = -1;
    if (f(tree[v + 1])) {
      res = find_first_exact(v + 1, l, mid, f);
    } else {
      res = find_first_exact(rv, mid + 1, r, f);
    }
    assert(res != -1);
    return res;
  }

  int find_last_exact(int v, int l, int r, const function<bool(const node&)> f) {
    if (!f(tree[v])) return -1;
    if (l == r) {
      return l;
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    push(v, l, r);

    int res = -1;
    if (f(tree[rv])) {
      res = find_last_exact(rv, mid + 1, r, f);
    } else {
      res = find_last_exact(v + 1, l, mid, f);
    }
    assert(res != -1);
    return res;
  }

  int find_first(int v, int l, int r, int ll, int rr, const function<bool(const node&)> f) {
    if (l >= ll && rr >= r) {
      return find_first_exact(v, l, r, f);
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    push(v, l, r);

    int res = -1;
    if (mid >= ll) {
      res = find_first(v + 1, l, mid, ll, rr, f);
    }
    if (res == -1 && mid < rr) {
      res = find_first(rv, mid + 1, r, ll, rr, f);
    }
    return res;
  }

  int find_last(int v, int l, int r, int ll, int rr, const function<bool(const node&)> f) {
    if (l > rr || r < ll) {
      return -1;
    }
    if (l >= ll && rr >= r) {
      return find_last_exact(v, l, r, f);
    }
    int mid = (l + r) >> 1;
    int rv = v + ((mid - l + 1) << 1);
    push(v, l, r);

    int res = -1;
    if (mid < rr) {
      res = find_last(rv, mid + 1, r, ll, rr, f);
    }
    if (res == -1 && mid >= ll) {
      res = find_last(v + 1, l, mid, ll, rr, f);
    }
    return res;
  }

  /*==========================================================================*/


  SegTree(int _n) : n(_n) {
    assert(n > 0);
    tree.resize((n << 1) - 1);
  }

  void clear() {
    tree.assign((n << 1) - 1, node{});  
  }
  
  template<typename T> SegTree(vector<T> v) : n((int) v.size()) {
    tree.resize(n * 2 - 1);
    build(0, 0, n - 1, v);
  }

  node get(int ind) {
    assert(ind >= 0 && ind < n);
    return get(0, 0, n - 1, ind, ind);
  }

  node get(int l, int r) {
    assert(l >= 0 && l < n && r >= 0 && r < n && r >= l);
    return get(0, 0, n - 1, l, r);
  }

  template<typename R, typename... T> R get(int l, int r, function<R(const R&, const R&)> f, T... t) {
    assert(l >= 0 && l < n && r >= 0 && r < n && r >= l);
    return get<R>(0, 0, n - 1, l, r, f, t...);
  }

  template<typename... T> void modify(int l, int r, T... t) {
    assert(l >= 0 && l < n && r >= 0 && r < n && r >= l);
    modify(0, 0, n - 1, l, r, t...);
  }

  template<typename... T> void point_modify(int ind, T... t) {
    assert(ind >= 0 && ind < n);
    modify(0, 0, n - 1, ind, ind, t...);
  }

  template<typename... T> void set_modify(int ind, T... t) {
    assert(ind >= 0 && ind < n);
    set_modify(0, 0, n - 1, ind, t...);
  }

  int find_first(int l, int r, const function<bool(const node&)> f) {
    assert(l >= 0 && l < n && r >= 0 && r < n && r >= l);
    return find_first(0, 0, n - 1, l, r, f);
  }

  int find_last(int l, int r, const function<bool(const node&)> f) {
    assert(l >= 0 && l < n && r >= 0 && r < n && r >= l);
    return find_last(0, 0, n - 1, l, r, f);
  }

  int find_first(const function<bool(const node&)> f) {
    return find_first(0, 0, n - 1, 0, n - 1, f);
  }

  int find_last(const function<bool(const node&)> f) {
    return find_last(0, 0, n - 1, 0, n - 1, f);
  }
};

const int inf = int(1e6);
struct node {
  //variables default values should be uneffective
  int sum = -inf;
  set<int> inds;
  int sub = 0;
  int tag = 0;
  void modify(int l, int r, int x, int type) {
    assert(l == l && r == r); // no to warnings
    if (type == 0) {
      tag += x;
      sum -= x;
      sub += x;
    } else {
      if (type == 2) {
        inds.insert(x);
      } else if (type == 1) {
        inds.erase(x);
      } else {
        assert(false);
      }
      sum = (inds.empty() ? -inf : *prev(inds.end())) + 1 - sub;
    }
  }

  node operator+ (const node& x) {
    node res;
    res.sum = max(sum, x.sum);
    return res;
  }

  int get (int l, int r) {
    return l + r;
  }
};

std::vector<int> countScans(std::vector<int> A,std::vector<int> X,std::vector<int> V){
  int n = int(A.size());
  int q = int(X.size());
  vector<int> sa = A;  
  for (auto e : V) {
    sa.push_back(e);
  }
  sort(sa.begin(), sa.end());
  sa.erase(unique(sa.begin(), sa.end()), sa.end());
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    a[i] = int(lower_bound(sa.begin(), sa.end(), A[i]) - sa.begin());
  }
  vector<int> v(q);
  for (int i = 0; i < q; ++i) {
    v[i] = int(lower_bound(sa.begin(), sa.end(), V[i]) - sa.begin());
  }
  debug(a, v);
  int size = int(sa.size());
  SegTree<node> st(size);
  auto Modify = [&](int i, int t) {
    debug(i, a[i], t);
    st.point_modify(a[i], i, 1 + t); 
    st.modify(a[i], size - 1, (t ? 1 : -1), 0); 
  };
  for (int i = 0; i < n; ++i) {
    Modify(i, 1);
  }
  vector<int> res(q);
  for (int i = 0; i < q; ++i) {
    Modify(X[i], 0);
    a[X[i]] = v[i];
    Modify(X[i], 1);
    debug(a);
    #ifdef DEBUG
      for (int j = 0; j < size; ++j) {
        auto nd = st.get(j);
        debug(j, nd.inds, nd.sum, nd.sub);
      }
    #endif
    res[i] = st.get(0, size - 1).sum;
  }
  return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 10 ms 972 KB Output is correct
4 Correct 11 ms 920 KB Output is correct
5 Correct 8 ms 972 KB Output is correct
6 Correct 9 ms 972 KB Output is correct
7 Correct 11 ms 972 KB Output is correct
8 Correct 8 ms 972 KB Output is correct
9 Correct 8 ms 972 KB Output is correct
10 Correct 8 ms 844 KB Output is correct
11 Correct 8 ms 844 KB Output is correct
12 Correct 7 ms 844 KB Output is correct
13 Correct 8 ms 844 KB Output is correct
14 Correct 7 ms 844 KB Output is correct
15 Correct 7 ms 844 KB Output is correct
16 Correct 8 ms 824 KB Output is correct
17 Correct 7 ms 844 KB Output is correct
18 Correct 7 ms 844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 10 ms 972 KB Output is correct
4 Correct 11 ms 920 KB Output is correct
5 Correct 8 ms 972 KB Output is correct
6 Correct 9 ms 972 KB Output is correct
7 Correct 11 ms 972 KB Output is correct
8 Correct 8 ms 972 KB Output is correct
9 Correct 8 ms 972 KB Output is correct
10 Correct 8 ms 844 KB Output is correct
11 Correct 8 ms 844 KB Output is correct
12 Correct 7 ms 844 KB Output is correct
13 Correct 8 ms 844 KB Output is correct
14 Correct 7 ms 844 KB Output is correct
15 Correct 7 ms 844 KB Output is correct
16 Correct 8 ms 824 KB Output is correct
17 Correct 7 ms 844 KB Output is correct
18 Correct 7 ms 844 KB Output is correct
19 Correct 34 ms 2900 KB Output is correct
20 Correct 39 ms 3144 KB Output is correct
21 Correct 36 ms 3152 KB Output is correct
22 Correct 35 ms 3188 KB Output is correct
23 Correct 33 ms 2940 KB Output is correct
24 Correct 34 ms 2908 KB Output is correct
25 Correct 33 ms 2768 KB Output is correct
26 Correct 32 ms 2764 KB Output is correct
27 Correct 32 ms 2564 KB Output is correct
28 Correct 32 ms 2624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 2116 KB Output is correct
2 Correct 88 ms 3736 KB Output is correct
3 Correct 135 ms 5284 KB Output is correct
4 Correct 133 ms 5264 KB Output is correct
5 Correct 142 ms 5280 KB Output is correct
6 Correct 131 ms 5364 KB Output is correct
7 Correct 128 ms 5376 KB Output is correct
8 Correct 135 ms 5296 KB Output is correct
9 Correct 133 ms 5332 KB Output is correct
10 Correct 119 ms 5376 KB Output is correct
11 Correct 105 ms 5340 KB Output is correct
12 Correct 107 ms 5420 KB Output is correct
13 Correct 106 ms 5348 KB Output is correct
14 Correct 134 ms 5400 KB Output is correct
15 Correct 106 ms 5348 KB Output is correct
16 Correct 101 ms 5404 KB Output is correct
17 Correct 103 ms 5408 KB Output is correct
18 Correct 108 ms 5548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 5 ms 588 KB Output is correct
3 Correct 10 ms 972 KB Output is correct
4 Correct 11 ms 920 KB Output is correct
5 Correct 8 ms 972 KB Output is correct
6 Correct 9 ms 972 KB Output is correct
7 Correct 11 ms 972 KB Output is correct
8 Correct 8 ms 972 KB Output is correct
9 Correct 8 ms 972 KB Output is correct
10 Correct 8 ms 844 KB Output is correct
11 Correct 8 ms 844 KB Output is correct
12 Correct 7 ms 844 KB Output is correct
13 Correct 8 ms 844 KB Output is correct
14 Correct 7 ms 844 KB Output is correct
15 Correct 7 ms 844 KB Output is correct
16 Correct 8 ms 824 KB Output is correct
17 Correct 7 ms 844 KB Output is correct
18 Correct 7 ms 844 KB Output is correct
19 Correct 34 ms 2900 KB Output is correct
20 Correct 39 ms 3144 KB Output is correct
21 Correct 36 ms 3152 KB Output is correct
22 Correct 35 ms 3188 KB Output is correct
23 Correct 33 ms 2940 KB Output is correct
24 Correct 34 ms 2908 KB Output is correct
25 Correct 33 ms 2768 KB Output is correct
26 Correct 32 ms 2764 KB Output is correct
27 Correct 32 ms 2564 KB Output is correct
28 Correct 32 ms 2624 KB Output is correct
29 Correct 28 ms 2116 KB Output is correct
30 Correct 88 ms 3736 KB Output is correct
31 Correct 135 ms 5284 KB Output is correct
32 Correct 133 ms 5264 KB Output is correct
33 Correct 142 ms 5280 KB Output is correct
34 Correct 131 ms 5364 KB Output is correct
35 Correct 128 ms 5376 KB Output is correct
36 Correct 135 ms 5296 KB Output is correct
37 Correct 133 ms 5332 KB Output is correct
38 Correct 119 ms 5376 KB Output is correct
39 Correct 105 ms 5340 KB Output is correct
40 Correct 107 ms 5420 KB Output is correct
41 Correct 106 ms 5348 KB Output is correct
42 Correct 134 ms 5400 KB Output is correct
43 Correct 106 ms 5348 KB Output is correct
44 Correct 101 ms 5404 KB Output is correct
45 Correct 103 ms 5408 KB Output is correct
46 Correct 108 ms 5548 KB Output is correct
47 Correct 1072 ms 54944 KB Output is correct
48 Correct 3977 ms 166456 KB Output is correct
49 Correct 4379 ms 183324 KB Output is correct
50 Correct 4558 ms 183380 KB Output is correct
51 Correct 4418 ms 183328 KB Output is correct
52 Correct 4478 ms 183256 KB Output is correct
53 Correct 4457 ms 183336 KB Output is correct
54 Correct 4001 ms 183404 KB Output is correct
55 Correct 4292 ms 183396 KB Output is correct
56 Correct 4065 ms 183416 KB Output is correct
57 Correct 4162 ms 183468 KB Output is correct
58 Correct 3948 ms 183404 KB Output is correct
59 Correct 3601 ms 166580 KB Output is correct
60 Correct 3734 ms 166580 KB Output is correct
61 Correct 3715 ms 166576 KB Output is correct
62 Correct 3665 ms 158572 KB Output is correct
63 Correct 3679 ms 158592 KB Output is correct
64 Correct 3678 ms 158604 KB Output is correct
65 Correct 3446 ms 150632 KB Output is correct
66 Correct 3454 ms 150632 KB Output is correct
67 Correct 3380 ms 150644 KB Output is correct