Submission #819895

#TimeUsernameProblemLanguageResultExecution timeMemory
819895vinisal353Teams (IOI15_teams)C++14
100 / 100
3607 ms406808 KiB
/************************************* * author: marvinthang * * created: 06.03.2023 21:08:07 * *************************************/ #include "teams.h" #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i--; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; } template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; } static char buf[450 << 20]; void* operator new(size_t s) { static size_t i = sizeof buf; assert(s < i); return (void*)&buf[i -= s]; } void operator delete(void*) {} struct Node { Node *lc, *rc; int sum; Node(int v = 0): lc(nullptr), rc(nullptr), sum(v) {} Node(Node *l, Node *r): lc(l), rc(r), sum(0) { if (l != nullptr) sum += l->sum; if (r != nullptr) sum += r->sum; } }; Node *build(int l, int r) { if (l == r) return new Node(); int m = l + r >> 1; return new Node(build(l, m), build(m + 1, r)); } Node *update(Node *cur, int l, int r, int p, int v) { if (l == r) return new Node(cur->sum + v); int m = l + r >> 1; if (p <= m) return new Node(update(cur->lc, l, m, p, v), cur->rc); return new Node(cur->lc, update(cur->rc, m + 1, r, p, v)); } int get_sum(Node *cur, int l, int r, int u, int v) { if (l > v || r < u) return 0; if (u <= l && r <= v) return cur->sum; int m = l + r >> 1; return get_sum(cur->lc, l, m, u, v) + get_sum(cur->rc, m + 1, r, u, v); } // end of template const int MAX = 5e5 + 5; int N, pref[MAX]; vector <int> rightAt[MAX]; Node *versions[MAX]; void init(int n, int A[], int B[]) { N = n; REP(i, N) rightAt[B[i]].push_back(A[i]); versions[N + 1] = build(1, N); FORDE(r, N, 1) { versions[r] = versions[r + 1]; for (int l: rightAt[r]) versions[r] = update(versions[r], 1, N, l, 1); } } int cnt[MAX], dp[MAX]; int can(int M, int K[]) { long long sum = 0; REP(i, M) sum += K[i]; if (sum > N) return 0; REP(i, M) ++cnt[K[i]]; vector <pair <int, int>> v; REP(i, M) if (cnt[K[i]]) { v.push_back(make_pair(K[i], K[i] * cnt[K[i]])); cnt[K[i]] = 0; } v.emplace_back(0, 0); sort(ALL(v)); stack <pair <int, int>> st; st.emplace(0, N + 2); auto getTime = [&] (int i, int j) { int l = v[j].fi + 1, r = N; while (l <= r) { int m = l + r >> 1; if (get_sum(versions[m], 1, N, v[i].fi + 1, v[j].fi) < dp[j] - dp[i]) r = m - 1; else l = m + 1; } return l; }; FOR(i, 1, v.size()) { while (st.top().se <= v[i].fi) st.pop(); dp[i] = dp[st.top().fi] + get_sum(versions[v[i].fi], 1, N, v[st.top().fi].fi + 1, v[i].fi) - v[i].se; if (dp[i] < 0) return 0; int t = getTime(st.top().fi, i); while (st.top().se < t) { st.pop(); t = getTime(st.top().fi, i); } st.emplace(i, t); } return 1; }

Compilation message (stderr)

teams.cpp: In function 'std::ostream& print_tuple_utils(std::ostream&, const T&)':
teams.cpp:41:91: warning: 'if constexpr' only available with '-std=c++17' or '-std=gnu++17'
   41 | template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
      |                                                                                           ^~~~~~~~~
teams.cpp: In function 'Node* build(int, int)':
teams.cpp:66:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |  int m = l + r >> 1;
      |          ~~^~~
teams.cpp: In function 'Node* update(Node*, int, int, int, int)':
teams.cpp:72:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   72 |  int m = l + r >> 1;
      |          ~~^~~
teams.cpp: In function 'int get_sum(Node*, int, int, int, int)':
teams.cpp:80:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |  int m = l + r >> 1;
      |          ~~^~~
teams.cpp: In lambda function:
teams.cpp:122:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  122 |    int m = l + r >> 1;
      |            ~~^~~
teams.cpp: In function 'int can(int, int*)':
teams.cpp:128:18: warning: conversion from 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   22 | #define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i)
      |                                                     ~~~
......
  128 |  FOR(i, 1, v.size()) {
teams.cpp:22:54: note: in definition of macro 'FOR'
   22 | #define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i)
      |                                                      ^
teams.cpp: At global scope:
teams.cpp:52:6: warning: the program should also define 'void operator delete(void*, std::size_t)' [-Wsized-deallocation]
   52 | void operator delete(void*) {}
      |      ^~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...