题目链接:

  URAL 1890 . Money out of Thin Air

题目描述:

  给出一个公司里面上司和下级的附属关系,还有每一个人的工资,然后有两种询问:

    1:employee x y z ,如果编号为x的员工如果工资小于y,就给他加薪z。

    2:department x y z ,如果编号为x的员工所管辖的范围内(包括自己),所有员工的工资平均数小于y,给该范围加薪z。

  问q次操作后这个公司内每个员工的工资为多少?

解题思路:

  根据上司和下级的附属关系,可以先建一个有向图,然后对有向图进行dfs,求出每个点的dfs序列,根据序列建线段树,对于每个操作,先判断操作区间内是否需要加薪,如果需要就进行加薪操作。HINT!!!!:判定和加薪操作一定要分开,要不然会出错,比如说在一段整体不需要加薪的区间内,在线段树上,有可能前半段需要加薪,但是后半部分员工的薪水比较高。

  写代码+debug 花费了两个小时,为什么不够熟练,GG!

 #include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef __int64 LL;
#define lson root*2
#define rson root*2+1
const int maxn = ;
const LL INF = 1e9+; struct node
{
int to, next;
} edge[maxn*];
struct Node
{
int l, r;
LL sum, val;
int len()
{
return (r - l + );
}
int mid ()
{
return (l + r) / ;
}
} tree[maxn*];
int head[maxn], stime[maxn], etime[maxn];
int tot, Max, df[maxn];
LL ans[maxn], w[maxn]; void add (int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot ++;
}
void dfs (int u)
{
stime[u] = ++ Max;
df[Max] = u; for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
dfs (v);
etime[v] = Max;
}
}
void build (int root, int l, int r)
{
tree[root].l = l;
tree[root].r = r;
tree[root].val = ; if (l == r)
{
tree[root].sum = w[df[l]];
return ;
} build (lson, l, tree[root].mid());
build (rson, tree[root].mid()+, r);
tree[root].sum = tree[lson].sum + tree[rson].sum;
} void pushdown (int root)
{
if (tree[root].val == || tree[root].len() == )
return ;
tree[lson].sum += tree[lson].len() * tree[root].val;
tree[rson].sum += tree[rson].len() * tree[root].val;
tree[lson].val += tree[root].val;
tree[rson].val += tree[root].val;
tree[root].val = ;
} LL query (int root, int l, int r)
{
if (tree[root].l == l && tree[root].r == r)
return tree[root].sum; pushdown (root); if (tree[root].mid() >= r)
return query (lson, l, r);
else if (tree[root].mid() < l)
return query (rson, l, r);
else
{
LL num = ;
num += query (lson, l, tree[root].mid());
num += query (rson, tree[root].mid()+, r);
return num;
}
}
void updata (int root, int l, int r, LL x)
{
if (tree[root].l == l && tree[root].r == r)
{
tree[root].sum += tree[root].len() * x;
tree[root].val += x;
return ;
} pushdown (root); if (tree[root].mid() >= r)
updata (lson, l, r, x);
else if (tree[root].mid() < l)
updata (rson, l, r, x);
else
{
updata (lson, l, tree[root].mid(), x);
updata (rson, tree[root].mid()+, r, x);
} tree[root].sum = tree[lson].sum + tree[rson].sum;
}
void display (int root)
{
if (tree[root].l == tree[root].r)
{
int num = tree[root].l;
ans[df[num]] = tree[root].sum;
return ;
} pushdown (root);
display (lson);
display (rson);
} int main ()
{
int n, q; while (scanf ("%d %d %I64d", &n, &q, &w[]) != EOF)
{
memset (head, -, sizeof(head));
memset (df, , sizeof(df));
memset (etime, , sizeof(etime));
tot = Max = ; for (int i=; i<n; i++)
{
int u;
scanf ("%d %I64d", &u, &w[i]);
add (u, i);
} dfs ();
etime[] = Max;
build (, , n); char str[];
LL x, y, z; while (q --)
{
scanf ("%s %I64d %I64d %I64d", str, &x, &y, &z);
if (strcmp (str, "employee") == )
{
LL tmp = query (, stime[x], stime[x]);
if (tmp < y)
updata(, stime[x], stime[x], z);
}
else
{
LL tmp = query (, stime[x], etime[x]);
LL num = (etime[x] - stime[x] + ) * y;
if (tmp < num)
updata(, stime[x], etime[x], z);
}
} display (); for (int i=; i<n; i++)
printf ("%I64d\n", ans[i]);
}
return ;
}

URAL 1890 . Money out of Thin Air (dfs序hash + 线段树)的更多相关文章

  1. bzoj3306: 树(dfs序+倍增+线段树)

    比较傻逼的一道题... 显然求子树最小值就是求出dfs序用线段树维护嘛 换根的时候树的形态不会改变,所以我们可以根据相对于根的位置分类讨论. 如果询问的x是根就直接输出整棵树的最小值. 如果询问的x是 ...

  2. Codeforces Round #200 (Div. 1) D. Water Tree(dfs序加线段树)

    思路: dfs序其实是很水的东西.  和树链剖分一样, 都是对树链的hash. 该题做法是:每次对子树全部赋值为1,对一个点赋值为0,查询子树最小值. 该题需要注意的是:当我们对一棵子树全都赋值为1的 ...

  3. bzoj2819 DFS序 + LCA + 线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2819 题意:树上单点修改及区间异或和查询. 思维难度不高,但是题比较硬核. 整体思路是维护每一个结 ...

  4. Codeforces 877E - Danil and a Part-time Job(dfs序+线段树)

    877E - Danil and a Part-time Job 思路:dfs序+线段树 dfs序:http://blog.csdn.net/qq_24489717/article/details/5 ...

  5. hdu4366 Successor (dfs序+zkw线段树)

    Successor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  6. 用dfs序处理线段树的好题吗?

    https://www.cnblogs.com/mountaink/p/9878918.html 分析:每次的选取必须选最优的一条链,那我们考虑一下选择这条链后,把这条路上的点的权值更新掉,再采取选最 ...

  7. 7月13日考试 题解(DFS序+期望+线段树优化建图)

    T1 sign 题目大意:给出一棵 N 个节点的树,求所有起点为叶节点的有向路径,其 上每一条边权值和的和.N<=10000 水题.考试的时候毒瘤出题人(学长orz)把读入顺序改了一下,于是很多 ...

  8. hdu 3974 Assign the task(dfs序上线段树)

    Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...

  9. Luogu P2982 [USACO10FEB]慢下来 Slowing down | dfs序、线段树

    题目链接 题目大意: 有一棵N个结点树和N头奶牛,一开始所有奶牛都在一号结点,奶牛们将按从编号1到编号N的顺序依次前往自己的目的地,求每头奶牛在去往自己目的地的途中将会经过多少已经有奶牛的结点. 题解 ...

随机推荐

  1. 用NHibernate处理带属性的多对多关系

    1.引言 老谭在面试开发者的时候,为了考察他们的数据库开发能力,经常祭出我的法宝,就是大学数据库教程中讲到的一个模式:学生选课.这个模式是这种: 在这个模式中,学生(Student)和课程(Cours ...

  2. 启用了不安全的HTTP方法解决办法 IBM APPSCAN

    启用了不安全的HTTP方法解决办法  IBM APPSCAN     安全风险:       可能会在Web 服务器上上载.修改或删除Web 页面.脚本和文件. 可能原因:       Web 服务器 ...

  3. poj 1821 Fence(单调队列优化DP)

    poj 1821 Fence \(solution:\) 这道题因为每一个粉刷的人都有一块"必刷的木板",所以可以预见我们的最终方案里的粉刷匠一定是按其必刷的木板的顺序排列的.这就 ...

  4. 注意css 小细节 颜色能缩写尽量缩写

    如 background: #333333; 改为 background: #333;

  5. golang基础-WaitGroup、kafka消费者

    kafka消费者 以下博客是通过生产者创建.发送消息至kafka 博客链接 现在我们站在消费者的角度,来进行收取消息 package main import ( "fmt" &qu ...

  6. SCX-4521F一体机MAC驱动

    如果您想下载SCX-4521F一体机MAC驱动,请从下面的链接中下载相应驱动:MAC打印驱动:http://org.downloadcenter.samsung.com/downloadfile/Co ...

  7. KbmMW资源汇总(特别是xalion的文章)

    KbmMW框架是收费的,不在此提供下载,如需购买,请自行联系作者Kim Madsen. 网址资源: 官网主页:http://www.components4programmers.com/product ...

  8. Python之如果添加扩展包

    1.首先下载好你需要的扩展包 下载地址是http://www.lfd.uci.edu/~gohlke/pythonlibs/ 2.将你下载好的.whl文件放在你的python文件夹中的Lib\site ...

  9. html5--6-53 阶段练习4-画廊

    html5--6-53 阶段练习4-画廊 学习要点 运用所学过的知识完成一个简单的小练习,理解对过渡动画的应用. @charset "utf-8"; /* CSS Document ...

  10. HDU - 1874 畅通工程续(最短路径)

    d.已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离. s.最短路径 c.Dijkstra单源最短路 /* Dijkstra单源最短路 权值必须是非负 单源最短路径,Dijkstra算法 ...