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,对 ...
随机推荐
- 006-springboot2.0.4 配置log4j2,以及打印mybatis的sql
一.pom配置 普通项目 <!-- log4j2 --> <dependency> <groupId>org.apache.logging.log4j</gr ...
- 十天精通CSS3(8)
变形--旋转 rotate() 旋转rotate()函数通过指定的角度参数使元素相对原点进行旋转.它主要在二维空间内进行操作,设置一个角度值,用来指定旋转的幅度.如果这个值为正值,元素相对原点中心顺时 ...
- Selenium定位元素-Xpath的使用方法
工具 Xpath的练习建议下载火狐浏览器,下载插件Firebug.Firepath. 由于最新版火狐不支持Firebug等扩展工具了,所以需要下载49版以下的版本安装https://ftp.mozil ...
- Scala系统学习(四):Scala数据类型
Scala与Java具有相同的数据类型,具有相同的内存占用和精度.以下是提供Scala中可用的所有数据类型的详细信息的表格: 序号 数据类型 说明 1 Byte 8位有符号值,范围从-128至127 ...
- Selenium+Java元素定位之三
首先自己先准备一个表格代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- 提示'HTTP消息不可读'
1.提示下面的错误信息 2.修改后的代码,费用接口 import unittest import requests import json import HTMLTestRunner ur1 = 'h ...
- 深度学习Momentum(动量方法)
转自:http://blog.csdn.net/bvl10101111/article/details/72615621 先上结论: 1.动量方法主要是为了解决Hessian矩阵病态条件问题(直观上讲 ...
- Linear Regression Using Gradient Descent 代码实现
参考吴恩达<机器学习>, 进行 Octave, Python(Numpy), C++(Eigen) 的原理实现, 同时用 scikit-learn, TensorFlow, dlib 进行 ...
- LibSVM源码剖析(java版)
之前学习了SVM的原理(见http://www.cnblogs.com/bentuwuying/p/6444249.html),以及SMO算法的理论基础(见http://www.cnblogs.com ...
- Linux命令: 在线练习网址
1.https://www.tutorialspoint.com/unix_terminal_online.php 2.从 这里 https://www.tutorialspoint.com/inde ...