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 [题目大意] 在一棵树上,给出一些边的边长,有修改边的边长的操作, 询问每次从当前点到目标点的最短距离 [题解] 树链剖分之 ...
随机推荐
- MSSQL 2012 拒绝了对对象 'extended_properties' (数据库 'mssqlsystemresource',架构 'sys')的 SELECT 权限
查看数据库的表的时候报如下错误: MSSQL 拒绝了对对象 ) 解决方法: 在数据库里相应的用户权限中,把db_denydatareader的复选框的勾去掉.db_denydatareader是拒绝访 ...
- eclipse中配置tomcat
配置eclipse中的tomcat非常简单,之前以为windows下和mac中可能会不一样,但是经过配置发现是一样的: 下面就是在eclipse中简单的配置tomcat如下(mac和windows中都 ...
- wampserver---------如何让局域网其他电脑访问我的apache
wamp版本:wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b 修改方式,看图:修改完记得重启.
- Thinkphp---------Call to a member function free_result() on a non-object
1.平时用框架用久了,直接执行原生的sql反而做起来反应迟钝了.今天遇到一个问题,就是直接执行一个添加的sql语句,然后我用了TP框架的M()->query();方法.运行以后,会报Call t ...
- C# 对象的序列化与反序列化 (DataContractJsonSerializer)
项目引用 System.Runtime.Serialization.dll 添加命名空间 using System.Runtime.Serialization.Json; 1.序列化对象,得到Json ...
- ajax+jquery+ashx如何实现上传文件
第一:建立Default.aspx页面 <html> <head runat="server"> <title>ajax图片上传</tit ...
- 【笔记】jquery append,appendTo,prepend,prependTo 介绍
在jquery权威指南里面学习到这一章,很有必要介绍一下里面的内容: 首先是append(content)这个函数: 意思是将内容content加入到所选择的对象内容的后面 例如:$("di ...
- Web Servic和Web API的区别
Web Service:1.它是基于SOAP协议的,数据格式是XML2.只支持HTTP协议3.它不是开源的,但可以被任意一个了解XML的人使用4.它只能部署在IIS上Web API:1.这是一个简单的 ...
- mui记录
事件addEventListener()绑定事件的对象方法.addEventListener()含有三个参数,一个是事件名称,另一个是事件执行的函数,最后一个是事件捕获.obj.addEventLis ...
- Git入门教程
参考文献: 1. Pro Git 2. Git教程 3. Git教程 4. 图解Git