結果
問題 | No.2676 A Tourist |
ユーザー | null |
提出日時 | 2024-02-15 03:49:13 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,493 ms / 5,000 ms |
コード長 | 12,109 bytes |
コンパイル時間 | 7,213 ms |
コンパイル使用メモリ | 467,836 KB |
実行使用メモリ | 54,904 KB |
最終ジャッジ日時 | 2024-09-29 23:12:20 |
合計ジャッジ時間 | 21,975 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 186 ms
16,512 KB |
testcase_02 | AC | 597 ms
36,136 KB |
testcase_03 | AC | 352 ms
36,064 KB |
testcase_04 | AC | 408 ms
36,176 KB |
testcase_05 | AC | 446 ms
36,044 KB |
testcase_06 | AC | 159 ms
16,768 KB |
testcase_07 | AC | 446 ms
37,012 KB |
testcase_08 | AC | 193 ms
37,056 KB |
testcase_09 | AC | 385 ms
37,004 KB |
testcase_10 | AC | 219 ms
36,912 KB |
testcase_11 | AC | 526 ms
34,772 KB |
testcase_12 | AC | 128 ms
22,528 KB |
testcase_13 | AC | 358 ms
54,904 KB |
testcase_14 | AC | 296 ms
54,840 KB |
testcase_15 | AC | 323 ms
54,852 KB |
testcase_16 | AC | 177 ms
16,512 KB |
testcase_17 | AC | 594 ms
36,220 KB |
testcase_18 | AC | 334 ms
36,336 KB |
testcase_19 | AC | 544 ms
36,264 KB |
testcase_20 | AC | 471 ms
36,316 KB |
testcase_21 | AC | 34 ms
6,820 KB |
testcase_22 | AC | 39 ms
6,816 KB |
testcase_23 | AC | 22 ms
6,816 KB |
testcase_24 | AC | 42 ms
6,816 KB |
testcase_25 | AC | 10 ms
6,820 KB |
testcase_26 | AC | 2 ms
6,820 KB |
testcase_27 | AC | 95 ms
16,752 KB |
testcase_28 | AC | 276 ms
36,852 KB |
testcase_29 | AC | 194 ms
36,880 KB |
testcase_30 | AC | 264 ms
36,940 KB |
testcase_31 | AC | 244 ms
36,984 KB |
testcase_32 | AC | 2,493 ms
54,860 KB |
コンパイルメッセージ
main.cpp:137:9: warning: #pragma once in main file 137 | #pragma once | ^~~~ main.cpp:356:9: warning: #pragma once in main file 356 | #pragma once | ^~~~
ソースコード
/* このコード、と~おれ! Be accepted! ∧_∧ (。・ω・。)つ━☆・*。 ⊂ ノ ・゜+. しーJ °。+ *´¨) .· ´¸.·*´¨) ¸.·*¨) (¸.·´ (¸.·'* ☆ */ #include <cstdio> #include <algorithm> #include <string> #include <cmath> #include <cstring> #include <vector> #include <numeric> #include <iostream> #include <random> #include <map> #include <unordered_map> #include <queue> #include <regex> #include <functional> #include <complex> #include <list> #include <cassert> #include <iomanip> #include <set> #include <stack> #include <bitset> #include <array> #include <chrono> #include <unordered_set> //#pragma GCC target("arch=skylake-avx512") //#pragma GCC target("avx2") //#pragma GCC optimize("O3") //#pragma GCC optimize("Ofast") //#pragma GCC target("sse4") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define repeat(i, n, m) for(int i = n; i < (m); ++i) #define rep(i, n) for(int i = 0; i < (n); ++i) #define printynl(a) printf(a ? "yes\n" : "no\n") #define printyn(a) printf(a ? "Yes\n" : "No\n") #define printYN(a) printf(a ? "YES\n" : "NO\n") #define printim(a) printf(a ? "possible\n" : "imposible\n") #define printdb(a) printf("%.50lf\n", a) #define printLdb(a) printf("%.50Lf\n", a) #define printdbd(a) printf("%.16lf\n", a) #define prints(s) printf("%s\n", s.c_str()) #define all(x) (x).begin(), (x).end() #define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI) #define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L) #define Please return #define AC 0 #define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d)) using ll = long long; using ull = unsigned long long; constexpr int INF = 1073741823; constexpr int MINF = -1073741823; constexpr ll LINF = ll(4661686018427387903); constexpr ll MOD = 1e9 + 7; constexpr ll mod = 998244353; constexpr long double eps = 1e-14; const long double PI = acosl(-1.0L); using namespace std; void scans(string& str) { char c; str = ""; scanf("%c", &c); if (c == '\n')scanf("%c", &c); while (c != '\n' && c != -1 && c != ' ') { str += c; scanf("%c", &c); } } void scanc(char& str) { char c; scanf("%c", &c); if (c == -1)return; while (c == '\n') { scanf("%c", &c); } str = c; } double acot(double x) { return PI / 2 - atan(x); } ll LSB(ll n) { return (n & (-n)); } template<typename T> inline T chmin(T& a, const T& b) { if (a > b)a = b; return a; } template<typename T> inline T chmax(T& a, const T& b) { if (a < b)a = b; return a; } //cpp_int #if __has_include(<boost/multiprecision/cpp_int.hpp>) #include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> using namespace boost::multiprecision; #else using cpp_int = ll; #endif //atcoder library #if __has_include(<atcoder/all>) #include <atcoder/all> //using namespace atcoder; #endif /* random_device seed_gen; mt19937 engine(seed_gen()); uniform_int_distribution dist(1, 100); */ /*----------------------------------------------------------------------------------*/ #pragma once template <typename T> struct edge { int src, to; T cost; edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {} edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {} edge& operator=(const int& x) { to = x; return *this; } operator int() const { return to; } }; template <typename T> using Edges = vector<edge<T>>; template <typename T> using WeightedGraph = vector<Edges<T>>; using UnweightedGraph = vector<vector<int>>; // Input of (Unweighted) Graph UnweightedGraph graph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { UnweightedGraph g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; if (is_1origin) x--, y--; g[x].push_back(y); if (!is_directed) g[y].push_back(x); } return g; } // Input of Weighted Graph template <typename T> WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false, bool is_1origin = true) { WeightedGraph<T> g(N); if (M == -1) M = N - 1; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; cin >> c; if (is_1origin) x--, y--; g[x].emplace_back(x, y, c); if (!is_directed) g[y].emplace_back(y, x, c); } return g; } // Input of Edges template <typename T> Edges<T> esgraph(int N, int M, int is_weighted = true, bool is_1origin = true) { Edges<T> es; for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; es.emplace_back(x, y, c); } return es; } // Input of Adjacency Matrix template <typename T> vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true, bool is_directed = false, bool is_1origin = true) { vector<vector<T>> d(N, vector<T>(N, INF)); for (int _ = 0; _ < M; _++) { int x, y; cin >> x >> y; T c; if (is_weighted) cin >> c; else c = 1; if (is_1origin) x--, y--; d[x][y] = c; if (!is_directed) d[y][x] = c; } return d; } template <typename G> struct HeavyLightDecomposition { private: void dfs_sz(int cur) { size[cur] = 1; for (auto& dst : g[cur]) { if (dst == par[cur]) { if (g[cur].size() >= 2 && int(dst) == int(g[cur][0])) swap(g[cur][0], g[cur][1]); else continue; } depth[dst] = depth[cur] + 1; par[dst] = cur; dfs_sz(dst); size[cur] += size[dst]; if (size[dst] > size[g[cur][0]]) { swap(dst, g[cur][0]); } } } void dfs_hld(int cur) { down[cur] = id++; for (auto dst : g[cur]) { if (dst == par[cur]) continue; nxt[dst] = (int(dst) == int(g[cur][0]) ? nxt[cur] : int(dst)); dfs_hld(dst); } up[cur] = id; } // [u, v) vector<pair<int, int>> ascend(int u, int v) const { vector<pair<int, int>> res; while (nxt[u] != nxt[v]) { res.emplace_back(down[u], down[nxt[u]]); u = par[nxt[u]]; } if (u != v) res.emplace_back(down[u], down[v] + 1); return res; } // (u, v] vector<pair<int, int>> descend(int u, int v) const { if (u == v) return {}; if (nxt[u] == nxt[v]) return { {down[u] + 1, down[v]} }; auto res = descend(u, par[nxt[v]]); res.emplace_back(down[nxt[v]], down[v]); return res; } public: G& g; int id; vector<int> size, depth, down, up, nxt, par; HeavyLightDecomposition(G& _g, int root = 0) : g(_g), id(0), size(g.size(), 0), depth(g.size(), 0), down(g.size(), -1), up(g.size(), -1), nxt(g.size(), root), par(g.size(), root) { dfs_sz(root); dfs_hld(root); } void build(int root) { dfs_sz(root); dfs_hld(root); } pair<int, int> idx(int i) const { return make_pair(down[i], up[i]); } template <typename F> void path_query(int u, int v, bool vertex, const F& f) { int l = lca(u, v); for (auto&& [a, b] : ascend(u, l)) { int s = a + 1, t = b; s > t ? f(t, s) : f(s, t); } if (vertex) f(down[l], down[l] + 1); for (auto&& [a, b] : descend(l, v)) { int s = a, t = b + 1; s > t ? f(t, s) : f(s, t); } } template <typename F> void path_noncommutative_query(int u, int v, bool vertex, const F& f) { int l = lca(u, v); for (auto&& [a, b] : ascend(u, l)) f(a + 1, b); if (vertex) f(down[l], down[l] + 1); for (auto&& [a, b] : descend(l, v)) f(a, b + 1); } template <typename F> void subtree_query(int u, bool vertex, const F& f) { f(down[u] + int(!vertex), up[u]); } int lca(int a, int b) { while (nxt[a] != nxt[b]) { if (down[a] < down[b]) swap(a, b); a = par[nxt[a]]; } return depth[a] < depth[b] ? a : b; } int dist(int a, int b) { return depth[a] + depth[b] - depth[lca(a, b)] * 2; } }; /** * @brief Heavy Light Decomposition(重軽分解) * @docs docs/tree/heavy-light-decomposition.md */ /** * @brief グラフテンプレート * @docs docs/graph/graph-template.md */ #pragma once template <typename T, typename F> struct SegmentTree { int N; int size; vector<T> seg; const F f; const T I; SegmentTree(F _f, const T& I_) : N(0), size(0), f(_f), I(I_) {} SegmentTree(int _N, F _f, const T& I_) : f(_f), I(I_) { init(_N); } SegmentTree(const vector<T>& v, F _f, T I_) : f(_f), I(I_) { init(v.size()); for (int i = 0; i < (int)v.size(); i++) { seg[i + size] = v[i]; } build(); } void init(int _N) { N = _N; size = 1; while (size < N) size <<= 1; seg.assign(2 * size, I); } void set(int k, T x) { seg[k + size] = x; } void build() { for (int k = size - 1; k > 0; k--) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); } } void update(int k, T x) { k += size; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); } } void add(int k, T x) { k += size; seg[k] += x; while (k >>= 1) { seg[k] = f(seg[2 * k], seg[2 * k + 1]); } } // query to [a, b) T query(int a, int b) { T L = I, R = I; for (a += size, b += size; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } T& operator[](const int& k) { return seg[k + size]; } // check(a[l] * ... * a[r-1]) が true となる最大の r // (右端まですべて true なら N を返す) template <class C> int max_right(int l, C check) { assert(0 <= l && l <= N); assert(check(I) == true); if (l == N) return N; l += size; T sm = I; do { while (l % 2 == 0) l >>= 1; if (!check(f(sm, seg[l]))) { while (l < size) { l = (2 * l); if (check(f(sm, seg[l]))) { sm = f(sm, seg[l]); l++; } } return l - size; } sm = f(sm, seg[l]); l++; } while ((l & -l) != l); return N; } // check(a[l] * ... * a[r-1]) が true となる最小の l // (左端まで true なら 0 を返す) template <typename C> int min_left(int r, C check) { assert(0 <= r && r <= N); assert(check(I) == true); if (r == 0) return 0; r += size; T sm = I; do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!check(f(seg[r], sm))) { while (r < size) { r = (2 * r + 1); if (check(f(seg[r], sm))) { sm = f(seg[r], sm); r--; } } return r + 1 - size; } sm = f(seg[r], sm); } while ((r & -r) != r); return 0; } }; void init_dfs(const int& cur, const int& prev, UnweightedGraph& g, const vector<ll>& a, vector<ll>& add) { for (const auto& aa : g[cur]) { if (aa != prev) { init_dfs(aa, cur, g, a, add); } add[cur] += a[aa]; } return; } int main() { int n, q; scanf("%d%d", &n, &q); vector<ll> a(n), add(n); rep(i, n)scanf("%lld", &a[i]); UnweightedGraph g = graph(n, n - 1); init_dfs(0, -1, g, a, add); HeavyLightDecomposition<vector<vector<int>>> hld(g); SegmentTree<ll, decltype(plus<ll>())> seg1(n, plus<ll>(), 0), seg2(n, plus<ll>(), 0), seg3(n, plus<ll>(), 0); rep(i, n) { seg1.set(hld.idx(i).first, a[i]); seg2.set(hld.idx(i).first, add[i]); } seg1.build(); seg2.build(); int sqn = sqrt((double)n); int siz = 0; unordered_map<int, int> id; rep(i, n) { if (g[i].size() > sqn) { id[i] = siz++; } } vector<ll> add_lazy(n); ll ans = 0; auto que1 = [&](int u, int v) { ans -= seg1.query(u, v); }; auto que2 = [&](int u, int v) { ans += seg2.query(u, v); }; auto que3 = [&](int u, int v) { ans += seg1.query(u, v); }; while (q--) { int t, u, v; scanf("%d%d%d", &t, &u, &v); if (t) { --u, --v; ans = 0; hld.path_query(u, v, true, que1); hld.path_query(u, v, true, que2); hld.path_query(u, u, true, que3); hld.path_query(v, v, true, que3); for (const auto& [p, q] : id) { if (hld.dist(u, v) + 2 == hld.dist(u, p) + hld.dist(v, p) or p == u or p == v) { ans += add_lazy[p]; } } for (const auto& [p, q] : id) { if (hld.dist(u, v) == hld.dist(u, p) + hld.dist(v, p) and p != u and p != v) { ans += add_lazy[p] * 2; } } printf("%lld\n", ans); } else { --u; if (g[u].size() <= sqn) { seg1.add(hld.idx(u).first, v); for (const auto& aa : g[u]) { seg2.add(hld.idx(aa).first, v); } } else { seg1.add(hld.idx(u).first, v); add_lazy[u] += v; } } } Please AC; }