SPOJ - QTREE3 Query on a tree again!
Description
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N. In the start, the color of any node in the tree is white.
We will ask you to perfrom some instructions of the following form:
- 0 i : change the color of the i-th node (from white to black, or from black to white);
or - 1 v : ask for the id of the first black node on the path from node 1 to node v. if it doesn't exist, you may return -1 as its result.
Input
In the first line there are two integers N and Q.
In the next N-1 lines describe the edges in the tree: a line with two integers a bdenotes an edge between a and b.
The next Q lines contain instructions "0 i" or "1 v" (1 ≤ i, v ≤ N).
Output
For each "1 v" operation, write one integer representing its result.
Example
Input:
9 8
1 2
1 3
2 4
2 9
5 9
7 9
8 9
6 8
1 3
0 8
1 6
1 7
0 2
1 9
0 2
1 9
Output:
-1
8
-1
2
-1
Constraints & Limits
There are 12 real input files.
For 1/3 of the test cases, N=5000, Q=400000.
For 1/3 of the test cases, N=10000, Q=300000.
For 1/3 of the test cases, N=100000, Q=100000.
题解
树链剖分后,用线段树或者树状数组维护区间和,找距离1最近的黑点即找区间和大于零的最小的左端点,对于线段树查询,我们优先访问左儿子即可,对于树状数组可以套二分查询深度最小的点
代码
二分+树状数组
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
vector<int> G[N];
int n, m;
int fa[N];
int son[N];
int sze[N];
int dep[N];
void dfs1(int u, int f) {
sze[u] = 1;
fa[u] = f;
son[u] = 0;
dep[u] = dep[f] + 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == f) continue;
dfs1(v, u);
sze[u] += sze[v];
if (sze[v] > sze[son[u]]) son[u] = v;
}
}
int top[N];
int cnt;
int pos[N];
int mp[N];
void dfs2(int u, int f, int t) {
top[u] = t;
pos[u] = ++cnt;
mp[cnt] = u;
if (son[u]) dfs2(son[u], u, t);
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == f || v == son[u]) continue;
dfs2(v, u, v);
}
}
int a[N];
int d[N];
void update(int x, int v) {
for (int i = x; i <= n; i += i & (-i)) d[i] += v;
}
int query(int x) {
int ans = 0;
for (int i = x; i; i -= i & (-i)) ans += d[i];
return ans;
}
int calcans(int u) {
int ans = 0;
int res = -1;
while (top[u] != top[1]) {
if (query(pos[u]) - query(pos[top[u]] - 1) > 0) {
int l = pos[top[u]], r = pos[u];
int tl = l;
while (l <= r) {
int mid = (l + r) >> 1;
if (query(mid) - query(tl - 1) > 0) {
r = mid - 1;
ans = mid;
}
else l = mid + 1;
}
if (ans) res = mp[ans];
}
u = fa[top[u]];
}
if (query(pos[u]) - query(pos[1] - 1) > 0) {
int l = pos[1], r = pos[u];
while (l <= r) {
int mid = (l + r) >> 1;
if (query(mid) > 0) {
r = mid - 1;
ans = mid;
}
else l = mid + 1;
}
if (ans) res = mp[ans];
}
return res;
}
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
G[i].clear();
}
memset(d, 0, sizeof(d));
memset(sze, 0, sizeof(sze));
memset(a, 0, sizeof(a));
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dep[0] = 0;
dfs1(1, 0);
cnt = 0;
dfs2(1, 0, 1);
int ch, k;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &ch, &k);
switch(ch) {
case 1: printf("%d
", calcans(k)); break;
case 0: {
if (a[k] == 0) {
update(pos[k], 1);
a[k] = 1;
}
else {
update(pos[k], -1);
a[k] = 0;
}
break;
}
}
}
return 0;
}
线段树
#include <bits/stdc++.h>
#define lson (o << 1)
#define rson (o << 1 | 1)
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
vector<int> G[N];
int n, m;
int fa[N];
int son[N];
int sze[N];
int dep[N];
void dfs1(int u, int f) {
sze[u] = 1;
fa[u] = f;
son[u] = 0;
dep[u] = dep[f] + 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == f) continue;
dfs1(v, u);
sze[u] += sze[v];
if (sze[v] > sze[son[u]]) son[u] = v;
}
}
int top[N];
int cnt;
int pos[N];
int mp[N];
void dfs2(int u, int f, int t) {
top[u] = t;
pos[u] = ++cnt;
mp[cnt] = u;
if (son[u]) dfs2(son[u], u, t);
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == f || v == son[u]) continue;
dfs2(v, u, v);
}
}
int a[N];
int sumv[N << 2];
void pushup(int o) {
sumv[o] = sumv[lson] + sumv[rson];
}
void update(int o, int l, int r, int pos) {
if (l == r) {
sumv[o] = !sumv[o];
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(lson, l, mid, pos);
else update(rson, mid + 1, r, pos);
pushup(o);
}
int query(int o, int l, int r, int ql, int qr) {
if (sumv[o] == 0) return 0;
if (l == r) return l;
int mid = (l + r) >> 1;
int ans = 0;
if (ql <= mid) ans = query(lson, l, mid, ql, qr);
if (ans) return ans;
if (qr > mid) ans = query(rson, mid + 1, r, ql, qr);
return ans;
}
int calcans(int u) {
int ans;
int res = -1;
while (1) {
ans = query(1, 1, n, pos[top[u]], pos[u]);
if (ans) res = mp[ans];
if (u == 1 || top[u] == 1) break;
u = fa[top[u]];
}
return res;
}
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
G[i].clear();
}
memset(sumv, 0, sizeof(sumv));
memset(sze, 0, sizeof(sze));
memset(a, 0, sizeof(a));
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dep[0] = 0;
dfs1(1, 0);
cnt = 0;
dfs2(1, 0, 1);
int ch, k;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &ch, &k);
switch(ch) {
case 1: printf("%d
", calcans(k)); break;
case 0: {
update(1, 1, n, pos[k]);
break;
}
}
}
return 0;
}
BZOJ-2243 染色
Description
给定一棵有n个节点的无根树和m个操作,操作有2类:
1、将节点a到节点b路径上所有点都染成颜色c;
2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),
如“112221”由3段组成:“11”、“222”和“1”。
请你写一个程序依次完成这m个操作。
Input
第一行包含2个整数n和m,分别表示节点数和操作数;
第二行包含n个正整数表示n个节点的初始颜色
下面 行每行包含两个整数x和y,表示x和y之间有一条无向边。
下面 行每行描述一个操作:
“C a b c”表示这是一个染色操作,把节点a到节点b路径上所有点(包括a和b)都染成颜色c;
“Q a b”表示这是一个询问操作,询问节点a到节点b(包括a和b)路径上的颜色段数量。
Output
对于每个询问操作,输出一行答案。
Sample Input
6 5
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5
Sample Output
3
1
2
Hint
数N<=105,操作数M<=105,所有的颜色C为整数且在[0, 10^9]之间。
题解
树剖后用线段树维护每个区间的颜色段数,和左端点右端点的颜色是什么,合并时判断颜色是否一样即可
代码
#include <bits/stdc++.h>
#define lson (o << 1)
#define rson (o << 1 | 1)
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
vector<int> G[N];
const ll inf = 1e9;
int n;
ll val[N];
int fa[N];
int son[N];
int sze[N];
int dep[N];
void dfs1(int u, int f) {
sze[u] = 1;
fa[u] = f;
son[u] = 0;
dep[u] = dep[f] + 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == f) continue;
dfs1(v, u);
sze[u] += sze[v];
if (sze[v] > sze[son[u]]) son[u] = v;
}
}
int top[N];
int cnt;
int pos[N];
int a[N];
void dfs2(int u, int f, int t) {
top[u] = t;
pos[u] = ++cnt;
a[cnt] = val[u];
if (son[u]) dfs2(son[u], u, t);
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == f || v == son[u]) continue;
dfs2(v, u, v);
}
}
ll sumv[N << 2];
ll cov[N << 2];
ll L[N << 2];
ll R[N << 2];
void pushup(int o) {
L[o] = L[lson];
R[o] = R[rson];
if (R[lson] == L[rson]) {
sumv[o] = sumv[lson] + sumv[rson] - 1;
}
else sumv[o] = sumv[lson] + sumv[rson];
}
void pushdown(int o, int l, int r) {
if (cov[o]) {
cov[lson] = cov[rson] = cov[o];
sumv[lson] = sumv[rson] = 1;
L[lson] = R[lson] = cov[o];
L[rson] = R[rson] = cov[o];
cov[o] = 0;
}
}
void build(int o, int l, int r) {
if (l == r) {
sumv[o] = 1;
cov[o] = 0;
L[o] = R[o] = a[l];
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid); build(rson, mid + 1, r);
pushup(o);
}
void update(int o, int l, int r, int ql, int qr, ll v) {
if (ql <= l && r <= qr) {
sumv[o] = 1;
cov[o] = v;
L[o] = R[o] = v;
return;
}
int mid = (l + r) >> 1;
pushdown(o, l, r);
if (ql <= mid) update(lson, l, mid, ql, qr, v);
if (qr > mid) update(rson, mid + 1, r, ql, qr, v);
pushup(o);
}
ll query(int o, int l, int r, int ql, int qr) {
if (ql == l && r == qr) {
return sumv[o];
}
int mid = (l + r) >> 1;
pushdown(o, l, r);
if (qr <= mid) return query(lson, l, mid, ql, qr);
else if (ql > mid) return query(rson, mid + 1, r, ql, qr);
else {
ll res = query(lson, l, mid, ql, mid) + query(rson, mid + 1, r, mid + 1, qr);
if (R[lson] == L[rson]) return res - 1;
else return res;
}
}
ll querycor(int o, int l, int r, int pos) {
if (l == r) {
return L[o];
}
int mid = (l + r) >> 1;
pushdown(o, l, r);
if (pos <= mid) return querycor(lson, l, mid, pos);
else return querycor(rson, mid + 1, r, pos);
}
ll calcsum(int u, int v) {
ll ans = 0;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
ans += query(1, 1, n, pos[top[u]], pos[u]);
int tmp = top[u];
u = fa[top[u]];
if (querycor(1, 1, n, pos[tmp]) == querycor(1, 1, n, pos[u])) ans--;
}
if (dep[u] < dep[v]) swap(u, v);
ans += query(1, 1, n, pos[v], pos[u]);
return ans;
}
void update1(int u, int v, ll val) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
update(1, 1, n, pos[top[u]], pos[u], val);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
update(1, 1, n, pos[v], pos[u], val);
}
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &n);
int m; scanf("%d", &m);
for (int i = 1; i <= n; i++) scanf("%lld", &val[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dep[0] = 0;
dfs1(1, 0);
cnt = 0;
dfs2(1, 0, 1);
build(1, 1, n);
char ch[10];
for (int i = 1; i <= m; i++) {
scanf("%s", ch);
int l, r, k;
ll v;
switch(ch[0]) {
case 'Q': scanf("%d%d", &l, &r); printf("%lld
", calcsum(l, r)); break;
case 'C': {
scanf("%d%d%lld", &l, &r, &v);
update1(l, r, v);
break;
}
}
}
return 0;
}
BZOJ-2157 旅游
Description
Ray 乐忠于旅游,这次他来到了T 城。T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接。为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间有且只有一条路径。换句话说, T 城中只有N − 1 座桥。Ray 发现,有些桥上可以看到美丽的景色,让人心情愉悦,但有些桥狭窄泥泞,令人烦躁。于是,他给每座桥定义一个愉悦度w,也就是说,Ray 经过这座桥会增加w 的愉悦度,这或许是正的也可能是负的。有时,Ray 看待同一座桥的心情也会发生改变。现在,Ray 想让你帮他计算从u 景点到v 景点能获得的总愉悦度。有时,他还想知道某段路上最美丽的桥所提供的最大愉悦度,或是某段路上最糟糕的一座桥提供的最低愉悦度。
Input
输入的第一行包含一个整数N,表示T 城中的景点个数。景点编号为 0...N − 1。接下来N − 1 行,每行三个整数u、v 和w,表示有一条u 到v,使 Ray 愉悦度增加w 的桥。桥的编号为1...N − 1。|w| <= 1000。输入的第N + 1 行包含一个整数M,表示Ray 的操作数目。接下来有M 行,每行描述了一个操作,操作有如下五种形式: C i w,表示Ray 对于经过第i 座桥的愉悦度变成了w。 N u v,表示Ray 对于经过景点u 到v 的路径上的每一座桥的愉悦度都变成原来的相反数。 SUM u v,表示询问从景点u 到v 所获得的总愉悦度。 MAX u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最大愉悦度。 MIN u v,表示询问从景点u 到v 的路径上的所有桥中某一座桥所提供的最小愉悦度。测试数据保证,任意时刻,Ray 对于经过每一座桥的愉悦度的绝对值小于等于1000。
Output
对于每一个询问(操作S、MAX 和MIN),输出答案。
Sample Input
3
0 1 1
1 2 2
8
SUM 0 2
MAX 0 2
N 0 1
SUM 0 2
MIN 0 2
C 1 3
SUM 0 2
MAX 0 2
Sample Output
3
2
1
-1
5
3
Hint
一共有10 个数据,对于第i (1 <= i <= 10) 个数据, N = M = i * 2000。
题解
本题给的是边权,对于边权我们不太方便树链剖分,我们将每条边的边权给子节点,这样根节点点权为0,然后就可以正常树剖了,对于取区间相反数的操作,我们就将最大值和最小值交换再取负数就可以了,区间和直接取负数.
#include <bits/stdc++.h>
#define lson (o << 1)
#define rson (o << 1 | 1)
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
struct node {
int v, w;
node(int v = 0, int w = 0): v(v), w(w) {}
};
vector<node> G[N];
const int inf = 1e9;
int n;
int val[N];
int fa[N];
int son[N];
int sze[N];
int dep[N];
void dfs1(int u, int f) {
sze[u] = 1;
fa[u] = f;
son[u] = 0;
dep[u] = dep[f] + 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i].v;
if (v == f) continue;
val[v] = G[u][i].w;
dfs1(v, u);
sze[u] += sze[v];
if (sze[v] > sze[son[u]]) {
son[u] = v;
}
}
}
int top[N];
int cnt;
int pos[N];
int a[N];
void dfs2(int u, int f, int t) {
top[u] = t;
pos[u] = ++cnt;
a[cnt] = val[u];
if (son[u]) dfs2(son[u], u, t);
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i].v;
if (v == f || v == son[u]) continue;
dfs2(v, u, v);
}
}
int sumv[N << 2];
int maxv[N << 2];
int minv[N << 2];
void pushup(int o) {
sumv[o] = sumv[lson] + sumv[rson];
maxv[o] = max(maxv[lson], maxv[rson]);
minv[o] = min(minv[lson], minv[rson]);
}
int negv[N << 2];
void myswap(int x) {
negv[x] ^= 1;
swap(maxv[x], minv[x]);
maxv[x] = -maxv[x];
minv[x] = -minv[x];
sumv[x] = -sumv[x];
}
void pushdown(int o, int l, int r) {
if (negv[o]) {
myswap(lson); myswap(rson);
negv[o] = 0;
}
}
void build(int o, int l, int r) {
if (l == r) {
sumv[o] = maxv[o] = minv[o] = a[l];
return;
}
int mid = (l + r) >> 1;
build(lson, l, mid); build(rson, mid + 1, r);
pushup(o);
}
void update(int o, int l, int r, int pos, int v) {
if (l == r) {
sumv[o] = maxv[o] = minv[o] = v;
return;
}
int mid = (l + r) >> 1;
pushdown(o, l, r);
if (pos <= mid) update(lson, l, mid, pos, v);
else update(rson, mid + 1, r, pos, v);
pushup(o);
}
void neg(int o, int l, int r, int ql, int qr) {
if (ql > r || qr < l) return;
if (ql <= l && r <= qr) {
myswap(o);
return;
}
int mid = (l + r) >> 1;
pushdown(o, l, r);
if (ql <= mid) neg(lson, l, mid, ql, qr);
if (qr > mid) neg(rson, mid + 1, r, ql, qr);
pushup(o);
}
int querysum(int o, int l, int r, int ql, int qr) {
if (ql > r || qr < l) return 0;
if (ql <= l && r <= qr) {
return sumv[o];
}
int mid = (l + r) >> 1;
int ans = 0;
pushdown(o, l, r);
if (ql <= mid) ans += querysum(lson, l, mid, ql, qr);
if (qr > mid) ans += querysum(rson, mid + 1, r, ql, qr);
return ans;
}
int querymax(int o, int l, int r, int ql, int qr) {
if (ql > r || qr < l) return -inf;
if (ql <= l && r <= qr) {
return maxv[o];
}
int mid = (l + r) >> 1;
int ans = -inf;
pushdown(o, l, r);
if (ql <= mid) ans = max(ans, querymax(lson, l, mid, ql, qr));
if (qr > mid) ans = max(ans, querymax(rson, mid + 1, r, ql, qr));
return ans;
}
int querymin(int o, int l, int r, int ql, int qr) {
if (ql > r || qr < l) return inf;
if (ql <= l && r <= qr) {
return minv[o];
}
int mid = (l + r) >> 1;
pushdown(o, l, r);
int ans = inf;
if (ql <= mid) ans = min(ans, querymin(lson, l, mid, ql, qr));
if (qr > mid) ans = min(ans, querymin(rson, mid + 1, r, ql, qr));
return ans;
}
int csum(int u, int v) {
int ans = 0;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
ans += querysum(1, 1, n, pos[top[u]], pos[u]);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
ans += querysum(1, 1, n, pos[v] + 1, pos[u]);
return ans;
}
int cmax(int u, int v) {
int ans = -inf;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
ans = max(ans, querymax(1, 1, n, pos[top[u]], pos[u]));
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
ans = max(ans, querymax(1, 1, n, pos[v] + 1, pos[u]));
return ans;
}
int cmin(int u, int v) {
int ans = inf;
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
ans = min(ans, querymin(1, 1, n, pos[top[u]], pos[u]));
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
ans = min(ans, querymin(1, 1, n, pos[v] + 1, pos[u]));
return ans;
}
void update1(int u, int v) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
neg(1, 1, n, pos[top[u]], pos[u]);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
neg(1, 1, n, pos[v] + 1, pos[u]);
}
struct edge {
int u, v;
} edge[N];
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &n);
val[1] = 0;
for (int i = 1; i < n; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u++; v++;
G[u].push_back(node(v, w));
G[v].push_back(node(u, w));
edge[i].u = u;
edge[i].v = v;
}
dep[0] = 0;
dfs1(1, 0);
cnt = 0;
dfs2(1, 0, 1);
build(1, 1, n);
int m; scanf("%d", &m);
char ch[10];
for (int i = 1; i <= m; i++) {
scanf("%s", ch);
int x, y;
scanf("%d%d", &x, &y);
if (ch[0] == 'S') {
x++, y++;
printf("%d
", csum(x, y));
}
else if (ch[0] == 'C') {
int v = dep[edge[x].u] < dep[edge[x].v] ? edge[x].v : edge[x].u;
update(1, 1, n, pos[v], y);
}
else if (ch[0] == 'N') {
x++; y++;
update1(x, y);
}
else if (ch[1] == 'A') {
x++; y++;
printf("%d
", cmax(x, y));
}
else {
x++; y++;
printf("%d
", cmin(x, y));
}
}
return 0;
}
continue