Total Highway Distance

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Little Hi and Little Ho are playing a construction simulation game. They build N cities (numbered from 1 to N) in the game and connect them by N-1 highways. It is guaranteed that each pair of cities are connected by the highways directly or indirectly.

The game has a very important value called Total Highway Distance (THD) which is the total distances of all pairs of cities. Suppose there are 3 cities and 2 highways. The highway between City 1 and City 2 is 200 miles and the highway between City 2 and City 3 is 300 miles. So the THD is 1000(200 + 500 + 300) miles because the distances between City 1 and City 2, City 1 and City 3, City 2 and City 3 are 200 miles, 500 miles and 300 miles respectively.

During the game Little Hi and Little Ho may change the length of some highways. They want to know the latest THD. Can you help them?

输入

Line 1: two integers N and M.

Line 2 .. N: three integers u, v, k indicating there is a highway of k miles between city u and city v.

Line N+1 .. N+M: each line describes an operation, either changing the length of a highway or querying the current THD. It is in one of the following format.

EDIT i j k, indicating change the length of the highway between city i and city j to k miles.

QUERY, for querying the THD.

For 30% of the data: 2<=N<=100, 1<=M<=20

For 60% of the data: 2<=N<=2000, 1<=M<=20

For 100% of the data: 2<=N<=100,000, 1<=M<=50,000, 1 <= u, v <= N, 0 <= k <= 1000.

输出

For each QUERY operation output one line containing the corresponding THD.

样例输入
3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY
样例输出
10
14
12
分析:每条边对答案的贡献都是两边点个数的乘积乘上这个长度,然后维护好长度来更新答案;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,val[maxn],son[maxn],fa[maxn];
ll ans;
vi a[maxn],b[maxn];
char q[];
int dfs(int now,int pre)
{
son[now]=;
for(int i=;i<a[now].size();i++)
{
int x=a[now][i],y=b[now][i];
if(x!=pre)
{
son[now]+=dfs(x,now);
fa[x]=now,val[x]=y;
ans+=(ll)son[x]*(n-son[x])*val[x];
}
}
return son[now];
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n-)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
a[u].pb(v),a[v].pb(u);
b[u].pb(w),b[v].pb(w);
}
dfs(,-);
rep(i,,m)
{
scanf("%s",q);
if(q[]=='Q')printf("%lld\n",ans);
else
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(fa[v]==u)swap(u,v);
ans=ans+(ll)son[u]*(n-son[u])*(w-val[u]);
val[u]=w;
}
}
//system("Pause");
return ;
}

Total Highway Distance的更多相关文章

  1. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  2. Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  3. LeetCode Total Hamming Distance

    原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...

  4. [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  5. 477. Total Hamming Distance总的二进制距离

    [抄题]: The Hamming distance between two integers is the number of positions at which the correspondin ...

  6. [LeetCode] 477. Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. [LeetCode] 477. Total Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  8. 【LeetCode】477. Total Hamming Distance 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  9. 461. Hamming Distance and 477. Total Hamming Distance in Python

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

随机推荐

  1. print a float number with 3 digits following

    just use the java's printf function. It is like C's printf. System.out.printf("%.3f\n", x) ...

  2. Entity Framework 学习高级篇2—改善EF代码的方法(下)

    ,IQueryable<Customers>>( (database) => database.Customers.Where(c => c.City == " ...

  3. shell查看进程

    用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]th ...

  4. idea 端口占用

    netstat -anp | grep 8080 lsof -i:8080 查看8080端口被什么进程占用 kill (-s 9) 12903 -s 9 强制 尽快 12903 上面查出的 PID 其 ...

  5. Spring Boot 系列教程1-HelloWorld

    入门 如果你用过Spring JavaConfig的话,会发现虽然没有了xml配置的繁琐,但是使用各种注解导入也是很大的坑, 然后在使用一下Spring Boot,你会有一缕清风拂过的感觉, 真是爽的 ...

  6. 怎样将MySQL数据库上传到服务器

    首先,需要将本地的数据库导出来,作为一个数据文件,以备稍后上传到服务器用,在本地登陆phpmyadmin控制面板: 登陆成功后,在左侧选择需要操作的数据库: 选择后,页面会自动刷新,然后再在右边点击[ ...

  7. Apache 的常见问题

    Apache "No services installed"问题的处理以及Apache提示 the requested operation has failed而无法启动 安装完 ...

  8. Sticks<DFS>

    题意: 给n个木棍,这些木棍是由m个长度均为L的木棍切割而来,求L的最小值. 思路: DFS+剪枝. 剪枝: 1:L的取值范围在n(max)和n(sum)之间,逐个枚举.sum%L!=0则L不能用. ...

  9. 新建aix实例

    http://www.cnblogs.com/kfarvid/archive/2010/12/21/1912553.html   DB2数据库 http://wenku.baidu.com/view/ ...

  10. java 环境的配置

    JAVA_HOMEC:\Program Files\Java\jdk1.6.0_02 PATHC:\Program Files\Java\jdk1.6.0_02\bin CLASSPATH.;%JAV ...