51nod1199 Money out of Thin Air
链剖即可。其实就是利用了链剖后子树都在一段连续的区间内所以可以做到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;
}
第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)。
输出共N行,每行1个数W[i],表示经过M次后,编号为0 - N - 1的节点的权值。
4 3
0 10
0 10
1 2
S 0 1 1
A 0 20 1
S 3 2 1
2
11
11
3
51nod1199 Money out of Thin Air的更多相关文章
- 51nod1199:Money out of Thin Air(线段树)
按dfs序一个一个加入线段树,可以让任何一颗子树的节点在线段树中连续,于是就可以用线段树维护整棵树了 和树剖的思想是一样的,大概一眼就看出来了,但是写了两个半天(躺 总结:记住以后写完数据结构或者数字 ...
- 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 1199 Money out of Thin Air (树链剖分+线段树)
1199 Money out of Thin Air 题目来源: Ural 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 一棵有N个节点的树,每 ...
- URAL 1890 . Money out of Thin Air (dfs序hash + 线段树)
题目链接: URAL 1890 . Money out of Thin Air 题目描述: 给出一个公司里面上司和下级的附属关系,还有每一个人的工资,然后有两种询问: 1:employee x y z ...
- 1890. Money out of Thin Air(线段树 dfs转换区间)
1890 将树的每个节点都转换为区间的形式 然后再利用线段树对结点更新 这题用了延迟标记 相对普通线段树 多了dfs的转换 把所要求的转换为某段区间 RE了N次 最后没办法了 记得有个加栈的语句 拿来 ...
- 51nod 1199 Money out of Thin Air(线段树+树剖分)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1199 题意: 思路:因为是一棵树,所以需要把它剖分一下再映射到线段树上, ...
- JMM(java内存模型)
What is a memory model, anyway? In multiprocessorsystems, processors generally have one or more laye ...
- 比特币_Bitcoin 简介
2008-11 Satoshi Nakamoto Bitcoin: A Peer-to-Peer Electronic Cash System http://p2pbucks.com/?p=99 ...
- Java内存模型深度解析:顺序一致性--转
原文地址:http://www.codeceo.com/article/java-memory-3.html 数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据 ...
随机推荐
- C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\Team Foundation\4.0\Cache\VersionControl.config is not valid and cannot be loaded.
Recently, we experienced a strange problem with TFS 2010. We spent a few days before we figured it o ...
- POJ 3255
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6605 Accepted: 2458 Descri ...
- POJ 1547
#include<iostream> #include<string> using namespace std; int main() { int length; int hi ...
- hdu 4764 Stone (巴什博弈,披着狼皮的羊,小样,以为换了身皮就不认识啦)
今天(2013/9/28)长春站,最后一场网络赛! 3~5分钟后有队伍率先发现伪装了的签到题(博弈) 思路: 与取石头的巴什博弈对比 题目要求第一个人取数字在[1,k]间的某数x,后手取x加[1,k] ...
- iOS开发-网易滚动导航栏
HACursor,是一个对横向ScrollView中的视图进行管理的UI控件.只要几行代码就可以集成类似于网易新闻对主题页面进行排序,删除操作的功能.主srollview参考iOS原生的UITable ...
- Ajax实例-购物车
一.概述 1.当添加或删除商品时,购物车会立即更新数据 2.思路: (1)建立商品类Item.java,存有商品属性name,prince,code(商品编码)等 (2)建立商品目录类Catalog. ...
- Qt之窗体透明 (三种不同的方法和效果)
关于窗体透明,经常遇到,网上的资料倒不少,也不知道写的时候是否验证过,很多都不正确...今天就在此一一阐述! 以下各效果是利用以前写过的一个小程序作为示例进行讲解!(代码过多,贴主要部分) ...
- DB2中字符、数字和日期类型之间的转换
DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别 ...
- Java中使用验证码和二维码
资源 需要: jelly-core-1.7.0.GA.jar 网站: http://lychie.github.io/products.html 将下载下来的 jelly-core-1.7.0 ...
- sublime3配置Quick-X+自动错误提示
sublime3配置 安装Package Control 配置Quick-x API提示 配置Lua自动语法错误提示 sublime3 安装 Package Control View->Show ...