text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const long long INF64 = 1e18; const double INF = 2e9 + 63; const long long mod = 1e9 + 7; const int maxn = 3e3 + 79; const long long sigma = 27; const double pi = acos(-1); struct VertVal { long long lowest0; long long lowest1; }; template <typename T, typename PushT> struct RMQ { vector<T> mas; T neutral; int size; std::function<T(const T&, const T&)> op; vector<PushT> masPush; PushT neutralPush; std::function<pair<PushT, PushT>(const PushT&)> opPush; std::function<void(PushT&, T&)> opApply; std::function<PushT(const PushT&, const PushT&)> opPendingCompose; RMQ(int size, std::function<T(const T&, const T&)> op, T neutral, std::function<pair<PushT, PushT>(const PushT&)> opPush, PushT neutralPush, std::function<void(PushT&, T&)> opApply, std::function<PushT(const PushT&, const PushT&)> opPendingCompose) { this->neutral = neutral; this->size = size; this->op = op; mas.assign(4 * size, neutral); this->opPush = opPush; this->opApply = opApply; this->opPendingCompose = opPendingCompose; this->neutralPush = neutralPush; masPush.assign(4 * size, neutralPush); } void setVal(int pos, T val) { setValImpl(pos, val, 0, size - 1, 1); } void setValImpl(int pos, T val, int l, int r, int v) { if (l == r && r == pos) { mas[v] = val; return; } if (l > pos || r < pos) return; int m = (l + r) / 2; setValImpl(pos, val, l, m, 2 * v); setValImpl(pos, val, m + 1, r, 2 * v + 1); mas[v] = op(mas[2 * v], mas[2 * v + 1]); } void modify(int l, int r, PushT d) { modifyImpl(l, r, d, 0, size - 1, 1); } void push(int v, bool isLeaf) { if (!isLeaf) { auto p = opPush(masPush[v]); masPush[2 * v] = opPendingCompose(masPush[2 * v], p.first); masPush[2 * v + 1] = opPendingCompose(masPush[2 * v + 1], p.second); } opApply(masPush[v], mas[v]); } void modifyImpl(int rq_l, int rq_r, long long d, int l, int r, int v) { push(v, l == r); if (rq_l > r || l > rq_r) return; if (rq_l <= l && r <= rq_r) { masPush[v] = opPendingCompose(masPush[v], d); push(v, l == r); return; } int m = (l + r) / 2; modifyImpl(rq_l, rq_r, d, l, m, 2 * v); modifyImpl(rq_l, rq_r, d, m + 1, r, 2 * v + 1); mas[v] = op(mas[2 * v], mas[2 * v + 1]); } T query(int l, int r) { return queryImpl(l, r, 0, size - 1, 1); } T queryImpl(int rq_l, int rq_r, int l, int r, int v) { push(v, l == r); if (rq_l > r || l > rq_r) return neutral; if (rq_l <= l && r <= rq_r) { return mas[v]; } int m = (l + r) / 2; T ans1 = queryImpl(rq_l, rq_r, l, m, 2 * v); T ans2 = queryImpl(rq_l, rq_r, m + 1, r, 2 * v + 1); return op(ans1, ans2); } }; struct Edge { int to; int getTo() const { return to; } long long getW() const { return 1; } }; struct WEdge { int to; long long w; int getTo() const { return to; } long long getW() const { return w; } }; struct None {}; template <typename EdgeType, typename VertType = None> struct Graph { vector<VertType> v; vector<vector<EdgeType>> g; Graph(int n) : g(n), v(n){}; void addEdge(int from, int to, EdgeType info) { info.to = to; g[from].push_back(info); info.to = from; g[to].push_back(info); } void addOEdge(int from, int to, EdgeType info) { info.to = to; g[from].push_back(info); } }; struct PFInfo { int par; long long dist; friend bool operator<(const PFInfo& a, const PFInfo& b) { return a.dist < b.dist; } }; template <typename EdgeType, typename VertType> vector<PFInfo> bfs(const Graph<EdgeType, VertType>& g, int start) { vector<PFInfo> res(g.g.size(), {-1, INF64}); queue<int> s; res[start].par = -2; res[start].dist = 0; s.push(start); while (!s.empty()) { int v = s.front(); s.pop(); for (auto& it : g.g[v]) { int to = it.getTo(); if (res[to].dist == INF64) { res[to].par = v; res[to].dist = res[v].dist + it.getW(); s.push(to); } } } return res; } vector<int> restorePath(const vector<PFInfo>& pfInfo, int to) { vector<int> res; while (true) { res.push_back(to); if (pfInfo[to].par == -1) { return {}; } if (pfInfo[to].par == -2) { return res; } to = pfInfo[to].par; } } struct MyEdge : Edge { int roadType; MyEdge(int roadType) : roadType(roadType){}; }; int cnt = 0; void dfs(Graph<MyEdge>& g, vector<pair<int, int>>& segs, vector<int>& oddness, vector<int>& depths, int v = 0, int curCnt = 0, int depth = 0, int p = -1) { oddness[cnt] = curCnt; depths[cnt] = depth; int start = cnt++; for (auto& it : g.g[v]) { int to = it.getTo(); if (to == p) continue; dfs(g, segs, oddness, depths, to, curCnt ^ it.roadType, depth + 1, v); } segs[v] = {start, cnt - 1}; } void solve() { int n; cin >> n; Graph<MyEdge> g(n); vector<pair<int, int>> edges; for (int i = 0; i < n - 1; i++) { int u, v, t; cin >> u >> v >> t; u--; v--; g.addEdge(u, v, {t}); edges.push_back({u, v}); } auto dists = bfs(g, 0); int end1 = max_element(dists.begin(), dists.end()) - dists.begin(); auto dists1 = bfs(g, end1); int end2 = max_element(dists1.begin(), dists1.end()) - dists1.begin(); auto dists2 = bfs(g, end2); vector<pair<int, int>> seqFrom1(n), seqFrom2(n); vector<int> oddnessFrom1(n), oddnessFrom2(n); vector<int> depths1(n), depths2(n); cnt = 0; dfs(g, seqFrom1, oddnessFrom1, depths1, end1); assert(cnt == n); RMQ<VertVal, int> rmq1( n, [](const VertVal& a, const VertVal& b) { return VertVal{max(a.lowest0, b.lowest0), max(a.lowest1, b.lowest1)}; }, VertVal{-1, -1}, [](const int& par) { return pair<bool, bool>(par, par); }, false, [](int& a, VertVal& b) { if (a) swap(b.lowest0, b.lowest1); a = false; }, [](const int& a, const int& b) { return a ^ b; }); for (int i = 0; i < n; i++) { if (oddnessFrom1[i]) { rmq1.setVal(i, {-1, depths1[i]}); } else { rmq1.setVal(i, {depths1[i], -1}); } } cnt = 0; dfs(g, seqFrom2, oddnessFrom2, depths2, end2); assert(cnt == n); RMQ<VertVal, int> rmq2( n, [](const VertVal& a, const VertVal& b) { return VertVal{max(a.lowest0, b.lowest0), max(a.lowest1, b.lowest1)}; }, VertVal{-1, -1}, [](const int& par) { return pair<bool, bool>(par, par); }, false, [](int& a, VertVal& b) { if (a) swap(b.lowest0, b.lowest1); a = false; }, [](const int& a, const int& b) { return a ^ b; }); auto tmp = rmq1.query(0, 1); for (int i = 0; i < n; i++) { if (oddnessFrom2[i]) { rmq2.setVal(i, {-1, depths2[i]}); } else { rmq2.setVal(i, {depths2[i], -1}); } } int m; cin >> m; for (int i = 0; i < m; i++) { int id; cin >> id; id--; auto p = edges[id]; int u = p.first; int v = p.second; int root = -1; if (dists1[u].par == v) { root = u; } else if (dists1[v].par == u) { root = v; } else assert(false); auto seg = seqFrom1[root]; rmq1.modify(seg.first, seg.second, true); root = -1; if (dists2[u].par == v) { root = u; } else if (dists2[v].par == u) { root = v; } else assert(false); seg = seqFrom2[root]; rmq2.modify(seg.first, seg.second, true); auto res1 = rmq1.query(0, n - 1); auto res2 = rmq2.query(0, n - 1); long long res = max(res1.lowest0, res2.lowest0); cout << res << "\n"; } } int main(int argc, char** argv) { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cout << setprecision(2) << fixed; int t; solve(); return 0; }
#include <bits/stdc++.h> namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } int bsf(unsigned int n) { return __builtin_ctz(n); } } // namespace internal } // namespace atcoder namespace atcoder { template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder using namespace std; using namespace atcoder; using ll = long long; using P = pair<int, int>; using VI = vector<int>; using VVI = vector<VI>; struct HLD { std::vector<std::vector<int>>& to; int root, n; std::vector<int> sz, parent, depth, idx, ridx, head; HLD(std::vector<std::vector<int>>& to, int root = 0) : to(to), root(root), n(to.size()), sz(n), parent(n), depth(n), idx(n), ridx(n), head(n) { init_tree_data(root); int x = 0; assign_idx(root, root, x); } void init_tree_data(int u, int p = -1, int d = 0) { parent[u] = p; depth[u] = d; int s = 1; for (int v : to[u]) { if (v == p) continue; init_tree_data(v, u, d + 1); s += sz[v]; } sz[u] = s; } void assign_idx(int u, int h, int& nxt, int p = -1) { head[u] = h; idx[u] = nxt++; if (sz[u] == 1) { ridx[u] = nxt; return; } int mxsize = 0; int mi; for (int v : to[u]) { if (v == p) continue; if (sz[v] > mxsize) { mxsize = sz[v]; mi = v; } } assign_idx(mi, h, nxt, u); for (int v : to[u]) { if (v == p || v == mi) continue; assign_idx(v, v, nxt, u); } ridx[u] = nxt; } }; struct S { int even, odd; }; S op(S x, S y) { return {max(x.even, y.even), max(x.odd, y.odd)}; } S e() { return {-1, -1}; } S mapping(bool f, S x) { if (f) return {x.odd, x.even}; else return x; } bool composition(bool f, bool g) { return f ^ g; } bool id() { return false; } struct E { int u, v, state; }; VVI to; P dfs(int u, int p = -1, int depth = 0) { P ret = {u, depth}; for (int v : to[u]) { if (v == p) continue; P r = dfs(v, u, depth + 1); if (r.second > ret.second) ret = r; } return ret; } int main() { ios::sync_with_stdio(false); int n; cin >> n; to.resize(n); vector<E> es(n - 1); for (int i = 0; i < (n - 1); ++i) { int u, v, t; cin >> u >> v >> t; --u, --v; es[i] = {u, v, t}; to[u].push_back(v); to[v].push_back(u); } int m; cin >> m; VI query(m); for (int i = 0; i < (m); ++i) { cin >> query[i]; query[i]--; } VI ab(2); ab[0] = dfs(0).first; ab[1] = dfs(ab[0]).first; VVI ans(2, VI(m)); for (int tt = 0; tt < (2); ++tt) { int a = ab[tt]; HLD hld(to, a); VI is_odd(n); for (int i = 0; i < (n - 1); ++i) { int u = es[i].u, v = es[i].v, s = es[i].state; if (hld.parent[u] == v) { swap(u, v); swap(es[i].u, es[i].v); } is_odd[v] = s; } queue<int> que; que.push(a); while (!que.empty()) { int u = que.front(); que.pop(); for (int v : to[u]) { if (v == hld.parent[u]) continue; is_odd[v] ^= is_odd[u]; que.push(v); } } vector<S> init_vec(n); for (int i = 0; i < (n); ++i) { if (is_odd[i]) { init_vec[hld.idx[i]] = {-1, hld.depth[i]}; } else { init_vec[hld.idx[i]] = {hld.depth[i], -1}; } } lazy_segtree<S, op, e, bool, mapping, composition, id> seg(init_vec); for (int i = 0; i < (m); ++i) { int ei = query[i]; int v = es[ei].v; int vin = hld.idx[v], vout = hld.ridx[v]; seg.apply(vin, vout, true); ans[tt][i] = seg.all_prod().even; } } for (int i = 0; i < (m); ++i) cout << max(ans[0][i], ans[1][i]) << '\n'; }
#include <bits/stdc++.h> struct MI { private: char bb[1 << 14]; FILE *f; char *bs, *be; char e; bool o, l; public: MI() : f(stdin), bs(0), be(0) {} inline char get() { if (o) { o = 0; return e; } if (bs == be) be = (bs = bb) + fread(bb, 1, sizeof(bb), f); if (bs == be) { l = 1; return -1; }; return *bs++; } inline void unget(char c) { o = 1; e = c; } template <class T> inline T read() { T r; *this > r; return r; } template <class T> inline MI &operator>(T &); }; template <class T> struct Q { const static bool U = T(-1) >= T(0); inline void operator()(MI &t, T &r) const { r = 0; char c; bool y = 0; if (U) for (;;) { c = t.get(); if (c == -1) goto E; if (isdigit(c)) break; } else for (;;) { c = t.get(); if (c == -1) goto E; if (c == '-') { c = t.get(); if (isdigit(c)) { y = 1; break; }; } else if (isdigit(c)) break; ; }; for (;;) { if (c == -1) goto E; if (isdigit(c)) r = r * 10 + (c ^ 48); else break; c = t.get(); } t.unget(c); E:; if (y) r = -r; } }; template <> struct Q<char> {}; template <class T> inline MI &MI::operator>(T &t) { Q<T>()(*this, t); return *this; } template <class T> std::ostream &operator<(std::ostream &out, const T &t) { return out << t; } using std::cout; MI cin; const int $n = 500005; const int $t = 1048600; template <typename T> inline bool gmax(T &a, const T &b) { return a < b && (a = b, true); } int n, fa[$n], mx[$n], se[$n], dep[$n], es[$n][2], t1, t2, dia; std::vector<std::pair<int, bool>> outs[$n]; bool val[$n]; void dfs(int x) { mx[x] = x; dep[x] = dep[fa[x]] + 1; for (auto [v, w] : outs[x]) { if (v == fa[x]) continue; fa[v] = x; val[v] = val[x] ^ w; dfs(v); if (dep[mx[v]] >= dep[mx[x]]) { se[x] = mx[x]; mx[x] = mx[v]; } else if (dep[mx[v]] > dep[se[x]]) se[x] = mx[v]; } if (se[x] && gmax(dia, dep[mx[x]] + dep[se[x]] - dep[x] * 2)) { t1 = mx[x]; t2 = se[x]; } } struct { int rt, fa[$n], dfn[$n], dt, siz[$n], dep[$n], seq[$n]; int mx[$t][2], ll, rr; bool rv[$t]; inline void upd(int x) { std::swap(mx[x][0], mx[x][1]); rv[x] ^= 1; } inline void pd(int x) { if (!rv[x]) return; upd((x << 1)); upd(((x << 1) | 1)); rv[x] = 0; } inline void pu(int x) { mx[x][0] = std::max(mx[(x << 1)][0], mx[((x << 1) | 1)][0]); mx[x][1] = std::max(mx[(x << 1)][1], mx[((x << 1) | 1)][1]); } void build(int x = 1, int l = 1, int r = n) { if (l == r) return (void)(mx[x][val[seq[l]]] = dep[seq[l]] - 1); const int mid = (l + r) >> 1; build((x << 1), l, mid); build(((x << 1) | 1), mid + 1, r); pu(x); } void $update(int x, int l, int r) { if (ll <= l && r <= rr) return upd(x); const int mid = (l + r) >> 1; pd(x); if (ll <= mid) $update((x << 1), l, mid); if (mid < rr) $update(((x << 1) | 1), mid + 1, r); pu(x); } inline void update(int l, int r) { ll = l; rr = r; $update(1, 1, n); } void dfs(int x) { seq[dfn[x] = ++dt] = x; siz[x] = 1; dep[x] = dep[fa[x]] + 1; for (auto [v, w] : outs[x]) { if (v == fa[x]) continue; fa[v] = x; dfs(v); siz[x] += siz[v]; } } inline void init(int rt) { this->rt = rt; dfs(rt); build(); } inline int flip(int x, int y) { if (fa[y] == x) std::swap(x, y); assert(fa[x] == y); update(dfn[x], dfn[x] + siz[x] - 1); return mx[1][val[rt]]; } } A, B; int main() { cin > n; for (int i = 1; i < n; ++i) { const int x = es[i][0] = (cin.read<int>()), y = es[i][1] = (cin.read<int>()), w = (cin.read<int>()); outs[x].emplace_back(y, w); outs[y].emplace_back(x, w); } dfs(1); A.init(t1); B.init(t2); for (int T = (cin.read<int>()); T; --T) { const int id = (cin.read<int>()), x = es[id][0], y = es[id][1]; cout < std::max(A.flip(x, y), B.flip(x, y)) < ('\n'); } }
#include <bits/stdc++.h> namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } int bsf(unsigned int n) { return __builtin_ctz(n); } } // namespace internal } // namespace atcoder namespace atcoder { template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder using namespace std; using namespace atcoder; using ll = long long; using P = pair<int, int>; using VI = vector<int>; using VVI = vector<VI>; struct HLD { std::vector<std::vector<int>>& to; int root, n; std::vector<int> sz, parent, depth, idx, ridx, head; HLD(std::vector<std::vector<int>>& to, int root = 0) : to(to), root(root), n(to.size()), sz(n), parent(n), depth(n), idx(n), ridx(n), head(n) { init_tree_data(root); int x = 0; assign_idx(root, root, x); } void init_tree_data(int u, int p = -1, int d = 0) { parent[u] = p; depth[u] = d; int s = 1; for (int v : to[u]) { if (v == p) continue; init_tree_data(v, u, d + 1); s += sz[v]; } sz[u] = s; } void assign_idx(int u, int h, int& nxt, int p = -1) { head[u] = h; idx[u] = nxt++; if (sz[u] == 1) { ridx[u] = nxt; return; } int mxsize = 0; int mi; for (int v : to[u]) { if (v == p) continue; if (sz[v] > mxsize) { mxsize = sz[v]; mi = v; } } assign_idx(mi, h, nxt, u); for (int v : to[u]) { if (v == p || v == mi) continue; assign_idx(v, v, nxt, u); } ridx[u] = nxt; } }; struct S { int even, odd; }; S op(S x, S y) { return {max(x.even, y.even), max(x.odd, y.odd)}; } S e() { return {-1, -1}; } S mapping(bool f, S x) { if (f) return {x.odd, x.even}; else return x; } bool composition(bool f, bool g) { return f ^ g; } bool id() { return false; } struct E { int u, v, state; }; VVI to; P dfs(int u, int p = -1, int depth = 0) { P ret = {u, depth}; for (int v : to[u]) { if (v == p) continue; P r = dfs(v, u, depth + 1); if (r.second > ret.second) ret = r; } return ret; } int main() { int n; cin >> n; to.resize(n); vector<E> es(n - 1); for (int i = 0; i < (n - 1); ++i) { int u, v, t; cin >> u >> v >> t; --u, --v; es[i] = {u, v, t}; to[u].push_back(v); to[v].push_back(u); } int m; cin >> m; VI query(m); for (int i = 0; i < (m); ++i) { cin >> query[i]; query[i]--; } VI ab(2); ab[0] = dfs(0).first; ab[1] = dfs(ab[0]).first; VVI ans(2, VI(m)); for (int tt = 0; tt < (2); ++tt) { int a = ab[tt]; HLD hld(to, a); vector<S> init_vec(n); for (int i = 0; i < (n); ++i) { init_vec[hld.idx[i]] = {hld.depth[i], -1}; } lazy_segtree<S, op, e, bool, mapping, composition, id> seg(init_vec); for (int i = 0; i < (n - 1); ++i) { int u = es[i].u, v = es[i].v, s = es[i].state; if (!s) continue; if (hld.parent[u] == v) swap(u, v); int vin = hld.idx[v], vout = hld.ridx[v]; seg.apply(vin, vout, true); } for (int i = 0; i < (m); ++i) { int qi = query[i]; int u = es[qi].u, v = es[qi].v; if (hld.parent[u] == v) swap(u, v); int vin = hld.idx[v], vout = hld.ridx[v]; seg.apply(vin, vout, true); ans[tt][i] = seg.all_prod().even; } } for (int i = 0; i < (m); ++i) cout << max(ans[0][i], ans[1][i]) << '\n'; }
#include <bits/stdc++.h> #pragma warning(disable : 4996) template <typename T> T min(T x, T y) { return x < y ? x : y; } template <typename T> T max(T x, T y) { return x > y ? x : y; }; const long long INF = 20000000050000; const long long mod = 1000000007; const int MAXN = 500005; std::pair<int, int> pmax(std::pair<int, int> x, std::pair<int, int> y) { return {max(x.first, y.first), max(x.second, y.second)}; } struct tree { int l; int r; int mx[2]; bool rev; tree *lson; tree *rson; tree() { l = r = 0; mx[0] = -MAXN; mx[1] = -MAXN; rev = false; lson = rson = NULL; } void pushdown() { if (!rev) return; std::swap(lson->mx[0], lson->mx[1]); std::swap(rson->mx[0], rson->mx[1]); lson->rev ^= 1; rson->rev ^= 1; rev = false; } void fix() { mx[0] = max(lson->mx[0], rson->mx[0]); mx[1] = max(lson->mx[1], rson->mx[1]); } void reverse(int L, int R) { if (l >= L && r <= R) { std::swap(mx[0], mx[1]); rev ^= 1; return; } pushdown(); int mid = (l + r) / 2; if (L <= mid) lson->reverse(L, R); if (R > mid) rson->reverse(L, R); fix(); } std::pair<int, int> query(int L, int R) { if (l >= L && r <= R) return {mx[0], mx[1]}; pushdown(); int mid = (l + r) / 2; std::pair<int, int> ans = std::make_pair(-MAXN, -MAXN); if (L <= mid) ans = pmax(ans, lson->query(L, R)); if (R > mid) ans = pmax(ans, rson->query(L, R)); return ans; } void build(int L, int R, int *dep, int *ty) { l = L, r = R; if (l == r) { mx[ty[l] & 1] = dep[l]; return; } int mid = (l + r) / 2; lson = new tree; rson = new tree; lson->build(l, mid, dep, ty); rson->build(mid + 1, r, dep, ty); fix(); } }; int N, u[MAXN], v[MAXN], S1, S2; std::vector<int> e[MAXN]; std::unordered_map<long long, int> M; long long Mid(long long u, long long v) { return u * MAXN + v; } void addedge(int u, int v, bool ty) { e[u].push_back(v); e[v].push_back(u); M[Mid(u, v)] = ty; M[Mid(v, u)] = ty; } struct STRUCT { tree T; int dep[MAXN], cnt[MAXN], D; int cur, dfn[MAXN], rn[MAXN], rk[MAXN]; void dfs(int v, int f) { if (!f) dep[v] = cnt[v] = cur = D = 0; if (dep[D] < dep[v]) D = v; dfn[v] = ++cur; rk[cur] = v; for (int i = 0; i < e[v].size(); i++) { if (e[v][i] == f) continue; dep[e[v][i]] = dep[v] + 1; cnt[e[v][i]] = cnt[v] + M[Mid(v, e[v][i])]; dfs(e[v][i], v); } rn[v] = cur; } void prepare(int S1) { static int tdep[MAXN], tcnt[MAXN]; dfs(S1, 0); for (int i = 1; i <= N; i++) { tdep[i] = dep[rk[i]]; tcnt[i] = cnt[rk[i]]; } T.build(1, N, tdep, tcnt); } void update(int u, int v) { int id = dep[u] > dep[v] ? u : v; T.reverse(dfn[id], rn[id]); } }; STRUCT T1, T2; void init() { int t; scanf("%d", &N); for (int i = 1; i < N; i++) { scanf("%d %d %d", u + i, v + i, &t); addedge(u[i], v[i], (bool)t); } T1.dfs(1, 0); S1 = T1.D; T1.dfs(S1, 0); S2 = T1.D; T1.prepare(S1); T2.prepare(S2); } void solve() { int M, t; scanf("%d", &M); while (M--) { scanf("%d", &t); T1.update(u[t], v[t]); T2.update(u[t], v[t]); printf("%d\n", (max(T1.T.mx[0], T2.T.mx[0]))); } } int main() { init(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); template <typename T> void chkmax(T& x, T y) { if (x < y) x = y; } template <typename T> void chkmin(T& x, T y) { if (x > y) x = y; } inline int read() { int x = 0; char c = getchar(); while (c < 48) c = getchar(); while (c > 47) x = x * 10 + (c ^ 48), c = getchar(); return x; } const int maxn = 5e5 + 10; int n, q; int S, T, ecnt = 1, h[maxn], eid[maxn << 1]; struct edges { int nxt, to, w; } E[maxn << 1]; void addline(int u, int v, int w) { E[++ecnt] = {h[u], v, w}, h[u] = ecnt; } int dfn[maxn], arr[maxn], dis[maxn], col[maxn], sz[maxn]; int bfs(int S) { static int Q[maxn]; memset(dis, -1, sizeof dis); int l = 1, r = 1; Q[1] = S, dis[S] = 0; while (l <= r) { int u = Q[l++]; for (int i = h[u]; i; i = E[i].nxt) { int v = E[i].to; if (dis[v] == -1) dis[v] = dis[u] + 1, Q[++r] = v; } } int res = S; for (int i = (1), iend = (n); i <= iend; ++i) if (dis[res] < dis[i]) res = i; return res; } void dfs(int u, int f) { static int now; dfn[u] = ++now, arr[now] = u, sz[u] = 1; for (int i = h[u]; i; i = E[i].nxt) { int v = E[i].to; if (v != f) { col[v] = col[u] ^ E[i].w, eid[i ^ 1] = eid[i] = v, dfs(v, u), sz[u] += sz[v]; } } } struct SGT { int val[maxn << 2][2], tag[maxn << 2]; void maintain(int k) { for (int t = (0), tend = (1); t <= tend; ++t) val[k][t] = max(val[k << 1][t], val[k << 1 | 1][t]); } void pushtag(int k) { tag[k] ^= 1, swap(val[k][0], val[k][1]); } void pushdown(int k) { if (tag[k]) tag[k] = 0, pushtag(k << 1), pushtag(k << 1 | 1); } void build(int k, int l, int r) { if (l == r) return val[k][col[arr[l]]] = dis[arr[l]], val[k][col[arr[l]] ^ 1] = -1, void(); build(k << 1, l, ((l + r) >> 1)), build(k << 1 | 1, ((l + r) >> 1) + 1, r), maintain(k); } void upd(int k, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return pushtag(k), void(); pushdown(k); if (ql <= ((l + r) >> 1)) upd(k << 1, l, ((l + r) >> 1), ql, qr); if (qr > ((l + r) >> 1)) upd(k << 1 | 1, ((l + r) >> 1) + 1, r, ql, qr); maintain(k); } int getc(int k, int l, int r, int p) { if (l == r) return val[k][0] == -1 ? 1 : 0; return pushdown(k), p <= ((l + r) >> 1) ? getc(k << 1, l, ((l + r) >> 1), p) : getc(k << 1 | 1, ((l + r) >> 1) + 1, r, p); } } TS, TT; void solve() { cin >> n; for (int i = (2), iend = (n); i <= iend; ++i) { int u = read(), v = read(), w = read(); addline(u, v, w), addline(v, u, w); } dfs(1, 0); S = bfs(1), T = bfs(S); TS.build(1, 1, n); bfs(T); TT.build(1, 1, n); cin >> q; while (q--) { int id = read(), v = eid[id * 2]; TS.upd(1, 1, n, dfn[v], dfn[v] + sz[v] - 1); TT.upd(1, 1, n, dfn[v], dfn[v] + sz[v] - 1); int cs = TS.getc(1, 1, n, dfn[S]), ct = TT.getc(1, 1, n, dfn[T]); printf("%d\n", max(TS.val[1][cs], TT.val[1][ct])); } } signed main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using LL = long long; using ULL = unsigned long long; using pii = pair<int, int>; using PLL = pair<LL, LL>; using UI = unsigned int; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const double EPS = 1e-8; const double PI = acos(-1.0); char nc() { static char buf[1000000], *p = buf, *q = buf; return p == q && (q = (p = buf) + fread(buf, 1, 1000000, stdin), p == q) ? EOF : *p++; } int rd() { int res = 0; char c = nc(); while (!isdigit(c)) c = nc(); while (isdigit(c)) res = res * 10 + c - '0', c = nc(); return res; } const int N = 5e5 + 10; int n, Q, dfs_cnt; bool edge[N]; vector<pii> V[N]; int buf[N]; bool tag[N]; struct Tree { int lft[N], rgt[N], bottom[N], l[N << 2], r[N << 2], S[N << 2][2]; bool lazy[N << 2]; void dfs(int x, int fa, int dep, bool f) { lft[x] = ++dfs_cnt; buf[dfs_cnt] = dep; tag[dfs_cnt] = f; for (auto p : V[x]) { if (p.first == fa) continue; bottom[p.second] = p.first; dfs(p.first, x, dep + 1, f ^ edge[p.second]); } rgt[x] = dfs_cnt; } void build(int o, int ll, int rr) { l[o] = ll; r[o] = rr; lazy[o] = 0; if (ll < rr) { int mid = l[o] + r[o] >> 1; build(o << 1, ll, mid); build(o << 1 | 1, mid + 1, rr); S[o][0] = max(S[o << 1][0], S[o << 1 | 1][0]); S[o][1] = max(S[o << 1][1], S[o << 1 | 1][1]); } else { if (tag[ll]) { S[o][0] = 0; S[o][1] = buf[ll]; } else { S[o][0] = buf[ll]; S[o][1] = 0; } } } int pushdown(int o) { int mid = l[o] + r[o] >> 1; if (lazy[o]) { update(o << 1, l[o], mid); update(o << 1 | 1, mid + 1, r[o]); lazy[o] = 0; } return mid; } void update(int o, int ll, int rr) { if (l[o] == ll && r[o] == rr) { lazy[o] ^= 1; swap(S[o][0], S[o][1]); return; } int mid = pushdown(o); if (rr <= mid) update(o << 1, ll, rr); else if (ll > mid) update(o << 1 | 1, ll, rr); else { update(o << 1, ll, mid); update(o << 1 | 1, mid + 1, rr); } S[o][0] = max(S[o << 1][0], S[o << 1 | 1][0]); S[o][1] = max(S[o << 1][1], S[o << 1 | 1][1]); } void init(int x) { dfs_cnt = 0; dfs(x, -1, 0, 0); build(1, 1, n); } void flip(int x) { x = bottom[x]; update(1, lft[x], rgt[x]); } } tree[2]; int dist[N]; int bfs(int x) { for (int i = (1); i < (n + 1); ++i) dist[i] = inf; dist[x] = 0; int ret = x; queue<int> Q; Q.emplace(x); while (!Q.empty()) { x = Q.front(); Q.pop(); if (dist[x] > dist[ret]) ret = x; for (pii p : V[x]) { if (dist[p.first] == inf) { dist[p.first] = dist[x] + 1; Q.emplace(p.first); } } } return ret; } int main() { int x, y, z; n = rd(); for (int i = (1); i < (n); ++i) { x = rd(); y = rd(); z = rd(); V[x].emplace_back(y, i); V[y].emplace_back(x, i); edge[i] = z; } x = bfs(1); y = bfs(x); tree[0].init(x); tree[1].init(y); Q = rd(); while (Q--) { x = rd(); tree[0].flip(x); tree[1].flip(x); printf("%d\n", max(tree[0].S[1][0], tree[1].S[1][0])); } return 0; }
#include <bits/stdc++.h> using namespace std; inline int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * f; } int _tmp[20]; int _tn; inline void printint(int x) { int f = 1; if (x == 0) { puts("0"); return; } if (x < 0) { f = -1; x = -x; } _tn = 0; while (x) { _tmp[++_tn] = x % 10; x /= 10; } if (f == -1) putchar('-'); for (int i = _tn; i >= 1; i--) putchar('0' + _tmp[i]); puts(""); } inline void chmax(int &x, int y) { x = max(x, y); } const int N = 5e5 + 5; const int INF = 123456789; int n, m, t = 1, q; int X[N], Y[N], Z[N], p[N], B[N], E[N], d[N], poi[N * 2]; vector<int> nei[N]; void dfs(int x) { B[x] = E[x] = ++m; poi[m] = x; for (int i = 0; i < int(nei[x].size()); i++) { int to = nei[x][i]; if (to != p[x]) { p[to] = x; d[to] = d[x] + 1; dfs(to); E[x] = ++m; poi[m] = x; } } } int lson[N * 4]; int rson[N * 4]; int val1[2][N * 4]; int val2[N * 4]; int val3[2][N * 4]; int val4[2][N * 4]; int val5[2][N * 4]; int tag[N * 4]; void update(int pos) { for (int i = 0; i < 2; i++) { val1[i][pos] = max(val1[i][lson[pos]], val1[i][rson[pos]]); val3[i][pos] = max(val3[i][lson[pos]], val3[i][rson[pos]]); val4[i][pos] = max(val4[i][lson[pos]], val4[i][rson[pos]]); val5[i][pos] = max(val5[i][lson[pos]], val5[i][rson[pos]]); } val2[pos] = max(val2[lson[pos]], val2[rson[pos]]); for (int i = 0; i < 2; i++) { if (val1[i][lson[pos]] != -INF && val2[rson[pos]] != -INF) chmax(val3[i][pos], val1[i][lson[pos]] + val2[rson[pos]]); if (val2[lson[pos]] != -INF && val1[i][rson[pos]] != -INF) chmax(val4[i][pos], val2[lson[pos]] + val1[i][rson[pos]]); } for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { if (val1[i][lson[pos]] != -INF && val4[i][rson[pos]] != -INF) chmax(val5[i ^ j][pos], val1[i][lson[pos]] + val4[i][rson[pos]]); if (val3[i][lson[pos]] != -INF && val1[i][rson[pos]] != -INF) chmax(val5[i ^ j][pos], val3[i][lson[pos]] + val1[i][rson[pos]]); } } void flip(int pos) { tag[pos] ^= 1; swap(val1[0][pos], val1[1][pos]); swap(val3[0][pos], val3[1][pos]); swap(val4[0][pos], val4[1][pos]); } void pushdown(int pos) { if (tag[pos]) { flip(lson[pos]); flip(rson[pos]); tag[pos] = 0; } } void build(int pos, int x, int y) { for (int i = 0; i < 2; i++) val1[i][pos] = val3[i][pos] = val4[i][pos] = val5[i][pos] = -INF; val2[pos] = -INF; if (x == y) { val1[0][pos] = d[poi[x]]; val2[pos] = -2 * d[poi[x]]; val3[0][pos] = val4[0][pos] = -d[poi[x]]; val5[0][pos] = 0; return; } int mid = (x + y) >> 1; lson[pos] = ++t; rson[pos] = ++t; build(lson[pos], x, mid); build(rson[pos], mid + 1, y); update(pos); } void modify(int pos, int x, int y, int l, int r) { if (x >= l && y <= r) { flip(pos); return; } if (x > r || y < l) return; pushdown(pos); int mid = (x + y) >> 1; modify(lson[pos], x, mid, l, r); modify(rson[pos], mid + 1, y, l, r); update(pos); } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d%d", X + i, Y + i, Z + i); nei[X[i]].push_back(Y[i]); nei[Y[i]].push_back(X[i]); } p[1] = 1; dfs(1); build(1, 1, m); for (int i = 1; i < n; i++) if (Z[i]) { int x = X[i], y = Y[i]; if (p[y] == x) swap(x, y); modify(1, 1, m, B[x], E[x]); } scanf("%d", &q); for (int i = 1; i <= q; i++) { int id; scanf("%d", &id); int x = X[id], y = Y[id]; if (p[y] == x) swap(x, y); modify(1, 1, m, B[x], E[x]); printf("%d\n", val5[0][1]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 9; int a[N], r[N]; struct ST { int t[4 * N][2], lazy[4 * N]; ST() { memset(t, 0, sizeof t); memset(lazy, 0, sizeof lazy); } inline void push(int n, int b, int e) { if (lazy[n] == 0) return; swap(t[n][0], t[n][1]); if (b != e) { lazy[(n << 1)] = lazy[(n << 1)] ^ lazy[n]; lazy[((n << 1) | 1)] = lazy[((n << 1) | 1)] ^ lazy[n]; } lazy[n] = 0; } inline void pull(int n) { t[n][0] = max(t[(n << 1)][0], t[((n << 1) | 1)][0]); t[n][1] = max(t[(n << 1)][1], t[((n << 1) | 1)][1]); } void build(int n, int b, int e) { lazy[n] = 0; if (b == e) { t[n][0] = a[r[b]]; t[n][1] = 0; return; } int mid = (b + e) >> 1; build((n << 1), b, mid); build(((n << 1) | 1), mid + 1, e); pull(n); } void upd(int n, int b, int e, int i, int j) { push(n, b, e); if (j < b || e < i) return; if (i <= b && e <= j) { lazy[n] = 1; push(n, b, e); return; } int mid = (b + e) >> 1; upd((n << 1), b, mid, i, j); upd(((n << 1) | 1), mid + 1, e, i, j); pull(n); } } t; vector<pair<int, int>> g[N]; int farthest(int s, int n, vector<int> &d) { d.assign(n + 1, N); d[s] = 0; vector<bool> vis(n + 1); queue<int> q; q.push(s); vis[s] = 1; int last = s; while (!q.empty()) { int u = q.front(); q.pop(); for (auto x : g[u]) { int v = x.first; if (vis[v]) continue; d[v] = d[u] + 1; q.push(v); vis[v] = 1; } last = u; } return last; } int T = 0, st[N], en[N]; void dfs(int u, int p = 0) { a[u] = a[p] + 1; st[u] = ++T; r[T] = u; for (auto x : g[u]) { int v = x.first; if (v ^ p) { dfs(v, u); } } en[u] = T; } int ans[N], Q[N], u[N], v[N], f[N]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i < n; i++) { cin >> u[i] >> v[i] >> f[i]; g[u[i]].push_back({v[i], f[i]}); g[v[i]].push_back({u[i], f[i]}); } int q; cin >> q; for (int i = 1; i <= q; i++) { cin >> Q[i]; } vector<int> d1, d2; int x = farthest(1, n, d1); int y = farthest(x, n, d2); T = 0; dfs(x); t.build(1, 1, n); for (int i = 1; i < n; i++) { if (a[u[i]] > a[v[i]]) { swap(u[i], v[i]); } if (f[i]) { t.upd(1, 1, n, st[v[i]], en[v[i]]); } } for (int i = 1; i <= q; i++) { t.upd(1, 1, n, st[v[Q[i]]], en[v[Q[i]]]); ans[i] = max(ans[i], t.t[1][0]); } T = 0; dfs(y); t.build(1, 1, n); for (int i = 1; i < n; i++) { if (a[u[i]] > a[v[i]]) { swap(u[i], v[i]); } if (f[i]) { t.upd(1, 1, n, st[v[i]], en[v[i]]); } } for (int i = 1; i <= q; i++) { t.upd(1, 1, n, st[v[Q[i]]], en[v[Q[i]]]); ans[i] = max(ans[i], t.t[1][0]); } for (int i = 1; i <= q; i++) { cout << ans[i] - 1 << '\n'; } return 0; }
#include <bits/stdc++.h> void swap(int &a, int &b) { int t = a; a = b; b = t; } int max(int a, int b) { return a > b ? a : b; } const int Maxn = 500000; int head[Maxn + 5], arrive[Maxn << 1 | 5], nxt[Maxn << 1 | 5], val[Maxn << 1 | 5], id[Maxn << 1 | 5], tot; void add_edge(int from, int to, int w, int _id) { arrive[++tot] = to; nxt[tot] = head[from]; val[tot] = w; id[tot] = _id; head[from] = tot; } int n, m; struct Tree { int root; int dfn[Maxn + 5], sz[Maxn + 5], rnk[Maxn + 5], dfn_tot; int fa_val[Maxn + 5]; int id_node[Maxn + 5]; int dep[Maxn + 5]; void init_dfs(int u, int fa) { dep[u] = dep[fa] + 1; sz[u] = 1; dfn[u] = ++dfn_tot; rnk[dfn_tot] = u; for (int i = head[u]; i; i = nxt[i]) { int v = arrive[i]; if (v == fa) { continue; } id_node[id[i]] = v; fa_val[v] = val[i]; init_dfs(v, u); sz[u] += sz[v]; } } struct Segment_Node { int max_0, max_1; bool lazy; } seg[Maxn << 2 | 5]; void build(int root = 1, int left = 1, int right = n) { seg[root].lazy = 0; if (left == right) { seg[root].max_0 = dep[rnk[left]]; seg[root].max_1 = 0; return; } int mid = (left + right) >> 1; build(root << 1, left, mid); build(root << 1 | 1, mid + 1, right); seg[root].max_0 = max(seg[root << 1].max_0, seg[root << 1 | 1].max_0); seg[root].max_1 = max(seg[root << 1].max_1, seg[root << 1 | 1].max_1); } void update_tag(int root) { seg[root].lazy ^= 1; swap(seg[root].max_0, seg[root].max_1); } void push_down(int root) { if (seg[root].lazy) { update_tag(root << 1); update_tag(root << 1 | 1); seg[root].lazy = 0; } } void update(int l, int r, int root = 1, int left = 1, int right = n) { if (l > right || r < left) { return; } if (l <= left && r >= right) { update_tag(root); return; } push_down(root); int mid = (left + right) >> 1; update(l, r, root << 1, left, mid); update(l, r, root << 1 | 1, mid + 1, right); seg[root].max_0 = max(seg[root << 1].max_0, seg[root << 1 | 1].max_0); seg[root].max_1 = max(seg[root << 1].max_1, seg[root << 1 | 1].max_1); } void _update(int u) { update(dfn[u], dfn[u] + sz[u] - 1); } void update(int u) { u = id_node[u]; update(dfn[u], dfn[u] + sz[u] - 1); } int query() { return seg[1].max_0; } void init(int u) { root = u; dfn_tot = 0; init_dfs(root, 0); build(); for (int i = 1; i <= n; i++) { if (fa_val[i]) { _update(i); } } } } tree_1, tree_2; int root_1, root_2; int dep[Maxn + 5]; void init_dfs(int u, int fa) { dep[u] = dep[fa] + 1; for (int i = head[u]; i; i = nxt[i]) { int v = arrive[i]; if (v == fa) { continue; } init_dfs(v, u); } } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add_edge(u, v, w, i); add_edge(v, u, w, i); } init_dfs(1, 0); for (int i = 1; i <= n; i++) { if (dep[i] > dep[root_1]) { root_1 = i; } } init_dfs(root_1, 0); for (int i = 1; i <= n; i++) { if (dep[i] > dep[root_2]) { root_2 = i; } } tree_1.init(root_1); tree_2.init(root_2); scanf("%d", &m); for (int i = 1; i <= m; i++) { int id; scanf("%d", &id); tree_1.update(id); tree_2.update(id); printf("%d\n", max(tree_1.query(), tree_2.query()) - 1); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long Mod = 1000000007LL; const int N = 1048576 + 10; const int Inf = 1e9; const long long Log = 30; vector<pair<int, int> > G[N]; int n; struct node { int a, b, c, A, C; int ab, Ab; int bc, bC; int abc, AbC; int lz; node() { a = b = c = A = C = -Inf; ab = Ab = bc = bC = -Inf; abc = AbC = -Inf; lz = 0; } }; int A[N], p = 0; int dep[N], dep2[N], st[N], fn[N]; void DFS(int u, int pr, int d = 0, int d2 = 0) { dep[u] = d; dep2[u] = d2; st[u] = p; A[p] = u; p++; for (auto [adj, w] : G[u]) { if (adj == pr) continue; DFS(adj, u, d + 1, d2 + w); A[p] = u; p++; } fn[u] = p; } node seg[2 * N]; void Apply(int id) { seg[id].lz ^= 1; swap(seg[id].A, seg[id].a); swap(seg[id].C, seg[id].c); swap(seg[id].Ab, seg[id].ab); swap(seg[id].bC, seg[id].bc); swap(seg[id].AbC, seg[id].abc); } void Shift(int id) { if (seg[id].lz) { Apply(id << 1); Apply(id << 1 | 1); seg[id].lz = 0; } } void Build(int id, int L, int R) { if (L + 1 == R) { if (dep2[A[L]] % 2 == 0) { seg[id].A = dep[A[L]]; seg[id].C = dep[A[L]]; seg[id].Ab = -dep[A[L]]; seg[id].bC = -dep[A[L]]; } else { seg[id].a = dep[A[L]]; seg[id].c = dep[A[L]]; seg[id].ab = -dep[A[L]]; seg[id].bc = -dep[A[L]]; } seg[id].b = -2 * dep[A[L]]; return; } int mid = (L + R) >> 1; Build(id << 1, L, mid); Build(id << 1 | 1, mid, R); seg[id].A = max(seg[id << 1].A, seg[id << 1 | 1].A); seg[id].C = max(seg[id << 1].C, seg[id << 1 | 1].C); seg[id].a = max(seg[id << 1].a, seg[id << 1 | 1].a); seg[id].c = max(seg[id << 1].c, seg[id << 1 | 1].c); seg[id].b = max(seg[id << 1].b, seg[id << 1 | 1].b); seg[id].Ab = max({seg[id << 1].Ab, seg[id << 1 | 1].Ab, seg[id << 1].A + seg[id << 1 | 1].b}); seg[id].ab = max({seg[id << 1].ab, seg[id << 1 | 1].ab, seg[id << 1].a + seg[id << 1 | 1].b}); seg[id].bC = max({seg[id << 1].bC, seg[id << 1 | 1].bC, seg[id << 1].b + seg[id << 1 | 1].C}); seg[id].bc = max({seg[id << 1].bc, seg[id << 1 | 1].bc, seg[id << 1].b + seg[id << 1 | 1].c}); seg[id].AbC = max({seg[id << 1].AbC, seg[id << 1 | 1].AbC, seg[id << 1].A + seg[id << 1 | 1].bC, seg[id << 1].Ab + seg[id << 1 | 1].C}); seg[id].abc = max({seg[id << 1].abc, seg[id << 1 | 1].abc, seg[id << 1].a + seg[id << 1 | 1].bc, seg[id << 1].ab + seg[id << 1 | 1].c}); } void Rev(int id, int l, int r, int L, int R) { if (r <= L || R <= l) return; if (l <= L && R <= r) { Apply(id); return; } Shift(id); int mid = (L + R) >> 1; Rev(id << 1, l, r, L, mid); Rev(id << 1 | 1, l, r, mid, R); seg[id].A = max(seg[id << 1].A, seg[id << 1 | 1].A); seg[id].C = max(seg[id << 1].C, seg[id << 1 | 1].C); seg[id].a = max(seg[id << 1].a, seg[id << 1 | 1].a); seg[id].c = max(seg[id << 1].c, seg[id << 1 | 1].c); seg[id].b = max(seg[id << 1].b, seg[id << 1 | 1].b); seg[id].Ab = max({seg[id << 1].Ab, seg[id << 1 | 1].Ab, seg[id << 1].A + seg[id << 1 | 1].b}); seg[id].ab = max({seg[id << 1].ab, seg[id << 1 | 1].ab, seg[id << 1].a + seg[id << 1 | 1].b}); seg[id].bC = max({seg[id << 1].bC, seg[id << 1 | 1].bC, seg[id << 1].b + seg[id << 1 | 1].C}); seg[id].bc = max({seg[id << 1].bc, seg[id << 1 | 1].bc, seg[id << 1].b + seg[id << 1 | 1].c}); seg[id].AbC = max({seg[id << 1].AbC, seg[id << 1 | 1].AbC, seg[id << 1].A + seg[id << 1 | 1].bC, seg[id << 1].Ab + seg[id << 1 | 1].C}); seg[id].abc = max({seg[id << 1].abc, seg[id << 1 | 1].abc, seg[id << 1].a + seg[id << 1 | 1].bc, seg[id << 1].ab + seg[id << 1 | 1].c}); } int U[N], V[N]; int main() { scanf("%d", &n); int u, v, w; for (int i = 1; i < n; i++) { scanf("%d%d%d", &u, &v, &w); G[u].push_back({v, w}); G[v].push_back({u, w}); U[i] = u; V[i] = v; } DFS(1, -1); int m, id; scanf("%d", &m); int x; Build(1, 0, p); for (int i = 0; i < m; i++) { scanf("%d", &id); if (dep[U[id]] > dep[V[id]]) x = U[id]; else x = V[id]; Rev(1, st[x], fn[x], 0, p); printf("%d\n", max({0, seg[1].abc, seg[1].AbC})); } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 9; const long long int MOD = 1e9 + 696969; mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } const long long INF = 1e18; const int maxn = 500100; namespace atcoder { namespace internal { int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } int bsf(unsigned int n) { return __builtin_ctz(n); } } // namespace internal } // namespace atcoder namespace atcoder { template <class S, S (*op)(S, S), S (*e)(), class F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()> struct lazy_segtree { public: lazy_segtree() : lazy_segtree(0) {} lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {} lazy_segtree(const std::vector<S> &v) : _n(int(v.size())) { log = internal::ceil_pow2(_n); size = 1 << log; d = std::vector<S>(2 * size, e()); lz = std::vector<F>(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push(r >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template <bool (*g)(S)> int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template <class G> int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template <bool (*g)(S)> int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template <class G> int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } private: int _n, size, log; std::vector<S> d; std::vector<F> lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder using namespace atcoder; int n, d[maxn]; bool odw[maxn]; vector<int> v[maxn]; int init[maxn]; int A[maxn], B[maxn]; int pre[maxn], maxpre[maxn]; int DL; int zap[maxn], q; struct S { int zero, one; }; using F = bool; S op(S l, S r) { return S{max(l.zero, r.zero), max(l.one, r.one)}; } S e() { return S{0, 0}; } S mapping(F l, S r) { if (!l) return r; return S{r.one, r.zero}; } F composition(F l, F r) { return (l ^ r); } F id() { return false; } void initdfs(int x) { odw[x] = 1; for (auto u : v[x]) { if (!odw[u]) { d[u] = d[x] + 1; initdfs(u); } } } void dfs(int x) { odw[x] = 1; pre[x] = ++DL; maxpre[x] = pre[x]; for (auto u : v[x]) { if (!odw[u]) { d[u] = d[x] + 1; dfs(u); maxpre[x] = max(maxpre[x], maxpre[u]); } } } void toggle(int edge, lazy_segtree<S, op, e, F, mapping, composition, id> &seg) { int node = B[edge]; int a = pre[node] - 1, b = maxpre[node]; seg.apply(a, b, true); } vector<int> solve(int root) { 0 && cerr; DL = 0; for (int i = (1); i <= (n); ++i) odw[i] = 0, d[i] = 0, maxpre[i] = 0, pre[i] = 0; dfs(root); for (int i = (1); i <= (n - 1); ++i) { if (d[A[i]] > d[B[i]]) swap(A[i], B[i]); } vector<int> res(q + 1, 0); vector<S> vec(n); for (int i = (1); i <= (n); ++i) { int kt = pre[i]; vec[kt - 1] = {d[i], 0}; } lazy_segtree<S, op, e, F, mapping, composition, id> seg(vec); for (int i = (1); i <= (n - 1); ++i) { if (init[i]) toggle(i, seg); } for (int i = (1); i <= (q); ++i) { toggle(zap[i], seg); S wyn = seg.all_prod(); res[i] = wyn.zero; } return res; } int main() { { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; cin >> n; for (int i = (1); i <= (n - 1); ++i) { cin >> A[i] >> B[i] >> init[i]; v[A[i]].push_back(B[i]); v[B[i]].push_back(A[i]); } cin >> q; for (int i = (1); i <= (q); ++i) cin >> zap[i]; for (int i = (1); i <= (n); ++i) odw[i] = 0, d[i] = 0; initdfs(1); int opt = 1; for (int i = (2); i <= (n); ++i) { if (d[i] > d[opt]) opt = i; } int one_end = opt; for (int i = (1); i <= (n); ++i) odw[i] = 0, d[i] = 0; initdfs(opt); opt = 1; for (int i = (2); i <= (n); ++i) { if (d[i] > d[opt]) opt = i; } int second_end = opt; auto a1 = solve(one_end); auto a2 = solve(second_end); for (int i = (1); i <= (q); ++i) cout << max(a1[i], a2[i]) << "\n"; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; struct Edge { int u, v, w; } e[N]; vector<int> g[N]; struct SegmentTree { int tg[N << 2], mx[N << 2][2]; int l[N], r[N], rk[N], fa[N], dep[N], c[N], n; void build(int k, int l, int r) { tg[k] = 0; if (l == r) { mx[k][c[rk[l]]] = dep[rk[l]]; mx[k][c[rk[l]] ^ 1] = 0; return; } int lk = k << 1, rk = k << 1 | 1, mid = (l + r) >> 1; build(lk, l, mid); build(rk, mid + 1, r); mx[k][0] = max(mx[lk][0], mx[rk][0]); mx[k][1] = max(mx[lk][1], mx[rk][1]); } void update(int a, int b, int k, int l, int r) { if (a > r || b < l) return; if (a <= l && r <= b) { tg[k] ^= 1; swap(mx[k][0], mx[k][1]); return; } int lk = k << 1, rk = k << 1 | 1, mid = (l + r) >> 1; if (tg[k]) { tg[k] = 0; tg[lk] ^= 1; tg[rk] ^= 1; swap(mx[lk][0], mx[lk][1]); swap(mx[rk][0], mx[rk][1]); } update(a, b, lk, l, mid); update(a, b, rk, mid + 1, r); mx[k][0] = max(mx[lk][0], mx[rk][0]); mx[k][1] = max(mx[lk][1], mx[rk][1]); } int query(int k) { int u = e[k].u, v = e[k].v; if (fa[u] == v) swap(u, v); update(l[v], r[v], 1, 1, n); return mx[1][0]; } void dfs(int u, int f) { l[u] = ++n; fa[u] = f; rk[n] = u; for (auto i : g[u]) { int v = e[i].u ^ e[i].v ^ u; if (v == f) continue; dep[v] = dep[u] + 1; c[v] = c[u] ^ e[i].w; dfs(v, u); } r[u] = n; } } tx, ty; int n, m, x, y; int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].w); g[e[i].u].push_back(i); g[e[i].v].push_back(i); } tx.dep[1] = tx.c[1] = tx.n = 0; tx.dfs(1, 1); x = 1; for (int i = 1; i <= n; ++i) if (tx.dep[i] > tx.dep[x]) x = i; tx.dep[x] = tx.c[x] = tx.n = 0; tx.dfs(x, x); y = 1; for (int i = 1; i <= n; ++i) if (tx.dep[i] > tx.dep[y]) y = i; ty.dep[y] = ty.c[y] = ty.n = 0; ty.dfs(y, y); tx.build(1, 1, n); ty.build(1, 1, n); scanf("%d", &m); while (m--) { int o; scanf("%d", &o); printf("%d\n", max(tx.query(o), ty.query(o))); } }
#include <bits/stdc++.h> using namespace std; int n; int a[500005]; struct EDGE { int to; EDGE *las; int id; }; EDGE e[500005 * 2]; int ne; EDGE *last[500005]; void link(int u, int v, int id) { e[ne] = {v, last[u], id}; last[u] = e + ne++; } struct Graph { int rt; int dep[500005], val[500005], in[500005], out[500005], re[500005], cnt; int rel[500005]; int mx[500005 * 4][2], tag[500005 * 4]; void predfs(int x, int fa) { in[x] = ++cnt; re[cnt] = x; for (EDGE *ei = last[x]; ei; ei = ei->las) if (ei->to != fa) { rel[ei->id] = ei->to; dep[ei->to] = dep[x] + 1; val[ei->to] = val[x] ^ a[ei->id]; predfs(ei->to, x); } out[x] = cnt; } void pd(int k) { if (tag[k]) { swap(mx[k << 1][0], mx[k << 1][1]); swap(mx[k << 1 | 1][0], mx[k << 1 | 1][1]); tag[k << 1] ^= 1, tag[k << 1 | 1] ^= 1; tag[k] = 0; } } void upd(int k) { mx[k][0] = max(mx[k << 1][0], mx[k << 1 | 1][0]); mx[k][1] = max(mx[k << 1][1], mx[k << 1 | 1][1]); } void build(int k, int l, int r) { if (l == r) { mx[k][val[re[l]] & 1] = dep[re[l]]; mx[k][val[re[l]] & 1 ^ 1] = -1; return; } int mid = l + r >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); upd(k); } void modify(int k, int l, int r, int st, int en) { if (st <= l && r <= en) { swap(mx[k][0], mx[k][1]); tag[k] ^= 1; return; } pd(k); int mid = l + r >> 1; if (st <= mid) modify(k << 1, l, mid, st, en); if (mid < en) modify(k << 1 | 1, mid + 1, r, st, en); upd(k); } int query() { return mx[1][0]; } void init() { predfs(rt, 0); build(1, 1, n); } } S, T; queue<int> q; void find() { static int dis[500005]; q.push(1); dis[1] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (EDGE *ei = last[x]; ei; ei = ei->las) if (!dis[ei->to]) dis[ei->to] = dis[x] + 1, q.push(ei->to); } S.rt = 1; for (int i = 2; i <= n; ++i) if (dis[i] > dis[S.rt]) S.rt = i; memset(dis, 0, sizeof dis); q.push(S.rt); dis[S.rt] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (EDGE *ei = last[x]; ei; ei = ei->las) if (!dis[ei->to]) dis[ei->to] = dis[x] + 1, q.push(ei->to); } T.rt = 1; for (int i = 2; i <= n; ++i) if (dis[i] > dis[T.rt]) T.rt = i; } int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { int u, v; scanf("%d%d%d", &u, &v, &a[i]); link(u, v, i); link(v, u, i); } find(); S.init(); T.init(); int Q; scanf("%d", &Q); while (Q--) { int x; scanf("%d", &x); S.modify(1, 1, n, S.in[S.rel[x]], S.out[S.rel[x]]); T.modify(1, 1, n, T.in[T.rel[x]], T.out[T.rel[x]]); printf("%d\n", max(S.query(), T.query())); } return 0; }
#include <bits/stdc++.h> using namespace std; const int Nmax = 500010; struct segTree { struct Node { int even, odd, lazy; }; Node arb[4 * Nmax]; int euler[Nmax], niv[Nmax]; void propag(int nod, int st, int dr) { if (arb[nod].lazy == 1) { swap(arb[nod].even, arb[nod].odd); if (st < dr) { arb[nod * 2].lazy ^= 1; arb[nod * 2 + 1].lazy ^= 1; } arb[nod].lazy = 0; } } void recalc(int nod) { arb[nod].even = max(arb[nod * 2].even, arb[nod * 2 + 1].even); arb[nod].odd = max(arb[nod * 2].odd, arb[nod * 2 + 1].odd); } void init(int nod, int st, int dr) { if (st == dr) { if (niv[euler[st]] >= 0) { arb[nod].even = niv[euler[st]]; arb[nod].odd = 0; arb[nod].lazy = 0; } else { arb[nod].odd = -niv[euler[st]]; arb[nod].even = 0; arb[nod].lazy = 0; } return; } int mid = (st + dr) / 2; init(nod * 2, st, mid); init(nod * 2 + 1, mid + 1, dr); recalc(nod); arb[nod].lazy = 0; } void update(int nod, int st, int dr, int left, int right) { if (left <= st && dr <= right) { arb[nod].lazy ^= 1; propag(nod, st, dr); return; } int mid = (st + dr) / 2; propag(nod, st, dr); if (left <= mid) update(nod * 2, st, mid, left, right); else propag(nod * 2, st, mid); if (mid < right) update(nod * 2 + 1, mid + 1, dr, left, right); else propag(nod * 2 + 1, mid + 1, dr); recalc(nod); } int query(int nod, int st, int dr, int left, int right) { propag(nod, st, dr); if (left <= st && dr <= right) return arb[nod].even; int mid = (st + dr) / 2, s = 0; if (left <= mid) s = query(nod * 2, st, mid, left, right); if (mid < right) s = max(s, query(nod * 2 + 1, mid + 1, dr, left, right)); return s; } }; struct edge { int vec, c, id; }; vector<edge> v[Nmax]; segTree t; int nodA, nodB, nivmax, l, first[Nmax], last[Nmax], sum[Nmax], dad[Nmax], qry[Nmax], ans[Nmax], n, m; void dfs(int nod, int lvl, int tata) { if (lvl > nivmax) { nivmax = lvl; nodB = nod; } for (auto vec : v[nod]) if (vec.vec != tata) dfs(vec.vec, lvl + 1, nod); } void dfs1(int nod, int lvl, int tata) { t.euler[++l] = nod; first[nod] = l; if (sum[nod] % 2) t.niv[nod] = -lvl; else t.niv[nod] = lvl; for (auto x : v[nod]) if (x.vec != tata) { dad[x.id] = x.vec; sum[x.vec] = sum[nod] + x.c; dfs1(x.vec, lvl + 1, nod); } last[nod] = l; } void solve(int root) { l = 0; sum[root] = 0; dfs1(root, 0, 0); t.init(1, 1, n); for (int i = 1; i <= m; ++i) { t.update(1, 1, n, first[dad[qry[i]]], last[dad[qry[i]]]); ans[i] = max(ans[i], t.arb[1].even); } } int main() { int x, y, c; scanf("%d", &n); for (int i = 1; i < n; ++i) { scanf("%d%d%d", &x, &y, &c); v[x].push_back({y, c, i}); v[y].push_back({x, c, i}); } scanf("%d", &m); for (int i = 1; i <= m; ++i) scanf("%d", &qry[i]); nivmax = -1; dfs(1, 0, 0); nodA = nodB; nivmax = -1; dfs(nodA, 0, 0); solve(nodA); solve(nodB); for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const long long INF = (long long)2e18; const int MOD = 998244353; int _abs(int x) { return x < 0 ? -x : x; } int add(int x, int y) { x += y; return x >= MOD ? x - MOD : x; } int sub(int x, int y) { x -= y; return x < 0 ? x + MOD : x; } void Add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } void Sub(int &x, int y) { x -= y; if (x < 0) x += MOD; } void Mul(int &x, int y) { x = (long long)(x) * (y) % MOD; } int qpow(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = (long long)(ret) * (x) % MOD; x = (long long)(x) * (x) % MOD; y >>= 1; } return ret; } void checkmin(int &x, int y) { if (x > y) x = y; } void checkmax(int &x, int y) { if (x < y) x = y; } void checkmin(long long &x, long long y) { if (x > y) x = y; } void checkmax(long long &x, long long y) { if (x < y) x = y; } inline int read() { int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } const int N = 500005; int n, m, d[N], mxdep, mxid; vector<pair<int, int> > v[N]; pair<int, int> E[N]; void dfs1(int u, int f) { if (mxdep < d[u]) mxdep = d[u], mxid = u; for (auto &e : v[u]) { int to = e.first, w = e.second; if (to == f) continue; d[to] = d[u] + 1; dfs1(to, u); } } struct Tree { int a[N], dep[N], rt; int dfn[N], out[N], id[N], dfn_clock = 0; int rev[N << 2], mx0[N << 2], mx1[N << 2]; void pushup(int x) { mx0[x] = max(mx0[x << 1], mx0[x << 1 | 1]); mx1[x] = max(mx1[x << 1], mx1[x << 1 | 1]); } void pushdown(int x) { if (rev[x]) { rev[x << 1] ^= rev[x]; rev[x << 1 | 1] ^= rev[x]; rev[x] = 0; swap(mx0[x << 1], mx1[x << 1]); swap(mx0[x << 1 | 1], mx1[x << 1 | 1]); } } void build(int x, int l, int r) { if (l == r) { if (a[id[l]] == 0) mx0[x] = dep[id[l]], mx1[x] = -inf; else mx0[x] = -inf, mx1[x] = dep[id[l]]; return; } int mid = (l + r) >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); pushup(x); } void update(int x, int l, int r, int L, int R) { if (L <= l && r <= R) { rev[x] ^= 1; swap(mx0[x], mx1[x]); return; } int mid = (l + r) >> 1; pushdown(x); if (mid >= L) update(x << 1, l, mid, L, R); if (mid < R) update(x << 1 | 1, mid + 1, r, L, R); pushup(x); } void dfs(int u, int f) { dfn[u] = ++dfn_clock; id[dfn_clock] = u; for (auto &e : v[u]) { int to = e.first, w = e.second; if (to == f) continue; dep[to] = dep[u] + 1; a[to] = (a[u] ^ w); dfs(to, u); } out[u] = dfn_clock; } void init() { dep[rt] = 0; dfs(rt, -1); build(1, 1, n); } void change(int x, int y) { if (dep[x] > dep[y]) swap(x, y); update(1, 1, n, dfn[y], out[y]); } } T1, T2; int main() { n = read(); for (int i = 1; i < n; i++) { int x = read(), y = read(), z = read(); E[i] = make_pair(x, y); v[x].push_back(make_pair(y, z)); v[y].push_back(make_pair(x, z)); } mxdep = -1; dfs1(1, -1); T1.rt = mxid; d[mxid] = 0; mxdep = -1; dfs1(mxid, -1); T2.rt = mxid; T1.init(); T2.init(); m = read(); while (m--) { int x = read(); T1.change(E[x].first, E[x].second); T2.change(E[x].first, E[x].second); int ans = max(T1.mx0[1], T2.mx0[1]); printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int qmin(int a, int b) { return (a < b) ? a : b; } int qmax(int a, int b) { return (a > b) ? a : b; } void readint(int &x) { x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); } int n; pair<pair<int, int>, int> road[500500]; vector<pair<int, int> > v[500500]; pair<int, int> getfar(int now, int dist, int par) { pair<int, int> ans = make_pair(dist, now); for (int i = 0; i < (int)v[now].size(); i++) { if (v[now][i].first != par) { pair<int, int> nxt = getfar(v[now][i].first, dist + 1, now); if (nxt > ans) ans = nxt; } } return ans; } pair<int, int> get_diameter() { int pos = getfar(0, 0, -1).second; int nxt_pos = getfar(pos, 0, -1).second; return make_pair(pos, nxt_pos); } int dfn_l[2][500500], dfn_r[2][500500]; int ord[2][500500]; int dist[2][500500]; int fa[2][500500]; bool color[2][500500]; int dat[2][2000200][2]; bool rev[2][2000200]; void pushdown(int tp, int k) { if (!rev[tp][k]) return; rev[tp][k + k] ^= 1, rev[tp][k + k + 1] ^= 1; swap(dat[tp][k + k][0], dat[tp][k + k][1]); swap(dat[tp][k + k + 1][0], dat[tp][k + k + 1][1]); rev[tp][k] = 0; } void segtree_flip(int tp, int tl, int tr, int l, int r, int k) { if (l > tr || r < tl) return; if (tl <= l && r <= tr) { rev[tp][k] ^= 1; swap(dat[tp][k][0], dat[tp][k][1]); return; } pushdown(tp, k); int mid = (l + r) >> 1; segtree_flip(tp, tl, tr, l, mid, k + k); segtree_flip(tp, tl, tr, mid + 1, r, k + k + 1); dat[tp][k][0] = qmax(dat[tp][k + k][0], dat[tp][k + k + 1][0]); dat[tp][k][1] = qmax(dat[tp][k + k][1], dat[tp][k + k + 1][1]); } void buildtree(int tp, int l, int r, int k) { if (l > r) return; if (l == r) { dat[tp][k][color[tp][ord[tp][l]]] = dist[tp][ord[tp][l]]; return; } int mid = (l + r) >> 1; buildtree(tp, l, mid, k + k); buildtree(tp, mid + 1, r, k + k + 1); dat[tp][k][0] = qmax(dat[tp][k + k][0], dat[tp][k + k + 1][0]); dat[tp][k][1] = qmax(dat[tp][k + k][1], dat[tp][k + k + 1][1]); } int cnt = 0; void dfs(int tp, int now, int par) { ord[tp][cnt] = now; dfn_l[tp][now] = cnt++; for (int i = 0; i < (int)v[now].size(); i++) { if (v[now][i].first != par) { dist[tp][v[now][i].first] = dist[tp][now] + 1; color[tp][v[now][i].first] = color[tp][now] ^ v[now][i].second; fa[tp][v[now][i].first] = now; dfs(tp, v[now][i].first, now); } } dfn_r[tp][now] = cnt; } void build_segtree(int now, int tp) { cnt = 0; dfs(tp, now, -1); buildtree(tp, 0, n - 1, 1); } int get_ans(int tp, int edge) { int son; if (road[edge].first.first == fa[tp][road[edge].first.second]) son = road[edge].first.second; else son = road[edge].first.first; segtree_flip(tp, dfn_l[tp][son], dfn_r[tp][son] - 1, 0, n - 1, 1); return dat[tp][1][0]; } int main() { readint(n); for (int i = 0; i < n - 1; i++) { readint(road[i].first.first), readint(road[i].first.second), readint(road[i].second); road[i].first.first--, road[i].first.second--; v[road[i].first.first].push_back( make_pair(road[i].first.second, road[i].second)); v[road[i].first.second].push_back( make_pair(road[i].first.first, road[i].second)); } pair<int, int> d = get_diameter(); memset(dat, 0, sizeof(dat)); build_segtree(d.first, 0); build_segtree(d.second, 1); int m; readint(m); for (int i = 0; i < m; i++) { int num; readint(num); num--; cout << qmax(get_ans(0, num), get_ans(1, num)) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; const int M = 8e5 + 5; const int inf = 0x3f3f3f3f; const long long mod = 1e8 + 7; const double eps = 1e-8; const long double pi = acos(-1.0L); long long read() { long long x = 0, t = 1; char ch; while (!isdigit(ch = getchar())) if (ch == '-') t = -1; while (isdigit(ch)) { x = 10 * x + ch - '0'; ch = getchar(); } return x * t; } vector<pair<int, int> > e[N]; struct edge { int u, v, w; } a[N]; int rt1, rt2, ma; pair<int, int> ma1[N], ma2[N]; struct node { int c[N << 2][2], lz[N << 2], in[N], out[N], rk[N], dep[N], col[N], tot; inline void pushdown(int i) { swap(c[(i << 1)][0], c[(i << 1)][1]); swap(c[(i << 1 | 1)][0], c[(i << 1 | 1)][1]); lz[(i << 1)] ^= 1; lz[(i << 1 | 1)] ^= 1; lz[i] = 0; } inline void pushup(int i) { c[i][0] = max(c[(i << 1)][0], c[(i << 1 | 1)][0]); c[i][1] = max(c[(i << 1)][1], c[(i << 1 | 1)][1]); } void build(int i, int l, int r) { c[i][0] = c[i][1] = lz[i] = 0; if (l == r) return (void)(c[i][col[rk[l]]] = dep[rk[l]]); int mid = l + r >> 1; build((i << 1), l, mid); build((i << 1 | 1), mid + 1, r); pushup(i); } void update(int i, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) return (void)(swap(c[i][0], c[i][1]), lz[i] ^= 1); int mid = l + r >> 1; if (lz[i]) pushdown(i); if (mid >= ll) update((i << 1), l, mid, ll, rr); if (mid < rr) update((i << 1 | 1), mid + 1, r, ll, rr); pushup(i); } void dfs(int u, int pre) { in[u] = ++tot; rk[tot] = u; dep[u] = dep[pre] + 1; for (auto x : e[u]) { int v = x.first, w = x.second; if (v == pre) continue; col[v] = col[u] ^ w; dfs(v, u); } out[u] = tot; } } tr[2]; void dfs2(int u, int pre) { ma1[u] = ma2[u] = make_pair(0, u); for (auto x : e[u]) { int v = x.first; if (v == pre) continue; dfs2(v, u); pair<int, int> t = ma1[v]; if (t > ma1[u]) swap(t, ma1[u]); if (t > ma2[u]) ma2[u] = t; } if (ma1[u].first + ma2[u].first > ma) ma = ma1[u].first + ma2[u].first, rt1 = ma1[u].second, rt2 = ma2[u].second; ma1[u].first++; ma2[u].first++; } int main() { int n = read(); for (int i = 1; i < n; i++) { int x = read(), y = read(), z = read(); a[i] = edge{x, y, z}; e[x].emplace_back(y, z); e[y].emplace_back(x, z); } dfs2(1, 0); tr[0].dfs(rt1, 0); tr[0].build(1, 1, n); tr[1].dfs(rt2, 0); tr[1].build(1, 1, n); int m = read(); for (int i = 1; i <= m; i++) { int x = read(), y = a[x].u; if (tr[0].dep[a[x].u] < tr[0].dep[a[x].v]) y = a[x].v; tr[0].update(1, 1, n, tr[0].in[y], tr[0].out[y]); y = a[x].u; if (tr[1].dep[a[x].u] < tr[1].dep[a[x].v]) y = a[x].v; tr[1].update(1, 1, n, tr[1].in[y], tr[1].out[y]); printf("%d\n", max(tr[0].c[1][0], tr[1].c[1][0]) - 1); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <class T> inline bool setmin(T &a, T b) { if (a > b) return a = b, 1; return 0; } template <class T> inline bool setmax(T &a, T b) { if (a < b) return a = b, 1; return 0; } template <class T> inline T fast(T a, T b, T mod) { long long res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } template <class T> istream &operator>>(istream &os, vector<T> &container) { for (auto &u : container) os >> u; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &container) { for (auto &u : container) os << u << " "; return os; } template <class T> inline T gcd(T a, T b) { while (b) swap(a %= b, b); return abs(a); } const long long INF = 1e9 + 7; const long long mod = 998244353; const long long BIG_INF = 1e18 + 7; const long long N = 1e6 + 7; const long long T = 1 << 19; const long double inf = 1e18; const long double eps = 1e-14; long long n, m, k; vector<vector<pair<int, int> > > G(N); int najd = 1, dist, cnt_pre; vector<int> preorder(N), glebokosc(N), rev_pre(N), kolor(N); vector<pair<int, int> > przedzial_na_drzewie(N); struct Tree { vector<int> merge(vector<int> &a, vector<int> &b) { return {max(a[0], b[0]), max(a[1], b[1])}; } vector<vector<int> > tree; vector<int> lazy; Tree() { tree = vector<vector<int> >(T * 2, vector<int>(2, -1)); lazy = vector<int>(T * 2, false); for (int i = 1; i <= cnt_pre; i++) tree[i + T][kolor[rev_pre[i]]] = glebokosc[rev_pre[i]]; for (int i = T - 1; i >= 0; i--) tree[i] = merge(tree[i * 2], tree[i * 2 + 1]); }; void push(int v) { if (lazy[v]) { swap(tree[v][0], tree[v][1]); if (v < T) { lazy[v * 2] ^= 1; lazy[v * 2 + 1] ^= 1; } lazy[v] = 0; } } void flip(int a, int b, int u = 1, int lo = 0, int hi = T - 1) { push(u); if (hi < a or b < lo) return; if (a <= lo and hi <= b) { lazy[u] ^= 1; push(u); return; } int mid = (lo + hi) / 2; flip(a, b, u * 2, lo, mid); flip(a, b, u * 2 + 1, mid + 1, hi); tree[u] = merge(tree[u * 2], tree[u * 2 + 1]); } void flip(pair<int, int> kraw) { int v = (glebokosc[kraw.first] < glebokosc[kraw.second] ? kraw.second : kraw.first); flip(przedzial_na_drzewie[v].first, przedzial_na_drzewie[v].second); } int ask() { return tree[1][0]; } }; void dfs2(int second, int p = -1, int gl = 0, int kol = 0) { if (p == -1) cnt_pre = 0; preorder[second] = ++cnt_pre; rev_pre[cnt_pre] = second; glebokosc[second] = gl; kolor[second] = kol; for (auto &u : G[second]) if (u.first != p) dfs2(u.first, second, gl + 1, kol ^ u.second); przedzial_na_drzewie[second] = {preorder[second], cnt_pre}; } vector<int> solve(vector<pair<int, int> > &kraw, vector<int> zap, int root) { dfs2(root); Tree tree; vector<int> ret; for (auto &u : zap) { tree.flip(kraw[u]); ret.push_back(tree.ask()); } return ret; } void dfs(int second = najd, int gl = 0, int przodek = -1) { if (gl == 0) dist = -1; if (gl > dist) { dist = gl; najd = second; } for (auto &u : G[second]) if (u.first != przodek) dfs(u.first, gl + 1, second); } void solve() { cin >> n; vector<pair<int, int> > kraw(n, {-1, -1}); for (int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); kraw[i] = {a, b}; } vector<int> konce; dfs(); konce.push_back(najd); dfs(); konce.push_back(najd); cin >> m; vector<int> zapytania(m); cin >> zapytania; vector<int> A = solve(kraw, zapytania, konce[0]); vector<int> B = solve(kraw, zapytania, konce[1]); for (int i = 0; i < m; i++) cout << max(A[i], B[i]) << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) solve(); }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; inline int read() { int x = 0, f = 1, c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f == 1 ? x : -x; } const int N = 5e5 + 4; struct edge { int v, w, nxt; } e[N << 1]; int first[N], cnt = 1; inline void addedge(int u, int v, int w) { e[++cnt] = (edge){v, w, first[u]}; first[u] = cnt; } int n, Q, tim, siz[N], dep[N], son[N], top[N], idx[N], dfn[N], fa[N], dis[N], iw[N]; void dfs_1(int x) { siz[x] = 1; idx[dfn[x] = ++tim] = x; for (int i = first[x], v; i; i = e[i].nxt) { v = e[i].v; if (v == fa[x]) continue; fa[v] = x; dep[v] = dep[x] + 1; dis[v] = dis[x] ^ e[i].w; iw[i >> 1] = v; dfs_1(v); siz[x] += siz[v]; if (siz[v] > siz[son[x]]) son[x] = v; } } int mn[N << 1][20], lg[N << 1], st[N]; void dfs_2(int x) { mn[st[x] = ++tim][0] = x; for (int i = first[x], v; i; i = e[i].nxt) { v = e[i].v; if (v == fa[x]) continue; dfs_2(v); mn[++tim][0] = x; } } inline int cmp(int x, int y) { return dep[x] < dep[y] ? x : y; } inline void prepare() { tim = 0; dfs_2(1); for (int i = 2; i <= tim; i++) lg[i] = lg[i >> 1] + 1; for (int j = 1; j <= lg[tim]; j++) for (int i = 1, r = 1 << j; r <= tim; i++, r++) mn[i][j] = cmp(mn[i][j - 1], mn[i + (1 << j - 1)][j - 1]); } inline int getlca(int x, int y) { x = st[x]; y = st[y]; if (x > y) x ^= y ^= x ^= y; int k = lg[y - x + 1]; return cmp(mn[x][k], mn[y - (1 << k) + 1][k]); } struct node { int p1, p2, w; node() {} node(int x, int y, int v) : p1(x), p2(y), w(v) {} }; inline bool upd(int &x, int v) { if (x < v) { x = v; return 1; } return 0; } inline int distan(int x, int y) { return (!x || !y) ? -1 : dep[x] + dep[y] - dep[getlca(x, y)] * 2; } inline node operator+(const node &a, const node &b) { node ret = a.w > b.w ? a : b; if (upd(ret.w, distan(a.p1, b.p1))) { ret.p1 = a.p1; ret.p2 = b.p1; } if (upd(ret.w, distan(a.p1, b.p2))) { ret.p1 = a.p1; ret.p2 = b.p2; } if (upd(ret.w, distan(a.p2, b.p1))) { ret.p1 = a.p2; ret.p2 = b.p1; } if (upd(ret.w, distan(a.p2, b.p2))) { ret.p1 = a.p2; ret.p2 = b.p2; } return ret; } namespace seg { node t[N << 2][2]; int lz[N << 2]; inline void pushup(int p) { t[p][0] = t[(p << 1)][0] + t[(p << 1 | 1)][0]; t[p][1] = t[(p << 1)][1] + t[(p << 1 | 1)][1]; } inline void pushnow(int p) { swap(t[p][0], t[p][1]); lz[p] ^= 1; } inline void pushdown(int p) { if (!lz[p]) return; pushnow((p << 1)); pushnow((p << 1 | 1)); lz[p] ^= 1; } void build(int p, int l, int r) { if (l == r) { t[p][dis[idx[r]]].p1 = t[p][dis[idx[r]]].p2 = idx[r]; t[p][dis[idx[r]] ^ 1].w = -1; return; } build((p << 1), l, ((l + r) >> 1)); build((p << 1 | 1), ((l + r) >> 1) + 1, r); pushup(p); } void modify(int p, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) { pushnow(p); return; } pushdown(p); if (ql <= ((l + r) >> 1)) modify((p << 1), l, ((l + r) >> 1), ql, qr); if (((l + r) >> 1) < qr) modify((p << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr); pushup(p); } } // namespace seg int main() { n = read(); for (int i = 1, u, v, w; i < n; i++) { u = read(); v = read(); w = read(); addedge(u, v, w); addedge(v, u, w); } dfs_1(1); prepare(); seg::build(1, 1, n); for (int Q = read(), x; Q--;) { x = iw[read()]; seg::modify(1, 1, n, dfn[x], dfn[x] + siz[x] - 1); cout << max(seg::t[1][0].w, seg::t[1][1].w) << "\n"; } return (0 - 0); }
#include <bits/stdc++.h> using namespace std; int N; vector<pair<int, int>> graph[500005]; bool typ[500005]; int dist[500005]; void dfs(int n, int p) { for (auto e : graph[n]) { if (e.first != p) { dist[e.first] = dist[n] + 1; dfs(e.first, n); } } } struct EulerTourSegTree { struct Node { int l, r, lzy; int mx[2]; }; vector<Node> seg; vector<int> lft; vector<int> rht; vector<int> et; vector<int> dep; int t, rt; void dfs(int n, int p) { lft[p] = ++t; et[t] = n; for (auto e : graph[n]) { if (e.second == p) { continue; } dep[e.first] = dep[n] + 1; dfs(e.first, e.second); } rht[p] = t; } void build(int l, int r, int idx) { seg[idx].l = l; seg[idx].r = r; if (l == r) { seg[idx].mx[0] = dep[et[l]]; return; } int mid = l + r >> 1; build(l, mid, 2 * idx); build(mid + 1, r, 2 * idx + 1); seg[idx].mx[0] = max(seg[2 * idx].mx[0], seg[2 * idx + 1].mx[0]); } void upd(int l, int r, int idx) { if (seg[idx].l == l && seg[idx].r == r) { seg[idx].lzy ^= 1; swap(seg[idx].mx[0], seg[idx].mx[1]); return; } if (seg[idx].lzy) { for (int i = 2 * idx; i <= 2 * idx + 1; i++) { seg[i].lzy ^= 1; swap(seg[i].mx[0], seg[i].mx[1]); } seg[idx].lzy = 0; } int mid = seg[idx].l + seg[idx].r >> 1; if (r <= mid) { upd(l, r, 2 * idx); } else if (l > mid) { upd(l, r, 2 * idx + 1); } else { upd(l, mid, 2 * idx); upd(mid + 1, r, 2 * idx + 1); } seg[idx].mx[0] = max(seg[2 * idx].mx[0], seg[2 * idx + 1].mx[0]); seg[idx].mx[1] = max(seg[2 * idx].mx[1], seg[2 * idx + 1].mx[1]); } void updhelper(int ed) { upd(lft[ed], rht[ed], 1); } EulerTourSegTree(int R) { rt = R; seg.resize(4 * N + 5); lft.resize(N + 5); rht.resize(N + 5); dep.resize(N + 5); et.resize(N + 5); t = 0; dfs(R, 0); build(1, N, 1); } EulerTourSegTree() {} }; EulerTourSegTree etseg[2]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; for (int i = 1; i < N; i++) { int a, b, t; cin >> a >> b >> t; typ[i] = t; graph[a].emplace_back(b, i); graph[b].emplace_back(a, i); } dfs(1, 0); int d1 = max_element(dist, dist + 1 + N) - dist; etseg[0] = EulerTourSegTree(d1); int d2 = max_element(etseg[0].dep.begin(), etseg[0].dep.end()) - etseg[0].dep.begin(); etseg[1] = EulerTourSegTree(d2); for (int i = 1; i < N; i++) { if (typ[i]) { etseg[0].updhelper(i); etseg[1].updhelper(i); } } int Q; cin >> Q; while (Q--) { int ed; cin >> ed; etseg[0].updhelper(ed); etseg[1].updhelper(ed); cout << max(etseg[0].seg[1].mx[0], etseg[1].seg[1].mx[0]) << "\n"; } }
#include <bits/stdc++.h> using namespace std; int n, x[500015], y[500015], val[500015], max1[500015], dep[500015], cnt, m; struct edge { int to, next, val; edge() {} edge(int a, int b, int c) { to = a; next = b; val = c; } } e[500015 << 1]; int head[500015]; void add(int u, int v, int w) { e[++cnt] = edge(v, head[u], w); head[u] = cnt; } void dfs1(int u, int fa) { dep[u] = dep[fa] + 1; max1[u] = u; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa) continue; dfs1(v, u); if (dep[max1[v]] > dep[max1[u]]) { max1[u] = max1[v]; } } } struct seg { int dfn[500015], rnk[500015], siz[500015], dis[500015], typ[500015], f[500015], Max[500015 << 2][2], lazy[500015 << 2], clk; void dfs(int u, int fa) { dfn[u] = ++clk; rnk[clk] = u; siz[u] = 1; dis[u] = dis[fa] + 1; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa) continue; f[v] = u; typ[v] = typ[u] ^ e[i].val; dfs(v, u); siz[u] += siz[v]; } } void pushup(int p) { for (int i = 0; i <= 1; i++) Max[p][i] = max(Max[(p << 1)][i], Max[(p << 1 | 1)][i]); } void down(int p) { lazy[p] ^= 1; swap(Max[p][0], Max[p][1]); } void pushdown(int p) { if (lazy[p]) { down((p << 1)); down((p << 1 | 1)); lazy[p] = 0; } } void build(int p, int l, int r) { if (l == r) { Max[p][typ[rnk[l]]] = dis[rnk[l]]; return; } int mid = (l + r) >> 1; build((p << 1), l, mid); build((p << 1 | 1), mid + 1, r); pushup(p); } void change(int p, int l, int r, int x, int y) { if (x <= l && r <= y) { down(p); return; } int mid = (l + r) >> 1; pushdown(p); if (x <= mid) change((p << 1), l, mid, x, y); if (y > mid) change((p << 1 | 1), mid + 1, r, x, y); pushup(p); } int query() { return Max[1][0]; } } t1, t2; int main() { memset(head, -1, sizeof head); scanf("%d", &n); for (int i = 1; i <= n - 1; i++) scanf("%d%d%d", &x[i], &y[i], &val[i]), add(x[i], y[i], val[i]), add(y[i], x[i], val[i]); dfs1(1, 0); int r1 = max1[1]; dfs1(r1, 0); int r2 = max1[r1]; t1.dfs(r1, 0); t2.dfs(r2, 0); t1.build(1, 1, n); t2.build(1, 1, n); scanf("%d", &m); for (int i = 1; i <= m; i++) { int uu; scanf("%d", &uu); if (t1.f[x[uu]] == y[uu]) t1.change(1, 1, n, t1.dfn[x[uu]], t1.dfn[x[uu]] + t1.siz[x[uu]] - 1); else t1.change(1, 1, n, t1.dfn[y[uu]], t1.dfn[y[uu]] + t1.siz[y[uu]] - 1); if (t2.f[x[uu]] == y[uu]) t2.change(1, 1, n, t2.dfn[x[uu]], t2.dfn[x[uu]] + t2.siz[x[uu]] - 1); else t2.change(1, 1, n, t2.dfn[y[uu]], t2.dfn[y[uu]] + t2.siz[y[uu]] - 1); printf("%d\n", max(t1.query(), t2.query()) - 1); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; vector<pair<int, int>> adj[500001]; pair<int, int> ma = {-1, -1}; void dfs(long long no, long long par = -1, long long lev = 0) { if (lev > ma.first) { ma = {lev, no}; } for (auto j : adj[no]) { if (j.first != par) { dfs(j.first, no, lev + 1); } } } int st[2][500001]; int endd[2][500001]; pair<int, int> val[2][500001]; int cur = 0; int co = 0; void dfs2(int no, int par2 = -1, int levv = 0, int val2 = 0) { st[cur][no] = co; val[cur][co] = {val2, levv}; co++; for (auto j : adj[no]) { if (j.first != par2) { dfs2(j.first, no, levv + 1, val2 ^ j.second); } } endd[cur][no] = co - 1; } int tree[2][500001 * 4][2]; int lazy[2][500001 * 4]; void build(int no, int l, int r) { if (l == r) { tree[cur][no][val[cur][l].first] = val[cur][l].second; } else { int mid = (l + r) / 2; build(no * 2 + 1, l, mid); build(no * 2 + 2, mid + 1, r); tree[cur][no][0] = max(tree[cur][no * 2 + 1][0], tree[cur][no * 2 + 2][0]); tree[cur][no][1] = max(tree[cur][no * 2 + 1][1], tree[cur][no * 2 + 2][1]); } } void update(int no, int l, int r, int aa, int bb) { if (lazy[cur][no]) { lazy[cur][no] = 0; if (l < r) { lazy[cur][no * 2 + 1] ^= 1; lazy[cur][no * 2 + 2] ^= 1; } swap(tree[cur][no][0], tree[cur][no][1]); } if (aa > r or bb < l) { return; } if (aa <= l and r <= bb) { swap(tree[cur][no][0], tree[cur][no][1]); if (l < r) { lazy[cur][no * 2 + 1] ^= 1; lazy[cur][no * 2 + 2] ^= 1; } } else { int mid = (l + r) / 2; update(no * 2 + 1, l, mid, aa, bb); update(no * 2 + 2, mid + 1, r, aa, bb); tree[cur][no][0] = max(tree[cur][no * 2 + 1][0], tree[cur][no * 2 + 2][0]); tree[cur][no][1] = max(tree[cur][no * 2 + 1][1], tree[cur][no * 2 + 2][1]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; vector<pair<int, int>> ed; for (int i = 0; i < n - 1; i++) { int aa, bb, cc; cin >> aa >> bb >> cc; aa--; bb--; adj[aa].push_back({bb, cc}); adj[bb].push_back({aa, cc}); ed.push_back({aa, bb}); } dfs(0); int x = ma.second; ma = {-1, -1}; dfs(x); int y = ma.second; cur = 0; dfs2(x); build(0, 0, n - 1); co = 0; cur = 1; dfs2(y); build(0, 0, n - 1); cin >> m; while (m--) { int ind; cin >> ind; ind--; pair<int, int> cc = {ed[ind].first, ed[ind].second}; if (st[0][cc.first] > st[0][cc.second]) { swap(cc.first, cc.second); } cur = 0; update(0, 0, n - 1, st[0][cc.second], endd[0][cc.second]); if (st[1][cc.first] > st[1][cc.second]) { swap(cc.first, cc.second); } cur = 1; update(0, 0, n - 1, st[1][cc.second], endd[1][cc.second]); cout << max(tree[0][0][0], tree[1][0][0]) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; string s = ""; if (abs(a3 - a4) > 1 || min(a1, a2) < max(a3, a4) || (a3 == a4 && max(a1, a2) <= a3)) { cout << -1; return 0; } else { if (a3 > a4) { while (a3--) { s += '4'; s += '7'; a1--; a2--; } } else if (a3 < a4) { while (a4--) { s += '7'; s += '4'; a1--; a2--; } } else { if (a1 == a3) { s += '7'; a2--; while (a3--) { s += '4'; s += '7'; a1--; a2--; } } else { s += '4'; a1--; while (a3--) { s += '7'; s += '4'; a1--; a2--; } } } } if (s[s.length() - 1] == '4') { for (int i = 0; i < s.length() - 1; i++) { if (s[i] == '4' && a1) { while (a1 > 0) { cout << '4'; a1--; } } cout << s[i]; } while (a2 > 0) { cout << '7'; a2--; } cout << 4; } else { while (a2 > 0) { s += '7'; a2--; } for (int i = 0; i < s.length(); i++) { if (s[i] == '4') { while (a1 > 0) { cout << '4'; a1--; } } cout << s[i]; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxint = -1u >> 1; template <class T> bool get_max(T& a, const T& b) { return b > a ? a = b, 1 : 0; } template <class T> bool get_min(T& a, const T& b) { return b < a ? a = b, 1 : 0; } const int maxn = 1000000 + 5; int n, m, r1, r2, sv[maxn]; void output() { for (int i = n; i >= 0; i--) { for (int j = 0; j < sv[i]; j++) { printf("7"); } if (i) printf("4"); } printf("\n"); } int main() { scanf("%d%d%d%d", &n, &m, &r1, &r2); memset(sv, 0, sizeof(sv)); for (int i = 1; i < n; i++) { if (r1 && r2) { if (!m) { printf("-1\n"); return 0; } else { r1--; r2--; sv[i]++; m--; } } } if (r1) { if (!m) { printf("-1\n"); return 0; } else { r1--; sv[0]++; m--; } } if (r2) { if (!m) { printf("-1\n"); return 0; } else { r2--; sv[n]++; m--; } } if (r1 + r2 != 0) { printf("-1\n"); return 0; } if (sv[0]) { sv[0] += m; } else { sv[1] += m; } output(); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4") using namespace std; const int inf = (int)2e9 + 99999, mod = (int)1e9 + 7; const long long linf = (long long)2e18 + 99999; int kob(int x, int y) { return ((long long)x * y) % mod; } int aza(int x, int y) { x -= y; if (x < 0) return x + mod; return x; } int kos(int x, int y) { x += y; if (x >= mod) return x - mod; return x; } const int maxn = (int)1e5 + 777, maxn2 = (int)1e6 + 55; int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; int a11 = a1, a22 = a2, a33 = a3, a44 = a4; a1 -= a3; a2 -= a3; bool bad = false; if (a3 <= a4) bad = 1, --a1; if (a3 < a4) --a2; if (abs(a3 - a4) >= 2 || a1 < 0 || a2 < 0) { if (a33 == a44 && a11 < a22) { a11 -= a33; a22 -= a33; a22--; if (a11 < 0 || a22 < 0) { cout << -1; exit(0); } for (int i = 1; i <= a33; ++i) cout << "74"; for (int i = 1; i <= a11; ++i) cout << 4; cout << 7; for (int i = 1; i <= a22; ++i) cout << 7; exit(0); } cout << "-1", exit(0); } if (a3 < a4) cout << '7'; for (int i = 1; i <= a1; ++i) cout << '4'; for (int i = 1; i <= a3; ++i) cout << "47"; for (int i = 1; i <= a2; ++i) cout << '7'; if (bad) cout << '4'; return 0; }
#include <bits/stdc++.h> int main(void) { int a1, a2, a3, a4; int i; scanf("%d%d%d%d", &a1, &a2, &a3, &a4); if (a3 > a4 + 1 || a4 > a3 + 1) { printf("-1"); return 0; } if (a1 >= a2 && (a3 > a2 || a4 > a2)) { printf("-1"); return 0; } if (a1 < a2 && (a3 > a1 || a4 > a1)) { printf("-1"); return 0; } if (a1 == a2 && a3 == a1 && a3 == a4) { printf("-1"); return 0; } if (a3 == a4) { if (a2 > a1 && a3 == a1) { for (i = 0; i < a1 + a2; i++) { if (i < a1 * 2) { printf("74"); i++; } if (a1 * 2 <= i && i < a1 + a2) printf("7"); } } else { for (i = 0; i < a1 + a2; i++) { if (i < a1 - a3) printf("4"); else if (a1 - a3 <= i && i <= a1 + a4 - 3) { printf("74"); i++; } else if (a1 + a4 - 2 <= i && i <= a1 + a2 - 2) printf("7"); else if (i == a1 + a2 - 1) printf("4"); } } } if (a3 == a4 + 1) { for (i = 0; i < a1 + a2; i++) { if (i < a1 - a4) printf("4"); else if (a1 - a4 <= i && i < a1 + a4) { printf("74"); i++; } else if (a1 + a4 <= i && i < a1 + a2) printf("7"); } } if (a3 + 1 == a4) { printf("7"); a4 -= 2; a2 -= 2; a1--; for (i = 0; i < a1 + a2; i++) { if (i < a1 - a4) printf("4"); else if (a1 - a4 <= i && i < a1 + a4) { printf("74"); i++; } else if (a1 + a4 <= i && i < a1 + a2) printf("7"); } printf("74"); } }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; struct DDMY { ostringstream o; template <class T> DDMY &operator,(const T &t) { o << t << ","; return *this; } string operator*() { return o.str().substr(0, o.str().size() - 1); } }; template <class T> std::ostream &operator<<(std::ostream &o, const std::vector<T> &v) { o << "["; for (std::size_t i = 0; i < v.size(); i++) o << v[i] << (i < v.size() - 1 ? "," : ""); return o << "]"; } template <class T, class U> std::ostream &operator<<(std::ostream &o, const std::pair<T, U> &v) { return o << v.first << ":" << v.second; } template <class T, class U> std::ostream &operator<<(std::ostream &o, const std::map<T, U> &v) { o << "{"; typename std::map<T, U>::const_iterator i = v.begin(), j = i; ++j; for (; i != v.end(); ++i, ++j) o << *i << (j != v.end() ? "," : ""); return o << "}"; } template <class T> std::ostream &operator<<(std::ostream &o, const std::set<T> &v) { o << "{"; typename std::set<T>::const_iterator i = v.begin(), j = i; ++j; for (; i != v.end(); ++i, ++j) o << *i << (j != v.end() ? "," : ""); return o << "}"; } void error() { cout << -1 << endl; exit(0); } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (max(a3, a4) > a1 || max(a3, a4) > a2) { error(); } string line; if (a3 >= a4) { for (int i = 0; i < a3; i++) { line += "47"; } a1 -= a3; a2 -= a3; a4 -= a3 - 1; if (a1 < 0 || a2 < 0 || a4 < 0) { error(); } if (a4 == 1) { if (a1 == 0) { if (a2 > 0) { line = '7' + line; a2--; if (a2 > 0) line += string(a2, '7'); } else { error(); } } else { line += string(a2, '7'); line += '4'; a1--; } } else if (a4 == 0) { line += string(a2, '7'); } else { error(); } if (a1 > 0) line = string(a1, '4') + line; } else { for (int i = 0; i < a4; i++) { line += "74"; } a1 -= a4; a2 -= a4; a3 -= a4 - 1; if (a1 < 0 || a2 < 0 || a4 < 0) { error(); } if (a3 == 0) { if (a4 > 1) { line = line.substr(0, line.size() - 2); line = line.substr(2); line = '7' + string(a1 + 1, '4') + line; line += string(a2 + 1, '7') + '4'; } else if (a4 == 1) { a1 += a4; a2 += a4; if (a2 > 0 && a1 > 0) { line = string(a2, '7') + string(a1, '4'); } else { error(); } } else if (a4 == 0) { if (a1 == 0 || a2 == 0) { if (a1 > 0) { line = string(a1, '4'); } else if (a2 > 0) { line = string(a2, '7'); } } else { error(); } } else { error(); } } else { error(); } } cout << line << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int n4, n7, n47, n74; bool solve() { if (abs(n47 - n74) > 1) return false; for (int fst = 0, _n = (10); fst < _n; fst++) if (fst == 4 || fst == 7) { if (fst == 4 && n47 < n74) continue; if (fst == 7 && n47 > n74) continue; int mn4 = (fst == 4 ? 1 : 0) + n74; int mn7 = (fst == 7 ? 1 : 0) + n47; if (mn4 > n4 || mn7 > n7) continue; vector<int> order; int groups = n47 + n74 + 1; int e4 = n4 - mn4; int e7 = n7 - mn7; for (int i = 0, _n = (groups); i < _n; i++) { int cur = ((fst == 4) ^ (i % 2 == 0) ^ 1) ? 4 : 7; printf("%d", cur); if (i <= 1 && cur == 4) for (int i = 0, _n = (e4); i < _n; i++) printf("4"); if (i >= groups - 2 && cur == 7) for (int i = 0, _n = (e7); i < _n; i++) printf("7"); } printf("\n"); return true; } return false; } int main() { scanf("%d%d%d%d", &n4, &n7, &n47, &n74); if (!solve()) printf("-1\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (a3 > a1 || a3 > a2 || a4 > a1 || a4 > a2 || a3 - a4 > 1 || a4 - a3 > 1) { cout << -1; return 0; } string s; s = '4'; --a1; int i = 0; while (a1 && a2 && a3 && a4) { if (s[i] == '4') { s += '7'; --a2, --a3, ++i; } if (s[i] == '7') { s += '4'; --a1, --a4, ++i; } } if (!a1) { if (s[s.length() - 1] == '4') { if ((!a2 && (a3 || a4)) || a3 > 1 || a4 > 1) { cout << -1; return 0; } if (a2 && a3 == 1) { s += '7'; a3 = 0; --a2; } if (!a2 && a4) { cout << -1; return 0; } if (a2 && a4 == 1) { s = "7" + s; --a2; a4 = 0; } for (int i = 0; i < s.length(); ++i) { if (i + 1 == s.length()) for (int i = 0; i < a2; ++i) printf("7"); printf("%c", s[i]); } return 0; } } if (!a2) { if (s[s.length() - 1] == '4') { if (a3 || a4) { cout << -1; return 0; } for (int i = 0; i < a1; ++i) printf("4"); cout << s; return 0; } if (s[s.length() - 1] == '7') { if ((!a1 && (a3 || a4)) || a3 || a4 > 1) { cout << -1; return 0; } if (a1 && a4 == 1) { s += '4'; --a1; a4 = 0; } for (int i = 0; i < a1; ++i) printf("4"); cout << s; return 0; } } if (!a3) { if (!a4) { for (int i = 0; i < a1; ++i) printf("4"); if (s[s.length() - 1] != '4') { cout << s; for (int i = 0; i < a2; ++i) printf("7"); } else { for (int i = 0; i < s.length(); ++i) { if (i + 1 == s.length()) for (int j = 0; j < a2; ++j) printf("7"); printf("%c", s[i]); } } return 0; } if (s[s.length() - 1] == '4') { if ((a4 && !a2) || a4 > 1) { cout << -1; return 0; } else if (a4 == 1 && a2) { --a2; --a4; printf("7"); } for (int i = 0; i < a1; ++i) printf("4"); for (int i = 0; i < s.length(); ++i) { if (i + 1 == s.length()) for (int j = 0; j < a2; ++j) printf("7"); printf("%c", s[i]); } return 0; } if (s[s.length() - 1] == '7') { if ((a4 && !a1) || a4 > 1) { cout << -1; return 0; } if (a4 == 1 && a1) { s += "4"; --a1; --a4; } for (int i = 0; i < a1; ++i) printf("4"); cout << s; for (int j = 0; j < a2; ++j) printf("7"); return 0; } } if (!a4) { if (!a3) { for (int i = 0; i < a1; ++i) printf("4"); cout << s; for (int i = 0; i < a2; ++i) printf("7"); return 0; } if (s[s.length() - 1] == '4') { if ((a3 && !a2) || a3 > 1) { cout << -1; return 0; } else if (a3 == 1 && a2) { s += "7"; --a2; --a3; } for (int i = 0; i < a1; ++i) printf("4"); cout << s; for (int j = 0; j < a2; ++j) printf("7"); return 0; } if (s[s.length() - 1] == '7') { if (a3) { cout << -1; return 0; } for (int i = 0; i < a1; ++i) printf("4"); cout << s; for (int i = 0; i < a2; ++i) printf("7"); return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int a[5]; int main() { scanf("%d %d %d %d", &a[1], &a[2], &a[3], &a[4]); if (a[1] < max(a[3], a[4]) || a[2] < max(a[3], a[4])) { printf("-1"); return 0; } if (abs(a[3] - a[4]) > 1) { printf("-1"); return 0; } if (a[3] == a[4]) { if (a[1] == a[3] && a[2] == a[4]) { printf("-1"); return 0; } if (a[1] == a[3]) { for (int i = 1; i <= a[3]; i++) { printf("74"); } for (int i = a[3] + 1; i <= a[2]; i++) { printf("7"); } return 0; } if (a[2] == a[3]) { for (int i = a[4] + 2; i <= a[1]; i++) { printf("4"); } for (int i = 1; i <= a[4]; i++) { printf("47"); } printf("4"); return 0; } for (int i = a[3] + 2; i <= a[1]; i++) printf("4"); for (int i = 1; i <= a[3]; i++) { printf("47"); } for (int i = a[3] + 1; i <= a[2]; i++) printf("7"); printf("4"); return 0; } if (a[3] < a[4]) { printf("7"); for (int i = a[4]; i <= a[1]; i++) printf("4"); for (int i = 1; i <= a[4] - 2; i++) { printf("74"); } for (int i = a[4]; i <= a[2]; i++) printf("7"); printf("4"); return 0; } if (a[4] < a[3]) { for (int i = a[3] + 1; i <= a[1]; i++) printf("4"); for (int i = 1; i <= a[3]; i++) printf("47"); for (int i = a[3] + 1; i <= a[2]; i++) printf("7"); return 0; } }
#include <bits/stdc++.h> using namespace std; const int nmax = 2000000 + 18; int n; int a1, a2, a3, a4; int ans[nmax]; void print() { for (int i = 1; i <= n; ++i) printf("%d", ans[i]); printf("\n"); } bool check47(int k) { int l = (k + 1) / 2; int r = k / 2; if (l > a1 || r > a2) return 0; int tot = 0; for (int i = 1; i + l - 1 <= a1; ++i) ans[++tot] = 4; for (int i = 1; i < r; ++i) { ans[++tot] = 7; if (i < l) ans[++tot] = 4; } for (int i = r; i <= a2; ++i) ans[++tot] = 7; if (r < l) ans[++tot] = 4; return 1; } bool check74(int k) { int l = (k + 1) / 2; int r = k / 2; if (l > a2 || r > a1) return 0; int tot = 0; ans[++tot] = 7; for (int i = 1; i + r - 1 <= a1; ++i) ans[++tot] = 4; for (int i = 2; i < l; ++i) { ans[++tot] = 7; if (i <= r) ans[++tot] = 4; } for (int i = l; i <= a2; ++i) ans[++tot] = 7; if (l <= r) ans[++tot] = 4; return 1; } int main() { scanf("%d%d%d%d", &a1, &a2, &a3, &a4); n = a1 + a2; if (abs(a3 - a4) > 1) printf("-1\n"); else if (a3 == a4) { if (check47(a3 * 2 + 1)) print(); else if (check74(a4 * 2 + 1)) print(); else printf("-1\n"); } else if (a3 > a4) { if (check47(a3 * 2)) print(); else printf("-1\n"); } else { if (check74(a4 * 2)) print(); else printf("-1\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int mx = 300009; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int a, b, c, d; cin >> a >> b >> c >> d; if (fabs(c - d) > 1) cout << -1 << '\n'; else { if (c == d) { string ans; for (int i = 0; i < 2 * c + 1; i++) { if (i % 2 == 0) ans += '4'; else ans += '7'; } if (a >= c + 1 and b >= c) { a -= c + 1; b -= c; string aa, bb; while (a) a--, aa += '4'; while (b) b--, bb += '7'; ans = aa + ans; if (bb.size()) { ans.pop_back(); bb += '4'; ans += bb; } cout << ans << '\n'; } else if (b >= c + 1 and a >= c) { reverse(ans.begin(), ans.end()); ans.pop_back(); reverse(ans.begin(), ans.end()); ans += '7'; a -= c; b -= c + 1; string aa, bb; while (a) a--, aa += '4'; while (b) b--, bb += '7'; ans = ans + bb; if (aa.size()) { reverse(ans.begin(), ans.end()); ans.pop_back(); ans = aa + ans; ans += '7'; reverse(ans.begin(), ans.end()); } cout << ans << '\n'; } else cout << -1 << '\n'; } else if (c - 1 == d) { string ans; for (int i = 0; i < c * 2; i++) { if (i % 2 == 0) ans += '4'; else ans += '7'; } if (a < c or b < c) cout << -1 << '\n'; else { a -= c; b -= c; string aa, bb; while (a > 0) a--, aa += '4'; while (b > 0) b--, bb += '7'; ans = aa + ans; ans += bb; cout << ans << '\n'; } } else if (d - 1 == c) { string ans; for (int i = 0; i < d * 2; i++) { if (i % 2 == 0) ans += '7'; else ans += '4'; } if (a < d or b < d) cout << -1 << '\n'; else { a -= d; b -= d; string aa, bb; while (a > 0) a--, aa += '4'; while (b > 0) b--, bb += '7'; if (aa.size()) { reverse(ans.begin(), ans.end()); ans.pop_back(); ans = ans + aa; ans += '7'; reverse(ans.begin(), ans.end()); } if (bb.size()) { bb += '4'; ans.pop_back(); ans += bb; } cout << ans << '\n'; } } else cout << -1 << '\n'; } }
#include <bits/stdc++.h> using namespace std; long long a1, a2, a3, a4; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) { cout << -1; return 0; } if (a3 > a4) { long long curr4 = a3; long long curr7 = a3; if (a1 < curr4 || a2 < curr7) { cout << -1; return 0; } for (long long i = 0; i < a1 - curr4; i++) cout << 4; for (long long i = 0; i < a3; i++) cout << 47; for (long long i = 0; i < a2 - curr7; i++) cout << 7; return 0; } if (a3 < a4) { long long curr4 = a4; long long curr7 = a4; if (a1 < curr4 || a2 < curr7) { cout << -1; return 0; } cout << 7; for (long long i = 0; i < a1 - curr4; i++) cout << 4; for (long long i = 0; i < a4 - 1; i++) cout << 47; for (long long i = 0; i < a2 - curr7; i++) cout << 7; cout << 4; return 0; } if ((a1 >= a3 && a2 > a3) || (a2 >= a3 && a1 > a3)) { if (a1 == a3) { cout << 7; for (long long i = 0; i < a1 - a3 + 1; i++) cout << 4; for (long long i = 0; i < a3 - 1; i++) cout << 74; for (long long i = 0; i < a2 - a3; i++) cout << 7; return 0; } for (long long i = 0; i < a1 - a3; i++) cout << 4; for (long long i = 0; i < a3 - 1; i++) cout << 74; for (long long i = 0; i < a2 - a3 + 1; i++) cout << 7; cout << 4; return 0; } cout << -1 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; deque<int> bst, buf; int b4, b7, b47, b74, _b4, _b7, _b47, _b74; void upd(deque<int>& w) { int c4 = 0, c7 = 0, c47 = 0, c74 = 0; for (int i = 0; i < ((int)(w).size()); ++i) { if (w[i] == 4) ++c4; if (w[i] == 7) ++c7; if (i && w[i - 1] == 4 && w[i] == 7) ++c47; if (i && w[i - 1] == 7 && w[i] == 4) ++c74; } if (c4 != _b4 || c7 != _b7 || c47 != _b47 || c74 != _b74) return; if (bst.empty() || w < bst) bst = w; } int main() { cin >> b4 >> b7 >> b47 >> b74; _b4 = b4; _b7 = b7; _b47 = b47; _b74 = b74; if (b47 == 0 && b74 == 0) { while (b4 > 0) --b4, buf.push_back(4); while (b7 > 0) --b7, buf.push_back(7); upd(buf); buf.clear(); } else { if (b47 > b74) { for (int i = 0; i < b47; ++i) { buf.push_back(4); buf.push_back(7); } b4 -= b47; b7 -= b47; while (b4 > 0) buf.push_front(4), --b4; while (b7 > 0) buf.push_back(7), --b7; upd(buf); buf.clear(); } else { if (b47 < b74) { b4 -= b74; b7 -= b74; for (int i = 0; i < b74; ++i) { if (i == 0) { buf.push_back(7); while (b4 > 0) --b4, buf.push_back(4); buf.push_back(4); } else if (i + 1 == b74) { while (b7 > 0) --b7, buf.push_back(7); buf.push_back(7); buf.push_back(4); } else buf.push_back(7), buf.push_back(4); } upd(buf); buf.clear(); } else { for (int i = 0; i < b47; ++i) buf.push_back(4), buf.push_back(7); buf.push_back(4); b4 -= b47 + 1; b7 -= b47; buf.pop_back(); while (b7 > 0) buf.push_back(7), --b7; buf.push_back(4); while (b4 > 0) buf.push_front(4), --b4; upd(buf); buf.clear(); b4 = _b4; b7 = _b7; for (int i = 0; i < b74; ++i) buf.push_back(7), buf.push_back(4); buf.push_back(7); b7 -= b74 + 1; b4 -= b74; while (b7 > 0) buf.push_back(7), --b7; buf.pop_front(); while (b4 > 0) buf.push_front(4), --b4; buf.push_front(7); upd(buf); buf.clear(); } } } if (bst.empty()) printf("-1\n"); else { for (int i = 0; i < ((int)(bst).size()); ++i) printf("%d", bst[i]); printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int a, b, c, d; cin >> a >> b >> c >> d; if (abs(c - d) > 1) { cout << "-1" << endl; return 0; } string s = ""; if (c == d) { s = ""; for (int i = (int)(0); i < (int)(c); i++) s += "47"; int q1 = c + 1; int q2 = c; if (q1 <= a && q2 <= b) { string t = ""; a -= (c + 1); b -= c; for (int i = (int)(0); i < (int)(b); i++) s += "7"; s += "4"; for (int i = (int)(0); i < (int)(a); i++) t += "4"; cout << t << s << endl; return 0; } q1 = c; q2 = c + 1; s = "74"; for (int i = (int)(0); i < (int)((a - q1)); i++) s += "4"; for (int i = (int)(1); i < (int)(c); i++) s += "74"; s += "7"; if (q1 <= a && q2 <= b) { string t = ""; a -= q1; b -= q2; for (int i = (int)(0); i < (int)(b); i++) s += "7"; cout << t << s << endl; return 0; } } else if (c > d) { s = ""; a -= c; b -= c; if (a < 0 or b < 0) { cout << -1 << endl; return 0; } for (int i = (int)(0); i < (int)(a); i++) s += "4"; for (int i = (int)(0); i < (int)(c); i++) s += "47"; for (int i = (int)(0); i < (int)(b); i++) s += "7"; cout << s << endl; return 0; } else { s = ""; a -= d; b -= d; if (a < 0 or b < 0) { cout << -1 << endl; return 0; } s += "74"; for (int i = (int)(0); i < (int)(a); i++) s += "4"; for (int i = (int)(2); i < (int)(d); i++) s += "74"; for (int i = (int)(0); i < (int)(b); i++) s += "7"; s += "74"; cout << s << endl; return 0; } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char s[2001000]; int main() { int a, b, c, d, i, j; scanf("%d%d%d%d", &a, &b, &c, &d); i = 0; if (c == d) { if (a > c && b >= c) { for (j = 0; j < a - c; s[i++] = '4', j++) ; for (j = 0; j < c - 1; s[i++] = '7', s[i++] = '4', j++) ; for (j = 0; j < b - c + 1; s[i++] = '7', j++) ; s[i++] = '4'; } else if (b > c && a >= c) { s[i++] = '7'; for (j = 0; j < a - c; s[i++] = '4', j++) ; for (j = 0; j < c; s[i++] = '4', s[i++] = '7', j++) ; for (j = 0; j < b - c - 1; s[i++] = '7', j++) ; } else { s[0] = '-'; s[1] = '1'; } } else if (c == d + 1) { if (a >= c && b >= c) { for (j = 0; j < a - c; s[i++] = '4', j++) ; for (j = 0; j < c; s[i++] = '4', s[i++] = '7', j++) ; for (j = 0; j < b - c; s[i++] = '7', j++) ; } else { s[0] = '-'; s[1] = '1'; } } else if (d == c + 1) { if (b >= d && a >= d) { s[i++] = '7'; for (j = 0; j < a - d; s[i++] = '4', j++) ; for (j = 0; j < d - 1; s[i++] = '4', s[i++] = '7', j++) ; for (j = 0; j < b - d; s[i++] = '7', j++) ; s[i++] = '4'; } else { s[0] = '-'; s[1] = '1'; } } else { s[0] = '-'; s[1] = '1'; } printf("%s\n", s); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n4, n7, n47, n74; cin >> n4 >> n7 >> n47 >> n74; if (abs(n47 - n74) > 1 || min(n4, n7) < max(n47, n74) || (n47 == n74 && max(n4, n7) < n47 + 1)) { cout << -1 << endl; return 0; } if (n47 > n74) { for (int i = 0; i < n4 - n74; i++) cout << "4"; for (int i = 0; i < n74; i++) cout << "74"; for (int i = 0; i < n7 - n74; i++) cout << "7"; cout << endl; } else if (n74 > n47) { cout << "7"; for (int i = 0; i < n4 - n47 - 1; i++) cout << "4"; for (int i = 0; i < n47; i++) cout << "47"; for (int i = 0; i < n7 - n47 - 1; i++) cout << "7"; cout << "4"; cout << endl; } else { if (n4 >= n47 + 1) { for (int i = 0; i < n4 - n47 - 1; i++) cout << "4"; for (int i = 0; i < n47; i++) cout << "47"; for (int i = 0; i < n7 - n47; i++) cout << "7"; cout << "4"; cout << endl; } else { cout << "7"; for (int i = 0; i < n4 - n47; i++) cout << "4"; for (int i = 0; i < n47; i++) cout << "47"; for (int i = 0; i < n7 - n47 - 1; i++) cout << "7"; cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; const long long MOD = 1000000007LL; int a1, a2, a3, a4; int main() { cin >> a1 >> a2 >> a3 >> a4; if (a3 == a4) { if (a1 == a3 && a2 > a3) { for (int i = 0; i < a3; i++) printf("74"); for (int i = 0; i < a2 - a3; i++) printf("7"); return 0; } if (a2 == a3 && a1 > a3) { for (int i = 0; i < a1 - a3; i++) printf("4"); for (int i = 0; i < a3; i++) printf("74"); return 0; } if (a1 > a3 && a2 > a3) { for (int i = 0; i < a1 - a3; i++) printf("4"); for (int i = 1; i < a3; i++) printf("74"); for (int i = 0; i < a2 - a3; i++) printf("7"); printf("74"); return 0; } puts("-1"); return 0; } if (a3 == a4 + 1) { if (a1 >= a3 && a2 >= a3) { for (int i = 0; i < a1 - a3; i++) printf("4"); for (int i = 0; i < a3; i++) printf("47"); for (int i = 0; i < a2 - a3; i++) printf("7"); return 0; } puts("-1"); return 0; } if (a4 == a3 + 1) { if (a2 >= a4 && a1 >= a4) { printf("74"); for (int i = 0; i < a1 - a4; i++) printf("4"); for (int i = 2; i < a4; i++) printf("74"); for (int i = 0; i < a2 - a4; i++) printf("7"); printf("74"); return 0; } puts("-1"); return 0; } puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; string ans; if (c > d) { if (c != d + 1) { puts("-1"); return 0; } else { a -= c; b -= c; if (a < 0 || b < 0) { puts("-1"); return 0; } for (int i = 0; i < a; i++) ans.push_back('4'); for (int i = 0; i < c; i++) { ans.push_back('4'); ans.push_back('7'); } for (int i = 0; i < b; i++) ans.push_back('7'); } } else if (c < d) { if (d != c + 1) { puts("-1"); return 0; } else { a -= d; b -= d; if (a < 0 || b < 0) { puts("-1"); return 0; } d--; ans.push_back('7'); for (int i = 0; i < a; i++) ans.push_back('4'); ans.push_back('4'); for (int i = 0; i < d; i++) { ans.push_back('7'); ans.push_back('4'); } int cur; for (int i = 0; i < ans.size(); i++) { if (ans[i] == '7') cur = i; } string tmp = ans.substr(cur + 1); ans = ans.substr(0, cur + 1); for (int i = 0; i < b; i++) ans.push_back('7'); ans += tmp; } } else { if (a - c - 1 >= 0) { a -= c + 1; b -= c; if (b < 0) { puts("-1"); return 0; } for (int i = 0; i < a; i++) ans.push_back('4'); for (int i = 0; i < c; i++) { ans.push_back('4'); ans.push_back('7'); } for (int i = 0; i < b; i++) ans.push_back('7'); ans.push_back('4'); } else { b -= c + 1; a -= c; if (b < 0 || a < 0) { puts("-1"); return 0; } c--; ans.push_back('7'); for (int i = 0; i < a; i++) ans.push_back('4'); ans.push_back('4'); for (int i = 0; i < c; i++) { ans.push_back('7'); ans.push_back('4'); } ans.push_back('7'); for (int i = 0; i < b; i++) ans.push_back('7'); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); if (abs(c - d) > 1) { puts("-1"); return 0; } if (c > d) { if (a - c < 0 || b - c < 0) { puts("-1"); return 0; } for (int i = 0; i < a - c; ++i) printf("4"); ; for (int i = 0; i < c; ++i) printf("47"); for (int i = 0; i < b - c; ++i) printf("7"); return 0; } if (c < d) { if (a - d < 0 || b - d < 0) { puts("-1"); return 0; } printf("7"); for (int i = 0; i < a - d; ++i) printf("4"); ; for (int i = 0; i < c; ++i) printf("47"); for (int i = 0; i < b - d; ++i) printf("7"); printf("4\n"); return 0; } if (a < 2 && b < 2) { puts("-1"); return 0; } if (a - 1 >= c && b >= c) { for (int i = 0; i < a - c - 1; ++i) printf("4"); ; for (int i = 0; i < c; ++i) printf("47"); for (int i = 0; i < b - c; ++i) printf("7"); puts("4"); return 0; } if (b - 1 >= c && a >= c) { printf("7"); for (int i = 0; i < c; ++i) printf("47"); for (int i = 0; i < b - c - 1; ++i) printf("7"); return 0; } puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const int MAXN = 500; const int INF = 100000009; int Min(int a, int b) { return a < b ? a : b; } int a4, a7, a47, a74; int main() { int i; while (cin >> a4 >> a7 >> a47 >> a74) { if (a74 - a47 > 1 || a47 - a74 > 1) { puts("-1"); continue; } if (a47 > a4 || a74 > a4 || a47 > a7 || a74 > a7) { puts("-1"); continue; } if (a47 == a74) { if (a4 == a47 && a7 == a47) { puts("-1"); continue; } if (a7 == 2 && a4 == 1) { if (a7 <= a74) { puts("-1"); continue; } for (i = 0; i < a74; i++) printf("74"); puts("7"); continue; } if (a47 <= a4 - 1) { for (i = a47; i < a4 - 1; i++) printf("4"); for (i = 0; i < a47; i++) printf("47"); for (i = a47; i < a7; i++) printf("7"); puts("4"); continue; } else { printf("7"); for (i = 0; i < a47; i++) printf("47"); for (i = a47; i < a7 - 1; i++) printf("7"); puts(""); continue; } } if (a47 > a74) { for (i = a47; i < a4; i++) printf("4"); for (i = 0; i < a47; i++) printf("47"); for (i = a47; i < a7; i++) printf("7"); puts(""); continue; } if (a74 > a47) { printf("74"); for (i = a74; i < a4; i++) printf("4"); for (i = 0; i < a74 - 2; i++) printf("74"); for (i = a74; i < a7; i++) printf("7"); puts("74"); continue; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long int Mmax(long long int a, long long int b) { if (a > b) return a; return b; } long long int Mmin(long long int a, long long int b) { if (a < b) return a; return b; } long long int nod(long long int a, long long int b) { while (a && b) { if (a > b) a %= b; else b %= a; } return Mmax(a, b); } long long int nok(long long int a, long long int b) { return a * b / nod(a, b); } bool IsPrime(long long int x) { if (x < 2) return false; long long int X = sqrt(x), i; for (i = 2; i <= X; i++) if (x % i == 0) return false; return true; } void hanoi(int n, int A, int C, int B) { if (n == 1) { cout << n << " " << A << " " << C << endl; } else { hanoi(n - 1, A, B, C); cout << n << " " << A << " " << C << endl; hanoi(n - 1, B, C, A); } } string pr2(string a, int d) { if (d == 0) return "0"; string b; long long int sz = a.size(), i, prenos = 0; for (i = 0; i < sz; i++) { b[i] = a[i]; } for (i = sz - 1; i > -1; i--) { a[i] = ((b[i] - '0') * d + prenos) % 10 + '0'; prenos = ((b[i] - '0') * d + prenos) / 10; } if (prenos) a = char(prenos + '0') + a; return a; } string sum(string a, string b) { bool carry = false; long long int i, sz1, sz2, maxsz, minsz; string c, d; sz1 = a.size(); sz2 = b.size(); maxsz = max(sz1, sz2); minsz = min(sz1, sz2); while (sz1 < maxsz) { sz1++; a = '0' + a; } while (sz2 < maxsz) { sz2++; b = '0' + b; } for (i = maxsz - 1; i > -1; i--) { d = char((a[i] + b[i] - 96 + carry) % 10 + 48) + d; if (a[i] + b[i] - 96 + carry > 9) carry = true; else carry = false; } if (carry == true) d = char('1') + d; return d; } string pr(string a, string b) { string res = "0", p, p2; int sz = a.size(), x = 0; for (sz = a.size(); sz > 0; sz--, x++) { int d = a[sz - 1] - '0'; a = a.erase(sz - 1, 1); p2 = pr2(b, d); p2 += p; res = sum(res, p2); p += "0"; } return res; } bool vesokosna(long long int x) { return (x % 4 == 0 && x % 100 || x % 400 == 0); } long long int reverse(long long int x) { long long int mirror = 0; while (x) { mirror = mirror * 10 + x % 10; x /= 10; } return mirror; } long long int ost(string x, long long int k) { long long int num = 0, i, sz = x.size(); for (i = 0; i < sz; i++) { num = num * 10; num += x[i] - '0'; num %= k; } return num; } int main() { int cnt4 = 0; int cnt7 = 0; int cnt47 = 0; int cnt74 = 0; cin >> cnt4 >> cnt7 >> cnt47 >> cnt74; if (cnt47 == cnt74 + 1) { int need = cnt47; if (cnt4 >= need && cnt7 >= need) { cnt4 -= need; cnt7 -= need; string a; int i; for (i = 0; i < cnt4; i++) a += "4"; for (i = 0; i < cnt47; i++) a += "47"; for (i = 0; i < cnt7; i++) a += "7"; cout << a << endl; } else cout << -1 << endl; return 0; } if (cnt74 == cnt47 + 1) { int need = cnt74; if (cnt4 >= need && cnt7 >= need) { cnt4 -= need; cnt7 -= need; string a; int i; a += "74"; for (i = 0; i < cnt4; i++) a += "4"; for (i = 2; i < cnt74; i++) a += "74"; for (i = 0; i < cnt7; i++) a += "7"; a += "74"; cout << a << endl; } else cout << -1 << endl; return 0; } if (cnt47 == cnt74) { int need = cnt47; if (cnt4 >= need + 1 && cnt7 >= need) { cnt4 -= need + 1; cnt7 -= need; string a = "4"; int i; for (i = 0; i < cnt4; i++) a += "4"; for (i = 1; i < cnt74; i++) a += "74"; for (i = 0; i < cnt7; i++) a += "7"; a += "74"; cout << a << endl; } else if (cnt4 >= need && cnt7 >= need + 1) { cnt4 -= need; cnt7 -= need + 1; string a = "7"; int i; for (i = 0; i < cnt4; i++) a += "4"; for (i = 0; i < cnt47; i++) a += "47"; for (i = 0; i < cnt7; i++) a += "7"; cout << a << endl; } else cout << -1 << endl; } else cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a1, a2, a3, a4; int a[2000000]; int main() { cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) { cout << -1 << endl; return 0; } int cnt = 0; if (a3 == a4) { if (a1 < a3 + 1) { a[0] = 7; cnt++; for (int i = 0; i < a3; ++i) { a[cnt] = 4; cnt++; a[cnt] = 7; cnt++; } a1 -= a3; a2 -= a3 + 1; } else { a[0] = 4; cnt++; for (int i = 0; i < a3; ++i) { a[cnt] = 7; cnt++; a[cnt] = 4; cnt++; } a1 -= a3 + 1; a2 -= a3; } } else { if (a3 < a4) { for (int i = 0; i < a4; ++i) { a[cnt] = 7; cnt++; a[cnt] = 4; cnt++; } a1 -= a4; a2 -= a4; } else { for (int i = 0; i < a3; ++i) { a[cnt] = 4; cnt++; a[cnt] = 7; cnt++; } a1 -= a3; a2 -= a3; } } if (a1 < 0 || a2 < 0) { cout << -1 << endl; return 0; } for (int i = 0; i < cnt; ++i) { printf("%d", a[i]); if (a[i] == 4 && (i == 0 || i == 1)) for (int j = 0; j < a1; ++j) printf("4"); if (a[i] == 7 && (i == cnt - 2 || i == cnt - 1)) for (int j = 0; j < a2; ++j) printf("7"); } return 0; }
#include <bits/stdc++.h> using namespace std; bool cmp(list<char> a, list<char> b) { for (auto it = a.begin(), it2 = b.begin(); it != a.end(); it++, it2++) { if (*it != *it2) { if (*it < *it2) return 1; else return 0; } } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int quatro, sete, q7, s4; cin >> quatro >> sete >> q7 >> s4; if (abs(q7 - s4) > 1) { cout << -1 << endl; return 0; } if (q7 > s4) { if (quatro < q7 || sete < q7) { cout << -1 << endl; return 0; } list<char> ans; for (int i = 0; i < q7; i++) ans.push_back('4'), ans.push_back('7'); while (quatro != q7) { ans.insert(ans.begin(), '4'); quatro--; } while (sete != q7) { ans.insert(ans.end(), '7'); sete--; } for (char c : ans) cout << c; cout << endl; } else if (s4 > q7) { if (quatro < s4 || sete < s4) { cout << -1 << endl; return 0; } list<char> ans; for (int i = 0; i < s4; i++) ans.push_back('7'), ans.push_back('4'); auto it = ans.begin(); it++; while (quatro != s4) { ans.insert(it, '4'); quatro--; } it = ans.end(); it--; while (sete != s4) { ans.insert(it, '7'); sete--; } for (char c : ans) cout << c; cout << endl; } else { int q = 0; list<char> ans1, ans2; int quatro2 = quatro, sete2 = sete; if (quatro - 1 < q7 || sete < q7) q++; else { for (int i = 0; i < q7; i++) ans1.push_back('4'), ans1.push_back('7'); ans1.push_back('4'); quatro--; while (quatro != q7) { ans1.insert(ans1.begin(), '4'); quatro--; } auto it = ans1.end(); it--; while (sete != q7) { ans1.insert(it, '7'); sete--; } } if (quatro2 < s4 || sete2 - 1 < s4) q++; else { for (int i = 0; i < s4; i++) ans2.push_back('7'), ans2.push_back('4'); ans2.push_back('7'); sete2--; auto it = ans2.begin(); it++; while (quatro2 != s4) { ans2.insert(it, '4'); quatro2--; } while (sete2 != s4) { ans2.insert(ans2.end(), '7'); sete2--; } } if ((int)ans1.size() == 0 && (int)ans2.size() == 0) { cout << -1 << endl; } else if ((int)ans1.size() == 0) { for (char c : ans2) cout << c; cout << endl; } else if ((int)ans2.size() == 0) { for (char c : ans1) cout << c; cout << endl; } else if (cmp(ans1, ans2)) { for (char c : ans1) cout << c; cout << endl; } else { for (char c : ans2) cout << c; cout << endl; } } }
#include <bits/stdc++.h> using namespace std; FILE *in; FILE *out; int main(void) { in = stdin; out = stdout; int a[4]; for (int i = 0; i < 4; i++) fscanf(in, "%d", &a[i]); if (a[2] == a[3]) { if (a[0] == a[2] && a[1] >= a[2] + 1) { for (int i = 0; i < a[3]; i++) fprintf(out, "74"); for (int i = a[1]; i > a[3]; i--) fprintf(out, "7"); fprintf(out, "\n"); } else if (a[1] == a[2] && a[0] >= a[2] + 1) { for (int i = a[0]; i > a[3]; i--) fprintf(out, "4"); for (int i = 0; i < a[3]; i++) fprintf(out, "74"); fprintf(out, "\n"); } else if (a[0] >= a[2] + 1 && a[1] >= a[2] + 1) { for (int i = a[0]; i > a[2] + 1; i--) fprintf(out, "4"); for (int i = 0; i < a[2]; i++) fprintf(out, "47"); fprintf(out, "7"); for (int i = a[1]; i > a[2] + 1; i--) fprintf(out, "7"); fprintf(out, "4"); fprintf(out, "\n"); } else fprintf(out, "-1\n"); } else if (a[2] == a[3] + 1) { if (a[0] >= a[2] && a[1] >= a[2]) { for (int i = a[0]; i > a[2]; i--) fprintf(out, "4"); for (int i = 0; i < a[2]; i++) fprintf(out, "47"); for (int i = a[1]; i > a[2]; i--) fprintf(out, "7"); fprintf(out, "\n"); } else fprintf(out, "-1\n"); } else if (a[2] == a[3] - 1) { if (a[0] >= a[3] && a[1] >= a[3]) { if (a[3] == 1) { for (int i = 0; i < a[1]; i++) fprintf(out, "7"); for (int i = 0; i < a[0]; i++) fprintf(out, "4"); fprintf(out, "\n"); } else { fprintf(out, "74"); for (int i = a[0]; i > a[3]; i--) fprintf(out, "4"); for (int i = 1; i < a[3] - 1; i++) fprintf(out, "74"); fprintf(out, "7"); for (int i = a[1]; i > a[3]; i--) fprintf(out, "7"); fprintf(out, "4"); fprintf(out, "\n"); } } else fprintf(out, "-1\n"); } else fprintf(out, "-1\n"); return 0; }
#include <bits/stdc++.h> using namespace std; void no() { puts("-1"); exit(0); } int main() { int n4, n7, n47, n74; cin >> n4 >> n7 >> n47 >> n74; if (abs(n47 - n74) > 1) no(); for (; n47 >= n74;) { int ned4 = n47 + (n47 == n74); int ned7 = n47; if (ned4 > n4) break; if (ned7 > n7) break; for (int i = (ned4); i < (n4); i++) putchar('4'); for (int i = (0); i < (n47); i++) printf("47"); for (int i = (ned7); i < (n7); i++) putchar('7'); if (n47 == n74) putchar('4'); return 0; } for (; n74 >= n47;) { int ned4 = n74; int ned7 = n74 + (n47 == n74); if (ned4 > n4) break; if (ned7 > n7) break; for (int i = (0); i < (n74); i++) { putchar('7'); if (n47 != n74 && i == n74 - 1) for (int i = (ned7); i < (n7); i++) putchar('7'); putchar('4'); if (i == 0) for (int i = (ned4); i < (n4); i++) putchar('4'); } if (n47 == n74) { putchar('7'); for (int i = (ned7); i < (n7); i++) putchar('7'); } return 0; } no(); }
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-5; int main() { int a1, a2, a3, a4; while (cin >> a1 >> a2 >> a3 >> a4) { string res; if (abs(a3 - a4) > 1 || a3 > a1 || a4 > a2) { cout << "-1" << endl; continue; } if (a3 == a4) { if (a3 >= a1) { if (a3 == a1) { for (int i = 0; i < a3; i++) { res += "74"; } if (a2 == a1) { cout << "-1" << endl; continue; } for (int i = 0; i < a2 - a1; i++) { res += "7"; } cout << res << endl; } else { cout << "-1" << endl; } continue; } for (int i = 0; i < a1 - a3; i++) { res += "4"; } for (int i = 0; i < a3 - 1; i++) { if (a2) { res += "74"; a2--; } else { res += "4"; } } if (!a2) { cout << "-1" << endl; continue; } for (int i = 0; i < a2; i++) { res += "7"; } res += "4"; } else if (a3 > a4) { if (a4 >= a1) { cout << "-1" << endl; continue; } for (int i = 0; i < a1 - a4; i++) { res += "4"; } for (int i = 0; i < a3 - 1; i++) { if (a2) { res += "74"; a2--; } else { res += "4"; } } if (!a2) { cout << "-1" << endl; continue; } for (int i = 0; i < a2; i++) { res += "7"; } } else if (a3 < a4) { if (a3 >= a1) { cout << "-1" << endl; continue; } res += "7"; a2--; for (int i = 0; i < a1 - a3; i++) { res += "4"; } for (int i = 0; i < a4 - 2; i++) { res += "74"; a2--; } if (!a2) { cout << "-1" << endl; continue; } for (int i = 0; i < a2; i++) { res += "7"; } res += "4"; } cout << res << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> dq; int f, s, fs, sf, i, n, f1, s1, k; cin >> f >> s >> fs >> sf; if (abs(fs - sf) > 1 || (f == 1 && s == 1) || fs > min(f, s) || sf > min(f, s)) { cout << -1; return 0; } if (fs > sf) { for (i = 0; i < fs; i++) { dq.push_back(4); dq.push_back(7); } f1 = s1 = fs; } else if (sf > fs) { for (i = 0; i < sf; i++) { dq.push_back(7); dq.push_back(4); } f1 = s1 = sf; } else { if (f >= s) { for (i = 0; i < fs; i++) { dq.push_back(4); dq.push_back(7); } dq.push_back(4); f1 = s1 = fs; f1++; } else { f1 = s1 = fs; if (fs < f) { for (i = 0; i < fs; i++) { dq.push_back(4); dq.push_back(7); } dq.push_back(4); f1++; } else { for (i = 0; i < fs; i++) { dq.push_back(7); dq.push_back(4); } dq.push_back(7); s1++; } } } if (f1 > f || s1 > s) { cout << -1; return 0; } for (i = 0; i < dq.size(); i++) if (dq[i] == 7) k = i; f -= f1, s -= s1; for (i = 0; i < dq.size(); i++) { cout << dq[i]; if (dq[i] == 4) { while (f > 0) { cout << 4; f--; } } if (i == k) { while (s > 0) { cout << 7; s--; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const long double PI = 2 * acos(0); const long double eps = 1e-15; const int inf = 1e9 + 7; const long long Linf = (long long)1e18 + 7; const int MOD = 1e9 + 7; const int LMAX = 1e6 + 7; const int MAX = 1e5 + 7; const int BASE = 1e9 + 7; int dx[] = {-1, 0, 0, 1}; int dy[] = {0, -1, 1, 0}; int main() { ios::sync_with_stdio(false); int a, b, c, d; cin >> a >> b >> c >> d; if (c == d) { a -= c + 1; b -= c; if (a >= 0 && b >= 0) { for (int i = (1); i <= (a); i++) { cout << 4; } for (int i = (1); i <= (c); i++) { cout << 47; } for (int i = (1); i <= (b); i++) { cout << 7; } cout << 4; } else { a += c + 1; b += c; a -= c; b -= c + 1; if (a >= 0 && b >= 0) { cout << 7; for (int i = (1); i <= (a); i++) cout << 4; for (int i = (1); i <= (c); i++) cout << 47; for (int i = (1); i <= (b); i++) cout << 7; } else cout << -1; } } else if (c == d + 1) { a -= c; b -= c; if (a >= 0 && b >= 0) { for (int i = (1); i <= (a); i++) { cout << 4; } for (int i = (1); i <= (c); i++) { cout << 47; } for (int i = (1); i <= (b); i++) { cout << 7; } } else cout << -1; } else if (d == c + 1) { a -= d; b -= d; if (a >= 0 && b >= 0) { cout << 7; for (int i = (1); i <= (a); i++) { cout << 4; } for (int i = (1); i <= (c); i++) { cout << 47; } for (int i = (1); i <= (b); i++) { cout << 7; } cout << 4; } else cout << -1; } else cout << -1; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int a, b, c, d; cin >> a >> b >> c >> d; if (abs(c - d) > 1 || c > a || d > a || c > b || d > b) { return cout << -1, 0; } if (c == 0 && d == 0 && a && b) return cout << -1, 0; string s; if (c == d) { if (a - c < 1 && b - c < 1) return cout << -1, 0; if (a - c >= 1) { for (int i = 0; i < a - c - 1; i++) cout << 4; for (int i = 0; i < c; i++) cout << 47; for (int i = 0; i < b - c; i++) cout << 7; cout << 4; } if (a - c >= 1) return 0; cout << 74; for (int i = 0; i < a - c; i++) cout << 4; for (int i = 0; i < c - 1; i++) cout << 74; cout << 7; for (int i = 0; i < b - c - 1; i++) cout << 7; } else if (c == d + 1) { for (int i = 0; i < a - c; i++) cout << 4; for (int i = 0; i < c; i++) cout << 47; for (int i = 0; i < b - c; i++) cout << 7; } else { if (d == 1) { for (int i = 0; i < b; i++) cout << 7; for (int i = 0; i < a; i++) cout << 4; return 0; } cout << 7; cout << 4; for (int i = 0; i < a - d; i++) cout << 4; for (int i = 0; i < d - 2; i++) cout << 74; cout << 7; for (int i = 0; i < b - d; i++) cout << 7; cout << 4; } return 0; }
#include <bits/stdc++.h> using namespace std; long a1, a2, a3, a4; string solve() { string res; if (abs(a3 - a4) > 1) return "-1"; if (a3 < a4) { res = ""; for (int j = 1; j <= a4; j++) res += "74"; } else { res = ""; for (int j = 1; j <= a3; j++) { res += "47"; } if (a3 == a4) res += "4"; } if (a3 == a4) { if (a1 == a3 && a1 < a2) { for (int i = 0; i < res.size(); i++) if (res[i] == '4') res[i] = '7'; else res[i] = '4'; } } for (int j = 0; j < res.size(); j++) if (res[j] == '4') --a1; else --a2; if (a1 < 0 || a2 < 0) return "-1"; long f4, l7; f4 = 1e9; l7 = -1; string st = res; for (long i = 0; i < st.size(); i++) { if (st[i] == '4') f4 = min(f4, i); else l7 = max(l7, i); } res = ""; for (int i = 0; i < st.size(); i++) { res += st[i]; if (i == f4) for (int j = 0; j < a1; j++) res += '4'; if (i == l7) for (int j = 0; j < a2; j++) res += '7'; } return res; } int main() { ios_base::sync_with_stdio(0); cin >> a1 >> a2 >> a3 >> a4; cout << solve() << endl; cin.get(); cin.get(); return 0; }
#include <bits/stdc++.h> using namespace std; void print(char x, int n) { for (int i = 0; i < n; i++) cout << x; } int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (a1 + a2 - 1 < a3 + a4 || abs(a3 - a4) > 1 || a2 < a4 || a1 < a3 || a3 > a2 || a4 - 1 > a1) { cout << -1 << endl; return 0; } if (a3 == a4) { if (a1 == a4) { print('7', 1); for (int i = 0; i < a3; i++) print('4', 1), print('7', 1); print('7', a2 - a3 - 1); return 0; } print('4', a1 - a3); for (int i = 0; i < a3 - 1; i++) print('7', 1), print('4', 1); print('7', a2 - a3 + 1); print('4', 1); return 0; } else if (a3 > a4) { print('4', a1 - a3 + 1); for (int i = 0; i < a3 - 1; i++) print('7', 1), print('4', 1); print('7', a2 - a3 + 1); return 0; } else { if (a1 == a3) { cout << -1 << endl; return 0; } print('7', 1); print('4', a1 - a3); for (int i = 0; i < a3 - 1; i++) print('7', 1), print('4', 1); print('7', a2 - a3); print('4', 1); } }
#include <bits/stdc++.h> using namespace std; int a1, a2, a3, a4; int main() { cin >> a1 >> a2 >> a3 >> a4; if (a3 == a4) { if (a1 - a3 - 1 >= 0 && a2 - a4 >= 0) { for (int i = 0; i < a1 - a3 - 1; i++) cout << "4"; for (int i = 0; i < a3; i++) cout << "47"; for (int i = 0; i < a2 - a4; i++) cout << "7"; cout << "4"; return 0; } if (a1 - a3 >= 0 && a2 - a4 - 1 >= 0) { for (int i = 0; i < a4; i++) cout << "74"; for (int i = 0; i < a1 - a3; i++) cout << "4"; for (int i = 0; i < a2 - a4 - 1; i++) cout << "7"; cout << "7"; return 0; } } else if (a3 == a4 + 1) { if (a1 - a3 >= 0 && a2 - a3 >= 0) { for (int i = 0; i < a1 - a3; i++) cout << "4"; for (int i = 0; i < a3; i++) cout << "47"; for (int i = 0; i < a2 - a3; i++) cout << "7"; return 0; } } else if (a3 + 1 == a4) { if (a1 - a4 + 1 > 0 && a2 - a4 + 1 > 0) { cout << "7"; for (int i = 0; i < a1 - a4 + 1; i++) cout << "4"; for (int i = 0; i < a4 - 2; i++) cout << "74"; for (int i = 0; i < a2 - a4 + 1; i++) cout << "7"; cout << "4"; return 0; } } cout << "-1"; return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-9; const int inf = 2000000000; const long long infLL = 9000000000000000000; int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; template <typename first, typename second> ostream &operator<<(ostream &os, const pair<first, second> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "}"; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename first, typename second> ostream &operator<<(ostream &os, const map<first, second> &v) { os << "["; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } void faltu() { cerr << '\n'; } template <typename T> void faltu(T a[], int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << '\n'; } template <typename T, typename... hello> void faltu(T arg, const hello &...rest) { cerr << arg << ' '; faltu(rest...); } void func(int a, int b, int c, int d) { vector<int> ans; d -= (c - 1); while (c--) { ans.push_back(4), ans.push_back(7); a--; b--; } if (a <= 0 && d == 1) { if (b <= 0) { cout << "-1"; return; } ans.insert(ans.begin(), 7); b--; d--; } else if (d == 1) { ans.push_back(4); a--; } if (b > 0) { while (b--) { int l = ans.size() - 1; ans.insert(ans.begin() + l, 7); } } if (a > 0) while (a--) cout << "4"; for (int &x : ans) cout << x; cout << '\n'; } void func2(int a, int b, int c, int d) { vector<int> ans; while (d--) { ans.push_back(7), ans.push_back(4); a--; b--; } while (b--) { int l = ans.size() - 1; ans.insert(ans.begin() + l, 7); } cout << *ans.begin(); if (a > 0) while (a--) cout << "4"; for (int i = 1; i < ans.size(); i++) cout << ans[i]; cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int a, b, c, d; cin >> a >> b >> c >> d; if (abs(c - d) > 1 || a < c || a < d || b < c || b < d) cout << "-1" << '\n'; else { if (c >= d) func(a, b, c, d); else func2(a, b, c, d); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; while (scanf("%d%d%d%d", &a, &b, &c, &d) != -1) { int i, j; a -= c; b -= c; d -= (c - 1); int s = 0, t = 0; if (d == 2) { s = 1; t = 1; a--; b--; } else if (d == 1) { if (a == 0) { b--; s = 1; } else { a--; t = 1; } } if (a < 0 || b < 0 || d < 0 || d >= 3) { puts("-1"); } else { if (s) printf("7"); for (i = 0; i < a; i++) printf("4"); for (i = 0; i < c; i++) printf("47"); for (i = 0; i < b; i++) printf("7"); if (t) printf("4"); puts(""); } } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int MAX = 2e5 + 5; const long long MAX2 = 11; const long long MOD = 1000000007; const long long MOD2 = 1000005329; const long long INF = 2e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 2000; long long a, b, c, d, tmp, sz; vector<int> ans; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> a >> b >> c >> d; if (min(a, b) < max(c, d) || abs(c - d) > 1) return cout << "-1\n", 0; if (c == d && max(a, b) <= c) return cout << "-1\n", 0; if (c > d) { for (int i = 1; i <= c; ++i) ans.push_back(4), ans.push_back(7), --a, --b; } else if (c < d) { for (int i = 1; i <= d; ++i) ans.push_back(7), ans.push_back(4), --a, --b; } else { if (a == c) { ans.push_back(7), --b; for (int i = 1; i <= c; ++i) ans.push_back(4), ans.push_back(7), --a, --b; } else { ans.push_back(4), --a; for (int i = 1; i <= c; ++i) ans.push_back(7), ans.push_back(4), --a, --b; } } if (ans.back() == 4) { ans.pop_back(); for (int i = 1; i <= b; ++i) ans.push_back(7); ans.push_back(4); } else for (int i = 1; i <= b; ++i) ans.push_back(7); for (auto i : ans) { cout << i; while (i == 4 && a) cout << 4, --a; } cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); int c4, c7, c47, c74; cin >> c4 >> c7 >> c47 >> c74; if (abs(c47 - c74) > 1) { cout << -1; return 0; } else if (c47 == c74 + 1) { if (c4 - c74 <= 0 || c7 - c74 <= 0) { cout << -1; return 0; } cout << string(c4 - c74, '4'); for (int i = 0; i < c74; i++) cout << "74"; cout << string(c7 - c74, '7'); } else if (c74 == c47 + 1) { if (c7 - c47 <= 0 || c4 - c47 <= 0) { cout << -1; return 0; } cout << '7'; for (int i = 0; i < c47; i++) { cout << "4"; if (i == 0 && c4 - c47 - 1 > 0) cout << string(c4 - c47 - 1, '4'); cout << "7"; } if (c7 - c47 - 1) cout << string(c7 - c47 - 1, '7'); cout << 4; } else { if (c47 + 1 > c4) { if (c74 + 1 > c7 || c4 < c74) { cout << -1; return 0; } for (int i = 0; i < c74; i++) { cout << "74"; if (c4 - c74 && i == 0) cout << string(c4 - c74, '4'); } cout << string(c7 - c74, '7'); } else { if (c7 < c47) { cout << -1; return 0; } if (c4 - c47 - 1) cout << string(c4 - c47 - 1, '4'); for (int i = 0; i < c47; i++) cout << "47"; if (c7 - c47) cout << string(c7 - c47, '7'); cout << "4"; } } }
#include <bits/stdc++.h> using namespace std; int i, a4, a7, a47, a74, tot; int S[6000000]; int main() { cin >> a4 >> a7 >> a47 >> a74; if (abs(a47 - a74) > 1 || max(a47, a74) > min(a4, a7)) { cout << "-1" << endl; return 0; } if (a47 > a74) { for (i = 1; i <= a47; ++i) { S[++tot] = 4; S[++tot] = 7; a4--; a7--; } for (i = 1; i <= a4; ++i) printf("4"); for (i = 1; i <= tot; ++i) printf("%d", S[i]); for (i = 1; i <= a7; ++i) printf("7"); } else if (a74 > a47) { for (i = 1; i <= a74; ++i) { S[++tot] = 7; S[++tot] = 4; a4--; a7--; } printf("%d", S[1]); for (i = 1; i <= a4; ++i) printf("4"); for (i = 2; i < tot; ++i) printf("%d", S[i]); for (i = 1; i <= a7; ++i) printf("7"); printf("%d", S[tot]); } else { for (i = 1; i <= a47; ++i) { S[++tot] = 4; S[++tot] = 7; a4--; a7--; } if (a4 > 0) { a4--; for (i = 1; i <= a7; ++i) S[++tot] = 7; S[++tot] = 4; } else if (a7 > 0) { a7--; printf("7"); for (i = 1; i <= a7; ++i) S[++tot] = 7; } else { cout << -1 << endl; return 0; } for (i = 1; i <= a4; ++i) printf("4"); for (i = 1; i <= tot; ++i) printf("%d", S[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; long long a1, a2, a3, a4; string s; signed main() { cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) { return cout << "-1", 0; } bool ok = 0; if (a3 == a4 + 1) { a1 -= a3; a2 -= a3; if (a1 < 0 || a2 < 0) return printf("-1\n"), 0; for (long long i = 1; i <= a1; i++) cout << '4'; for (long long i = 1; i <= a3; i++) cout << "47"; for (long long i = 1; i <= a2; i++) cout << '7'; } else if (a3 == a4) { if (a1 >= a3 + 1 && a2 >= a3) { a1 -= (a3 + 1); a2 -= a3; for (long long i = 1; i <= a1; i++) cout << '4'; for (long long i = 1; i <= a3; i++) cout << "47"; for (long long i = 1; i <= a2; ++i) cout << '7'; cout << '4'; } else if (a1 >= a3 && a2 >= a3 + 1) { a1 -= a3; a2 -= (a3 + 1); cout << '7'; for (long long i = 1; i <= a1; i++) cout << '4'; for (long long i = 1; i <= a3; i++) cout << "47"; for (long long i = 1; i <= a2; i++) cout << '7'; } else return printf("-1\n"), 0; } else { a1 -= a4; a2 -= a4; if (a1 < 0 || a2 < 0) return printf("-1\n"), 0; cout << '7'; for (long long i = 1; i <= a1; i++) cout << '4'; for (long long i = 1; i <= a3; i++) cout << "47"; for (long long i = 1; i <= a2; i++) cout << '7'; cout << '4'; } }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; bool try1(int no4, int no7, int no47, int no74) { if (no47 == no74 + 1) { string ans = ""; int no4s = no47; int no7s = no47; if (no4 < no4s || no7 < no7s) return false; if (no4s == 0 || no7s == 0) return false; while (no4 >= no4s) { ans.push_back('4'); no4--; } no4s--; for (int i = 0; i < no4s; i++) { ans.push_back('7'); no7--; ans.push_back('4'); no4--; } for (int i = 0; i < no7; i++) ans.push_back('7'); cout << ans << endl; return true; } else if (no47 == no74) { string ans = ""; int no4s = no47 + 1; int no7s = no47; if (no4 < no4s || no7 < no7s) return false; if (no4s == 0 || no7s == 0) return false; while (no4 >= no4s) { ans.push_back('4'); no4--; } no4s--; for (int i = 0; i < no4s; i++) { ans.push_back('7'); no7--; if (i == no4s - 1) { while (no7 > 0) { ans.push_back('7'); no7--; } } ans.push_back('4'); no4--; } cout << ans << endl; return true; } else return false; } bool try2(int no4, int no7, int no47, int no74) { if (no74 == no47 + 1) { string ans = ""; int no4s = no74; int no7s = no74; if (no4 < no4s || no7 < no7s) return false; if (no4s == 0 || no7s == 0) return false; ans.push_back('7'); no7--; while (no4 >= no4s) { ans.push_back('4'); no4--; } no4s--; for (int i = 0; i < no4s; i++) { ans.push_back('7'); no7--; if (i == no4s - 1) { while (no7 > 0) { ans.push_back('7'); no7--; } } ans.push_back('4'); no4--; } cout << ans << endl; return true; } else if (no74 == no47) { string ans = ""; int no4s = no74; int no7s = no74 + 1; if (no4 < no4s || no7 < no7s) return false; if (no4s == 0 || no7s == 0) return false; ans.push_back('7'); no7--; while (no4 >= no4s) { ans.push_back('4'); no4--; } no4s--; for (int i = 0; i < no4s; i++) { ans.push_back('7'); no7--; ans.push_back('4'); no4--; } while (no7 > 0) { ans.push_back('7'); no7--; } cout << ans << endl; return true; } else return false; } int main() { ios_base::sync_with_stdio(false); int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (a3 == a4) { if (!try1(a1, a2, a3, a4) && !try2(a1, a2, a3, a4)) cout << -1 << endl; } else if (a3 == a4 + 1) { if (!try1(a1, a2, a3, a4) && !try2(a1, a2, a3, a4)) cout << -1 << endl; } else if (a4 == a3 + 1) { if (!try1(a1, a2, a3, a4) && !try2(a1, a2, a3, a4)) cout << -1 << endl; } else { cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> int main() { int a1, a2, a3, a4, i, j, k, m, n; scanf("%d%d%d%d", &a1, &a2, &a3, &a4); if (a3 - a4 != 0) { if (a3 - a4 > 1 || a4 - a3 > 1) { printf("-1\n"); } else { if (a3 > a4) { if (a1 < a3 || a2 < a3) { printf("-1\n"); } else { m = a1 - a3; n = a2 - a3; for (i = 0; i < m; i++) printf("4"); for (i = 0; i < a3; i++) printf("47"); for (i = 0; i < n; i++) printf("7"); printf("\n"); } } else if (a4 > a3) { if (a1 < a4 || a2 < a4) { printf("-1\n"); } else { m = a1 - a4; n = a2 - a4; printf("74"); for (i = 0; i < m; i++) printf("4"); for (i = 2; i < a4; i++) printf("74"); for (i = 0; i < n; i++) printf("7"); printf("74"); printf("\n"); } } } } else { if (a3 == 0) { if (a2 * a1 != 0) printf("-1\n"); else { if (a1 != 0) { for (i = 0; i < a1; i++) printf("4"); } else { for (i = 0; i < a2; i++) printf("7"); } printf("\n"); } } else { if (a2 < a3 || a1 < a3 || (a2 == a3 && a1 == a3)) { printf("-1\n"); } else if (a1 == a3) { n = a2 - a3; for (i = 0; i < a3; i++) printf("74"); for (i = 0; i < n; i++) printf("7"); printf("\n"); } else if (a2 == a3) { m = a1 - a3; n = a2 - a3; for (i = 0; i < m; i++) printf("4"); for (i = 0; i < a3; i++) printf("74"); printf("\n"); } else { m = a1 - a3; n = a2 - a3; for (i = 1; i < m; i++) printf("4"); for (i = 0; i < a3; i++) printf("47"); for (i = 0; i < n; i++) printf("7"); printf("4"); printf("\n"); } } } }
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) { cout << "-1" << endl; return 0; } if (min(a1, a2) < max(a3, a4)) { cout << "-1" << endl; return 0; } if (a3 < a4) { cout << "7"; for (int i = a4; i <= a1; i++) cout << "4"; for (int i = 2; i < a4; i++) cout << "74"; for (int i = a4; i <= a2; i++) cout << "7"; cout << "4"; } if (a3 == a4) { if (a1 > a3) { for (int i = a3 + 2; i <= a1; i++) cout << "4"; for (int i = 1; i <= a3; i++) cout << "47"; for (int i = a3 + 1; i <= a2; i++) cout << "7"; cout << "4"; return 0; } if (a2 > a3) { cout << "74"; for (int i = a3 + 1; i <= a1; i++) cout << "4"; for (int i = 2; i <= a3; i++) cout << "74"; cout << "7"; for (int i = a3 + 2; i <= a2; i++) cout << "7"; return 0; } cout << "-1" << endl; return 0; } if (a3 > a4) { for (int i = a3 + 1; i <= a1; i++) cout << "4"; for (int i = 1; i <= a3; i++) cout << "47"; for (int i = a3 + 1; i <= a2; i++) cout << "7"; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if ((a1 < a2 ? a1 : a2) < (a3 > a4 ? a3 : a4)) { cout << -1; return 0; } if (a4 == a3 + 1) { { printf("74"); for (int i = 0; i < a1 - a4; i++) printf("4"); for (int i = 0; i < a4 - 2; i++) printf("74"); for (int i = 0; i < a2 - a4 + 1; i++) printf("7"); printf("4"); return 0; } } else if (a3 == a4) { if (a3 > a1 || a3 > a2 || (a3 == a1 && a3 == a2)) { cout << -1; return 0; } for (int i = 0; i < a1 - a3; i++) printf("4"); for (int i = 0; i < a3 - 1; i++) printf("74"); if (a1 > a3) for (int i = 0; i < a2 - a3 + 1; i++) printf("7"); else printf("7"); printf("4"); if (a1 <= a3) for (int i = 0; i < a2 - a3; i++) printf("7"); return 0; } else if (a4 == a3 - 1) { { for (int i = 0; i < a1 - a3; i++) printf("4"); for (int i = 0; i < a3; i++) printf("47"); for (int i = 0; i < a2 - a3; i++) printf("7"); return 0; } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int tc, cs = 1, i, j, k; int n, m; int a, b, c, d; cin >> a >> b >> c >> d; string ans = ""; int f = 0, s = 0; if (c == d) { if (c == 1) { if (a == 1) { ans = "747"; f = 1; s = 2; } else { ans = "474"; f = 2; s = 1; } } else if (c == 2) { if (b > a && (a != 10 || b != 100)) { ans = "74747"; f = 2; s = 3; } else { ans = "47474"; f = 3; s = 2; } } else if (a > b) { for (i = 0; i < c; i++) { ans += "47"; } ans += '4'; f = c + 1; s = c; } else if (a == b) { for (i = 0; i < c; i++) ans += "47"; ans += "74"; f = c + 1; s = c + 1; } else if (a != b) { if (a > b || (a == 100 && b == 200)) { for (i = 0; i < c; i++) ans += "47"; ans += "4"; f = c + 1; s = c; } else { for (i = 0; i < c; i++) ans += "74"; ans += "7"; f = c; s = c + 1; } } else { ans += '7'; for (i = 0; i < c; i++) ans += "47"; s = c + 1; f = c; } } else if (c - 1 == d) { ans += '4'; f++; for (i = 0; i < d; i++) ans += "74", f++, s++; ans += '7'; s++; } else if (d - 1 == c) { ans += '7'; s++; for (i = 0; i < c; i++) ans += "47", f++, s++; ans += '4'; f++; } else { cout << -1 << endl; return 0; } if (f > a || s > b) { cout << -1 << endl; return 0; } int pos1 = 0; for (i = 0; i < ans.size(); i++) { if (ans[i] == '4') { pos1 = i; break; } } string sub1 = ans.substr(0, pos1); string sub2 = ans.substr(pos1); for (i = 0; i < a - f; i++) { sub1 += '4'; } sub1 += sub2; ans = sub1; int pos2 = 0; for (i = 0; i < ans.size(); i++) { if (ans[i] == '7') pos2 = i; } sub1 = ans.substr(0, pos2); sub2 = ans.substr(pos2); for (i = 0; i < b - s; i++) { sub1 += '7'; } sub1 += sub2; cout << sub1 << endl; return 0; }
#include <bits/stdc++.h> int a1, a2, a3, a4; int main() { scanf("%d%d%d%d", &a1, &a2, &a3, &a4); if (a3 == a4 + 1) { if (a1 < a3) { puts("-1"); return 0; } if (a2 < a3) { puts("-1"); return 0; } for (int i = 0; i < a1 - a3; i++) printf("4"); for (int i = 0; i < a3; i++) printf("47"); for (int i = 0; i < a2 - a3; i++) printf("7"); puts(""); return 0; } if (a3 == a4) { if (a1 < a3) { puts("-1"); return 0; } if (a1 == a3) { if (a2 <= a3) { puts("-1"); return 0; } for (int i = 0; i < a3; i++) { printf("74"); } for (int i = 0; i < a2 - a3; i++) { printf("7"); } puts(""); return 0; } if (a1 > a3) { if (a2 < a3) { puts("-1"); return 0; } for (int i = 0; i < a1 - a3; i++) printf("4"); printf("7"); for (int i = 0; i < a4 - 1; i++) printf("47"); for (int i = 0; i < a2 - a3; i++) printf("7"); puts("4"); return 0; } } if (a3 == a4 - 1) { if (a1 <= a3) { puts("-1"); return 0; } if (a2 < a4) { puts("-1"); return 0; } printf("7"); for (int i = 0; i < a1 - a3; i++) { printf("4"); } for (int i = 1; i < a4 - 1; i++) { printf("74"); } for (int i = 0; i < a2 - a4 + 1; i++) { printf("7"); } printf("4\n"); return 0; } puts("-1"); }
#include <bits/stdc++.h> using namespace std; int a, b, c, d, tmp; string s; int main() { scanf("%d%d%d%d", &a, &b, &c, &d); if (abs(c - d) > 1) { printf("-1"); return 0; } if (c + 1 == d) { while (d--) { s += "74"; a--; b--; } } else if (c == d + 1) { while (c--) { s += "47"; a--; b--; } } else { while (c--) { s += "47"; a--; b--; } if (a > 0) s += "4", a--; else s = "7" + s, b--; } if (a < 0 || b < 0) { printf("-1"); return 0; } for (int i = s.size() - 1; i >= 0; i--) { if (s[i] == '7') { tmp = i; break; } } for (int i = 0; i < s.size(); i++) { putchar(s[i]); if (s[i] == '4' && a > 0) while (a--) putchar('4'); if (i == tmp) while (b--) putchar('7'); } }
#include <bits/stdc++.h> int a, b, c, d, n; char s[4 << 20]; void output() { for (int i = 0; i < n; ++i) if (s[i] == '4') a--; else b--; for (int i = 0; i < n; ++i) if (s[i] == '4') { while (a) { printf("4"); a--; } printf("4"); } else { while ((i + 2 >= n) && b) { printf("7"); b--; } printf("7"); } printf("\n"); exit(0); } void look(char c4, char c7, int a, int b, int c, int d) { n = 0; while (c) { if (n) d--; s[n++] = c4; s[n++] = c7; a--; b--; c--; } if (d > 0) { s[n++] = c4; a--; d--; } if ((a < 0) || (b < 0) || (c != 0) || (d != 0)) return; output(); } int main() { scanf("%d%d%d%d", &a, &b, &c, &d); look('4', '7', a, b, c, d); look('7', '4', b, a, d, c); printf("-1\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 30) - 1; const long double eps = 1e-9; const long double pi = fabs(atan2(0.0, -1.0)); int *ass; void ML() { for (;;) { ass = new int[2500000]; for (int i = 0; i < 2500000; i++) ass[i] = rand(); } } int c4, c7, c47, c74; int p4, p7, p47, p74; void LoAd() { cin >> c4 >> c7 >> c47 >> c74; } string res; void add4() { res.push_back('4'); } void add7() { res.push_back('7'); } void add47() { res.push_back('4'); res.push_back('7'); } void add74() { res.push_back('7'); res.push_back('4'); } void fail() { printf("-1"); exit(0); } void check() { int n = (int)res.size(); for (int i = 0; i < n; i++) if ('4' == res[i]) p4++; for (int i = 0; i < n; i++) if ('7' == res[i]) p7++; for (int i = 1; i < n; i++) if ('7' == res[i] && '4' == res[i - 1]) p47++; for (int i = 1; i < n; i++) if ('4' == res[i] && '7' == res[i - 1]) p74++; if (c47 != p47 || c74 != p74 || c4 != p4 || c7 != p7) fail(); } void SoLvE() { switch (c47 - c74) { case -1: if (c74 == 1) { for (int i = 0; i < c7; i++) add7(); for (int i = 0; i < c4; i++) add4(); } else { add74(); for (int i = c74; i < c4; i++) add4(); for (int i = 2; i < c74; i++) add74(); for (int i = c74; i < c7; i++) add7(); add74(); } break; case 0: if (c4 > c74) { for (int i = c74; i < c4; i++) add4(); for (int i = 1; i < c74; i++) add74(); for (int i = c74; i < c7; i++) add7(); add74(); } else { for (int i = 0; i < c74; i++) add74(); for (int i = c74; i < c7; i++) add7(); } break; case 1: for (int i = c74; i < c4; i++) add4(); for (int i = 0; i < c74; i++) add74(); for (int i = c74; i < c7; i++) add7(); break; default: fail(); } check(); cout << res; } int main() { srand((int)time(NULL)); LoAd(); SoLvE(); return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int a, b, c, d; cin >> a >> b >> c >> d; if (abs(c - d) > 1) { cout << -1 << '\n'; return; } if (min(a, b) < max(c, d)) { cout << -1 << '\n'; return; } int minn = min(c - 1, d); a -= minn; b -= minn; c -= minn; d -= minn; string ans; while (a > 0) ans += '4', a--; if (d >= c && ans.size() > 1) ans.pop_back(), a++; while (minn > 0) ans += "74", minn--; bool ok = b > 1; while (b > 0) ans += '7', b--; c--; if (d - c > 1) ans.pop_back(), b++; else if (d - c > 0 && a == 0 && ok) ans.pop_back(), b++; if (d - c > 0 && a > 0) ans += '4', a--, d--; if (d - c > 0 && b > 0) ans = '7' + ans, b--, d--; if (a != 0 || b != 0 || c != 0 || d != 0) { cout << -1 << '\n'; return; } cout << ans << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long a1, a2, a3, a4, i; string append(string *s) { string four, sev, st; st = *s; for (i = 0; i < a1; i++) four += '4'; for (i = 0; i < a2; i++) sev += '7'; long long fr, sv; for (i = 0; i < int(st.size()); i++) { if (st[i] == '4') { st.insert(i, four); break; } } for (i = int(st.size()) - 1; i >= 0; i--) { if (st[i] == '7') { st.insert(i, sev); break; } } return st; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { cin >> a1 >> a2 >> a3 >> a4; string ans, st; if (abs(a3 - a4) > 1) { cout << -1; return 0; } else if (a3 != a4) { if (a3 == a4 + 1) { char c = '4'; long long p = a3 + a4 + 1; while (p--) { st += c; if (c == '4') c = '7'; else c = '4'; } a1 -= a3; a2 -= a3; } else if (a3 = a4 - 1) { char c = '7'; long long p = a3 + a4 + 1; while (p--) { st += c; if (c == '4') c = '7'; else c = '4'; } a1 -= a4; a2 -= a4; } if (a1 < 0 || a2 < 0) { cout << -1; return 0; } else { ans = append(&st); cout << ans; return 0; } } char c = '4'; long long p = a3 + a4 + 1; while (p--) { st += c; if (c == '4') c = '7'; else c = '4'; } a1 -= (a3 + 1); a2 -= a4; if (a1 >= 0 and a2 >= 0) { ans = append(&st); cout << ans; return 0; } st.clear(); a1 += (a3 + 1); a2 += a4; p = a3 + a4 + 1; c = '7'; while (p--) { st += c; if (c == '4') c = '7'; else c = '4'; } a1 -= a3; a2 -= (a3 + 1); if (a1 >= 0 and a2 >= 0) { ans = append(&st); cout << ans; } else cout << -1; } return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b, c, d; void solve2() { long long tmp = a + b, aa = a, bb = b, cc = c, dd = d, lst = 0, fr = 1; string second = ""; for (int i = 1; i <= tmp; i++) { if (i % 2 == 0) second += '4', aa--; else second += '7', bb--, lst = i - 1; if (i > 1 && second.back() == '4' && second[i - 2] == '7') dd--; else if (i > 1 && second.back() == '7' && second[i - 2] == '4') cc--; if (cc < 0 || dd < 0 || aa < 0 || bb < 0) { cout << "-1\n"; return; } if (cc == 0 && dd == 0) break; } if (cc > 0 || dd > 0) { cout << "-1\n"; return; } for (int i = 0; i < second.size(); i++) { cout << second[i]; if (i == fr) { while (aa > 0) { aa--; cout << '4'; } } else if (i == lst) { while (bb > 0) { bb--; cout << '7'; } } } } void solve() { long long tmp = a + b, aa = a, bb = b, cc = c, dd = d, lst = 0; string second = ""; for (int i = 1; i <= tmp; i++) { if (i % 2) second += '4', aa--; else second += '7', bb--, lst = i - 1; if (i > 1 && second.back() == '4' && second[i - 2] == '7') dd--; else if (i > 1 && second.back() == '7' && second[i - 2] == '4') cc--; if (cc < 0 || dd < 0 || aa < 0 || bb < 0) { solve2(); return; } if (cc == 0 && dd == 0) break; } if (cc > 0 || dd > 0) { solve2(); return; } while (aa > 0) { cout << '4'; aa--; } for (int i = 0; i < second.size(); i++) { cout << second[i]; if (i == lst) { while (bb > 0) { bb--; cout << '7'; } } } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a >> b >> c >> d; if (abs(c - d) > 1) return cout << "-1\n", 0; solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e18; const long long mod = (long long)1e9 + 7; const double pi = acos(-1.0); const double eps = (double)1e-9; const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; const int N = 1000500; string s, ans; int a, b, c, d; string get() { int x, y; x = y = 0; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == '4') x++; else y++; } if (x > a || y > b) return "-1"; bool b1, b2; b1 = b2 = 0; int n1 = a - x; int n2 = b - y; x = 0; string res; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == '4') { if (!x) for (int j = 1; j <= n1; j++) res += '4'; x++; } else { y--; if (!y) for (int j = 1; j <= n2; j++) res += '7'; } res += s[i]; } return res; } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); cin >> a >> b >> c >> d; if (abs(c - d) > 1) { cout << -1; return 0; } if (c == d) { string x, y; x = y = ""; for (int i = 1; i <= c; i++) s += "47"; s += '4'; x = get(); s = ""; for (int i = 1; i <= d; i++) s += "74"; s += '7'; y = get(); if (x != "-1" && y != "-1") ans = min(x, y); else if (x == "-1") ans = y; else if (y == "-1") ans = x; else ans = "-1"; } else if (c > d) { for (int i = 1; i <= c; i++) s += "47"; ans = get(); } else { for (int i = 1; i <= d; i++) s += "74"; ans = get(); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; if (abs(c - d) > 1) { puts("-1"); return 0; } bool flag = false; int t1 = c, t2 = d; if (c >= d) { string res = ""; bool first = true; int cnt4 = 0, cnt7 = 0; while (c || d) { res += '4'; ++cnt4; if (!first) --d; if (!c) break; ++cnt7; res += '7'; --c; first = false; } int id7 = -1; if (cnt7 < b) { for (int i = res.size(); i >= 0; --i) if (res[i] == '7') { id7 = i; break; } } else if (cnt7 > b || c || d) { flag = true; } if (!flag) { if (cnt4 <= a) { for (int i = cnt4; i < a; ++i) printf("4"); for (int i = 0; i < res.size(); ++i) { if (i == id7) for (int j = cnt7; j < b; ++j) printf("7"); printf("%c", res[i]); } } else if (cnt4 > a) { flag = true; } } } else flag = true; if (flag) { c = t1; d = t2; string res = ""; bool first = true; int cnt4 = 0, cnt7 = 0; while (c || d) { res += '7'; ++cnt7; if (!first) --c; if (!d) break; ++cnt4; res += '4'; --d; first = false; } if (cnt4 > a || c || d) { puts("-1"); return 0; } int id7 = -1; if (cnt7 < b) { for (int i = res.size(); i >= 0; --i) if (res[i] == '7') { id7 = i; break; } } else if (cnt7 > b) { puts("-1"); return 0; } for (int i = 0; i < res.size(); ++i) { if (i == 1) for (int j = cnt4; j < a; ++j) printf("4"); if (i == id7) for (int j = cnt7; j < b; ++j) printf("7"); printf("%c", res[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 10, MAX = 2e5 + 1e4, MOD = 1e9 + 7, MAXL = 25; void OUT(long double o, int x) { cout << fixed << setprecision(x) << o; return; } long long a[MAX]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a[1] >> a[2] >> a[3] >> a[4]; if (abs(a[3] - a[4]) > 1) return cout << -1, 0; if (a[3] == a[4]) { if (max(a[1], a[2]) > a[3] && min(a[1], a[2]) >= a[3]) { if (a[1] >= a[2] || a[1] > a[3]) { for (int i = 1; i <= a[1] - a[3]; i++) cout << 4; cout << 7; for (int i = 2; i <= a[3]; i++) cout << 47; for (int i = 1; i <= a[2] - a[3]; i++) cout << 7; cout << 4; } else { cout << 7; for (int i = 1; i <= a[1] - a[3] + 1; i++) cout << 4; for (int i = 2; i <= a[3]; i++) cout << 74; for (int i = 1; i <= a[2] - a[3]; i++) cout << 7; } } else return cout << -1, 0; } else { if (min(a[1], a[2]) < max(a[3], a[4])) return cout << -1, 0; if (a[3] > a[4]) { for (int i = 1; i <= a[1] - a[3] + 1; i++) cout << 4; cout << 7; for (int i = 2; i <= a[3]; i++) cout << 47; for (int i = 1; i <= a[2] - a[3]; i++) cout << 7; } else { cout << 7; for (int i = 1; i <= a[1] - a[4] + 1; i++) cout << 4; for (int i = 2; i < a[4]; i++) cout << 74; for (int i = 1; i <= a[2] - a[4] + 1; i++) cout << 7; cout << 4; } } return 0; }
#include <bits/stdc++.h> int c4, c7; int main() { int i, a, b, c, d, ans = 0; scanf("%d %d %d %d", &a, &b, &c, &d); c4 = a; c7 = b; if (c == (d - 1)) { c4 -= c; c7 -= c; if (c4 < 0 || c7 < 0) printf("-1\n"); else if (c4 == 0 || c7 == 0) printf("-1\n"); else { printf("7"); for (i = 0; i < c4 - 1; ++i) printf("4"); for (i = 0; i < c; ++i) printf("47"); for (i = 0; i < c7 - 1; ++i) printf("7"); printf("4"); printf("\n"); } } else if (c == d) { c4 -= c; c7 -= c; if (c4 < 0 || c7 < 0) printf("-1\n"); else if (c4 == 0 && c7 == 0) printf("-1\n"); else if (c4 == 0) { printf("7"); for (i = 0; i < c4; ++i) printf("4"); for (i = 0; i < c; ++i) printf("47"); for (i = 0; i < c7 - 1; ++i) printf("7"); printf("\n"); } else { for (i = 0; i < c4 - 1; ++i) printf("4"); for (i = 0; i < c; ++i) printf("47"); for (i = 0; i < c7; ++i) printf("7"); printf("4"); printf("\n"); } } else if (c == (d + 1)) { c4 -= c; c7 -= c; if (c4 < 0 || c7 < 0) printf("-1\n"); else { for (i = 0; i < c4; ++i) printf("4"); for (i = 0; i < c; ++i) printf("47"); for (i = 0; i < c7; ++i) printf("7"); printf("\n"); } } else { printf("-1\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); long long a, b, c, d; cin >> a >> b >> c >> d; if (c == d) { if (a >= c && b >= c && (a != c || b != c)) { if (a - c == 0) { cout << 7; } else { for (long long i = 0; i < a - c - 1; i++) { cout << 4; } } for (long long i = 0; i < c; i++) { cout << 47; } if (a - c == 0) { for (long long i = 0; i < b - c - 1; i++) { cout << 7; } } else { for (long long i = 0; i < b - c; i++) { cout << 7; } cout << 4; } } else { cout << -1; } } else if (c > d && c - d == 1 && a >= c && b >= c) { for (long long i = 0; i < a - c; i++) cout << 4; for (long long i = 0; i < c; i++) { cout << 47; } for (long long i = 0; i < b - c; i++) cout << 7; } else if (d > c && d - c == 1 && a >= d && b >= d) { for (long long i = 0; i < d; i++) { cout << 74; if (i == 0) { for (long long i = 0; i < a - d; i++) { cout << 4; } } if (i == d - 2) { for (long long i = 0; i < b - d; i++) { cout << 7; } } } } else { cout << -1; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e5 + 5; const long long INF = 1e6; long long a1, a2, a3, a4; bool beyond(long long x, long long y) { if (x > a1 || y > a2) { return true; } return false; } int main() { while (~scanf("%lld %lld %lld %lld", &a1, &a2, &a3, &a4)) { if (fabs(a3 - a4) >= 2) { printf("-1\n"); return 0; } else if (a3 == a4) { if (!beyond(a3 + 1, a4)) { a1 -= a3 + 1; a2 -= a4; for (long long i = 1; i <= a1 + 1; i++) { printf("4"); } printf("7"); for (long long i = 2; i <= a3; i++) { printf("47"); } for (long long i = 1; i <= a2; i++) { printf("7"); } printf("4"); } else if (!beyond(a3, a4 + 1)) { a1 -= a3; a2 -= a4 + 1; printf("7"); for (long long i = 1; i <= a1 + 1; i++) { printf("4"); } for (long long i = 2; i <= a3; i++) { printf("74"); } while (a2) { printf("7"); a2--; } printf("7"); } else { printf("-1\n"); } } else if (a3 > a4) { if (!beyond(a3, a3)) { a1 -= a3; a2 -= a3; for (long long i = 1; i <= a1 + 1; i++) { printf("4"); } printf("7"); for (long long i = 2; i <= a3; i++) { printf("47"); } for (long long i = 1; i <= a2; i++) { printf("7"); } } else { printf("-1"); return 0; } } else { if (!beyond(a4, a4)) { a1 -= a4; a2 -= a4; printf("7"); for (long long i = 1; i <= a1 + 1; i++) { printf("4"); } for (long long i = 2; i <= a4 - 1; i++) { printf("74"); } a2++; while (a2) { printf("7"); a2--; } printf("4"); } else { printf("-1"); } } } }
#include <bits/stdc++.h> using namespace std; int main() { int f, s, fs, sf; cin >> f >> s >> fs >> sf; if (abs(fs - sf) >= 2) { puts("-1"); return 0; } if (fs == sf) { if (f < sf) { puts("-1"); return 0; } if (s < sf) { puts("-1"); return 0; } if (max(s, f) < sf + 1) { puts("-1"); return 0; } deque<char> res; for (int i = 0; i < fs; ++i) { if (f - sf - 1 >= 0) { res.push_back('4'); res.push_back('7'); } else { res.push_back('7'); res.push_back('4'); } } if (f - sf - 1 >= 0) { int rem4 = f - fs - 1; int rem7 = s - fs; while (rem4--) res.push_front('4'); while (rem7--) res.push_back('7'); res.push_back('4'); for (char c : res) { printf("%c", c); } puts(""); return 0; } res.push_back('7'); int rem4 = f - fs; int rem7 = s - fs - 1; while (rem7--) res.push_back('7'); res.pop_front(); while (rem4--) res.push_front('4'); res.push_front('7'); for (char c : res) printf("%c", c); puts(""); return 0; } else { if (f < max(sf, fs)) { puts("-1"); return 0; } if (s < max(sf, fs)) { puts("-1"); return 0; } deque<char> res; if (sf > fs) { for (int i = 0; i < sf; ++i) { res.push_back('7'); res.push_back('4'); } } else { for (int i = 0; i < fs; ++i) { res.push_back('4'); res.push_back('7'); } } int rem4 = f - max(sf, fs); int rem7 = s - max(sf, fs); if (sf > fs) { res.pop_back(); while (rem7--) res.push_back('7'); res.push_back('4'); res.pop_front(); while (rem4--) res.push_front('4'); res.push_front('7'); } else { while (rem4--) res.push_front('4'); while (rem7--) res.push_back('7'); } for (char c : res) printf("%c", c); puts(""); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; string ans; int qa, qb; ans.clear(); qa = a - c - (c == d), qb = b - c; if (d >= c - 1 && d <= c && qa >= 0 && qb >= 0) { for (int i = (int)0; i < (int)qa; i++) cout << '4'; for (int i = (int)0; i < (int)c; i++) cout << "47"; for (int i = (int)0; i < (int)qb; i++) cout << '7'; if (c == d) cout << '4'; cout << '\n'; return 0; } ans.clear(); qa = a - d, qb = b - d - (c == d); if (c >= d - 1 && c <= d && qa >= 0 && qb >= 0) { cout << '7'; for (int i = (int)0; i < (int)qa + 1; i++) cout << '4'; for (int i = (int)0; i < (int)d - 2; i++) cout << "74"; if (c == d) { if (d > 1) cout << "74"; for (int i = (int)0; i < (int)qb + 1; i++) cout << '7'; } else { for (int i = (int)0; i < (int)qb + 1; i++) cout << '7'; if (d > 1) cout << "4"; } cout << '\n'; return 0; } puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int a1, a2, a3, a4; deque<char> ans; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> a1 >> a2 >> a3 >> a4; if (a3 > min(a1, a2) || a4 > min(a1, a2) || abs(a3 - a4) > 1) { cout << "-1"; return 0; } for (int i = 1; i <= a3; i++) { ans.push_back('4'); ans.push_back('7'); } a1 -= a3; a2 -= a3; a4 -= max(0, a3 - 1); a3 = 0; if (a1 && a4) { ans.push_back('4'); a1--; a4--; } if (a2 && a4) { ans.push_front('7'); a2--; a4--; } if (a3 || a4) { cout << "-1"; return 0; } int first4 = -1, last7 = 0; for (int i = 0; i < ans.size(); i++) { if ((ans[i] == '4') && first4 == -1) first4 = i; if (ans[i] == '7') last7 = i; } for (int i = 0; i < ans.size(); i++) { if (i == first4) { while (a1 > 0) { cout << "4"; a1--; } } if (i == last7) { while (a2 > 0) { cout << "7"; a2--; } } cout << ans[i]; } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; using namespace chrono; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } long long int advceil(long long int num, long long int den) { return (num % den == 0 ? num / den : num / den + 1); } long long int lstbt(long long int val) { long long int msk = val & (val - 1); return log2(val ^ msk); } long long int modmul(long long int a, long long int b) { a %= 1000000007; b %= 1000000007; long long int res = 0; while (b > 0) { if (b & 1) res = (res + a) % 1000000007; a = (a * 2) % 1000000007; b /= 2; } return res; } long long int modexpo(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = modmul(res, a); a = modmul(a, a); b /= 2; } return res; } long long int gcd(long long int a, long long int b) { return a == 0 ? b : gcd(b % a, a); } vector<long long int> CALCfactor(long long int n) { vector<long long int> factor(n + 2, 0); for (long long int i = 4; i <= n; i += 2) factor[i] = 2; for (long long int j = 3; j <= n; j += 2) { if (factor[j]) continue; for (long long int i = 2 * j; i <= n; i += j) factor[i] = j; } return factor; } vector<long long int> CALCprimeNUM(long long int n) { vector<long long int> factor = CALCfactor(n); vector<long long int> primeNUM; primeNUM.reserve(n + 2); for (long long int i = 2; i <= n; i++) if (!factor[i]) primeNUM.push_back(i); return primeNUM; } vector<long long int> CALCprimeFACTOR(long long int n) { vector<long long int> factor = CALCfactor(n); vector<long long int> ans; while (factor[n] != 0) { ans.push_back(factor[n]); n /= factor[n]; } ans.push_back(n); return ans; } vector<long long int> unique(vector<long long int> x) { sort(x.begin(), x.end()); set<long long int> s; vector<long long int> ans; ans.reserve(x.size()); for (auto elem : x) s.insert(elem); for (auto elem : s) ans.push_back(elem); return ans; } vector<long long int> ZALGO(string s, string pat) { long long int lens = pat.length(); pat += char(36); pat += s; s = pat; vector<long long int> pref(s.length() + 1, 0); pref[0] = s.length(); long long int spt = -1; long long int ept = -1; long long int processCnt = 0; for (long long int i = 1; i < s.length(); i++) { bool isdone = false; if (i <= ept and i >= spt) { if (pref[i - spt] + i <= ept) pref[i] = pref[i - spt], isdone = true; processCnt++; } if (!isdone) { long long int ptr = i; long long int lo = 0; while (s[lo] == s[ptr]) ptr++, lo++, processCnt++; if (ept < ptr - 1) { spt = i; ept = ptr - 1; } pref[i] = lo; } } vector<long long int> ans; ans.reserve(s.length() - lens); for (long long int i = lens + 1; i < s.length(); i++) ans.push_back(pref[i]); return ans; } void spclSort( vector<pair<pair<long long int, long long int>, long long int>>& v) { long long int n = v.size(); { vector<long long int> cnt(n); for (auto elem : v) { cnt[elem.first.second]++; } vector<pair<pair<long long int, long long int>, long long int>> a_new(n); vector<long long int> pos(cnt.size()); pos[0] = 0; for (long long int i = 1; i < cnt.size(); i++) { pos[i] = cnt[i - 1] + pos[i - 1]; } for (auto elem : v) { a_new[pos[elem.first.second]] = elem; pos[elem.first.second]++; } v = a_new; } { vector<long long int> cnt(n, 0); for (auto elem : v) { cnt[elem.first.first]++; } vector<pair<pair<long long int, long long int>, long long int>> a_new(n); vector<long long int> pos(cnt.size()); pos[0] = 0; for (long long int i = 1; i < cnt.size(); i++) { pos[i] = cnt[i - 1] + pos[i - 1]; } for (auto elem : v) { a_new[pos[elem.first.first]] = elem; pos[elem.first.first]++; } v = a_new; } } pair<vector<vector<long long int>>, vector<vector<long long int>>> getOrdering( string s) { s += char(36); vector<vector<long long int>> dp; dp.reserve(log2(s.length()) + 3); vector<vector<long long int>> dp2; dp2.reserve(log2(s.length()) + 3); vector<long long int> ordering(s.length()), eqClass(s.length()); { vector<pair<char, long long int>> temp(s.length()); for (long long int i = 0; i < s.length(); i++) temp[i] = {s[i], i}; sort(temp.begin(), temp.end()); for (long long int i = 0; i < temp.size(); i++) ordering[temp[i].second] = i; eqClass[temp[0].second] = 0; for (long long int i = 1; i < temp.size(); i++) { if (temp[i].first != temp[i - 1].first) eqClass[temp[i].second] = eqClass[temp[i - 1].second] + 1; else eqClass[temp[i].second] = eqClass[temp[i - 1].second]; } dp.push_back(ordering); dp2.push_back(eqClass); } long long int k = 1; while ((1 << (k - 1)) < s.length()) { vector<pair<pair<long long int, long long int>, long long int>> arr( s.length()); for (long long int i = 0; i < s.length(); i++) { arr[i] = {{eqClass[i], eqClass[(i + (1 << (k - 1))) % s.length()]}, i}; } spclSort(arr); for (long long int i = 0; i < s.length(); i++) ordering[arr[i].second] = i; eqClass[arr[0].second] = 0; for (long long int i = 1; i < s.size(); i++) { if (arr[i].first != arr[i - 1].first) eqClass[arr[i].second] = eqClass[arr[i - 1].second] + 1; else eqClass[arr[i].second] = eqClass[arr[i - 1].second]; } dp.push_back(ordering); dp2.push_back(eqClass); k++; } return {dp, dp2}; } vector<long long int> sortOrdering(vector<vector<long long int>> dp) { vector<long long int> arr = dp[dp.size() - 1]; vector<long long int> ans(arr.size()); long long int cnt = 0; for (auto elem : arr) { ans[elem] = cnt; cnt++; } return ans; } vector<long long int> getLps(string pat) { long long int i = 1; long long int j = 0; vector<long long int> lps(pat.length(), 0); lps[0] = 0; while (i < pat.length()) { if (pat[i] == pat[j]) lps[i] = j + 1, i++, j++; else if (j == 0) lps[i] = 0, i++; else j = lps[j - 1]; } return lps; } pair<vector<long long int>, vector<long long int>> getFact(long long int n) { vector<long long int> fact(n + 1, 1), invfact(n + 1, 1); for (long long int i = 1; i <= n; i++) fact[i] = (i * (fact[i - 1])) % 1000000007; for (long long int i = 1; i <= n; i++) invfact[i] = (modexpo(i, 1000000007 - 2) * invfact[i - 1]) % 1000000007; return {fact, invfact}; } void solve() { long long int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) { cout << "-1" << "\n"; return; } string s = ""; if (a3 > a4 or (a3 == a4 and a3 + 1 <= a1)) for (long long int i = 1; i <= a3; i++) s += "47"; else for (long long int i = 1; i <= a4; i++) s += "74"; if (a3 == a4 and s[0] == '4') s += "4"; if (a3 == a4 and s[0] == '7') s += "7"; long long int acnt = 0, bcnt = 0; for (long long int i = 0; i < s.length(); i++) acnt += (s[i] == '4'), bcnt += (s[i] == '7'); if (acnt > a1 or bcnt > a2) { cout << "-1" << "\n"; return; } long long int rema = a1 - acnt; long long int remb = a2 - bcnt; for (long long int i = 0; i < s.length(); i++) if (s[i] == '4') { s[i] = '-'; break; } for (long long int i = s.length() - 1; i >= 0; i--) if (s[i] == '7') { s[i] = '$'; break; } for (long long int i = 0; i < s.length(); i++) { if (s[i] == '$' and (++remb)) while (remb > 0 and remb--) cout << "7"; else if (s[i] == '-' and (++rema)) while (rema > 0 and rema--) cout << "4"; else cout << s[i]; } cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start1 = high_resolution_clock::now(); solve(); auto stop1 = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop1 - start1); }
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, a4; int i, j; cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) return cout << -1 << endl, 0; string str = ""; if (a3 == a4) { for (i = 1; i <= a3; i++) { if (i == 1) str += "474"; else str += "74"; } bool flag = false; int c1 = 0, c2 = 0; for (i = 0; i < str.size(); i++) { c1 += str[i] == '4'; c2 += str[i] == '7'; } if (c1 > a1) { str.clear(); for (i = 1; i <= a3; i++) { if (i == 1) str += "747"; else str += "47"; } } } else if (a3 > a4) { for (i = 1; i <= a4; i++) { if (i == 1) str += "474"; else str += "74"; } str += "7"; } else { for (i = 1; i <= a3; i++) { if (i == 1) str += "747"; else str += "47"; } str += "4"; } int c1 = 0, c2 = 0; for (i = 0; i < str.size(); i++) { c1 += str[i] == '4'; c2 += str[i] == '7'; if (c1 > a1 || c2 > a2) return cout << -1 << endl, 0; } a1 -= c1; a2 -= c2; string aa = "", bb = ""; for (i = 0; i < a1; i++) aa += "4"; for (i = 0; i < a2; i++) bb += "7"; if (str.size() == 0) str = aa + bb; else { for (i = 0;; i++) if (str[i] == '4') break; str = str.substr(0, i + 1) + aa + str.substr(i + 1, str.size() - i); for (i = str.size() - 1;; i--) if (str[i] == '7') break; str = str.substr(0, i + 1) + bb + str.substr(i + 1, str.size() - i); } cout << str << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); if (a < max(c, d) || b < max(c, d)) { printf("-1\n"); } else if (c == 0 && d == 0) { for (int i = 0; i < a; i++) printf("4"); for (int i = 0; i < b; i++) printf("7"); printf("\n"); } else if (c == d) { if (a == b && a == c) { printf("-1\n"); } else if (a == c) { for (int i = 0; i < c; i++) printf("74"); for (int i = 0; i < b - c; i++) printf("7"); printf("\n"); } else if (b == c) { for (int i = 0; i < a - c; i++) printf("4"); for (int i = 0; i < c; i++) printf("74"); printf("\n"); } else { for (int i = 0; i < a - c - 1; i++) printf("4"); for (int i = 0; i < c; i++) printf("47"); for (int i = 0; i < b - c; i++) printf("7"); printf("4"); printf("\n"); } } else if (c - d == 1) { for (int i = 0; i < a - c; i++) printf("4"); for (int i = 0; i < c; i++) printf("47"); for (int i = 0; i < b - c; i++) printf("7"); printf("\n"); } else if (d - c == 1) { printf("74"); for (int i = 0; i < a - d; i++) printf("4"); for (int i = 0; i < d - 2; i++) printf("74"); for (int i = 0; i < b - d; i++) printf("7"); printf("74"); printf("\n"); } else printf("-1\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string res; int a4, a7, a47, a74; cin >> a4 >> a7 >> a47 >> a74; if (a47 == a74) { if (a4 >= a47 + 1 && a7 >= a74) { for (int i = 0; i < a4 - a74 - 1; i++) res.push_back('4'); for (int i = 0; i < a47; i++) res.append("47"); for (int i = 0; i < a7 - a74; i++) res.push_back('7'); res.push_back('4'); } else if (a4 >= a47 && a7 >= a74 + 1) { res.push_back('7'); for (int i = 0; i < a4 - a74; i++) res.push_back('4'); for (int i = 0; i < a47; i++) res.append("47"); for (int i = 0; i < a7 - a74 - 1; i++) res.push_back('7'); } else goto impos; } else if (a47 == a74 + 1) { if (a4 >= a47 && a7 >= a47) { for (int i = 0; i < a4 - a47; i++) res.push_back('4'); for (int i = 0; i < a47; i++) res.append("47"); for (int i = 0; i < a7 - a47; i++) res.push_back('7'); } else goto impos; } else if (a74 == a47 + 1) { if (a4 >= a74 && a7 >= a74) { res.push_back('7'); for (int i = 0; i < a4 - a47 - 1; i++) res.push_back('4'); for (int i = 0; i < a47; i++) res.append("47"); for (int i = 0; i < a7 - a47 - 1; i++) res.push_back('7'); res.push_back('4'); } else goto impos; } else goto impos; pos: cout << res << endl; return 0; impos: cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a1, a2, a3, a4; int main() { scanf("%d%d%d%d", &a1, &a2, &a3, &a4); if (min(a1, a2) < max(a3, a4)) { puts("-1"); return 0; } if (a1 + a2 == a3 + a4) { puts("-1"); return 0; } if (a3 - a4 == 1) { int n = a3; for (int i = 1; i <= a1 - n + 1; ++i) { putchar('4'); } for (int i = 1; i < n; ++i) { putchar('7'); putchar('4'); } for (int i = 1; i <= a2 - n + 1; ++i) { putchar('7'); } return 0; } if (a3 == a4) { int n = a3 + 1; if (a1 >= n) { for (int i = 1; i <= a1 - n + 1; ++i) { putchar('4'); } for (int i = 1; i < n - 1; ++i) { putchar('7'); putchar('4'); } for (int i = 1; i <= a2 - n + 2; ++i) { putchar('7'); } putchar('4'); return 0; } else { for (int i = 1; i < n; ++i) { putchar('7'); putchar('4'); } for (int i = 1; i <= a2 - n + 1; ++i) { putchar('7'); } return 0; } } if (a4 - a3 == 1) { int n = a4; putchar('7'); for (int i = 1; i <= a1 - n + 1; ++i) { putchar('4'); } for (int i = 1; i < n - 1; ++i) { putchar('7'); putchar('4'); } for (int i = 1; i <= a2 - n + 1; ++i) { putchar('7'); } putchar('4'); return 0; } puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int n, i, j, k, sum, tmp, ans, a, b, c, d; int arr[1000010]; char s[1000010]; bool flag; int main() { cin >> a >> b >> c >> d; if ((c > d ? c : d) - (c < d ? c : d) > 1) { cout << -1 << endl; return 0; } if ((c > d ? c : d) > (a < b ? a : b)) { cout << -1 << endl; return 0; } if (c == d && a == b && a == c) { cout << -1 << endl; return 0; } if (c == d) { if (a > c) { for (i = 0; i < a - c - 1; i++) cout << "4"; for (i = 0; i < c; i++) cout << "47"; for (i = 0; i < b - d; i++) cout << "7"; cout << "4" << endl; } else { for (i = 0; i < d; i++) cout << "74"; for (i = 0; i < b - d; i++) cout << "7"; } } else if (c > d) { for (i = 0; i < a - c; i++) cout << "4"; for (i = 0; i < c; i++) cout << "47"; for (i = 0; i < b - c; i++) cout << "7"; } else { cout << "7"; for (i = 0; i < a - c; i++) cout << "4"; for (i = 0; i < c - 1; i++) cout << "74"; for (i = 0; i < b - c; i++) cout << "7"; cout << "4"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) { cout << -1; return 0; } if (a3 > a1 || a4 > a2 || a4 > a1 || a3 > a2 || (a1 == a3 && a2 == a4)) { cout << -1; return 0; } if (a3 > a4) { for (int i = 0; i < a1 - a3; i++) cout << 4; for (int i = 0; i < a3; i++) cout << 47; for (int i = 0; i < a2 - 1 - a4; i++) cout << 7; return 0; } if (a4 > a3) { cout << 7; for (int i = 0; i < a1 - a4; i++) cout << 4; for (int i = 0; i < a3; i++) cout << 47; for (int i = 0; i < a2 - a4; i++) cout << 7; cout << 4; return 0; } if (a4 == a3) { if (a1 == a3) { for (int i = 0; i < a4; i++) cout << 74; for (int i = 0; i < a2 - a4; i++) cout << 7; return 0; } else { for (int i = 0; i < a1 - a3 - 1; i++) cout << 4; for (int i = 0; i < a3; i++) cout << 47; for (int i = 0; i < a2 - a4; i++) cout << 7; cout << 4; } } }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; const int oo = ~0u >> 2, mo = (int)1e9 + 7; const int mn = 510; int a, b, c, d; string s; int main() { cin >> a >> b >> c >> d; s = "4", --a; while (a && b && c && d) s += "74", --a, --b, --c, --d; if (c && b) s += "7", --c, --b; if (d && b) s = "7" + s, --d, --b; if (c || d) { printf("-1\n"); return 0; } string l, r; int i = 0; while (i < ((int)(s).size()) && s[i] != '4') ++i; l = s.substr(0, i), r = s.substr(i, ((int)(s).size())); while (a) l += "4", --a; s = l + r; i = ((int)(s).size()); while (i && s[i - 1] != '7') --i; l = s.substr(0, i), r = s.substr(i, ((int)(s).size())); while (b) l += "7", --b; s = l + r; cout << s << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; string ans; if (a3 == a4 + 1) { for (int i = 0; i < a1 - a3; i++) ans += '4'; for (int i = 0; i < a3; i++) ans += "47"; for (int i = 0; i < a2 - a3; i++) ans += '7'; } else if (a3 == a4) { if (0 <= a1 - a3 - 1) { for (int i = 0; i < a1 - a3 - 1; i++) ans += '4'; for (int i = 0; i < a3; i++) ans += "47"; for (int i = 0; i < a2 - a3; i++) ans += '7'; ans += '4'; } else { ans = "7"; for (int i = 0; i < a1 - a3; i++) ans += '4'; for (int i = 0; i < a3; i++) ans += "47"; for (int i = 0; i < a2 - a3 - 1; i++) ans += '7'; } } else if (a3 == a4 - 1) { ans += "74"; for (int i = 0; i < a1 - a4; i++) ans += '4'; for (int i = 0; i < a4 - 2; i++) ans += "74"; for (int i = 0; i < a2 - a4; i++) ans += '7'; ans += "74"; } int n = ans.size(); for (int i = 0; i < n; i++) { if (ans[i] == '4') a1--; if (ans[i] == '7') a2--; if (i < n && ans[i] == '4' && ans[i + 1] == '7') a3--; if (i < n && ans[i] == '7' && ans[i + 1] == '4') a4--; } if (a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0) cout << ans << endl; else cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 4000005; int s[MAX], a1, a2, a3, a4; int main() { int i, j, k; while (scanf("%d%d%d%d", &a1, &a2, &a3, &a4) != EOF) { if (abs(a4 - a3) > 1) { puts("-1"); continue; } if (a3 == a4 + 1) { if (a1 < a3 || a2 < a3) { puts("-1"); continue; } a1 -= a3; a2 -= a3; for (i = 1; i <= a1; i++) printf("4"); for (i = 1; i <= a3; i++) printf("47"); for (i = 1; i <= a2; i++) printf("7"); puts(""); } else if (a4 == a3 + 1) { if (a1 < a4 || a2 < a4) { puts("-1"); continue; } printf("7"); for (i = 1; i <= a1 - a4 + 1; i++) printf("4"); for (i = 1; i <= a4 - 2; i++) printf("74"); for (i = 1; i <= a2 - (a4 - 1); i++) printf("7"); printf("4\n"); puts(""); } else { if (a1 < a3 || a2 < a3 || a1 + a2 < a3 + a4 + 1) { puts("-1"); continue; } if (a1 == a3) { for (i = 1; i <= a3; i++) printf("74"); for (i = 1; i <= a2 - a3; i++) printf("7"); puts(""); } else if (a2 == a3) { for (i = 1; i <= a1 - a3 - 1; i++) printf("4"); for (i = 1; i <= a3; i++) printf("47"); printf("4\n"); } else { for (i = 1; i <= a1 - a3 - 1; i++) printf("4"); for (i = 1; i <= a3; i++) printf("47"); for (i = 1; i <= a2 - a3; i++) printf("7"); printf("4\n"); } } } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") using namespace std; long long a, b, c, d; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> a >> b >> c >> d; if (abs(c - d) > 1) { return cout << "-1", 0; } if (c == d) { if (a > c && b >= c) { a -= (c + 1); b -= c; for (long long i = 1; i <= a; i++) { cout << 4; } for (long long i = 1; i <= c; i++) { cout << 47; } for (long long i = 1; i <= b; i++) { cout << 7; } cout << 4; return 0; } else if (a >= c && b > c) { b -= (c + 1); a -= c; cout << '7'; for (long long i = 1; i <= a + 1; i++) { cout << 4; } for (long long i = 1; i < c; i++) { cout << 74; } for (long long i = 1; i <= b + 1; i++) { cout << 7; } return 0; } else return cout << "-1", 0; } if (a < max(c, d) || b < max(c, d)) { return cout << "-1", 0; } if (c < d) { a -= d; b -= d; cout << '7'; for (long long i = 1; i <= a + 1; i++) { cout << 4; } for (long long i = 1; i < d - 1; i++) { cout << 74; } for (long long i = 1; i <= b + 1; i++) { cout << 7; } cout << 4; } else { d = c; a -= d; b -= d; for (long long i = 1; i <= a; i++) { cout << 4; } for (long long i = 1; i <= d; i++) { cout << 47; } for (long long i = 1; i <= b; i++) { cout << 7; } } }
#include <bits/stdc++.h> using namespace std; void r1() { printf("-1"); return; } void r2(string rr) { printf("%s\n", rr.c_str()); return; } int main() { string ret; int i; int a1, a2, a3, a4; scanf("%d %d %d %d", &a1, &a2, &a3, &a4); if (abs(a3 - a4) > 1) { r1(); return 0; } if (a3 == a4) { if (a1 > a3 && a2 >= a4) { for (i = 1; i <= a1 - a3 - 1; i++) ret += '4'; for (i = 1; i <= a3; i++) ret += "47"; for (i = 1; i <= a2 - a4; i++) ret += '7'; ret += '4'; r2(ret); return 0; } else if (a1 >= a3 && a2 > a4) { ret += '7'; for (i = 1; i <= a1 - a3; i++) ret += '4'; for (i = 1; i <= a3; i++) ret += "47"; for (i = 1; i <= a2 - a4 - 1; i++) ret += '7'; r2(ret); return 0; } else { r1(); return 0; } } if (a3 < a4) { if (a1 >= a4 && a2 >= a4) { string tmp; for (i = 1; i <= a4; i++) tmp += "74"; string s4; for (i = 1; i <= a1 - a4; i++) s4 += '4'; string s7; for (i = 1; i <= a2 - a4; i++) s7 += '7'; ret = tmp.substr(0, 2) + s4 + tmp.substr(2, tmp.size() - 3) + s7 + '4'; r2(ret); return 0; } else { r1(); return 0; } } if (a3 > a4) { if (a1 >= a3 && a2 >= a3) { for (i = 1; i <= a1 - a3; i++) ret += '4'; for (i = 1; i <= a3; i++) ret += "47"; for (i = 1; i <= a2 - a3; i++) ret += '7'; r2(ret); return 0; } else { r1(); return 0; } } }
#include <bits/stdc++.h> using namespace std; int a1, a2, a3, a4; string s; void check() { int n = s.size(); for (int i = 0; i <= n - 1; i++) if (s[i] == '7') a2--; else a1--; if (a1 < 0 || a2 < 0) { cout << -1; return; } int u, v; for (int i = 0; i <= n - 1; i++) if (s[i] == '7') v = i; for (int i = n - 1; i >= 0; i--) if (s[i] == '4') u = i; for (int i = 0; i <= n - 1; i++) { cout << s[i]; if (i == u) for (int j = 1; j <= a1; j++) cout << 4; if (i == v) for (int j = 1; j <= a2; j++) cout << 7; } } int main() { cin >> a1 >> a2 >> a3 >> a4; if (abs(a3 - a4) > 1) cout << -1; else if (a3 == a4) { s = ""; for (int i = 1; i <= a3; i++) s += "47"; if (a3 + 1 <= a1 && a4 <= a2) { s += "4"; check(); } else if (a4 + 1 <= a2 && a1 <= a3) { s = "7" + s; check(); } else cout << -1; } else if (a3 > a4) { s = ""; for (int i = 1; i <= a3; i++) s += "47"; check(); } else { s = ""; for (int i = 1; i <= a4; i++) s += "74"; check(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long M = 2e5 + 10, oo = 1e9 + 7; vector<long long> va, vb, fc(M, 0), tcnt(M, 0), cnt(M, 0); vector<pair<long long, long long> > st; map<long long, long long> en, dc; long long dp[40][40]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long a, b, c, d; string sa = "4", sb = "7", sc = "47", sd = "74", ans = "", tmp = ""; cin >> a >> b >> c >> d; if (abs(c - d) > 1 || a < max(c, d) || b < max(c, d)) { cout << "-1" << endl; } else { if (c >= d) { for (long long i = 0; i < c; i++) ans += sc; for (long long i = 0; i < b - c; i++) ans += sb; } if (c > d) { tmp = ""; for (long long i = 0; i < a - c; i++) tmp += sa; tmp += ans; ans = tmp; } if (c == d) { tmp = ""; if (a > c) { for (long long i = 0; i < a - c - 1; i++) { tmp += sa; } tmp = tmp + ans + sa; ans = tmp; } else if (b > c) { ans.erase(ans.size() - 1, 1); tmp = sb + ans; ans = tmp; } else { cout << "-1" << endl; return 0; } } if (c < d) { for (long long i = 0; i < d - 1; i++) ans += sd; if (b > d) for (long long i = 0; i < b - d; i++) { ans.erase(ans.size() - 1, 1); ans += sb; ans += sa; } if (a > d) { tmp = ""; for (int i = 0; i < a - d; i++) tmp += sa; tmp += ans; ans = tmp; } tmp = sd + ans; ans = tmp; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int a1, a2, a3, a4; int main() { scanf("%d %d %d %d", &a1, &a2, &a3, &a4); if (a3 + 1 == a4) { a1 -= a4; a2 -= a4; if (a1 < 0 || a2 < 0) printf("-1\n"); else { printf("7"); for (int i = 0; i <= a1; i++) printf("4"); for (int i = 2; i < a4; i++) printf("74"); for (int i = 0; i <= a2; i++) printf("7"); printf("4\n"); } } else if (a3 == a4) { a1 -= a3 + 1; a2 -= a4; if (a1 < 0 || a2 < 0) { a1 += a3 + 1; a2 += a4; a1 -= a3; a2 -= a4 + 1; if (a1 < 0 || a2 < 0) printf("-1\n"); else { printf("7"); for (int i = 0; i < a1; i++) printf("4"); for (int i = 0; i < a3; i++) printf("47"); for (int i = 0; i < a2; i++) printf("7"); printf("\n"); } } else { for (int i = 0; i <= a1; i++) printf("4"); for (int i = 1; i < a3; i++) printf("74"); for (int i = 0; i <= a2; i++) printf("7"); printf("4\n"); } } else if (a3 == a4 + 1) { a1 -= a3; a2 -= a3; if (a1 < 0 || a2 < 0) printf("-1\n"); else { for (int i = 0; i <= a1; i++) printf("4"); printf("7"); for (int i = 2; i <= a3; i++) printf("47"); for (int i = 1; i <= a2; i++) printf("7"); printf("\n"); } } else printf("-1\n"); return 0; }
#include <bits/stdc++.h> using namespace std; char ans[2000010]; bool bfind; void dfs(int a1, int a2, int a3, int a4, int len) { if (a1 < 0 || a2 < 0 || a3 < 0 || a4 < 0) return; if (a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0) { ans[len] = '\0'; puts(ans); bfind = true; } if (bfind) return; ans[len] = '4'; if (len == 0 || ans[len - 1] == '4') dfs(a1 - 1, a2, a3, a4, len + 1); else dfs(a1 - 1, a2, a3, a4 - 1, len + 1); ans[len] = '7'; if (len == 0 || ans[len - 1] == '7') dfs(a1, a2 - 1, a3, a4, len + 1); else dfs(a1, a2 - 1, a3 - 1, a4, len + 1); } int main() { int i, t, s; int cnt4, cnt7, cnt47, cnt74; int ans; bool ok; while (scanf("%d%d%d%d", &cnt4, &cnt7, &cnt47, &cnt74) != EOF) { ok = true; if (abs(cnt47 - cnt74) > 1) ok = false; if (cnt47 > cnt4 || cnt47 > cnt7 || cnt74 > cnt4 || cnt74 > cnt7) ok = false; if (cnt47 == cnt74 && cnt4 <= cnt47 && cnt7 <= cnt47) ok = false; if (cnt47 == 0 && cnt74 == 0 && cnt4 > 0 && cnt7 > 0) ok = false; if (cnt47 < 0 || cnt74 < 0) ok = false; if (ok == false) { puts("-1"); continue; } if (cnt74 > cnt47) { putchar('7'); putchar('4'); cnt4--; cnt7--; cnt74--; cnt7 -= (cnt74 - 1); cnt4 -= (cnt74); while (cnt4-- > 0) putchar('4'); while (--cnt74 > 0) { putchar('7'); putchar('4'); } while (cnt7-- > 0) putchar('7'); putchar('4'); } else if (cnt74 < cnt47) { cnt7 -= cnt47; cnt4 -= cnt47; while (cnt4-- > 0) putchar('4'); while (cnt47-- > 0) { putchar('4'); putchar('7'); } while (cnt7-- > 0) putchar('7'); } else if (cnt74 == cnt47) { if (cnt4 > cnt47) { cnt4 -= (cnt47 + 1); cnt7 -= cnt47; while (cnt4-- > 0) putchar('4'); while (cnt47-- > 0) { putchar('4'); putchar('7'); } while (cnt7-- > 0) putchar('7'); putchar('4'); } else if (cnt7 > cnt74) { cnt4 -= cnt74; cnt7 -= (cnt74 + 1); while (cnt4-- > 0) putchar('4'); while (cnt74-- > 0) { putchar('7'); putchar('4'); } putchar('7'); while (cnt7-- > 0) putchar('7'); } else { printf("-1"); } } else { printf("-1"); } putchar('\n'); } return 0; }
#include <bits/stdc++.h> using namespace std; int a1, a2, a3, a4; inline void out(string s, int n) { for (int i = 0; i < n; i++) printf("%s", s.c_str()); } int main() { while (~scanf("%d%d%d%d", &a1, &a2, &a3, &a4)) { string c1 = "4", c2 = "7"; bool ok = true; if (a3 == a4) { if (a1 >= a3 + 1 && a2 >= a3) { out(c1, a1 - a3 - 1); out(c1 + c2, a3); out(c2, a2 - a3); out(c1, 1); } else if (a2 >= a3 + 1 && a1 >= a3) { out(c2, 1); out(c1, a1 - a3); out(c1 + c2, a3); out(c2, a2 - a3 - 1); } else { ok = false; } } else if (a3 == a4 + 1) { if (a1 >= a3 && a2 >= a3) { out(c1, a1 - a3); out(c1 + c2, a3); out(c2, a2 - a3); } else { ok = false; } } else if (a4 == a3 + 1) { if (a1 >= a4 && a2 >= a4) { out(c2, 1); out(c1, a1 - a3 - 1); out(c1 + c2, a3); out(c2, a2 - a3 - 1); out(c1, 1); } else { ok = false; } } else { ok = false; } puts(ok ? "" : "-1"); } return 0; }
#include <bits/stdc++.h> using namespace std; string ans = ""; int h, t, b1, b2, b3; void ss() { string st = ""; if (h) st += '7'; for (int i = 0; i < b1; ++i) st += '4'; for (int i = 0; i < b2; ++i) st += "74"; for (int i = 0; i < b3; ++i) st += '7'; if (t) st += '4'; if (ans == "" || ans > st) ans = st; } int main() { int a1, a2, a3, a4; cin >> a1 >> a2 >> a3 >> a4; for (h = 0; h <= 1; ++h) for (t = 0; t <= 1; ++t) { b2 = a3 - 1; b1 = a1 - b2 - t; b3 = a2 - b2 - h; if (b2 + h + t == a4 && b1 > 0 && b3 > 0) ss(); b2 = a3; b1 = a1 - b2 - t; b3 = a2 - b2 - h; if ((b2 + h == a4 && b1 > 0 && b3 == 0) || (b2 + t == a4 && b1 == 0 && b3 > 0)) ss(); b2 = a3 + 1; b1 = a1 - b2 - t; b3 = a2 - b2 - h; if (b2 == a4 && b1 == 0 && b3 == 0) ss(); } if (ans == "") { cout << -1; } else { cout << ans; } }
#include <bits/stdc++.h> using namespace std; int n, m, p, q; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> p >> q; if (abs(p - q) >= 2 || p > (min(m, n)) || q > min(m, n) || (!p && !q && n && m)) { cout << "-1"; return 0; } else if (!p && !q && !m && !n) return 0; else { string s = ""; if (p == q) { if (m == n && n == p) { cout << "-1"; return 0; } else { bool ok = 1; for (int i = 1; i <= p; ++i) { s += "47"; } s += '4'; m -= p; n -= (p + 1); if (m < 0 || n < 0) { ok = 0; m += p; n += (p + 1); s = ""; } if (ok) { for (int i = 0; i < s.size(); ++i) { cout << s[i]; if (i == 0 && n) for (int j = 1; j <= n; ++j) cout << 4; if (i == s.size() - 2 && m) for (int j = 1; j <= m; ++j) cout << 7; } return 0; } else { for (int i = 1; i <= p; ++i) { s += "74"; } s += '7'; m -= (p + 1); n -= p; if (m < 0 || n < 0) { cout << "-1"; return 0; } for (int i = 0; i < s.size(); ++i) { cout << s[i]; if (i == 0 && n) for (int j = 1; j <= n; ++j) cout << 4; if (i == s.size() - 2 && m) for (int j = 1; j <= m; ++j) cout << 7; } return 0; } cout << -1; return 0; } } else { string s = ""; if (p > q) { for (int i = 1; i <= p; ++i) s += "47"; m -= p; n -= p; if (m < 0 || n < 0) { cout << "-1"; return 0; } for (int i = 0; i < s.size(); ++i) { cout << s[i]; if (!i && n) for (int j = 1; j <= n; ++j) cout << 4; if (i == s.size() - 1 && m) for (int j = 1; j <= m; ++j) cout << 7; } return 0; } else { for (int i = 1; i <= q; ++i) s += "74"; m -= q; n -= q; if (m < 0 || n < 0) { cout << "-1"; return 0; } for (int i = 0; i < s.size(); ++i) { cout << s[i]; if (i == 1 && n) for (int j = 1; j <= n; ++j) cout << 4; if (i == s.size() - 2 && m) for (int j = 1; j <= m; ++j) cout << 7; } return 0; } } } }