COJ 1007 WZJ的数据结构(七) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983
| WZJ的数据结构(七) |
| 难度级别:C; 运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B |
|
试题描述
|
|
给你一棵N个节点的无根树,每个点有一个权值(开始都是0)。请你设计一个数据结构,完成以下功能: 给你a、b、v,请将a到b路径中的节点权值都增加v(包括a点与b点)。最后输出每个节点的权值。 |
|
输入
|
|
第一行为一个正整数N。
接下来N-1行为每一条边,每行2个正整数a,b,表示有一条从a到b的边(从1开始编号)。 第N+1行为一个正整数Q,表示Q次操作。 接下来Q行为每一次询问,每行3个正整数a、b、v。 |
|
输出
|
|
最后输出每个点的权值,格式见样例。
|
|
输入示例
|
|
9
2 1 3 2 4 3 3 5 3 8 9 8 8 7 6 7 5 2 5 10 4 6 3 1 9 5 2 7 10 5 5 100 |
|
输出示例
|
|
5
25 28 3 110 3 13 18 5 |
|
其他说明
|
|
1<=N,Q<=100000
|
树链剖分版:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+,inf=-1u>>,maxn3=*maxn;
struct Tedge{int x,y,w,next;}adj[maxn*];int ms=,fch[maxn];
struct Edge{int from,to,dist;}e[maxn];
void AddEdge(int u,int v,int w){adj[++ms]=(Tedge){u,v,w,fch[u]};fch[u]=ms;return;}
int top[maxn],dep[maxn],son[maxn],siz[maxn],fa[maxn],sumv[maxn3],addv[maxn3],num[maxn],sz=,ql,qr,cv,_sum,n,Q;
void dfs(int u){
siz[u]=;dep[u]=dep[fa[u]]+;
for(int i=fch[u];i;i=adj[i].next){
int v=adj[i].y;
if(v!=fa[u]){
fa[v]=u;
dfs(v);
if(siz[son[u]]<siz[v]) son[u]=v;
siz[u]+=siz[v];
}
} return;
}
void build(int u,int tp){
num[u]=++sz;top[u]=tp;
if(son[u]) build(son[u],tp);
for(int i=fch[u];i;i=adj[i].next){
int v=adj[i].y;
if(v!=fa[u]&&v!=son[u]) build(v,v);
} return;
}
void update(int o,int L,int R){
if(ql<=L&&R<=qr) addv[o]+=cv;
else{
int M=L+R>>,lc=o<<,rc=lc|;
if(ql<=M) update(lc,L,M);
if(qr>M) update(rc,M+,R);
} return;
}
void change(int a,int b){
_sum=;
int f1=top[a],f2=top[b];
while(f1!=f2){
if(dep[f1]<dep[f2]) swap(f1,f2),swap(a,b);
ql=num[f1];qr=num[a];update(,,n);
a=fa[f1];f1=top[a];
}
if(dep[a]>dep[b]) swap(a,b);
ql=num[a];qr=num[b];update(,,n);return;//为毛不是孩子了
}
void clear_set(int o,int L,int R,int add){
if(L==R) sumv[L]+=add+addv[o];
else{
int M=L+R>>,lc=o<<,rc=lc|;
clear_set(lc,L,M,add+addv[o]);
clear_set(rc,M+,R,add+addv[o]);
} return;
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
void init(){
n=read();
for(int i=;i<n;i++){
int a=read(),b=read();
AddEdge(a,b,);AddEdge(b,a,);
e[i]=(Edge){a,b,};
}
dfs();build(,);
for(int i=;i<n;i++){
if(dep[e[i].from]>dep[e[i].to]) swap(e[i].from,e[i].to);
}
return;
}
void work(){
Q=read();
while(Q--){
int a=read(),b=read(),c=read();
cv=c;change(a,b);
}
return;
}
void print(){
clear_set(,,n,);
for(int i=;i<=n;i++){
write(sumv[num[i]]);ENT;
}
return;
}
int main(){init();work();print();return ;}
LCT
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+;
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
struct node {
node *ch[],*fa;
bool rev;
int x,sum,siz,mul,add;
inline void add_rev_tag(){
swap(ch[],ch[]);rev^=;return;
}
inline void add_plus_tag(int a){
sum+=siz*a;x+=a;add+=a;return;
}
inline void add_mul_tag(int m){
sum*=m;x*=m;mul*=m;add*=add*m;return;
}
inline void down(){
if(rev){
if(ch[]) ch[]->add_rev_tag();
if(ch[]) ch[]->add_rev_tag();
rev=;
}
if(add){
if(ch[]) ch[]->add_plus_tag(add);
if(ch[]) ch[]->add_plus_tag(add);
add=;
}
if(mul>){
if(ch[]) ch[]->add_mul_tag(mul);
if(ch[]) ch[]->add_mul_tag(mul);
mul=;
} return;
}
inline void update(){
sum=x;siz=;
if(ch[]) sum+=ch[]->sum,siz+=ch[]->siz;
if(ch[]) sum+=ch[]->sum,siz+=ch[]->siz;
return;
}
}lct[maxn];
inline int get_parent(node *x,node *&fa){return (fa=x->fa)?fa->ch[]==x?:fa->ch[]==x?:-:-;}
inline void rotate(node *x){
int t1,t2;
node *fa,*gfa;
t1=get_parent(x,fa);
t2=get_parent(fa,gfa);
if ((fa->ch[t1]=x->ch[t1^])) fa->ch[t1]->fa=fa;
x->ch[t1^]=fa;fa->fa=x;x->fa=gfa;
if (t2!=-) gfa->ch[t2]=x;
fa->update();return;
}
inline void pushdown(node *x){
static node *stack[maxn];
int cnt=;
while(){
stack[cnt++]=x;
node *fa=x->fa;
if (!fa || (fa->ch[]!=x && fa->ch[]!=x)) break;
x=fa;
}
while(cnt--) stack[cnt]->down();
return;
}
inline node * splay(node *x){
pushdown(x);
while(){
int t1,t2;
node *fa,*gfa;
t1=get_parent(x,fa);
if(t1==-) break;
t2=get_parent(fa,gfa);
if(t2==-){
rotate(x);break;
} else if (t1==t2){
rotate(fa);rotate(x);
} else{
rotate(x);rotate(x);
}
}
x->update();
return x;
}
inline node * access(node *x){
node *ret=NULL;
while (x) splay(x)->ch[]=ret,(ret=x)->update(),x=x->fa;
return ret;
}
inline void makeroot(int x){access(lct+x)->add_rev_tag();}
inline void link(int u,int v){
makeroot(u);splay(lct+u)->fa=lct+v;return;
}
inline void cut(int u,int v){
makeroot(u);
node *p=(access(lct+v),splay(lct+v));
p->ch[]->fa=NULL;
p->ch[]=NULL;
p->update();
}
int n,q;
int main(){
n=read();
int i;
for(i=;i<=n;i++) {
lct[i].x=lct[i].sum=;
lct[i].siz=;
lct[i].mul=;
lct[i].add=;
}
for(i=;i<n;i++){
int u,v;
u=read();v=read();
link(u,v);
}
q=read();int x,y,c;
while(q--){
x=read();y=read();c=read();
makeroot(x);access(y+lct)->add_plus_tag(c);
}
for(int i=;i<=n;i++) splay(i+lct),write(lct[i].x),ENT;
/*while(q--){
char ch=getchar();
while(ch<=32) ch=getchar();
int u,v,x,y,c;
if(ch=='+'){
u=read();v=read();c=read();
makeroot(u);
access(lct+v)->add_plus_tag(c);
}else if(ch=='-'){
u=read();v=read();x=read();y=read();
cut(u,v);link(x,y);
}else if(ch=='*'){
u=read();v=read();c=read();
makeroot(u);
access(lct+v)->add_mul_tag(c);
}else if(ch=='/'){
u=read();v=read();
makeroot(u);
printf("%u\n",access(lct+v)->sum);
}
}*/
return ;
}
搜索
复制
COJ 1007 WZJ的数据结构(七) 树上操作的更多相关文章
- COJ 1008 WZJ的数据结构(八) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...
- COJ 0995 WZJ的数据结构(负五)区间操作
WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...
- COJ 1010 WZJ的数据结构(十) 线段树区间操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1001 WZJ的数据结构(十) 难度级别:D: 运行时间限制:3000ms: ...
- COJ 0970 WZJ的数据结构(负三十)树分治
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- COJ 0967 WZJ的数据结构(负三十三)
WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...
- COJ 0990 WZJ的数据结构(负十)
WZJ的数据结构(负十) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个N个节点的有根树,从1到N编号,根节点为1并给 ...
- COJ 0979 WZJ的数据结构(负二十一)
WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...
- COJ 0981 WZJ的数据结构(负十九)树综合
WZJ的数据结构(负十九) 难度级别:E: 运行时间限制:3500ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 WZJ的数据结构中有很多都是关于树的.这让很多练习 ...
- COJ 1003 WZJ的数据结构(三)ST表
WZJ的数据结构(三) 难度级别:B: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为N的 ...
随机推荐
- FFmpeg的HEVC解码器源码简单分析:解析器(Parser)部分
===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...
- NSDateFormatter 格式说明
格式化参数如下: G: 公元时代,例如AD公元 yy: 年的后2位 yyyy: 完整年 MM: 月,显示为1-12 MMM: 月,显示为英文月份简写,如 Jan M ...
- Servlet与JSP的关系
Servlet与JSP的异同点: 相似点: 都可以生成动态网页 不同点: JSP擅长网页制作,生成页面直观,但不易追踪与排错 Servlet是纯Java,擅长处理流程与业务逻辑,缺点是页面不直观
- python-property、docstring--笔记
已经有人总结的非常详细,而且写得不错,就直接贴过来用了 property作为装饰器函数,负责把一个方法变成属性调用的 廖雪峰关于property的讲解 http://www.liaoxuefeng.c ...
- Python之路,Day25-----暂无正在更新中
Python之路,Day25-----暂无正在更新中
- markdown 简明语法
今天同事聊到markdown用法 之前不怎么了解 先把网上的建明语法贴出来 以备后用. 基本符号 *,-,+ 3个符号效果都一样,这3个符号被称为 Markdown符号 空白行表示另起一个段落 `是 ...
- Python 函数的使用 外加引入文件
#coding=utf-8 #!user/bin/python import sys import test2 def functionsss(name,sex,age=25): print name ...
- 关于timestamp的二三事
之所以要写timestamp的随笔,是因为之前对它的理解存在误区,so. I have to remind myself by writing this informal essay. 微软文档链接: ...
- (转)PHP数组的总结(很全面啊)
一.什么是数组数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体.数组的每个实体都包含两项:键和值. 二.声明数据在PHP中声明数组的方式主要有两种:一是应用array()函数声明数组 ...
- git针对Android Studio的使用
1.将文件放到项目根目录下 .gitignore 文件内容: *.iml.gradle/local.properties/.idea/workspace.xml/.idea/libraries.DS_ ...