Submission #870996

#TimeUsernameProblemLanguageResultExecution timeMemory
870996marvinthangBridges (APIO19_bridges)C++17
Compilation error
0 ms0 KiB
/************************************* * author: marvinthang * * created: 09.11.2023 22:50:14 * *************************************/ #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-- > 0; ) #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 U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } 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> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } 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 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 << '}'; } // end of template const int BLOCK_SIZE = 320; struct Edge { int u, v, w; bool operator < (const Edge &other) const { return w > other.w; } }; struct Query { int t, u, w; }; void process(void) { int n, m, q; cin >> n >> m; vector <Edge> edges; REP(i, m) { int u, v, w; cin >> u >> v >> w; --u; --v; edges.emplace_back(u, v, w); } vector <int> ord(m); iota(ALL(ord), 0); sort(ALL(ord), [&] (int a, int b) { return edges[a].w > edges[b].w; }); vector <Query> queries; cin >> q; REP(i, q) { int t, u, w; cin >> t >> u >> w; --u; queries.emplace_back(t, u, w); } vector <int> par(n, -1); stack <pair <int, int>> his; function <int(int, bool)> find = [&] (int u, bool save) { if (par[u] < 0) return u; if (save) his.emplace(u, par[u]); return par[u] = find(par[u], save); }; auto join = [&] (int u, int v, bool save) { if ((u = find(u, save)) == (v = find(v, save))) return false; if (par[u] > par[v]) swap(u, v); if (save) his.emplace(u, par[u]); if (save) his.emplace(v, par[v]); par[u] += par[v]; par[v] = u; return true; }; auto rollback = [&] { while (!his.empty()) { par[his.top().fi] = his.top().se; his.pop(); } }; vector <bool> used(m); vector <int> res(q, -1); for (int l = 0; l < q; ) { int r = min(q, l + BLOCK_SIZE); vector <int> qr; FOR(i, l, r) { if (queries[i].t == 1) used[queries[i].u] = true; else qr.push_back(i); } fill(ALL(par), -1); sort(ALL(qr), [&] (int a, int b) { return queries[a].w > queries[b].w; }); int p = 0; for (int i: qr) { auto [t, u, w] = queries[i]; while (p < m && edges[ord[p]].w >= w) { if (!used[ord[p]]) join(edges[ord[p]].u, edges[ord[p]].v, false); ++p; } FORD(j, i, l) if (queries[j].t == 1 && queries[j].w >= w) { int x = queries[j].u; if (!used[x]) continue; used[x] = false; join(edges[x].u, edges[x].v, true); } FOR(j, i + 1, r) if (queries[j].t == 1) { int x = queries[j].u; if (!used[x] || edges[x].w < w) continue; used[x] = false; join(edges[x].u, edges[x].v, true); } FOR(j, l, r) if (queries[j].t == 1) used[queries[j].u] = true; res[i] = -par[find(u, true)]; rollback(); } vector <int> nxt; FORD(i, r, l) if (queries[i].t == 1) { int x = queries[i].u; if (!used[x]) continue; used[x] = false; edges[x].w = queries[i].w; nxt.push_back(x); } for (int i: nxt) used[i] = true; sort(ALL(nxt), [&] (int a, int b) { return edges[a].w > edges[b].w; }); int i = 0, j = 0; vector <int> new_ord; while (i < m || j < nxt.size()) { if (i < m && used[ord[i]]) ++i; else if (i == m || (j < nxt.size() && edges[ord[i]].w <= edges[nxt[j]].w)) new_ord.push_back(nxt[j++]); else new_ord.push_back(ord[i++]); } for (int i: nxt) used[i] = false; ord = move(new_ord); l = r; } REP(i, q) if (res[i] != -1) cout << res[i] << '\n'; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr); file("bridges"); // int t; cin >> t; while (t--) process(); // cerr << "Time elapsed: " << TIME << " s.\n"; return (0^0); }

Compilation message (stderr)

bridges.cpp: In function 'void process()':
bridges.cpp:140:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |   while (i < m || j < nxt.size()) {
      |                   ~~^~~~~~~~~~~~
bridges.cpp:142:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |    else if (i == m || (j < nxt.size() && edges[ord[i]].w <= edges[nxt[j]].w)) new_ord.push_back(nxt[j++]);
      |                        ~~^~~~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/c++allocator.h:33,
                 from /usr/include/c++/10/bits/allocator.h:46,
                 from /usr/include/c++/10/string:41,
                 from /usr/include/c++/10/bits/locale_classes.h:40,
                 from /usr/include/c++/10/bits/ios_base.h:41,
                 from /usr/include/c++/10/ios:42,
                 from /usr/include/c++/10/istream:38,
                 from /usr/include/c++/10/sstream:38,
                 from /usr/include/c++/10/complex:45,
                 from /usr/include/c++/10/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
                 from bridges.cpp:6:
/usr/include/c++/10/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Edge; _Args = {int&, int&, int&}; _Tp = Edge]':
/usr/include/c++/10/bits/alloc_traits.h:512:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = Edge; _Args = {int&, int&, int&}; _Tp = Edge; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<Edge>]'
/usr/include/c++/10/bits/vector.tcc:115:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int&, int&, int&}; _Tp = Edge; _Alloc = std::allocator<Edge>; std::vector<_Tp, _Alloc>::reference = Edge&]'
bridges.cpp:61:29:   required from here
/usr/include/c++/10/ext/new_allocator.h:150:4: error: new initializer expression list treated as compound expression [-fpermissive]
  150 |  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/ext/new_allocator.h:150:4: error: no matching function for call to 'Edge::Edge(int&)'
bridges.cpp:47:8: note: candidate: 'Edge::Edge()'
   47 | struct Edge {
      |        ^~~~
bridges.cpp:47:8: note:   candidate expects 0 arguments, 1 provided
bridges.cpp:47:8: note: candidate: 'constexpr Edge::Edge(const Edge&)'
bridges.cpp:47:8: note:   no known conversion for argument 1 from 'int' to 'const Edge&'
bridges.cpp:47:8: note: candidate: 'constexpr Edge::Edge(Edge&&)'
bridges.cpp:47:8: note:   no known conversion for argument 1 from 'int' to 'Edge&&'
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/c++allocator.h:33,
                 from /usr/include/c++/10/bits/allocator.h:46,
                 from /usr/include/c++/10/string:41,
                 from /usr/include/c++/10/bits/locale_classes.h:40,
                 from /usr/include/c++/10/bits/ios_base.h:41,
                 from /usr/include/c++/10/ios:42,
                 from /usr/include/c++/10/istream:38,
                 from /usr/include/c++/10/sstream:38,
                 from /usr/include/c++/10/complex:45,
                 from /usr/include/c++/10/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:54,
                 from bridges.cpp:6:
/usr/include/c++/10/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Query; _Args = {int&, int&, int&}; _Tp = Query]':
/usr/include/c++/10/bits/alloc_traits.h:512:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = Query; _Args = {int&, int&, int&}; _Tp = Query; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<Query>]'
/usr/include/c++/10/bits/vector.tcc:115:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int&, int&, int&}; _Tp = Query; _Alloc = std::allocator<Query>; std::vector<_Tp, _Alloc>::reference = Query&]'
bridges.cpp:70:31:   required from here
/usr/include/c++/10/ext/new_allocator.h:150:4: error: new initializer expression list treated as compound expression [-fpermissive]
  150 |  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/ext/new_allocator.h:150:4: error: no matching function for call to 'Query::Query(int&)'
bridges.cpp:52:8: note: candidate: 'Query::Query()'
   52 | struct Query {
      |        ^~~~~
bridges.cpp:52:8: note:   candidate expects 0 arguments, 1 provided
bridges.cpp:52:8: note: candidate: 'constexpr Query::Query(const Query&)'
bridges.cpp:52:8: note:   no known conversion for argument 1 from 'int' to 'const Query&'
bridges.cpp:52:8: note: candidate: 'constexpr Query::Query(Query&&)'
bridges.cpp:52:8: note:   no known conversion for argument 1 from 'int' to 'Query&&'
bridges.cpp: In function 'int main()':
bridges.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:154:2: note: in expansion of macro 'file'
  154 |  file("bridges");
      |  ^~~~
bridges.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
bridges.cpp:154:2: note: in expansion of macro 'file'
  154 |  file("bridges");
      |  ^~~~