poj 2763 Housewife Wind// lca+ST+vector模拟+树状数组 (模板)
Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15926 Accepted: 4347 Description
After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.
Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!'
At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.
Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?Input
The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.
The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.
The following q lines each is one of the following two types:
Message A: 0 u
A kid in hut u calls Wind. She should go to hut u from her current position.
Message B: 1 i w
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.Output
For each message A, print an integer X, the time required to take the next child.
Sample Input
3 3 1 1 2 1 2 3 2 0 2 1 2 3 0 3
Sample Output
1 3
又被vector卡了!!!!!!! 那我搞一套vector模板还不行吗????????
题意:给出一棵树,给出q个询问,q0求树上任意两点距离,q1修改树上任意两点距离。
基于RMQ求lca,u,v两点的距离就是sum(u到lca(u,v),v到lca(u,v))。
而这个距离,可以通过访问dfs出来的序列得到,并用树状数组维护。修改时候修改update树状数组即可。
lca通过访问dfs出来的序列,用ST表维护最小值的下标,访问时也访问最小值的下标,即可。
注意记录每条路的对应的dfs序列的位置,注意细节!
#include<iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
#define LL long long
const int max_n=1e5+5,logn=20;
struct no{int from,to,cost,next,ord;};//ord是第几条出边
no e[max_n*2];
int head[max_n],edgenum;
int n,q,s;
int w[max_n];
int root;
int vs[max_n*2];//dfs 访问顺序(加上回溯的)
int de[max_n*2];//节点深度
int id[max_n];//顶点在vs中首次出现的下标'
int dp[max_n*2][logn]; //ST
int bit[max_n*2];
int dfsord[max_n*2];//访问顺序对应的点的下标 i*2-1是向下,i*2是向上
void initv(){
edgenum=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int o){
no E={u,v,w,head[u],o};
e[edgenum]=E;
head[u]=edgenum++;
}
int query(int i){
int ans=0;
while(i>0){
ans=ans+bit[i];
i=i-(i&-i);
}
return ans;
}
void update(int i,int x,int nn){ //注意nn
while(i<=nn){
bit[i]=bit[i]+x;
i=i+(i&-i);
}
}
void ST(int N) {
for(int i=1;i<=N;i++)
dp[i][0]=i;
for (int j=1;(1<<j)<=N;j++) {
for (int i =1;i+(1<<j)-1<=N;i++) {
int aa = dp[i][j-1];
int bb = dp[i + (1<<(j-1))][j-1];
if(de[aa] <= de[bb])
dp[i][j] = aa;
else
dp[i][j] = bb;
}
}
}
int RMQ(int L, int R) {
int k = 0;
while((1<<(k+1)) <= R-L+1) k++;
int aa = dp[L][k];
int bb = dp[R - (1<<k) + 1][k];
if(de[aa] <= de[bb])
return aa;
else
return bb;
}
void dfs(int v,int p,int d,int &k){
id[v]=++k;
vs[k]=v;
de[k]=d;
for(int i=head[v];i!=-1;i=e[i].next){
if(e[i].to!=p){ //向下
dfsord[e[i].ord*2-1]=k;//记录访问顺序下的点的下标
update(k,e[i].cost,n*2-2);//更新树状数组 (左指向右)
dfs(e[i].to,v,d+1,k);
dfsord[e[i].ord*2]=k;
update(k,-e[i].cost,n*2-2);//更新树状数组,(右指向左)
vs[++k]=v;
de[k]=d;
}
}
}
void init(int V){
int k=0;
dfs(root,-1,0,k);//求出vs,de,id
ST(V*2-1);//求出ST表
//printf("de:%d\n",k);
}
int lca(int u,int v){
return vs[RMQ(min(id[u],id[v]),max(id[u],id[v]))];
}
int main(){
scanf("%d%d%d",&n,&q,&s);root=s;
initv();
for(int i=1;i<n;i++){
int u,v;scanf("%d%d%d",&u,&v,&w[i]);
addedge(u,v,w[i],i);
addedge(v,u,w[i],i);
/*g[u].push_back(no{v,w[i],i});
g[v].push_back(no{u,w[i],i});*/
}
init(n);
while(q--){
int op;scanf("%d",&op);
if(op==0){
int x;scanf("%d",&x);
int lc=id[lca(x,s)]-1; //注意偏移量
int qx=query(id[x]-1),qs=query(id[s]-1),qlc=query(lc);
printf("%d\n",qx+qs-2*qlc);
s=x;
}
if(op==1){
int i,ww;scanf("%d%d",&i,&ww);
ww=ww-w[i]; //注意是增加量
int x1=dfsord[i*2-1],x2=dfsord[i*2]; //与上述同一
update(x1,ww,n*2-2);
update(x2,-ww,n*2-2);
w[i]=ww+w[i]; //注意更改
}
}
return 0;
}
ST模板:
void ST(int N) { //从1-n
for(int i=1;i<=N;i++)
dp[i][0]=i;
for (int j=1;(1<<j)<=N;j++) {
for (int i =1;i+(1<<j)-1<=N;i++) {
int aa = dp[i][j-1];
int bb = dp[i + (1<<(j-1))][j-1];
if(de[aa] <= de[bb])
dp[i][j] = aa;
else
dp[i][j] = bb;
}
}
}
int RMQ(int L, int R) {
int k = 0;
while((1<<(k+1)) <= R-L+1) k++;
int aa = dp[L][k];
int bb = dp[R - (1<<k) + 1][k];
if(de[aa] <= de[bb])
return aa;
else
return bb;
}
手动vecotr
struct no{int from,to,cost,next,ord;};//ord是第几条出边
no e[max_n];
int head[max_n],edgenum;
void initv(){
edgenum=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int o){
no E={u,v,w,head[u],o};
e[edgenum]=E;
head[u]=edgenum++;
}
for(int i=head[v];i!=-1;i=e[i].next){
no now=e[i];
}
addedge(u,v,w[i],i);
addedge(v,u,w[i],i);