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,对 ...
随机推荐
- Spark2.x学习笔记:Spark SQL的SQL
Spark SQL所支持的SQL语法 select [distinct] [column names]|[wildcard] from tableName [join clause tableName ...
- lua的文件管理
lua没有自己的文件管理 只有读取和写入文件,但是可以通过调用lfs(LuaFileSystem),lfs是一个 用于lua进行文件访问的库,支持lua5.1和lua5.2,并且跨平台 lfs的使用: ...
- Andrew Ng-ML-第十五章-降维
1.数据压缩 数据压缩不仅能够减小存储空间,并且能够加速学习算法.那么什么是数据压缩呢?下面给出了一个简单的例子: 图1.数据压缩的概念 举了两个例子,一个是横轴x1是厘米,纵轴特征x2是英尺,这明显 ...
- selenium webdriver处理浏览器Cookie
有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie 的测试是无法通过白盒和集成测试完成的.WebDriver 提供了操作Cookie 的相关方法可以读取.添加和删除cook ...
- Canvas标签基础
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- sql server中批量插入与更新两种解决方案分享(存储过程)
转自http://www.shangxueba.com/jingyan/1940447.html 1.游标方式 SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONG ...
- IO—代码—基础及其用例
字节流:文件.图片.歌曲 使用字节流的应用场景:如果是读写的数据都不需要转换成字符的时候,则使用字节流. 字节流处理单元为1个字节, 操作字节和字节数组.不能直接处理Unicode字符 字节流可用于任 ...
- vue数据双向绑定原理
vue的数据双向绑定的小例子: .html <!DOCTYPE html> <html> <head> <meta charset=utf-> < ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON Roberts1
zw版[转发·台湾nvp系列Delphi例程]HALCON Roberts1 procedure TForm1.Button1Click(Sender: TObject);var img, img1: ...
- Linux其他:环境变量配置
计算机==>右键==>属性==>高级系统设置==>环境变量==> 系统变量path后面+';python路径名