QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#717783 | #9492. 树上简单求和 | Hunster | 0 | 3137ms | 137884kb | C++23 | 9.0kb | 2024-11-06 18:56:37 | 2024-11-06 18:56:38 |
Judging History
answer
#include <bits/stdc++.h>
namespace IO {
#ifdef _DEBUG
constexpr int MAX_SIZE = 1;
#else
constexpr int MAX_SIZE = 1 << 20;
#endif
class Reader {
private:
FILE *file;
char buff[MAX_SIZE], *p1, *p2;
int getchar() {
if (p1 == p2) p2 = (p1 = buff) + fread(buff, 1, MAX_SIZE, file);
return p1 == p2 ? EOF : *p1++;
}
public:
Reader(FILE *_file) {
file = _file;
p1 = p2 = buff;
}
Reader(const char* file_dir) {
file = std::fopen(file_dir, "r");
p1 = p2 = buff;
}
void read() {}
template<typename... Args>
void read(int &x, Args &...other) {
int f, ch;
x = f = 0;
ch = getchar();
for (; !isdigit(ch); ch = getchar()) f ^= ch == '-';
for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
if (f) x = -x;
read(other...);
}
template<typename... Args>
void read(long long &x, Args &...other) {
int f, ch;
x = f = 0;
ch = getchar();
for (; !isdigit(ch); ch = getchar()) f ^= ch == '-';
for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
if (f) x = -x;
read(other...);
}
template<typename... Args>
void read(__int128 &x, Args &...other) {
int f, ch;
x = f = 0;
ch = getchar();
for (; !isdigit(ch); ch = getchar()) f ^= ch == '-';
for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
if (f) x = -x;
read(other...);
}
template<typename... Args>
void read(unsigned int &x, Args &...other) {
int ch;
x = 0;
ch = getchar();
for (; !isdigit(ch); ch = getchar()) ;
for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
read(other...);
}
template<typename... Args>
void read(unsigned long long &x, Args &...other) {
int ch;
x = 0;
ch = getchar();
for (; !isdigit(ch); ch = getchar()) ;
for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
read(other...);
}
template<typename... Args>
void read(unsigned __int128 &x, Args &...other) {
int ch;
x = 0;
ch = getchar();
for (; !isdigit(ch); ch = getchar()) ;
for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
read(other...);
}
template<typename... Args>
void read(double &x, Args &...other) {
int f, ch;
x = f = 0;
ch = getchar();
for (; !isdigit(ch); ch = getchar()) f ^= ch == '-';
for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
if (ch == '.') {
double e = 1;
for (ch = getchar(); isdigit(ch); ch = getchar())
x += (ch ^ 48) * (e *= .1);
}
if (f) x = -x;
read(other...);
}
template<typename... Args>
void read(char &ch, Args &...other) {
ch = getchar();
while (!isgraph(ch)) ch = getchar();
read(other...);
}
template<typename... Args>
void read(std::string &s, Args &...other) {
char ch;
s = "";
ch = getchar();
for (; !isgraph(ch);) ch = getchar();
for (; isgraph(ch); ch = getchar()) s += ch;
read(other...);
}
template<typename... Args>
void read(long double &x, Args &...other) {
int f, ch;
x = f = 0;
ch = getchar();
for (; !isdigit(ch); ch = getchar()) f ^= ch == '-';
for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
if (ch == '.') {
long double e = 1;
for (ch = getchar(); isdigit(ch); ch = getchar())
x += (ch ^ 48) * (e *= .1);
}
if (f) x = -x;
read(other...);
}
void read(char* s) {
char ch;
ch = getchar();
while (!isgraph(ch)) ch = getchar();
for (; isgraph(ch); ch = getchar()) *s++ = ch;
*s = 0;
}
};
}
IO::Reader reader(stdin);
using LL = long long;
using ULL = unsigned long long;
constexpr int N = 200005;
int n, m;
ULL a[N];
std::vector<int> tree[2][N];
int fa[2][N][20], dep[2][N], dfn[2][N], size[2][N], dfc[2];
template <int o>
void pre_dfs(int u, int p) {
if (o == 1) a[u] += a[p];
size[o][u] = 1;
dfn[o][u] = ++dfc[o];
dep[o][u] = dep[o][p] + 1;
fa[o][u][0] = p;
for (int i = 1; i < 20; i++) fa[o][u][i] = fa[o][fa[o][u][i - 1]][i - 1];
for (int v : tree[o][u]) {
if (v == p) continue;
pre_dfs<o>(v, u);
size[o][u] += size[o][v];
}
}
template <int o>
int lca(int x, int y) {
if (dep[o][x] > dep[o][y]) std::swap(x, y);
for (int i = 19; i >= 0; i--) if (dep[o][fa[o][y][i]] >= dep[o][x]) y = fa[o][y][i];
if (x == y) return x;
for (int i = 19; i >= 0; i--) if (fa[o][x][i] != fa[o][y][i]) x = fa[o][x][i], y = fa[o][y][i];
return fa[o][x][0];
}
constexpr int B = 700;
int tot, key[N], pos[1003];
int get_key(int u, int p) {
int max = 0;
for (int v : tree[1][u]) {
if (v == p) continue;
max = std::max(max, get_key(v, u) + 1);
}
if (max == B) {
key[u] = ++tot;
pos[tot] = u;
max = 0;
}
return max;
}
std::vector<std::pair<int, ULL>> h[N], g[N], d[1003];
struct Blocks {
ULL h[1003];
ULL g[1003][1003];
int bl[1003], br[1003], bel[N];
void build() {
for (int i = 1; i <= n; i++) bel[i] = (i + B - 1) / B;
for (int i = 1; i <= n; i++) br[bel[i]] = i;
for (int i = n; i >= 1; i--) bl[bel[i]] = i;
}
void add(int x, ULL v) {
int b = bel[x];
for (int i = b; i <= bel[n]; i++) h[i] += v;
for (int i = x; i <= br[b]; i++) g[b][i - bl[b] + 1] += v;
}
ULL query(int x) {
int b = bel[x];
return h[b - 1] + g[b][x - bl[b] + 1];
}
};
Blocks blocks;
ULL ans[N];
int tag[N], val[N];
void dfs(int u, int p) {
val[u] = val[p] + tag[u];
for (int v : tree[0][u]) {
if (v == p) continue;
dfs(v, u);
}
}
int main() {
reader.read(n, m);
for (int i = 1; i <= n; i++) reader.read(a[i]);
for (int i = 1; i < n; i++) {
int x, y;
reader.read(x, y);
tree[0][x].push_back(y);
tree[0][y].push_back(x);
}
for (int i = 1; i < n; i++) {
int x, y;
reader.read(x, y);
tree[1][x].push_back(y);
tree[1][y].push_back(x);
}
pre_dfs<0>(1, 0);
pre_dfs<1>(1, 0);
get_key(1, 0);
for (int i = 1; i <= m; i++) {
int x, y; ULL k;
reader.read(x, y, k);
int p0 = lca<0>(x, y);
h[i].push_back({x, k});
h[i].push_back({y, k});
h[i].push_back({p0, -k});
if (fa[0][p0][0]) h[i].push_back({fa[0][p0][0], -k});
int p1 = lca<1>(x, y);
g[i].push_back({x, 1});
g[i].push_back({y, 1});
g[i].push_back({p1, -1});
if (fa[1][p1][0]) g[i].push_back({fa[1][p1][0], -1});
ans[i] = a[x] + a[y] - a[p1] - a[fa[1][p1][0]];
}
blocks.build();
const auto shorten = [&](std::vector<std::pair<int, ULL>> &a) {
std::sort(a.begin(), a.end());
std::vector<std::pair<int, int>> res;
for (auto [x, v] : a) {
if (res.size() && res.back().first == x) res.back().second += v;
else res.push_back({x, v});
}
a.clear();
for (auto [x, v] : res) if (v) a.push_back({x, v});
};
for (int i = 1; i <= m; i++) {
shorten(h[i]);
shorten(g[i]);
}
for (int i = 1; i <= m; i++) {
for (auto [x, v] : h[i]) blocks.add(dfn[0][x], v);
for (auto [x, v] : g[i]) {
while (x && !key[x]) {
ans[i] += v * (blocks.query(dfn[0][x] + size[0][x] - 1) - blocks.query(dfn[0][x] - 1));
x = fa[1][x][0];
}
if (x) d[key[x]].push_back({i, v});
}
}
for (int i = 1; i <= tot; i++) {
for (int u = pos[i]; u; u = fa[1][u][0]) tag[u] = 1;
dfs(1, 0);
int p = 0;
ULL cur = 0;
for (auto [id, v] : d[i]) {
while (p + 1 <= id) {
p++;
for (auto [x, v] : h[p]) cur += val[x] * v;
}
ans[id] += v * cur;
}
for (int u = pos[i]; u; u = fa[1][u][0]) tag[u] = 0;
}
for (int i = 1; i <= m; i++) printf("%llu\n", ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 19100kb
input:
3000 3000 7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...
output:
18084912154864480132 1306240368994159045 14909241734497821526 18035410860790259784 7233019454624334038 3025450379003025255 9069278426413646133 8626043022908402744 18438643916396376090 18316659411635568858 16584942215583751847 3629410404241777049 1802207554240456916 1810303022627293284 14989822718295...
result:
wrong answer 1st lines differ - expected: '12105153858659381124', found: '18084912154864480132'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 1690ms
memory: 120192kb
input:
200000 200000 622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...
output:
6485477968396417051 13896917763838635958 10065243704487247146 14496576404860540366 8780809623764949921 12338225004636288629 9763894989791142048 8420077585629197307 3194927145347814731 12839711141936246833 3593522446118147697 8351328291639928700 3484352900017253792 2969273815091808937 144734400024020...
result:
wrong answer 1st lines differ - expected: '9042998055336671259', found: '6485477968396417051'
Subtask #5:
score: 0
Time Limit Exceeded
Test #27:
score: 0
Time Limit Exceeded
input:
200000 200000 1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...
output:
result:
Subtask #6:
score: 0
Wrong Answer
Test #34:
score: 0
Wrong Answer
time: 3137ms
memory: 137884kb
input:
200000 200000 6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...
output:
14296129693928146337 13132536201794115875 11384709103519682853 13145164480004509069 2345901624969439875 9038517425370787267 15776857924666082554 9158920128250688591 4582588839034172112 16821516164929565343 13303093709533817109 5095096687221178863 16303440236580922384 5670832075301986001 105510372322...
result:
wrong answer 1st lines differ - expected: '5519324519442957729', found: '14296129693928146337'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%