QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#223079 | #7608. Cliques | ucup-team2307# | WA | 103ms | 36200kb | C++14 | 4.0kb | 2023-10-21 21:19:36 | 2023-10-21 21:19:36 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
const int mod = 1e9 + 7;
int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
struct Node {
ll sm = 1, lazy = 1;
void apply(ll x) {
lazy = lazy * x % mod;
sm = sm * x % mod;
}
friend Node operator+(const Node &a, const Node &b) {
return Node{add(a.sm, b.sm)};
}
};
struct ST {
int n;
vector<Node> tree;
ST(int N) : n(N), tree(4*N + 4) { init(1, 0, n - 1); }
void init(int v, int l, int r) {
if(l == r) {
tree[v].sm = 1;
} else {
int m = (l + r) / 2;
init(2 * v, l, m);
init(2* v + 1, m + 1, r);
tree[v] = tree[2*v]+tree[2*v+1];
}
}
void push(int v) {
if(tree[v].lazy) {
tree[2*v].apply(tree[v].lazy);
tree[2*v + 1].apply(tree[v].lazy);
tree[v].lazy = 1;
}
}
void update(int v, int l, int r, int ql, int qr, int d) {
if(qr < l || r < ql) return;
if(ql <= l && r <= qr) {
tree[v].apply(d);
return;
}
push(v);
int m = (l + r) / 2;
update(2 * v, l, m, ql, qr, d);
update(2 * v + 1, m + 1, r, ql, qr, d);
tree[v] = tree[2*v] + tree[2*v+1];
}
void update(int ql, int qr, int d) {
update(1, 0, n - 1, ql, qr, d);
}
int get() {
return tree[1].sm;
}
void dfs(int v, int l, int r) {
if(l == r) {
cout << tree[v].sm << " ";
} else {
int m = (l + r) / 2;
push(v);
dfs(2*v, l, m);
dfs(2*v+1, m + 1, r);
}
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
// ST s(10);
// s.dfs(1, 0, 9); cout << " | " << s.get() << endl;
// s.update(0, 9, 2);
// s.dfs(1, 0, 9); cout << " | " << s.get() << endl;
// s.update(4, 4, 2);
// s.dfs(1, 0, 9); cout << " | " << s.get() << endl;
// return 0;
int n, m;
cin >> n;
vector<vector<int>> g(n);
for(int f, t, i = 1; i < n; i++) {
cin >> f >> t;
g[--f].push_back(--t);
g[t].push_back(f);
}
vector<int> sz(n), par(n);
auto sizes = [&](auto self, int v) -> void {
sz[v] = 1;
for(auto &i : g[v]) {
g[i].erase(find(all(g[i]), v));
par[i] = v;
self(self, i);
sz[v] += sz[i];
if(sz[i] > sz[g[v][0]])
swap(g[v][0], i);
}
};
sizes(sizes, 0);
int timer = 0;
vector<int> tin(n), tout(n), head(n);
auto hld = [&](auto self, int v) -> void {
tin[v] = timer++;
for(auto i : g[v]) {
head[i] = i == g[v][0] ? head[v] : i;
self(self, i);
}
tout[v] = timer;
};
hld(hld, 0);
ST V(n), E(n);
auto is_ancestor = [&](int u, int v) {
return tin[u] <= tin[v] && tin[v] < tout[u];
};
auto up = [&](int &a, int &b, int d, ST &st) {
while(!is_ancestor(head[a], b)) {
// cout << head[a] << " to " << a << endl;
st.update(tin[head[a]], tin[a], d);
a = par[head[a]];
}
};
auto update = [&](int u, int v, int d, ST &st, int touch_lca) {
up(u, v, d, st);
up(v, u, d, st);
if(!is_ancestor(u, v)) swap(v, u);
// cout << u << " to " << v << endl;
st.update(tin[u] + !touch_lca, tin[v], d);
};
int u, v;
char c;
cin >> m;
while(m--) {
cin >> c >> u >> v;
--u, --v;
int d = c == '+' ? 2 : (mod + 1) / 2;
update(u, v, d, V, 1);
update(u, v, d, E, 0);
// V.dfs(1, 0, n - 1);cout << endl;
// E.dfs(1, 0, n - 1);cout << endl;
cout << V.get() - E.get() << '\n';
// cout << endl;
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
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: 0
Accepted
time: 0ms
memory: 3604kb
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 3 1 3 7 15 7 15 31 63 127 255 257 255 259 515 1027 1283 643 385 769 1537 769 785 865 481 737 369
result:
ok 30 lines
Test #3:
score: -100
Wrong Answer
time: 103ms
memory: 36200kb
input:
131072 3641 72511 10338 18718 8949 15478 79108 62887 42154 28540 65359 102645 93744 48493 3277 103211 43542 61315 93315 118634 24021 57937 31034 436 30411 6208 1388 25159 93424 128520 119820 87281 5860 97559 59648 8225 57244 58766 119685 13716 130165 60958 79806 116338 97486 80167 101963 95499 51263...
output:
1 2 4 8 13 27 43 67 119 215 359 423 847 1167 1935 2447 2975 4511 5023 8639 6911 13759 12991 15103 23295 43775 47999 91647 85375 167295 169343 301695 600703 666239 1332351 2664575 2678911 2687103 5374207 5898495 11731455 11731967 22283263 22807551 21955327 43910399 43911423 44207359 44207360 44469504...
result:
wrong answer 55th lines differ - expected: '64068096', found: '-935931911'