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 ;
}

最新文章

  1. ASP中Lable控件的定位问题
  2. 解决Android SDK Content Loader 0%的问题
  3. Java备份约9亿条数据
  4. CGI实现页面的动态生成
  5. MEAN Stack:创建RESTful web service
  6. poj1023
  7. juicer模板引擎使用
  8. linux文件夹介绍
  9. 猪圈密码python脚本实现
  10. 实践作业2:黑盒测试实践——小组任务分工 Day 1
  11. springboot整合系列
  12. JavaScript的DOM编程--03--读写属性节点
  13. jquery 第一章
  14. Centos7的防火墙关闭
  15. java基础基础总结----- 常用了解java(二)
  16. Mongoose JS findOne always returns null
  17. 横竖两个数字塔的效果BAT批处理怎么写?
  18. 转载:MochiWeb一些资料的链接
  19. m2014-architecture-webserver-&gt;百万记录级mysql数据库及Discuz!论坛优化
  20. Docker 入门笔记

热门文章

  1. shell 变量说明
  2. mongodb 慢SQL查询
  3. C语言头文件
  4. UVALive - 3942 Remember the Word
  5. http请求连接
  6. dedecms 标签的基本用法
  7. php 判断是不是https链接
  8. CodeForces 500 A. New Year Transportation
  9. Viewpager以及ViewPagerIndicator的相关使用
  10. Android如何使用API