QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#133971 | #4941. Tree Beauty | PanC_ake | WA | 43ms | 31936kb | C++20 | 2.3kb | 2023-08-02 19:15:15 | 2023-08-02 19:15:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int maxn = 100010;
const int M = 18;
template <typename T>
struct BIT {
int n;
T tr1[maxn], tr2[maxn];
void init(int _n) {
n = _n;
for (int i = 0; i <= n; i++) {
tr1[i] = tr2[i] = T();
}
}
inline int lowbit(int x) { return x & -x; }
void add(int x, T c) {
for (int i = x; i <= n; i += lowbit(i)) tr1[i] += c;
for (int i = x; i <= n; i += lowbit(i)) tr2[i] += x * c;
}
void modify(int l, int r, T c) {
add(l, c);
if (r + 1 <= n) add(r + 1, -c);
}
T query(int x) {
T res = T();
for (int i = x; i; i -= lowbit(i)) res += tr1[i];
res *= (x + 1);
for (int i = x; i; i -= lowbit(i)) res -= tr2[i];
return res;
}
T query(int l, int r) { return query(r) - query(l - 1); }
};
int n, q, tot, dep[maxn], p[maxn], lid[maxn], rid[maxn], son[maxn][M + 1], tag[maxn][M + 1];
vector<int> e[maxn];
BIT<int> tr;
void dfs(int u, int f) {
lid[u] = ++tot;
son[u][0] = 1;
dep[u] = dep[f] + 1;
for(int v: e[u]) {
dfs(v, u);
for(int i = 1; i <= M; i++) son[u][i] += son[v][i - 1];
}
rid[u] = tot;
}
void addtag(int u, int x, int k) {
if(k == 1) {
tr.modify(lid[u], rid[u], x);
return;
}
for(int i = 0; i <= M; i++) {
tag[u][i] += x;
x /= k;
}
}
int calc(int u) {
int res = tr.query(lid[u], rid[u]);
int v = u;
while(v) {
if(dep[v] + 18 < dep[u]) break;
for(int i = 0; dep[v] + 18 >= dep[u] + i; i++) {
res += son[u][i] * tag[v][i + dep[u] - dep[v]];
}
v = p[v];
}
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> q;
for (int i = 2; i <= n; i++) {
cin >> p[i];
e[p[i]].push_back(i);
}
dfs(1, 0);
tr.init(n);
while(q--) {
int ty;
cin >> ty;
if(ty == 1) {
int u, x, k;
cin >> u >> x >> k;
addtag(u, x, k);
} else {
int u;
cin >> u;
cout << calc(u) << endl;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 9984kb
input:
5 5 1 1 3 3 1 1 99 2 2 1 2 3 1 3 2 3 2 3
output:
245 97 99
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 3ms
memory: 11684kb
input:
1 1 2 1
output:
0
result:
ok single line: '0'
Test #3:
score: -100
Wrong Answer
time: 43ms
memory: 31936kb
input:
100000 100000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
1818724600 1818724600 1818724600 672469600 2897885600 1987509504 2897885600 2760335000 2897885600 2897885600 7403713200 11137131600 7514202655 4383229800 19100548501 22773583200 15090370500 15297793500 14191537500 9074946248 12902639283 26557008900 24837132600 22267972000 27726020850 31286228647 745...
result:
wrong answer 5th lines differ - expected: '2920352548', found: '2897885600'