constroy has some gears, whose radii may vary. Two gears may be adjacent due to one of the following conditions:
they mesh together, which causes them to have equal linear velocity on edge; or they are fixed on the same shaft, which causes them to have equal angular velocity.
He guarantees that there are no two gears that satisfy both of the above conditions, and there is at most one transmission path between every two gears, where a transmission path is a sequence of different gears, in which two consecutive gears are adjacent.
Now, constroy assigns an angular velocity to one of these gears, and then he wants to know the largest angular velocity among them.
But sd0061 thinks it is too easy for you, so he decides to replace some gears and then ask you the question.
The input contains multiple (about 30) test cases.
For each test case, the first line contains three integers n, m, q (0≤n,m,q≤105, m<n), indicating the number of gears, the number of adjacent pairs and the number of operations respectively.
The second line contains n integers, the i-th number of which is ri (ri∈{20,21,…,230}), denoting the radius of the i-th gear.
Each of the next m lines contains three integers a, x, y (a∈{1,2}, 1≤x,y≤n, x≠y), representing that the x-th gear and the y-th one are adjacent due to the a-th condition.
The next q lines describe the operations in chronological order. Each line contains three integers a, x, y (a∈{1,2}, 1≤x≤n, y∈{20,21,…,230}), representing an operation where:
if a=1, the operation is to replace the x-th gear with another one of radius y;
if a=2, the operation is to ask you to determine the maximum possible angular velocity among all the gears if constroy assigns an angular velocity of y to the x-th gear.
Note that the gears are always at rest.
For each test case, firstly, output “Case #x:” in one line (without quotes), where x indicates the case number starting from 1.
Then, for each operation with a=2, output a real number in one line, denoting the natural logarithm of the maximum angular velocity, with the precision for exactly three digits after the decimal point.
4 3 4
1 4 16 2
1 2 4
1 2 3
2 1 4
1 1 16
1 2 4
2 4 4
1 4 16
4 3 5
2 16 4 8
2 1 2
1 2 3
1 1 4
2 1 4
1 3 8
2 1 16
1 4 1
2 1 8
Case #1:
1.386
Case #2:
2.773
3.466
2.773
给定n
个齿轮,齿轮之间有两种连接方式:角速度相等或线速度相等构成森林。
有q
次操作,一种操作是改变某个齿轮的半径,一种操作是给定某个齿轮一个角速度,求齿轮组中最大的角速度
题意要求我们求出所有齿轮相对于根节点的相对角速度,考虑到可以使用dfs序+线段树进行维护。由于有两种连接方式,所以分两种情况进行讨论:
根据中学物理我们知道对于线速度相等的点
w
1
∗
r
1
=
w
2
∗
r
2
w1*r1=w2*r2
w1∗r1=w2∗r2,再加上题目数据半径均为
2
n
2^{n}
2n,所以可以两边取对数转化为
l
o
g
2
w
1
+
l
o
g
2
r
1
=
l
o
g
2
w
2
+
l
o
g
2
r
2
log_{2}w1+ log_{2}r1=log_{2}w2+log_{2}r2
log2w1+log2r1=log2w2+log2r2,可以将区间乘法转化为加法。
处理时应该注意用并查集来判断集合,dfs时可以先搜索角边相连的点然后记录一个中序,再搜索线边相连的点,这样易于区间修改。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <stack>
using namespace std;
const int N = 1e5 + 50;
int n, m, q, rad[N], cnt, first[N], nxt[N * 2], to[N * 2], bz[N], p[N], len, l[N], r[N], mid[N], tim, c[N * 2], t[N * 4], val[N], lazy[N * 4];
void pushDown(int p)
{
lazy[p << 1] += lazy[p];
lazy[p << 1 | 1] += lazy[p];
t[p << 1] += lazy[p];
t[p << 1 | 1] += lazy[p];
lazy[p] = 0;
}
void build(int p, int l, int r)
{
if (l == r)
{
t[p] = val[l];
return;
}
int m = (l + r) >> 1;
build(p << 1, l, m);
build(p << 1 | 1, m + 1, r);
t[p] = max(t[p << 1], t[p << 1 | 1]);
}
void update(int p, int l, int r, int a, int b, int x)
{
if (a <= l and b >= r)
{
lazy[p] += x;
t[p] += x;
return;
}
if (lazy[p])
pushDown(p);
int m = (l + r) >> 1;
if (a <= m)
update(p << 1, l, m, a, b, x);
if (b > m)
update(p << 1 | 1, m + 1, r, a, b, x);
t[p] = max(t[p << 1], t[p << 1 | 1]);
}
int query(int p, int l, int r, int a, int b)
{
if (a <= l and b >= r)
{
return t[p];
}
if (lazy[p])
pushDown(p);
int m = (l + r) >> 1, maxn = INT_MIN;
if (a <= m)
maxn = max(maxn, query(p << 1, l, m, a, b));
if (b > m)
maxn = max(maxn, query(p << 1 | 1, m + 1, r, a, b));
return maxn;
}
void add(int x, int y, int z)
{
nxt[++len] = first[x];
first[x] = len;
to[len] = y;
c[len] = z;
}
void dfs(int x, int fa, int dis)
{
l[x] = ++tim;
val[tim] = dis;
for (int i = first[x]; i; i = nxt[i])
if (c[i] == 2 and to[i] != fa)
{
dfs(to[i], x, dis);
bz[to[i]] = c[i];
}
mid[x] = tim;
for (int i = first[x]; i; i = nxt[i])
if (c[i] == 1 and to[i] != fa)
{
dfs(to[i], x, dis + rad[x] - rad[to[i]]);
bz[to[i]] = c[i];
}
r[x] = tim;
}
int find(int x)
{
return p[x] == 0 ? x : (p[x] = find(p[x]));
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("r.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
while (scanf("%d%d%d", &n, &m, &q) != EOF)
{
len = tim = 0;
cnt++;
printf("Case #%d:\n", cnt);
memset(l, 0, sizeof(l));
memset(p, 0, sizeof(p));
memset(first, 0, sizeof(first));
memset(t, 0, sizeof(t));
memset(lazy, 0, sizeof(lazy));
for (int i = 1; i <= n; i++)
scanf("%d", &rad[i]), rad[i] = log2(rad[i]);
for (int a, x, y, i = 1; i <= m; i++)
{
scanf("%d%d%d", &a, &x, &y);
add(x, y, a);
add(y, x, a);
x = find(x), y = find(y), p[x] = y;
}
for (int i = 1; i <= n; i++)
if (p[i] == 0)
{
dfs(i, 0, 0);
}
build(1, 1, tim);
for (int a, x, y, i = 1; i <= q; i++)
{
scanf("%d%d%d", &a, &x, &y);
y = log2(y);
if (a == 1)
{
if (bz[x] == 1)
update(1, 1, tim, l[x], mid[x], rad[x] - y);
else
update(1, 1, tim, mid[x] + 1, r[x], y - rad[x]);
rad[x] = y;
}
else
{
int fa = find(x);
printf("%.3lf\n", (y - query(1, 1, tim, l[x], l[x]) + query(1, 1, tim, l[fa], r[fa])) * log(2.0));
}
}
}
}