Submission #877009

#TimeUsernameProblemLanguageResultExecution timeMemory
877009406Reconstruction Project (JOI22_reconstruction)C++17
Compilation error
0 ms0 KiB
#pragma GCC optimize ("O3,unroll-loops")
#pragma GCC target("avx2")
//besmellah
//accept sho :D
//de akhe mashti NM * DSU nabayad AC she?
//oon moghe NM log bood hichi
//alan vali NM DSU ee
//man ozrkhahi mikonam az tarah
//che bug sangini zadim
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
//#define int long long
using namespace std;
using ar = array<int, 2>;
using ar4 = array<int, 4>;

const int M = 1e5 + 5, N = 505, Q = 1e6 + 5;
const long long INF = 1ll << 30;

inline namespace dsu {
        int dpr[N];
        void init() {
                memset(dpr, -1, sizeof dpr);
        }
        int gpr(int x) {
                return dpr[x] < 0 ? x : dpr[x] = gpr(dpr[x]);
        }
        bool merge(int u, int v) {
                u = gpr(u), v = gpr(v);
                if (u == v) return false;
                if (dpr[u] < dpr[v]) swap(u, v);
                dpr[v] += dpr[u];
                dpr[u] = v;
                return true;
        }
}
inline bool cmp(const ar5 &X, const ar5 &Y) {
        return X[0] < Y[0];
}
vector<ar> adj[N];
int n, m, WD[N];
short pr[N], T, st[N], fn[N];
ar4 E[M];
vector<ar4> undo, bk, Es, to_calc;

void dfs(int v, int p) {
        seen[v] = true;
        st[v] = T++;
        for (auto [u, w]: adj[v]) if (u != p) {
                WD[u] = w;
                pr[u] = v;
                dfs(u, v);
        }
        fn[v] = T++;
}
        
ar4 validate(vector<ar4> &E) {
        vector<ar4> ok;
        ok.reserve(E.size());
        dsu::init();
        ar4 del = {-1, -1, -1, -1};
        for (auto A: E) {
                auto [w, u, v, t] = A;
                if (dsu::merge(u, v))
                        ok.push_back(A);
                else del = A;
        }
        E.swap(ok);
        return del;
}

signed main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr); 
        cin >> n >> m;
        FOR(i, 0, m) {
                cin >> E[i][1] >> E[i][2] >> E[i][0];
                --E[i][1], --E[i][2];
                if (E[i][1] > E[i][2]) swap(E[i][1], E[i][2]);
        }
        sort(E, E + m);
        for (int i = m - 1; i >= 0; --i) {
                auto &[w, u, v, t] = E[i];
                t = 1;
                bk.insert(lower_bound(bk.begin(), bk.end(), E[i], cmp), E[i]);
                undo.push_back(validate(bk));
        }
        int q; cin >> q;
        int p = 0, minl = -INF;
        long long td, W, cnt = 0;
        //ar3 dar vaghe ar4-e 
        vector<ar4> fr;
        FOR(i, 0, q) {
                int x; cin >> x;
                bool t = false;
                while (p < m && x >= E[p][0]) {
                        ar4 X = {-E[p][0], E[p][1], E[p][2], , 0};
                        fr.insert(lower_bound(fr.begin(), fr.end(), X, cmp), X);
                        auto U = undo.back();
                        undo.pop_back();
                        bk.erase(find_if(bk.begin(), bk.end(), [&](const auto &X) {
                                        return X[0] == E[p][0] && X[1] == E[p][1] && X[2] == E[p][2];
                        }));
                        if (U[0] != -1)
                                bk.insert(lower_bound(bk.begin(), bk.end(), U, cmp), U);
                        ++p;
                        t = true;
                }
                if (t || x >= minl) { //build_mst() (M bar hadeaksar)
                        assert(++cnt <= 5 * m);
                        validate(fr);
                        dsu::init();
                        W = td = 0;
                        minl = INF;
                        //al = bk + fr (bt)
                        vector<ar4> al; //badihish
                        al.resize(fr.size() + bk.size());
                        for (auto &A: fr) A[0] += x;
                        for (auto &A: bk) A[0] -= x;
                        merge(fr.begin(), fr.end(), bk.begin(), bk.end(), al.begin(), cmp);
                        for (auto &A: fr) A[0] -= x;
                        for (auto &A: bk) A[0] += x;
                        //sort(al.begin(), al.end());
                        FOR(i, 0, n) adj[i].clear();
                        for (auto A: al) {
                                auto &[w, u, v, t] = A;
                                w = (t ? w + x : x - w);
                                if (dsu::merge(u, v)) {
                                        W += (t ? w : -w);
                                        td += (t ? -1 : +1);
                                        adj[u].push_back({v, w});
                                        adj[v].push_back({u, w});
                                }
                                else if (t) {
                                       to_calc.push_back(A);
                                }
                                else {
                                        A[0] *= -1;
                                        fr.erase(find(fr.begin(), fr.end(), A));
                                }
                        }
                        seen = 0;
                        T = 0;
                        FOR(i, 0, n) if (!seen[i])
                                dfs(i, i);
                        for (auto A: to_calc) {
                                auto [w, u, v, t] = A;
                                int W = INF;

                                minl = min(W + w >> 1, minl);

                        }
                        to_calc.clear();
                }
                cout << 1ll * td * x + W << '\n';
        }
        return 0;
}

Compilation message (stderr)

reconstruction.cpp:37:23: error: 'ar5' does not name a type; did you mean 'ar4'?
   37 | inline bool cmp(const ar5 &X, const ar5 &Y) {
      |                       ^~~
      |                       ar4
reconstruction.cpp:37:37: error: 'ar5' does not name a type; did you mean 'ar4'?
   37 | inline bool cmp(const ar5 &X, const ar5 &Y) {
      |                                     ^~~
      |                                     ar4
reconstruction.cpp: In function 'bool cmp(const int&, const int&)':
reconstruction.cpp:38:17: error: invalid types 'const int[int]' for array subscript
   38 |         return X[0] < Y[0];
      |                 ^
reconstruction.cpp:38:24: error: invalid types 'const int[int]' for array subscript
   38 |         return X[0] < Y[0];
      |                        ^
reconstruction.cpp: In function 'void dfs(int, int)':
reconstruction.cpp:47:9: error: 'seen' was not declared in this scope
   47 |         seen[v] = true;
      |         ^~~~
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:97:62: error: expected primary-expression before ',' token
   97 |                         ar4 X = {-E[p][0], E[p][1], E[p][2], , 0};
      |                                                              ^
reconstruction.cpp:142:25: error: 'seen' was not declared in this scope
  142 |                         seen = 0;
      |                         ^~~~
reconstruction.cpp:150:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  150 |                                 minl = min(W + w >> 1, minl);
      |                                            ~~^~~
In file included from /usr/include/c++/10/bits/stl_algobase.h:71,
                 from /usr/include/c++/10/bits/specfun.h:45,
                 from /usr/include/c++/10/cmath:1927,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
                 from reconstruction.cpp:10:
/usr/include/c++/10/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _Compare = bool (*)(const int&, const int&)]':
/usr/include/c++/10/bits/stl_algo.h:4905:14:   required from '_OutputIterator std::__merge(_InputIterator1, _InputIterator1, _InputIterator2, _InputIterator2, _OutputIterator, _Compare) [with _InputIterator1 = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _InputIterator2 = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _OutputIterator = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(const int&, const int&)>]'
/usr/include/c++/10/bits/stl_algo.h:5014:37:   required from '_OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter, _Compare) [with _IIter1 = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _IIter2 = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _OIter = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _Compare = bool (*)(const int&, const int&)]'
reconstruction.cpp:120:90:   required from here
/usr/include/c++/10/bits/predefined_ops.h:156:30: error: invalid initialization of reference of type 'const int&' from expression of type 'std::array<int, 4>'
  156 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/10/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _Value = const std::array<int, 4>; _Compare = bool (*)(const int&, const int&)]':
/usr/include/c++/10/bits/stl_algobase.h:1324:14:   required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _Tp = std::array<int, 4>; _Compare = __gnu_cxx::__ops::_Iter_comp_val<bool (*)(const int&, const int&)>]'
/usr/include/c++/10/bits/stl_algo.h:2041:32:   required from '_FIter std::lower_bound(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = __gnu_cxx::__normal_iterator<std::array<int, 4>*, std::vector<std::array<int, 4> > >; _Tp = std::array<int, 4>; _Compare = bool (*)(const int&, const int&)]'
reconstruction.cpp:85:70:   required from here
/usr/include/c++/10/bits/predefined_ops.h:194:23: error: invalid initialization of reference of type 'const int&' from expression of type 'std::array<int, 4>'
  194 |  { return bool(_M_comp(*__it, __val)); }
      |                ~~~~~~~^~~~~~~~~~~~~~