QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#370861 | #7608. Cliques | ideograph_advantage# | WA | 1ms | 3696kb | C++20 | 4.0kb | 2024-03-29 17:56:17 | 2024-03-29 17:56:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define iter(v) v.begin(), v.end()
#define SZ(v) int(v.size())
#define pb emplace_back
#define ff first
#define ss second
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef zisk
void debug(){cerr << "\n";}
template<class T, class ... U>
void debug(T a, U ... b){cerr << a << " ", debug(b...);}
template<class T> void pary(T l, T r){
while(l != r) cerr << *l << " ", l++;
cerr << "\n";
}
#else
#define debug(...) void()
#define pary(...) void()
#endif
const ll mod = 1e9 + 7;
const ll inv2 = (mod + 1) / 2;
struct Node{
ll s = 1;
ll tag = 1;
};
#define lc 2 * id
#define rc 2 * id + 1
int n;
struct SegTree{
vector<Node> seg;
explicit SegTree(): seg(4 * n) {
}
void addtag(int id, ll x){
seg[id].s = seg[id].s * x % mod;
seg[id].tag = seg[id].tag * x % mod;
}
// ll lra = 0, ra = 0, la = 0, lr = 0, a = 0, l = 0, r = 0;
void pull(int id){
seg[id].s = (seg[lc].s + seg[rc].s) % mod;
}
void push(int id){
if(seg[id].tag != 1){
addtag(lc, seg[id].tag);
addtag(rc, seg[id].tag);
seg[id].tag = 1;
}
}
void modify(int ql, int qr, ll x, int L = 1, int R = n, int id = 1){
if(ql <= L && R <= qr){
addtag(id, x);
return;
}
push(id);
int M = (L + R) / 2;
if(qr <= M) modify(ql, qr, x, L, M, lc);
else if(ql > M) modify(ql, qr, x, M + 1, R, rc);
else{
modify(ql, qr, x, L, M, lc);
modify(ql, qr, x, M + 1, R, rc);
}
pull(id);
}
void _print(int L = 1, int R = n, int id = 1){
if(L == R){
cerr << seg[id].s << " ";
return;
}
push(id);
int M = (L + R) / 2;
_print(L, M, lc);
_print(M + 1, R, rc);
}
void print(){
_print();
cerr << "\n";
}
};
vector<vector<int>> G, pa;
vector<int> sz, in, out, head, nxt, dep;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
G.resize(n + 10);
pa.assign(n + 10, vector<int>(20));
sz.resize( n + 10);
in.resize( n + 10);
out.resize( n + 10);
head.resize( n + 10);
nxt.resize( n + 10);
dep.resize(n + 10);
for(int i = 0; i < n - 1; i ++){
int x, y;
cin >> x >> y;
G[x].pb(y);
G[y].pb(x);
}
auto dfs_sz = [&](int c, int p, auto dfs) -> void {
pa[c][0] = p;
dep[c] = dep[p] + 1;
for(int i : G[c]){
if(i == p) continue;
dfs(i, c, dfs);
sz[c] += sz[i];
}
sz[c]++;
};
dfs_sz(1, 1, dfs_sz);
for(int i = 2; i <= n; i++){
G[i].erase(find(iter(G[i]), pa[i][0]));
sort(iter(G[i]), [&](int x, int y){return sz[x] > sz[y];});
}
{
int t = 1;
auto dfs_hld = [&](int c, int h, auto dfs) -> void {
in[c] = t++;
head[c] = h;
for(int i : G[c]){
if(i == G[c][0]) dfs(i, h, dfs);
else dfs(i, i, dfs);
}
out[c] = t;
};
dfs_hld(1, 1, dfs_hld);
}
for(int i = 1; i < 20; i++){
for(int j = 1; j <= n; j++){
pa[j][i] = pa[pa[j][i-1]][i-1];
}
}
auto get_lca = [&](int u, int v){
if(dep[u] < dep[v]) swap(u, v);
for(int i = 19; i >= 0; i--){
if(dep[pa[u][i]] >= dep[v]) u = pa[u][i];
}
assert(dep[u] == dep[v]);
if(u == v) return u;
for(int i = 19; i >= 0; i--){
if(pa[u][i] != pa[v][i]){
u = pa[u][i];
v = pa[v][i];
}
}
assert(u != v);
u = pa[u][0];
v = pa[v][0];
assert(u == v);
return u;
};
SegTree V;
SegTree E;
E.modify(1, 1, 0);
//V.print();
//E.print();
int q;
cin >> q;
while(q--){
char c;
int x, y, t;
cin >> c >> x >> y;
t = c == '+' ? 2 : inv2;
int z = get_lca(x, y);
if(x == y){
V.modify(in[x], in[x], t);
}else{
while(x != y) {
//debug(x, y);
if(dep[head[x]] < dep[head[y]]) swap(x, y);
if(head[x] == head[y]){
if(dep[x] < dep[y]) swap(x, y);
V.modify(in[y] + 1, in[x], t);
E.modify(in[y] + 1, in[x], t);
//debug("modify1", x, y, in[y] + 1, in[x]);
x = y;
}else{
int hx = head[x];
V.modify(in[hx], in[x], t);
E.modify(in[hx], in[x], t);
//debug("modify2", x, y, pa[hx][0], in[hx], in[x]);
x = pa[hx][0];
}
}
V.modify(in[z], in[z], t);
}
//V.print();
//E.print();
cout << (V.seg[1].s - E.seg[1].s - 1 + 2 * mod) % mod << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3696kb
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: 3632kb
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:
1000000006 2 6 2 0 2 6 14 6 14 31 63 127 255 257 255 259 515 1027 1283 643 385 769 1537 769 785 865 481 737 369
result:
wrong answer 1st lines differ - expected: '1', found: '1000000006'