Money out of Thin Air

Time limit: 1.0 second
Memory limit: 64 MB
Each employee of the company Oceanic Airlines, except for the director, has exactly one immediate superior. To encourage the best employees and best departments, the director can issue two kinds of orders:
  1. “employee x y z” — if the salary of employee x is less than y dollars, increase it by zdollars;
  2. “department x y z” — if the average salary in the department headed by employee x is less than y dollars, increase the salary of each employee at this department by z dollars (the department includes employee x and all her subordinates, not necessarily immediate).
Given the salaries of all the employees of Oceanic Airlines at the beginning of a year and all the salary increase orders issued by the director during the year, find the salaries of the employees by the end of the year. You may assume that the company didn't hire any new employees and didn't fire anyone during the year.

Input

The first line contains integers nq, and s0, which are the number of employees at Oceanic Airlines, the number of salary increase orders, and the director's salary at the beginning of the year (1 ≤ nq ≤ 50 000; 0 ≤ s0 ≤ 109). The employees are numbered from 0 to n − 1; the director's number is zero. In the ith of the following n − 1 lines you are given integers piand si, which are the number of the immediate superior and the salary at the beginning of the year of the employee with number i (0 ≤ pi ≤ i − 1; 0 ≤ si ≤ 109). The following q lines are the director's orders given chronologically. Each order has the form “employee x y z” or “department x y z” (the notation xyz is explained above), where 0 ≤ x ≤ n − 1 and 1 ≤ yz ≤ 109.

Output

Output the salaries of all employees at Oceanic Airlines at the end of the year in the ascending order of the employees' numbers.

Sample

input output
4 3 1
0 10
0 10
1 10
employee 2 15 1
employee 3 5 1
department 0 10 1
2
11
12
11

分析:关键是对员工的原标号进行先序遍历后重新标号,这样每个员工所领导的部门就是一个连续的区间;

   然后线段树进行区间修改,注意输出答案再把新标号代回原标号;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <hash_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
const int dis[][]={,,-,,,-,,};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,h[maxn],c[maxn],id[maxn],idx[maxn],tot,now,l[maxn],r[maxn];
struct Node
{
ll sum, lazy;
} T[maxn<<]; void PushUp(int rt)
{
T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
} void PushDown(int L, int R, int rt)
{
int mid = (L + R) >> ;
ll t = T[rt].lazy;
T[rt<<].sum += t * (mid - L + );
T[rt<<|].sum += t * (R - mid);
T[rt<<].lazy += t;
T[rt<<|].lazy += t;
T[rt].lazy = ;
} void Build(int L, int R, int rt)
{
if(L == R)
{
T[rt].sum=c[idx[now++]];
return ;
}
int mid = (L + R) >> ;
Build(Lson);
Build(Rson);
PushUp(rt);
} void Update(int l, int r, ll v, int L, int R, int rt)
{
if(l==L && r==R)
{
T[rt].lazy += v;
T[rt].sum += v * (R - L + );
return ;
}
int mid = (L + R) >> ;
if(T[rt].lazy) PushDown(L, R, rt);
if(r <= mid) Update(l, r, v, Lson);
else if(l > mid) Update(l, r, v, Rson);
else
{
Update(l, mid, v, Lson);
Update(mid+, r, v, Rson);
}
PushUp(rt);
} ll Query(int l, int r, int L, int R, int rt)
{
if(l==L && r== R)
{
return T[rt].sum;
}
int mid = (L + R) >> ;
if(T[rt].lazy) PushDown(L, R, rt);
if(r <= mid) return Query(l, r, Lson);
else if(l > mid) return Query(l, r, Rson);
return Query(l, mid, Lson) + Query(mid + , r, Rson);
}
struct node1
{
int to,nxt;
}p[maxn];
struct node2
{
char p[];
int x,y,z;
}q[maxn];
void add(int x,int y)
{
tot++;
p[tot].to=y;
p[tot].nxt=h[x];
h[x]=tot;
}
void dfs(int u)
{
id[u]=++now;
idx[now]=u;
l[u]=now;
for(int i=h[u];i;i=p[i].nxt)
{
dfs(p[i].to);
}
r[u]=now;
return;
}
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&c[]);
rep(i,,n)
{
int a,b;
scanf("%d%d",&a,&b);
a++;
add(a,i);
c[i]=b;
}
rep(i,,m)scanf("%s%d%d%d",q[i].p,&q[i].x,&q[i].y,&q[i].z),q[i].x++;
dfs();
now=;
Build(,n,);
rep(i,,m)
{
if(q[i].p[]=='e')
{
if(Query(id[q[i].x],id[q[i].x],,n,)<q[i].y)
Update(id[q[i].x],id[q[i].x],q[i].z,,n,);
}
else
{
if((double)Query(l[q[i].x],r[q[i].x],,n,)/(r[q[i].x]-l[q[i].x]+)<q[i].y)
Update(l[q[i].x],r[q[i].x],q[i].z,,n,);
}
}
rep(i,,n)printf("%lld\n",Query(id[i],id[i],,n,));
//system("Pause");
return ;
}

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

  1. 51nod1199 Money out of Thin Air

    链剖即可.其实就是利用了链剖后子树都在一段连续的区间内所以可以做到O(logn)查询和修改. 线段树细节打错了..要专心!肉眼差错都能找出一堆出来显然是不行的!. #include<cstdio ...

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

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

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

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

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

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

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

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

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

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

  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. 常见B2C网站购物车的设计

    对于大部分B2C网站来说,购物车是网站的咽喉之地,订单是白花花的银子,所有银子都必然流经购物车,购物车不能有失.优秀的购物车设计至少需要完成两项使命:一是方便用户多买货多掏银子:二是帮用户保管好待购的 ...

  2. maven 国内镜像地址

    由于连接国外网站时网速特慢,为解决这个问题,os china 建立了一个maven 的私服.为了记忆,特将此记录. settings.xml 设置镜像方法步骤如下: 1. mirrors 设置 < ...

  3. chrome浏览器调试功能之后端篇

    作为后端开发人员,可能有很多同学不怎么了解chrome调试功能,而即将成为大神的我们,怎么也得会,知其然更要知其所以然,今天我带领大家好好的梳理一下,chrome浏览器调试,个人把它分成了前端功能和后 ...

  4. 关于Select * 与Select 字段名 的问题!

    [转]http://blog.csdn.net/tongyu2009/article/details/8252418 1.SELECT * 语句取出表中的所有字段,不论该字段的数据对调用的应用程序是否 ...

  5. PCI源码学习笔记

    这个是PCI的关键结构体. static const struct file_operations proc_bus_pci_operations = { .owner = THIS_MODULE, ...

  6. Java学习笔记之static

    1.static可以用于修饰成员变量.方法以及块,并不会改变类中成员的权限修饰,如:private修饰的成员变量,类外只能类名或非私有方法调用,而不能使用对象名调用. 2.static方法和成员变量, ...

  7. 【转】哦,mysql 的其它发行版本Percona, mariadb

    原文:http://geek.csdn.net/news/detail/130146 2016年11月25日,沃趣科技"智慧应用 数据先行"2016产品发布会暨新三板挂牌庆祝会在杭 ...

  8. cocos2d-lua SDK接入

    1.lua 调用Java函数 1.1 在java中创建一个静态函数(比如在org.cocos2dx.lua.AppActivity.java中)名为Login public static void m ...

  9. 编译hadoop2.4

    摘自 http://www.aboutyun.com/thread-8130-1-1.html.http://www.dataguru.cn/forum.php?mod=viewthread& ...

  10. Redis(2)用jedis实现在java中使用redis

    昨天已经在windows环境下安装使用了redis. 下面准备在java项目中测试使用redis. redis官网推荐使用jedis来访问redis.所以首先准备了jedis的jar包,以及需要依赖的 ...