poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)²)修改与查询

时间:2021-04-05 16:53:25
 /**
problem: http://poj.org/problem?id=2763
**/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
using namespace std; const int MAXN = ; template <typename T>
class SegmentTree {
private:
struct Node {
int left, right;
T sum, lazy;
} node[MAXN << ];
T data[MAXN];
void pushUp(int root) {
node[root].sum = node[root << ].sum + node[root << | ].sum;
}
void pushDown(int root) {
if(node[root].left == node[root].right) return;
int lson = root << ;
int rson = root << | ;
node[lson].sum = node[root].lazy * (node[lson].right - node[lson].left + );
node[rson].sum = node[root].lazy * (node[rson].right - node[rson].left + );
node[lson].lazy = node[root].lazy;
node[rson].lazy = node[root].lazy;
node[root].lazy = ;
}
public:
void build(int left, int right, int root = ) {
node[root].left = left;
node[root].right = right;
node[root].lazy = ;
if(left == right) {
node[root].sum = data[left];
} else {
int mid = (left + right) >> ;
build(left, mid, root << );
build(mid + , right, root << | );
pushUp(root);
}
}
void update(int left, int right, T value, int root = ) {
int lson = root << ;
int rson = root << | ;
if(node[root].lazy) pushDown(root);
if(node[root].left == left && node[root].right == right) {
node[root].sum = value * (right - left + );
node[root].lazy = value;
return ;
}
if(left >= node[rson].left) {
update(left, right, value, rson);
} else if(right <= node[lson].right) {
update(left, right, value, lson);
} else {
update(left, node[lson].right, value, lson);
update(node[rson].left, right, value, rson);
}
pushUp(root);
}
T query(int left, int right, int root = ) {
int lson = root << ;
int rson = root << | ;
if(node[root].lazy) pushDown(root);
if(node[root].left == left && node[root].right == right) {
return node[root].sum;
}
if(left >= node[rson].left) {
return query(left, right, rson);
} else if(right <= node[lson].right) {
return query(left, right, lson);
} else {
return query(left, node[lson].right, lson) + query(node[rson].left, right, rson);
}
}
void clear(int n, const vector<int> &d) {
for(int i = ; i <= n; i ++) {
this->data[i] = d[i];
}
build(, n);
}
}; template <typename T>
class TreeToLink {
private:
struct Point {
int size, son, depth, father, top, newId;
T data;
} point[MAXN];
struct Edge {
int to, next;
} edge[MAXN << ];
int oldId[MAXN], first[MAXN], sign, sumOfPoint, cnt;
SegmentTree<T> st;
void dfs1(int u, int father = , int depth = ) {
point[u].depth = depth;
point[u].father = father;
point[u].size = ;
int maxson = -;
for(int i = first[u]; i != -; i = edge[i].next) {
int to = edge[i].to;
if(to == father) continue;
dfs1(to, u, depth + );
point[u].size += point[to].size;
if(point[to].size > maxson) {
point[u].son = to;
maxson = point[to].size;
}
}
}
void dfs2(int u, int top) {
point[u].newId = ++cnt;
oldId[cnt] = u;
point[u].top = top;
if(point[u].son == -) {
return ;
}
dfs2(point[u].son, top);
for(int i = first[u]; i != -; i = edge[i].next) {
int to = edge[i].to;
if(to == point[u].son || to == point[u].father) continue;
dfs2(to, to);
}
}
public:
void clear(int n) {
sumOfPoint = n;
sign = ;
cnt = ;
for(int i = ; i <= n; i ++) {
first[i] = -;
point[i].son = -;
// scanf("%d", &point[i].data); // input
point[i].data = ;
}
}
void addEdgeOneWay(int u, int v) {
edge[sign].to = v;
edge[sign].next = first[u];
first[u] = sign ++;
}
void addEdgeTwoWay(int u, int v) {
addEdgeOneWay(u, v);
addEdgeOneWay(v, u);
}
void preWork(int x = ) {
dfs1(x);
dfs2(x, x);
vector<int> data(sumOfPoint + );
for(int i = ; i <= sumOfPoint; i ++) {
data[i] = point[oldId[i]].data;
}
st.clear(sumOfPoint, data);
}
void updatePath(int x, int y, T z){
while(point[x].top != point[y].top){
if(point[point[x].top].depth < point[point[y].top].depth)
swap(x, y);
st.update(point[point[x].top].newId, point[x].newId, z);
x = point[point[x].top].father;
}
if(point[x].depth > point[y].depth)
swap(x, y);
st.update(point[x].newId, point[y].newId, z);
}
T queryPath(int x, int y){
T ans = ;
while(point[x].top != point[y].top){
if(point[point[x].top].depth < point[point[y].top].depth)
swap(x, y);
ans += st.query(point[point[x].top].newId, point[x].newId);
x = point[point[x].top].father;
}
if(x == y) return ans; // Edge
if(point[x].depth > point[y].depth)
swap(x, y);
// ans += st.query(point[x].newId, point[y].newId); // Point
ans += st.query(point[point[x].son].newId, point[y].newId); // Edge
return ans;
}
void updateSon(int x, T z){
st.update(point[x].newId, point[x].newId + point[x].size - , z);
}
T querySon(int x){
return st.query(point[x].newId, point[x].newId + point[x].size - );
}
T queryPoint(int x) {
return queryPath(x, x);
}
void updatePoint(int x, T z) {
updatePath(x, x, z);
}
bool deeper(int u, int v){
return point[u].depth > point[v].depth;
}
}; class Solution {
private:
int n, q, s;
TreeToLink<int> ttl;
struct Edge{
int u, v, w;
}edge[MAXN];
public:
void solve() {
scanf("%d%d%d", &n, &q, &s);
ttl.clear(n);
for(int i = ; i < n; i ++){
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
ttl.addEdgeTwoWay(edge[i].u, edge[i].v);
}
ttl.preWork(s);
for(int i = ; i < n; i ++){
if(ttl.deeper(edge[i].u, edge[i].v)){
swap(edge[i].u, edge[i].v);
}
ttl.updatePoint(edge[i].v, edge[i].w);
}
int now = s;
for(int i = , a, b, c; i < q; i ++){
scanf("%d%d", &a, &b);
if(a == ){
scanf("%d", &c);
ttl.updatePoint(edge[b].v, c);
}else{
printf("%d\n", ttl.queryPath(now, b));
now = b;
}
}
}
} DarkScoCu; int main() {
DarkScoCu.solve();
return ;
}