QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#226206 | #7608. Cliques | tselmegkh# | WA | 1ms | 3648kb | C++17 | 6.8kb | 2023-10-25 18:22:55 | 2023-10-25 18:22:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
T power(T a, long long b) {
T s = 1;
for (; b; a *= a, b >>= 1) if (b & 1) s *= a;
return s;
}
template <int mod>
struct modular {
using mint = modular;
int v;
modular() : v(0) {}
modular(long long x) {if ((v = x % mod) < 0) v += mod;}
mint operator-() const {return -v;}
mint inv() const {return power(*this, mod - 2);}
mint &operator+=(const mint &a) {if ((v += a.v) >= mod) v -= mod; return *this;}
mint &operator-=(const mint &a) {if ((v -= a.v) < 0) v += mod; return *this;}
mint &operator*=(const mint &a) {v = (int)((long long)v * a.v % mod); return *this;}
mint &operator/=(const mint &a) {return *this *= a.inv();}
friend bool operator==(const mint &a, const mint &b){return a.v == b.v;}
friend bool operator!=(const mint &a, const mint &b){return a.v != b.v;}
friend mint operator+(const mint &a, const mint &b) {return mint(a) += b;}
friend mint operator-(const mint &a, const mint &b) {return mint(a) -= b;}
friend mint operator*(const mint &a, const mint &b) {return mint(a) *= b;}
friend mint operator/(const mint &a, const mint &b) {return mint(a) /= b;}
friend istream &operator>>(istream &is, mint &a) {return is >> a.v;}
friend ostream &operator<<(ostream &os, const mint &a) {return os << a.v;}
};
const int mod = 1e9 + 7;
using mint = modular<mod>;
template <class S, class T>
struct zalhuu {
int n;
vector<S> s; S se;
vector<T> t; T te;
S (*f)(S, S);
void (*g)(S&, T);
void (*h)(T&, T);
zalhuu() {
}
zalhuu(int m, S (*x)(S, S), void (*y)(S&, T), void (*z)(T&, T), S es, T et) :
f(x), g(y), h(z), se(es), te(et) {
n = 2 << __lg(m);
s.resize(2 * n, se);
t.resize(2 * n, te);
}
zalhuu(const vector<S> &a, S (*x)(S, S), void (*y)(S&, T), void (*z)(T&, T), S es, T et) :
zalhuu(a.size(), x, y, z, es, et) {
for (int i = 0; i < a.size(); i++) {
s[i + n] = a[i];
}
for (int i = n - 1; i; i--) {
pull(i);
}
}
void apply(int i, const T &v) {
g(s[i], v);
h(t[i], v);
}
void pull(int i) {
s[i] = f(s[i * 2], s[i * 2 + 1]);
}
void push(int i) {
apply(i * 2 + 0, t[i]);
apply(i * 2 + 1, t[i]);
t[i] = te;
}
void apply(int i, int l, int r, int L, int R, const T &v) {
if (R <= l || r <= L) {
return;
}
if (L <= l && r <= R) {
apply(i, v);
return;
}
int m = (l + r) / 2;
push(i);
apply(i * 2 + 0, l, m, L, R, v);
apply(i * 2 + 1, m, r, L, R, v);
pull(i);
}
void update(int i, int l, int r, int p, const S &v) {
if (l + 1 == r) {
s[i] = v;
return;
}
int m = (l + r) / 2;
push(i);
if (p < m) {
update(i * 2 + 0, l, m, p, v);
} else {
update(i * 2 + 1, m, r, p, v);
}
pull(i);
}
S get(int i, int l, int r, int p) {
if (l + 1 == r) {
return s[i];
}
int m = (l + r) / 2;
push(i);
if (p < m) {
return get(i * 2 + 0, l, m, p);
} else {
return get(i * 2 + 1, m, r, p);
}
}
S query(int i, int l, int r, int L, int R) {
if (R <= l || r <= L) {
return se;
}
if (L <= l && r <= R) {
return s[i];
}
int m = (l + r) / 2;
push(i);
return f(query(i * 2, l, m, L, R), query(i * 2 + 1, m, r, L, R));
}
void apply(int l, int r, const T &v) {
apply(1, 0, n, l, r, v);
}
void update(int i, const S &v) {
update(1, 0, n, i, v);
}
S get(int i) {
return get(1, 0, n, i);
}
S query(int l, int r) {
return query(1, 0, n, l, r);
}
};
const int I = 50000;
mint pw[I + 1];
using T = int;
using S = pair<mint, int>;
S f(S a, S b) {
return {a.first + b.first, a.second + b.second};
}
void g(S &a, T b) {
a.first *= pw[b];
a.second += b;
}
void h(T &a, T b) {
a += b;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
pw[0] = 1;
for (int i = 1; i <= I; i++) {
pw[i] = pw[i - 1] * 2;
}
vector<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int> sz(N), dep(N);
vector par(N, vector<int>(19, -1));
auto init = [&](auto init, int x) -> void {
if (par[x][0] != -1) {
adj[x].erase(find(adj[x].begin(), adj[x].end(), par[x][0]));
}
for (int i = 0; i < 18 && par[x][i] != -1; i++) {
par[x][i + 1] = par[par[x][i]][i];
}
sz[x] = 1;
for (int &y : adj[x]) {
dep[y] = dep[x] + 1;
par[y][0] = x;
init(init, y);
sz[x] += sz[y];
if (sz[y] > sz[adj[x][0]]) {
swap(y, adj[x][0]);
}
}
};
init(init, 0);
vector<int> pos(N), top(N);
int timer = 0;
auto dfs = [&](auto dfs, int x) -> void {
pos[x] = timer++;
for (int y : adj[x]) {
top[y] = y == adj[x][0] ? top[x] : y;
dfs(dfs, y);
}
};
dfs(dfs, 0);
auto lca = [&](int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 0; i < 19; i++) {
if ((dep[x] - dep[y]) >> i & 1) {
x = par[x][i];
}
}
if (x == y) return x;
for (int i = 18; i >= 0; i--) {
if (par[x][i] != par[y][i]) {
x = par[x][i];
y = par[y][i];
}
}
return par[x][0];
};
zalhuu<S, T> st(N, f, g, h, pair{0, 0}, 0);
vector<int> cnt(N);
int Q; cin >> Q; while (Q--) {
char c;
cin >> c;
int x, y;
cin >> x >> y;
x--, y--;
int z = lca(x, y);
cnt[z] += c == '+' ? 1 : -1;
auto k = st.get(pos[z]);
st.update(pos[z], pair{(pw[cnt[z]] - 1) * pw[k.second], k.second});
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
st.apply(pos[top[x]], pos[x] + 1, c == '+' ? 1 : -1);
x = par[top[x]][0];
}
if (dep[x] < dep[y]) swap(x, y);
st.apply(pos[y] + 1, pos[x] + 1, c == '+' ? 1 : -1);
cout << st.query(0, N).first << "\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3648kb
input:
5 1 2 5 1 2 3 4 2 6 + 4 5 + 2 2 + 1 3 - 2 2 + 2 3 + 4 4
output:
1 3 7 3 7 9
result:
ok 6 lines
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3628kb
input:
20 8 7 19 10 12 14 3 16 17 13 7 10 5 6 1 9 15 12 5 2 16 13 3 11 20 14 18 6 1 14 16 20 11 10 3 4 20 6 30 + 10 20 + 14 20 + 12 17 - 14 20 - 12 17 + 4 6 + 8 20 + 3 6 - 10 20 + 2 17 + 1 16 + 6 10 + 9 10 + 5 15 + 7 8 - 7 8 + 2 5 + 3 18 + 1 20 + 8 16 - 3 18 - 5 15 + 4 20 + 14 16 - 4 6 + 8 19 + 4 7 - 1 16 ...
output:
1 3 7 1 0 3 7 15 7 15 31 63 127 255 257 255 259 515 1027 1283 515 7 511 1023 511 527 607 99 111 255
result:
wrong answer 4th lines differ - expected: '3', found: '1'