QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#626577 | #7608. Cliques | ucup-team4992 | WA | 3ms | 12120kb | C++20 | 3.4kb | 2024-10-10 10:21:20 | 2024-10-10 10:21:22 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 200000+5;
const ll mod = 1e9+7;
int n, q, tot;
ll inv2, pw[MAXN], ipw[MAXN];
ll sum[MAXN<<2], tag[MAXN<<2], len[MAXN<<2];
int fa[MAXN], sz[MAXN], dep[MAXN], hson[MAXN];
int top[MAXN], dfn[MAXN], rnk[MAXN];
vector<int> G[MAXN];
ll fpow(ll a, ll p, ll mod){
ll res = 1;
while(p){
if(p&1) res = res*a%mod;
a = a*a%mod;
p >>= 1;
}
return res;
}
void dfs1(int u, int f){
dep[u] = dep[f]+1;
fa[u] = f;
sz[u] = 1;
for(int v : G[u]){
if(v == f) continue;
dfs1(v, u);
sz[u] += sz[v];
if(sz[v] > sz[hson[u]])
hson[u] = v;
}
}
void dfs2(int u, int tp){
top[u] = tp;
dfn[u] = ++tot;
rnk[tot] = u;
if(!hson[u]) return;
dfs2(hson[u], tp);
for(int v : G[u]){
if(v == fa[u] || v == hson[u]) continue;
dfs2(v, v);
}
}
void build(int x, int l, int r){
len[x] = r-l+1;
if(l == r){
return;
}
}
void pushup(int x){
sum[x] = (sum[x<<1]+sum[x<<1|1])%mod;
}
void pushdown(int x){
int ls = x<<1, rs = x<<1|1;
if(tag[x] > 0){
sum[ls] = sum[ls]*pw[tag[x]]%mod;
sum[rs] = sum[rs]*pw[tag[x]]%mod;
tag[ls] += tag[x];
tag[rs] += tag[x];
tag[x] = 0;
}
else if(tag[x] < 0){
sum[ls] = sum[ls]*ipw[-tag[x]]%mod;
sum[rs] = sum[rs]*ipw[-tag[x]]%mod;
tag[ls] += tag[x];
tag[rs] += tag[x];
tag[x] = 0;
}
}
void add(int x, int l, int r, int L, int R, int v){
// cout << "add " << x << " " << l << " " << r << " " << L << " " << R << " " << v << "\n";
if(L <= l && r <= R){
if(v > 0){
sum[x] = sum[x]*2%mod;
tag[x]++;
}
else{
sum[x] = sum[x]*inv2%mod;
tag[x]--;
}
return;
}
pushdown(x);
int mid = (l+r)>>1;
if(L <= mid) add(x<<1, l, mid, L, R, v);
if(R > mid) add(x<<1|1, mid+1, r, L, R, v);
pushup(x);
}
void update(int x, int l, int r, int c, int v){
if(l == r){
// cout << "l = " << l << " tag = " << tag[x] << "\n";
if(v > 0){
tag[x]--;
sum[x] = (sum[x] + pw[tag[x]]) % mod;
}
else{
sum[x] = (sum[x] - pw[tag[x]] + mod) % mod;
tag[x]++;
}
return;
}
pushdown(x);
int mid = (l+r)>>1;
if(c <= mid) update(x<<1, l, mid, c, v);
else update(x<<1|1, mid+1, r, c, v);
pushup(x);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
pw[0] = 1;
ipw[0] = 1;
inv2 = fpow(2, mod-2, mod);
for(int i = 1; i <= 50000; i++){
pw[i] = pw[i-1]*2%mod;
ipw[i] = ipw[i-1]*inv2%mod;
}
cin >> n;
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs1(1, 0);
dfs2(1, 1);
build(1, 1, n);
cin >> q;
while(q--){
string op;
int u, v;
cin >> op >> u >> v;
if(op == "+"){
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]])
swap(u, v);
add(1, 1, n, dfn[top[u]], dfn[u], 1);
u = fa[top[u]];
}
if(dep[u] > dep[v]) swap(u, v);
// cout << "sum " << sum[1] << "\n";
// cout << "lca = " << u << "\n";
add(1, 1, n, dfn[u], dfn[v], 1);
update(1, 1, n, dfn[u], 1);
}
else{
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]])
swap(u, v);
add(1, 1, n, dfn[top[u]], dfn[u], -1);
u = fa[top[u]];
}
if(dep[u] > dep[v]) swap(u, v);
// cout << "sum " << sum[1] << "\n";
// cout << "lca = " << u << "\n";
add(1, 1, n, dfn[u], dfn[v], -1);
update(1, 1, n, dfn[u], -1);
}
cout << sum[1] << "\n";
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 12108kb
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: 0ms
memory: 12120kb
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 500000007 750000007 750000009 750000013 750000021 250000010 750000022 500000038 500000072 138 277 279 277 281 553 1103 1359 687 407 807 1613 813 829 909 500000507 500000785 500000401
result:
wrong answer 4th lines differ - expected: '3', found: '500000007'