HDU6039 Gear Up

Problem Description

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.

Input

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.

Output

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.

Example Input

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

Example Output

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 w1r1=w2r2,再加上题目数据半径均为 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时可以先搜索角边相连的点然后记录一个中序,再搜索线边相连的点,这样易于区间修改。

C++

#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));
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值