UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#214837#2642. colorThySecret0704ms10708kbC++114.1kb2024-11-22 18:51:252024-11-22 23:10:41

answer

#include <bits/stdc++.h>

using namespace std;

// #define int long long
// #define x first
// #define y second
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)

inline void debug() { cerr << '\n'; }
template<typename Type, typename... Other>
inline void debug(const Type& x, const Other&... y) { cerr << x << ' '; debug(y...); }
#define DEBUG(a...) cerr << "[" << #a << "] = ", debug(a);

typedef long long LL;
typedef pair<int, int> PII;

const int N = 200010;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

template<typename Type>
inline void read(Type &res)
{
    res = 0;
    int ch = getchar(), flag = 0;
    while (!isdigit(ch)) flag |= ch == '-', ch = getchar();
    while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
    res = flag ? -res : res;
}
template<typename Type, typename... Other>
inline void read(Type &res, Other&... y) { read(res), read(y...); }

int n, m, a[N], val[N];
int h[N], e[N << 1], ne[N << 1], idx;
int depth[N], fa[N], sz[N], hson[N];
int dfn[N], timestamp;
int top[N], id[N];

struct Tree
{
    #define lc u << 1
    #define rc u << 1 | 1

	struct Node
	{
		int l, r, sum, tag;
		inline int len() {return r - l + 1; }
	} tr[N << 2];
	
	void pushup(int u)
	{
		tr[u].sum = (tr[lc].sum + tr[rc].sum) % mod;
	}
	
	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], void(0);
		int mid = l + r >> 1;
		build(lc, l, mid), build(rc, mid + 1, r);
		pushup(u);
	}
	
	void pushdown(int u)
	{
		if (!tr[u].tag) return;
		tr[lc].sum = (tr[lc].sum + tr[u].tag * tr[lc].len()) % mod;
		tr[rc].sum = (tr[rc].sum + tr[u].tag * tr[rc].len()) % mod;
		tr[lc].tag += tr[u].tag, tr[rc].tag += tr[u].tag;
		tr[u].tag = 0;
	}
	
	void modify(int u, int l, int r, int k)
	{
		if (l <= tr[u].l && tr[u].r <= r)
		{
			tr[u].sum = (tr[u].sum + tr[u].len() * k) % mod;
			tr[u].tag += k;
			return;
		}
		
		pushdown(u);
		int mid = tr[u].l + tr[u].r >> 1;
		if (l <= mid) modify(lc, l, r, k);
		if (r > mid) modify(rc, l, r, k);
		pushup(u);
	}
	
	int query(int u, int l, int r, int k)
	{
		if (l <= tr[u].l && tr[u].r <= r)
		{
            if (k == 1) return tr[u].sum;
            else return tr[u].len() - tr[u].sum;
        }
		pushdown(u);
		int mid = tr[u].l + tr[u].r >> 1;
		int res = 0;
		if (l <= mid) res = (res + query(lc, l, r, k)) % mod;
		if (r > mid) res = (res + query(rc, l, r, k)) % mod;
		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)
{
	depth[ver] = depth[pre] + 1, 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);
		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);	// 再遍历轻子节点
	}
}

bool query_range(int x, int y, int k)
{
	int res = 0;
	while (top[x] != top[y])
	{
		if (depth[top[x]] < depth[top[y]]) swap(x, y);
		if (SGT.query(1, dfn[top[x]], dfn[x], k)) return true;
		x = fa[top[x]];
	}
	if (depth[x] > depth[y]) swap(x, y);
	return SGT.query(1, dfn[x], dfn[y], k) > 0;
}

signed main()
{
    memset(h, -1, sizeof h), idx = -1;
    read(n, m);
    for (int i = 1; i <= n; i ++)
    {
        char opt; cin >> opt;
        a[i] = opt == 'H' ? 1 : 0;
    }
    for (int i = 1; i < n; i ++)
    {
        int a, b; read(a, b);
        add(a, b), add(b, a);
    }
    dfs1(1, 0), dfs2(1, 1);
    SGT.build(1, 1, n);

    char opt; int x, y;
    while (m --)
    {
        scanf("%s%d%d", &opt, &x, &y);
        bool cnt = query_range(x, y, opt == 'H');
        cout << cnt;
    }
    cout << '\n';
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 2156kb

input:

920 900
HHHHHHHHHHHHHHHHHHHHHHHHHGHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHHHHHHGHHGHHHHHHHGGHGHHHGHHGHGHHHHHH...

output:

0010110111110111101111001100011101110010001110110110001101100101111100101111110001111001110110001111...

result:

wrong answer 1st lines differ - expected: '001010011110011110101000100001...101010011101001110111011...

Test #2:

score: 0
Wrong Answer
time: 0ms
memory: 2156kb

input:

927 949
HHHHGHGGGHHHHGHGHHHHHGHGGHGGGHHHGGHHHHHHGGGGGGHHHGGHHHHGHHHHGGGHHHGHGHHHHGGGGHHGHHGGHGHGGGGG...

output:

1111010011111111101101100110110111111101111001111111111111101111110011111111111110110111111010111111...

result:

wrong answer 1st lines differ - expected: '111100001111011110110110011011...001100101101011111101111...

Test #3:

score: 0
Wrong Answer
time: 0ms
memory: 2156kb

input:

934 998
HHHHGHHHGHHGGGHGGHGHGHHHGHHGHGGGHHHHHGHHGGHHHHHHGHHHGGHHHHHGGHHHGHHHHHHHGHHGHGHGGHHGHGHHHGGH...

output:

0101011111111111110111011111111111111101101100111111001111111111111111011111101101111011001011111111...

result:

wrong answer 1st lines differ - expected: '110101011111101011010101111111...101110111001110010111110...

Test #4:

score: 0
Wrong Answer
time: 0ms
memory: 2156kb

input:

941 947
HHHHHHHHHHHHHHHGHHHGHHHGHHHHGHHHGHHHHHHHHGHHHHHHHHHGHHHHHGHHHHHGHHGHHHHGHGGHHGHHHHHHHHHHGGHH...

output:

1111111011111110001101011111111100111101100101110111111111110001111111111110111111111101101111101111...

result:

wrong answer 1st lines differ - expected: '111111101101011000110101010111...010110001111011001100000...

Test #5:

score: 0
Wrong Answer
time: 125ms
memory: 10496kb

input:

92189 98896
HHHHHHHHHGHHHHGGGHGHHHHHHGHHHHHGHHHHHHHHHGHGHHHGHHHHHHGGHHHGHGGHHHHHHHHHGHHHHHHGHGHGHHHG...

output:

1110111111111111111111111111111111111011110111111111111111110111111101111111111111111111111111111111...

result:

wrong answer 1st lines differ - expected: '111011111111111111111111111111...111111111111111111111111...

Test #6:

score: 0
Wrong Answer
time: 119ms
memory: 10664kb

input:

95803 95747
HHHHHHHHHHHHHGHHHHHHHHHHHHHHHHHHGHHGHGHHHHGHGHHHHHHGGHHHHHHHHHHHHGHHHGHHHHHHHHHGHGGHHHHH...

output:

0111111111111111111111111111111111111011111101111110111111111111111011110111111101111111101100111111...

result:

wrong answer 1st lines differ - expected: '101101110111111111111111110111...111110110111111011111111...

Test #7:

score: 0
Wrong Answer
time: 110ms
memory: 10520kb

input:

92610 90996
HHGGGHHGHGGGHGHGHGGGGGGHGGGHGGHGHGHGGHHHHHGHGGHGHGGGGHGGGHGGHHGHHHGGHGHGGHGGGHGGGGGGGGGG...

output:

1111111111111111111111111111111111111110111111111111101111111111011111111111111101111111111111111111...

result:

wrong answer 1st lines differ - expected: '111111111111111111101111111111...111111001111111111111111...

Test #8:

score: 0
Wrong Answer
time: 122ms
memory: 10680kb

input:

96224 91494
HHHHHHHHGGHHGHHHHHHHHHHHHHGHHHGHHHHHHHHHHHHHHHHHGGHHHHHHGHHHHHHHHHHHHHHHHHGHGGGHGHGHHHHH...

output:

1111011110111110111111111111111011011111111111011101111011111111111111111111111111111111111111111101...

result:

wrong answer 1st lines differ - expected: '111101011011110011111111111101...110101101111111111111110...

Test #9:

score: 0
Wrong Answer
time: 112ms
memory: 10540kb

input:

93031 96743
HHHHHHHHHGHHHHHHHHHHHHHHHHHGGHHHHGHHHGHHHHHHHHHGHHHHHHHHHHHHHHGGHHHHHGGHGHHHGHHHGHHHHHHG...

output:

0110011111101111111111111111101111101111110111111111111111111111111111111111110001011110011111011111...

result:

wrong answer 1st lines differ - expected: '010001111110111101111111011100...111111110111001111110101...

Test #10:

score: 0
Wrong Answer
time: 116ms
memory: 10708kb

input:

96645 93594
HHHHHHHHHGHGHHHHHHHGHHHHGGGHHHHHHHHHHHHHHHHHHGHHHHHGHGHHHHHGHGHHHHHGHHHHGHHHHGGHHGHHHHGH...

output:

0111000111111110111111111111111111111011111111111110111111011111111111111110111110101111111111111111...

result:

wrong answer 1st lines differ - expected: '010100011011111001111111111111...111001111111111111011001...