ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#214844 | #2642. color | STASISZHY | 100 | 203ms | 2028kb | C++11 | 3.6kb | 2024-11-22 19:06:15 | 2024-11-22 23:11:40 |
answer
// Problem: P3384 【模板】重链剖分/树链剖分
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3384
// Memory Limit: 128 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// https://www.cnblogs.com/ThySecret/p/18524700
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define DEBUG
#define lc u << 1
#define rc u << 1 | 1
#define File(a) freopen(a".in", "r", stdin); freopen(a".out", "w", stdout)
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10, M = N << 1;
const int INF = 0x3f3f3f3f;
int n, m, root, mod;
int h[N], e[M], ne[M], idx;
int a[N], val[N];
int depth[N], sz[N], hson[N];
int dfn[N], timestamp;
int top[N], id[N], s[N], fa[N];
struct Tree
{
struct Node
{
int l, r, sum[2];
inline int len() {return r - l + 1; }
} tr[N << 2];
void pushup(int u)
{
tr[u].sum[0] = (tr[lc].sum[0] || tr[rc].sum[0]);
tr[u].sum[1] = (tr[lc].sum[1] || tr[rc].sum[1]);
}
void build(int u, int l, int r)
{
tr[u].l = l, tr[u].r = r;
if (l == r) return tr[u].sum[val[l]] = true, void(0);
int mid = l + r >> 1;
build(lc, l, mid), build(rc, mid + 1, r);
pushup(u);
}
bool query(int u, int l, int r, bool op)
{
if (l <= tr[u].l && tr[u].r <= r)
return tr[u].sum[op];
int mid = tr[u].l + tr[u].r >> 1, res = 0;
if (l <= mid) res |= query(lc, l, r, op);
if (r > mid) res |= query(rc, l, r, op);
return res;
}
} SGT;
inline void add(int a, int b)
{
e[++ idx] = b, ne[idx] = h[a], h[a] = idx;
}
void dfs1(int ver, int pre, int deep)
{
depth[ver] = deep, fa[ver] = pre, sz[ver] = 1;
int maxn = -1;
for (int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if (j == pre) continue;
dfs1(j, ver, deep + 1);
sz[ver] += sz[j];
if (maxn == -1 || maxn < sz[j]) maxn = sz[j], hson[ver] = j;
}
}
void dfs2(int ver, int topf)
{
dfn[ver] = ++ timestamp, val[timestamp] = a[ver], top[ver] = topf;
if (!hson[ver]) return;
dfs2(hson[ver], topf);
for (int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if (j == fa[ver] || j == hson[ver]) continue;
dfs2(j, j);
}
}
inline bool query_range(int x, int y, bool op)
{
bool res = 0;
while (top[x] != top[y])
{
if (depth[top[x]] < depth[top[y]]) swap(x, y);
res |= SGT.query(1, dfn[top[x]], dfn[x], op);
x = fa[top[x]];
}
if (depth[x] > depth[y]) swap(x, y);
res |= SGT.query(1, dfn[x], dfn[y], op);
return res;
}
int find(int x)
{
if(x == fa[x]) return x;
else return fa[x] = find(fa[x]);
}
signed main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// memset(h, -1, sizeof h);
// cin >> n >> m;
// // for (int i = 1; i <= n; i ++) cin >> a[i];
// for(int i = 1; i <= n; i ++)
// {
// char ch; cin >> ch;
// a[i] = (ch == 'H');
// }
// for (int i = 1; i < n; i ++)
// {
// int u, v; cin >> u >> v;
// add(u, v), add(v, u);
// }
//
// dfs1(root, 0, 1);
// dfs2(root, root);
// SGT.build(1, 1, n);
//
// while (m --)
// {
// int x, y; char op; cin >> x >> y >> op;
// cout << query_range(x, y, op == 'H');
// }
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
char ch; cin >> ch;
fa[i] = i, s[i] = (ch == 'H');
}
for(int i = 1; i < n; i ++)
{
int u, v; cin >> u >> v;
int fu = find(u), fv = find(v);
if(fu > fv) swap(fu, fv);
if(s[fu] == s[fv]) fa[fv] = fu;
}
while(m --)
{
int l, r; char op; cin >> l >> r >> op;
bool flag = (op == 'H');
if(find(l) == find(r) && s[find(l)] ^ flag) cout << 0;
else cout << 1;
}
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1280kb
input:
920 900 HHHHHHHHHHHHHHHHHHHHHHHHHGHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHHHHHHGHHGHHHHHHHGGHGHHHGHHGHGHHHHHH...
output:
0010100111100111101010001000010101100010001110110110001101100101111000101111100001111001110100001111...
result:
ok single line: '001010011110011110101000100001...1010100111010011101110111010101'
Test #2:
score: 10
Accepted
time: 0ms
memory: 1276kb
input:
927 949 HHHHGHGGGHHHHGHGHHHHHGHGGHGGGHHHGGHHHHHHGGGGGGHHHGGHHHHGHHHHGGGHHHGHGHHHHGGGGHHGHHGGHGHGGGGG...
output:
1111000011110111101101100110110011110101011000110111111111101110110011111011111110110011101010111111...
result:
ok single line: '111100001111011110110110011011...0011001011010111111011111001110'
Test #3:
score: 10
Accepted
time: 1ms
memory: 1276kb
input:
934 998 HHHHGHHHGHHGGGHGGHGHGHHHGHHGHGGGHHHHHGHHGGHHHHHHGHHHGGHHHHHGGHHHGHHHHHHHGHHGHGHGGHHGHGHHHGGH...
output:
1101010111111010110101011111110111111101101100011111001101110111111111011111101001111011001010101111...
result:
ok single line: '110101011111101011010101111111...1011101110011100101111100101110'
Test #4:
score: 10
Accepted
time: 0ms
memory: 1280kb
input:
941 947 HHHHHHHHHHHHHHHGHHHGHHHGHHHHGHHHGHHHHHHHHGHHHHHHHHHGHHHHHGHHHHHGHHGHHHHGHGGHHGHHHHHHHHHHGGHH...
output:
1111111011010110001101010101110100110001000101100101111101110001111111111110111011010101001111101111...
result:
ok single line: '111111101101011000110101010111...0101100011110110011000000000011'
Test #5:
score: 10
Accepted
time: 36ms
memory: 1992kb
input:
92189 98896 HHHHHHHHHGHHHHGGGHGHHHHHHGHHHHHGHHHHHHHHHGHGHHHGHHHHHHGGHHHGHGGHHHHHHHHHGHHHHHHGHGHGHHHG...
output:
1110111111111111111111111111111111111011110111111111111111110111111101111111111111111111111111101111...
result:
ok single line: '111011111111111111111111111111...1111111111111111111111111111111'
Test #6:
score: 10
Accepted
time: 34ms
memory: 2024kb
input:
95803 95747 HHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHGHHGHGHHHHGHGHHHHHHGGHHHHHHHHHHHHGHHHGHHHHHHHHHGHGGHHHHH...
output:
1011011101111111111111111101111111111011101101111110111111101111111011110111111101111111001100101111...
result:
ok single line: '101101110111111111111111110111...1111101101111110111111111001111'
Test #7:
score: 10
Accepted
time: 37ms
memory: 2000kb
input:
92610 90996 HHGGGHHGHGGGHGHGHGGGGGGHGGGHGGHGHGHGGHHHHHGHGGHGHGGGGHGGGHGGHHGHHHGGHGHGGHGGGHGGGGGGGGGG...
output:
1111111111111111111011111111111111111110111111111111100111111111011111111111101101111111111111011111...
result:
ok single line: '111111111111111111101111111111...1111110011111111111111111111111'
Test #8:
score: 10
Accepted
time: 28ms
memory: 2024kb
input:
96224 91494 HHHHHHHHGGHHGHHHHHHHHHHHHHGHHHGHHHHHHHHHHHHHHHHHGGHHHHHHGHHHHHHHHHHHHHHHHHGHGGGHGHGHHHHH...
output:
1111010110111100111111111111011011010101011111011101111011111011111011111111111111111111111011111101...
result:
ok single line: '111101011011110011111111111101...1101011011111111111111101101111'
Test #9:
score: 10
Accepted
time: 35ms
memory: 2000kb
input:
93031 96743 HHHHHHHHHGHHHHHHHHHHHHHHHHHGGHHHHGHHHGHHHHHHHHHGHHHHHHHHHHHHHHGGHHHHHGGHGHHHGHHHGHHHHHHG...
output:
0100011111101111011111110111001111101111110101110110111111111111111101111110110001011110011111011111...
result:
ok single line: '010001111110111101111111011100...1111111101110011111101011111110'
Test #10:
score: 10
Accepted
time: 32ms
memory: 2028kb
input:
96645 93594 HHHHHHHHHGHGHHHHHHHGHHHHGGGHHHHHHHHHHHHHHHHHHGHHHHHGHGHHHHHGHGHHHHHGHHHHGHHHHGGHHGHHHHGH...
output:
0101000110111110011111111111110111101011011101111110111111011011111111111110111110101111111111111111...
result:
ok single line: '010100011011111001111111111111...1110011111111111110110011111111'