結果

問題 No.2676 A Tourist
ユーザー nullnull
提出日時 2024-02-14 20:57:39
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 10,904 bytes
コンパイル時間 9,647 ms
コンパイル使用メモリ 535,596 KB
実行使用メモリ 44,524 KB
最終ジャッジ日時 2024-09-29 23:10:13
合計ジャッジ時間 15,461 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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
      |         ^~~~

ソースコード

diff #

/*
このコード、と~おれ!
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] += add[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);
	rep(i, n) { 
		seg1.set(hld.idx(i).first, a[i]);
		seg2.set(hld.idx(i).first, add[i]);
	}
	seg1.build();
	seg2.build();
	//未完成

	Please AC;
}
0