51nod 1199 Money out of Thin Air(线段树+树剖分)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1199
题意:

思路:
因为是一棵树,所以需要把它剖分一下再映射到线段树上,剖分的话只需要dfs一遍树即可,得到的dfs序就是每个结点在线段树中的位置,子树上的节点的编号都是连续的。
接下来的操作就是线段树的查询和更新了,这部分并不难。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn=+; ll n, m, dfs_clock;
int w[maxn],son[maxn],dfn[maxn],num[maxn];
ll lazy[maxn<<]; vector<int> G[maxn]; struct node
{
int l, r;
ll sum;
}t[maxn<<]; void dfs(int u)
{
dfn[u]=++dfs_clock;
son[u]=;
for(int i=;i<G[u].size();i++)
{
int v=G[u][i];
dfs(v);
son[u]+=son[v];
}
} void PushUp(int o)
{
t[o].sum=t[o<<].sum+t[o<<|].sum;
} void PushDown(int o)
{
if(lazy[o])
{
lazy[o<<]+=lazy[o];
lazy[o<<|]+=lazy[o];
int m=t[o].r-t[o].l+;
t[o<<].sum+=lazy[o]*(m-(m>>));
t[o<<|].sum+=lazy[o]*(m>>);
lazy[o]=;
}
} void build(int l ,int r, int o)
{
lazy[o]=;
t[o].l=l;
t[o].r=r;
t[o].sum=;
if(l==r)
{
t[o].sum=num[l];
return;
}
int mid=(l+r)>>;
build(l,mid,o<<);
build(mid+,r,o<<|);
PushUp(o);
} void update(int ql, int qr, int l, int r, ll z, int o)
{
if(ql<=l && qr>=r)
{
t[o].sum+=(r-l+)*z;
lazy[o]+=z;
return;
}
PushDown(o);
int mid=(l+r)>>;
if(ql<=mid) update(ql,qr,l,mid,z,o<<);
if(qr>mid) update(ql,qr,mid+,r,z,o<<|);
PushUp(o);
} ll query(int ql, int qr, int l, int r, int o)
{
if(ql<=l && qr>=r) return t[o].sum;
PushDown(o);
int mid=(l+r)>>;
ll ans=;
if(ql<=mid) ans+=query(ql,qr,l,mid,o<<);
if(qr>mid) ans+=query(ql,qr,mid+,r,o<<|);
return ans;
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<n;i++) G[i].clear();
for(int i=;i<n;i++)
{
int p; scanf("%d%d",&p,&w[i]);
G[p].push_back(i);
}
w[]=;
dfs_clock=;
dfs();
for(int i=;i<n;i++) num[dfn[i]]=w[i];
build(,n,);
char op[]; ll x, y, z;
memset(lazy,,sizeof(lazy));
while(m--)
{
scanf("%s%lld%lld%lld",op,&x,&y,&z);
if(op[]=='S')
{
if(query(dfn[x],dfn[x],,n,)<y) update(dfn[x],dfn[x],,n,z,);
}
else
{
if(query(dfn[x],dfn[x]+son[x]-,,n,)<son[x]*y) update(dfn[x],dfn[x]+son[x]-,,n,z,);
}
}
for(int i=;i<n;i++) printf("%lld\n",query(dfn[i],dfn[i],,n,));
return ;
}
51nod 1199 Money out of Thin Air(线段树+树剖分)的更多相关文章
- 51Nod 1199 Money out of Thin Air (树链剖分+线段树)
1199 Money out of Thin Air 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 一棵有N个节点的树,每 ...
- 51nod1199 Money out of Thin Air
链剖即可.其实就是利用了链剖后子树都在一段连续的区间内所以可以做到O(logn)查询和修改. 线段树细节打错了..要专心!肉眼差错都能找出一堆出来显然是不行的!. #include<cstdio ...
- URAL 1890 . Money out of Thin Air (dfs序hash + 线段树)
题目链接: URAL 1890 . Money out of Thin Air 题目描述: 给出一个公司里面上司和下级的附属关系,还有每一个人的工资,然后有两种询问: 1:employee x y z ...
- ural1890 Money out of Thin Air
Money out of Thin Air Time limit: 1.0 secondMemory limit: 64 MB Each employee of the company Oceanic ...
- 51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径
51nod 1766 树上的最远点对 | LCA ST表 线段树 树的直径 题面 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个区间内各选一点之间的最大距离,即 ...
- 51nod 1206 Picture 矩形周长求并 | 线段树 扫描线
51nod 1206 Picture 矩形周长求并 | 线段树 扫描线 #include <cstdio> #include <cmath> #include <cstr ...
- 51nod 1462 树据结构 | 树链剖分 矩阵乘法
题目链接 51nod 1462 题目描述 给一颗以1为根的树. 每个点有两个权值:vi, ti,一开始全部是零. Q次操作: 读入o, u, d o = 1 对u到根上所有点的vi += d o = ...
- 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)
题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...
- 树链剖分+线段树 CF 593D Happy Tree Party(快乐树聚会)
题目链接 题意: 有n个点的一棵树,两种操作: 1. a到b的路径上,给一个y,对于路径上每一条边,进行操作,问最后的y: 2. 修改某个条边p的值为c 思路: 链上操作的问题,想树链剖分和LCT,对 ...
随机推荐
- iOS开发-url包括中文报错解决的方法
常常, 我们用通过这个方案调用API. NSString* urlString = [NSString stringWithFormat:@"http://api.douban.com/v2 ...
- CentOS工作内容(二)关闭SELinux
CentOS工作内容(二)关闭SELinux CentOS安装完成后,有很多配置要改,不过最重要就是关闭SELinux SELinux是增强安全性的一项功能,不是SELinux不好,而是当功能安全性较 ...
- qsv转换为mp4
1:下载 装换工具:http://www.downza.cn/soft/27484.html 2:双击打开exe可执行程序. 3:添加要转换的文件,和转换后要存储的位置 4:开始转换,转换为flv格 ...
- [LeetCode] 867. Transpose Matrix_Easy
Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it ...
- jmeter 测试websocket接口(一)
jmeter 测试websocket接口时,需要对jmeter添加测试websocket的jar包. 下载地址: https://download.csdn.net/download/qq_14913 ...
- django的分页器
Django中分页器的使用 django分页器模块 #分页器 from django.core.paginator import Paginator,EmptyPage,PageNotAnIntege ...
- 用python实现一个简单的socket网络聊天通讯 (Linux --py2.7平台与windows--py3.6平台)
windows --> windows 写法均在py3.6 客户端写法 import socket client = socket.socket() client.connect(('192 ...
- Nature重磅:Hinton、LeCun、Bengio三巨头权威科普深度学习
http://wallstreetcn.com/node/248376 借助深度学习,多处理层组成的计算模型可通过多层抽象来学习数据表征( representations).这些方法显著推动了语音识别 ...
- CQRS/ES框架调研
1.Enode一个C#写的CQRS/ES框架,由汤雪华设计及实现,github上有相关源码,其个人博客上有详细的孵化.设计思路.版本迭代及最新的完善: 2.axon framwork,java编写,网 ...
- Linux中Postfix邮件原理介绍(一)
邮件相关协议 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议, 工作在TCP的25端口.它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式 ...