链剖即可。其实就是利用了链剖后子树都在一段连续的区间内所以可以做到O(logn)查询和修改。

线段树细节打错了。。要专心!肉眼差错都能找出一堆出来显然是不行的!。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define qwq(x) for(edge *o=head[x];o;o=o->next)
#define lson l,mid,x<<1
#define rson mid+1,r,x<<1|1
#define ll long long
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const int nmax=5e4+5;
const int inf=0x7f7f7f7f;
struct edge{
int to;edge *next;
};
edge es[nmax<<1],*pt=es,*head[nmax];
void add(int u,int v){
pt->to=v;pt->next=head[u];head[u]=pt++;
pt->to=u;pt->next=head[v];head[v]=pt++;
}
int size[nmax],son[nmax],idx[nmax],id[nmax],w[nmax],n,m;
ll sm[nmax<<2],col[nmax<<2];
void dfs(int x,int fa){
size[x]=1;
qwq(x) if(o->to!=fa){
dfs(o->to,x);size[x]+=size[o->to];
if(!son[x]||size[o->to]>size[son[x]]) son[x]=o->to;
}
}
void DFS(int x){
id[++id[0]]=x;idx[x]=id[0];
if(son[x]) DFS(son[x]);
qwq(x) if(!idx[o->to]) DFS(o->to);
}
void build(int l,int r,int x){
col[x]=-1;
if(l==r) {
sm[x]=w[id[l]];return ;
}
int mid=(l+r)>>1;build(lson);build(rson);sm[x]=sm[x<<1]+sm[x<<1|1];
}
void pushdown(int x,int cnt){
if(col[x]!=-1){
if(col[x<<1]!=-1) col[x<<1]+=col[x];else col[x<<1]=col[x];
if(col[x<<1|1]!=-1) col[x<<1|1]+=col[x];else col[x<<1|1]=col[x];
sm[x<<1]+=col[x]*(cnt-(cnt>>1));sm[x<<1|1]+=col[x]*(cnt>>1);
col[x]=-1;
}
}
void update(int p,int add,int l,int r,int x){
if(l==r) {
sm[x]+=add;return ;
}
pushdown(x,r-l+1);
int mid=(l+r)>>1;
p<=mid?update(p,add,lson):update(p,add,rson);
sm[x]=sm[x<<1]+sm[x<<1|1];
}
void Update(int tl,int tr,int add,int l,int r,int x){
if(tl<=l&&tr>=r) {
if(col[x]!=-1) col[x]+=add;else col[x]=add;
sm[x]+=(ll)add*(r-l+1);return ;
}
pushdown(x,r-l+1);
int mid=(l+r)>>1;
if(tl<=mid) Update(tl,tr,add,lson);
if(tr>mid) Update(tl,tr,add,rson);
sm[x]=sm[x<<1]+sm[x<<1|1];
}
ll query(int tl,int tr,int l,int r,int x){
if(tl<=l&&tr>=r) return sm[x];
pushdown(x,r-l+1);
int mid=(l+r)>>1;ll ans=0;
if(tl<=mid) ans+=query(tl,tr,lson);
if(tr>mid) ans+=query(tl,tr,rson);
return ans;
}
void UPDATE(int u,int v,int d){
ll sm=query(idx[u],idx[u]+size[u]-1,1,n,1);
if(sm>=(ll)v*size[u]) return ;
Update(idx[u],idx[u]+size[u]-1,d,1,n,1);
}
void print(int l,int r,int x){
printf("%d %d %d\n",l,r,sm[x]);
if(l==r) return ;
int mid=(l+r)>>1;
print(lson);print(rson);
}
ll get(int p,int l,int r,int x){
if(l==r) return sm[x];
pushdown(x,r-l+1);
int mid=(l+r)>>1;
return p<=mid?get(p,lson):get(p,rson);
}
char s[5];
int main(){
n=read(),m=read();int u,v,d;
rep(i,2,n) u=read()+1,w[i]=read(),add(u,i);
dfs(1,0);DFS(1);build(1,n,1);
//printf("->_->\n");print(1,n,1);
rep(i,1,m){
scanf("%s",s);u=read()+1,v=read(),d=read();
if(s[0]=='S') {
if(get(idx[u],1,n,1)<v) update(idx[u],d,1,n,1);
}
else UPDATE(u,v,d);
//printf("->_->\n");print(1,n,1);
}
rep(i,1,n) printf("%lld\n",get(idx[i],1,n,1));
return 0;
}

  

题目来源: Ural
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注
一棵有N个节点的树,每个节点对应1个编号及1个权值,有2种不同的操作。

操作1:S x y z,表示如果编号为x的节点的权值 < y,则将节点x的权值加上z。(Single)
操作2:A x y z,表示如果编号为x的节点以及其所有子节点的权值平均值 < y,则将节点x及其所有子节点的权值加上z。(All)
给出树节点之间的关系,进行M次操作,问所有操作完成后,各个节点的权值为多少?
节点的编号为0 - N - 1,根节点的编号为0,并且初始情况下,根节点的权值也是0。
 
Input
第1行:2个数N, M,N为节点的数量,M为操作的数量(1 <= N, M <= 50000)。
第2 - N行:每行描述一个节点N[i]的信息,第2行对应编号为1的节点,第N行对应编号为N - 1的节点。具体内容为:每行2个数P[i], W[i]。P[i]为当前节点的父节点的编号,W[i]为当前节点的权值。(0 <= W[i] <= 10^5, P[i] < i)
第N + 1 - N + M行:每行表示一个操作,N x y z或A x y z,(0 <= y, z <= 10^5)。
Output
输出共N行,每行1个数W[i],表示经过M次后,编号为0 - N - 1的节点的权值。
Input示例
4 3
0 10
0 10
1 2
S 0 1 1
A 0 20 1
S 3 2 1
Output示例
2
11
11
3

51nod1199 Money out of Thin Air的更多相关文章

  1. 51nod1199:Money out of Thin Air(线段树)

    按dfs序一个一个加入线段树,可以让任何一颗子树的节点在线段树中连续,于是就可以用线段树维护整棵树了 和树剖的思想是一样的,大概一眼就看出来了,但是写了两个半天(躺 总结:记住以后写完数据结构或者数字 ...

  2. 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 ...

  3. 51Nod 1199 Money out of Thin Air (树链剖分+线段树)

    1199 Money out of Thin Air  题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 一棵有N个节点的树,每 ...

  4. URAL 1890 . Money out of Thin Air (dfs序hash + 线段树)

    题目链接: URAL 1890 . Money out of Thin Air 题目描述: 给出一个公司里面上司和下级的附属关系,还有每一个人的工资,然后有两种询问: 1:employee x y z ...

  5. 1890. Money out of Thin Air(线段树 dfs转换区间)

    1890 将树的每个节点都转换为区间的形式 然后再利用线段树对结点更新 这题用了延迟标记 相对普通线段树 多了dfs的转换 把所要求的转换为某段区间 RE了N次 最后没办法了 记得有个加栈的语句 拿来 ...

  6. 51nod 1199 Money out of Thin Air(线段树+树剖分)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1199 题意: 思路:因为是一棵树,所以需要把它剖分一下再映射到线段树上, ...

  7. JMM(java内存模型)

    What is a memory model, anyway? In multiprocessorsystems, processors generally have one or more laye ...

  8. 比特币_Bitcoin 简介

    2008-11   Satoshi Nakamoto  Bitcoin: A Peer-to-Peer Electronic Cash System http://p2pbucks.com/?p=99 ...

  9. Java内存模型深度解析:顺序一致性--转

    原文地址:http://www.codeceo.com/article/java-memory-3.html 数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据 ...

随机推荐

  1. Mac下使用Apache TCPMon

    Mac下使用Apache TCPMon 参考链接: TCPMon Tutorial Anyone know how to get TCPMON working on a mac? Apache TCP ...

  2. Python中编码问题?

    一.键盘输入 raw_input('请输入:'.decode('utf-8').encode('gbk'))raw_input(unicode('请输入:','utf-8').encode('gbk' ...

  3. Lucene教程--转载

    Lucene教程 1 lucene简介1.1 什么是lucene    Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么拿来 ...

  4. Ehcache使用

    http://www.360doc.com/content/14/0423/17/16946725_371472946.shtml http://www.myexception.cn/web-appl ...

  5. POJ 3150 Cellular Automaton(矩阵乘法+二分)

    题目链接 题意 : 给出n个数形成环形,一次转化就是将每一个数前后的d个数字的和对m取余,然后作为这个数,问进行k次转化后,数组变成什么. 思路 :下述来自here 首先来看一下Sample里的第一组 ...

  6. Good Bye 2015 A. New Year and Days 签到

    A. New Year and Days   Today is Wednesday, the third day of the week. What's more interesting is tha ...

  7. 安装 M2eclipse 插件

    安装 M2eclipse 插件 在 Eclipse 中集成 Maven 插件能极大的方便创建 Maven project 并对其进行操作.使用以下的步骤来完成 M2eclipse 插件的安装: 在 E ...

  8. Linux操作系统下的Sudo命令

    查看.修改或者执行某些命令需要root用户的权限,如果不想直接切换到root用户,就可以使用sudo命令.sudo命令用于针对单个命令授予临时权限.sudo仅在需要时授予用户权限,减少了用户因为错误执 ...

  9. ant+jmeter+crontab实现自动化性能测试

    准备工作: 1.下载jmeter(我下载的apache-jmeter-2.13.zip) 2.配置jmeter环境变量,即path前添加jmeter的bin路径) 3.下载ant(我使用的apache ...

  10. 关于yum仓库的中的软件包下载

    在Linux系统下,很多软件的安装我们都会选择使用yum的方式安装,因为简单方便,易于管理. 有时我们可能会有这样的一个需求:即喜欢上yum仓库中的一个软件了,想要下载到本地.该怎么办呢? 实现方法不 ...