poj 2763 Housewife Wind
分析:这道题是树链剖分的裸题,把边的信息保存在深度大的那个节点上就行了。
一开始写的邻接表,居然TLE了。后来百度发现有人说前向星跑得比较快?我不是很明白,但是改成前向星以后的确快了很多,邻接表是T,而前向星只需要2s左右。
这个代码还是邻接表,因为我不信,又修改了一下邻接表上几个我觉得会增加常数的地方,交上去还是T了- -代码里前向星的部分被注释掉了。
/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31;
/*****************************************************/
const int maxn = 1e5 + 5;
int siz[maxn], top[maxn], son[maxn], dep[maxn], fat[maxn], tid[maxn], rid[maxn];
int seg[maxn << 2], a[maxn];
int uu[maxn], vv[maxn], ww[maxn], tim;
int N, Q, S;
std::vector<int> G[maxn];
// struct Edge {
// int to, next;
// }edge[maxn << 1];
// int head[maxn], tot;
// void addedge(int u, int v) {
// edge[tot] = (Edge){v, head[u]};
// head[u] = tot ++;
// }
void dfs1(int u, int fa, int d) {
siz[u] = 1;
fat[u] = fa;
dep[u] = d;
int sz = G[u].size();
for (int i = 0; i < sz; i ++) {
int v = G[u][i];
if (v == fa) continue;
dfs1(v, u, d + 1);
siz[u] += siz[v];
if (son[u] == -1 || siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int tp) {
tid[u] = ++ tim;
top[u] = tp;
if (son[u] != -1) dfs2(son[u], tp);
int sz = G[u].size();
for (int i = 0; i < sz; i ++) {
int v = G[u][i];
if (v == son[u] || v == fat[u]) continue;
dfs2(v, v);
}
}
void PushUp(int v) {
seg[v] = seg[slch] + seg[srch];
}
void build(int l, int r, int v) {
if (l == r)
seg[v] = a[l];
else {
sgetmid;
build(lson);
build(rson);
PushUp(v);
}
}
void update(int p, int val, int l, int r, int v) {
if (l == r) {
seg[v] = val;
}
else {
sgetmid;
if (p <= m) update(p, val, lson);
else update(p, val, rson);
PushUp(v);
}
}
int query(int L, int R, int l, int r, int v) {
if (L <= l && r <= R) return seg[v];
int ans = 0;
sgetmid;
if (L <= m) ans += query(L, R, lson);
if (R > m) ans += query(L, R, rson);
return ans;
}
int getv(int u, int v) {
int tp1 = top[u], tp2 = top[v];
int ans = 0;
while (tp1 != tp2) {
if (dep[tp1] < dep[tp2]) {
swap(tp1, tp2);
swap(u, v);
}
ans += query(tid[tp1], tid[u], 1, N, 1);
u = fat[tp1];
tp1 = top[u];
}
if (u == v) return ans;
if (dep[u] > dep[v]) swap(u, v);
ans += query(tid[son[u]], tid[v], 1, N, 1);
return ans;
}
int main(int argc, char const *argv[]) {
while (~scanf("%d%d%d", &N, &Q, &S)) {
mem(son, -1);
tim = 0;
for (int i = 1; i < N; i ++) {
scanf("%d%d%d", &uu[i], &vv[i], &ww[i]);
G[uu[i]].pb(vv[i]);
G[vv[i]].pb(uu[i]);
}
dfs1(1, -1, 1);
dfs2(1, 1);
for (int i = 1; i < N; i ++) {
if (dep[uu[i]] > dep[vv[i]]) swap(uu[i], vv[i]);
a[tid[vv[i]]] = ww[i];
}
build(1, N, 1);
for (int i = 0; i < Q; i ++) {
int op, x, y;
scanf("%d", &op);
if (op) {
scanf("%d%d", &x, &y);
update(tid[vv[x]], y, 1, N, 1);
}
else {
scanf("%d", &x);
int ans = getv(S, x);
printf("%d\n", ans);
S = x;
}
}
}
return 0;
}
poj 2763 Housewife Wind的更多相关文章
- POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )
POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...
- POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新
题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...
- poj 2763 Housewife Wind(树链拆分)
id=2763" target="_blank" style="">题目链接:poj 2763 Housewife Wind 题目大意:给定一棵 ...
- POJ 2763 Housewife Wind(DFS序+LCA+树状数组)
Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11419 Accepted: 3140 D ...
- POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)
Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10378 Accepted: 2886 D ...
- POJ 2763 Housewife Wind (树链剖分 有修改单边权)
题目链接:http://poj.org/problem?id=2763 n个节点的树上知道了每条边权,然后有两种操作:0操作是输出 当前节点到 x节点的最短距离,并移动到 x 节点位置:1操作是第i条 ...
- poj 2763 Housewife Wind (树链剖分)
题目链接:http://poj.org/problem?id=2763 题意: 给定一棵含n个结点的树和树的边权,共有q次操作,分为两种 0 c :求从位置s到c的距离,然后s变成c 1 a b:把第 ...
- POJ 2763 Housewife Wind 纯粹LCA写法(简单无脑)
Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordina ...
- poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)²)修改与查询
/** problem: http://poj.org/problem?id=2763 **/ #include<stdio.h> #include<stdlib.h> #in ...
- POJ 2763 Housewife Wind(树链剖分+树状数组)
[题目链接] http://poj.org/problem?id=2763 [题目大意] 在一棵树上,给出一些边的边长,有修改边的边长的操作, 询问每次从当前点到目标点的最短距离 [题解] 树链剖分之 ...
随机推荐
- 将数据文件从普通文件系统移动到ASM
场景一:数据库可以关闭1.关闭并mount数据库 $ sqlplus '/as sysdba' SQL> shutdown immediate SQL> startup mount; 2. ...
- Tomcat8启动报there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
09-Dec-2016 10:57:49.150 WARNING [localhost-startStop-1] org.apache.catalina.webresources.Cache.getR ...
- POJ 2104 K-th Number(主席树——附讲解)
Description You are working for Macrohard company in data structures department. After failing your ...
- Android studio 查看签名
根据密钥查看 根据安装包查看:改apk为zip 解压 打开 META-INF --->cmd: keytool -printcert -file CERT.RSA
- 利用jquery获取html中被选中的input的值
单个按钮 <div id="wrap"> <input type="radio" name="payMethod" val ...
- HTML之常用标签
一.H标签 标题(Heading)是通过<h1>-<h6>等标签进行定义的. <h1>定义最大的标题,<h6>定义最小的标题. 未完待续....
- jQuery 选择器 (基础恶补之三)+Ajax
jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法: $(selector). ...
- [课程设计]Scrum 2.6 多鱼点餐系统开发进度(下单一览页面-菜式添加功能实现)
Scrum 2.6 多鱼点餐系统开发进度 (下单一览页面-菜式添加功能实现) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题 ...
- java开发环境的主题色的变化
eclipse:Help->Install New Software->Work with:Update Site - http://eclipse-color-theme.github ...
- BFS AOJ 0558 Chess
AOJ 0558 Chess http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0558 在H * W的地图上有N个奶酪工厂,每个 ...