题目链接:

  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. 备忘录模式-Memento

    备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 备忘录模式结构图: 何时使用备忘录模式: Memento模式比适合 ...

  2. luogu3379 【模板】最近公共祖先(LCA) 倍增法

    题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...

  3. webpack 构建多页面应用

    如何使用webpack构建多页面应用,这是一个我一直在想和解决的问题.网上也给出了很多的例子,很多想法.猛一看,觉得有那么点儿意思,但仔细看也就那样. 使用webpack这个构建工具,可以使我们少考虑 ...

  4. hdu 1963 Investment 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 题目意思:有 本金 money,还有一些股票的种类,第 i 种股票买入需要 value[i] 这 ...

  5. django错误 - Reason given for failure: CSRF cookie not set.

    练习Django表单提交时遇到如下问题: 在网上各种查找,终于找到了解决方法. 1.在from 表单中添加 {% csrf_token %} 2.在视图中添加 from django.template ...

  6. servlet的<url-pattern>

    ① 完全匹配 <url-pattern>/test/list.do</url-pattern> ② 路径匹配 <url-pattern>/*</url-pat ...

  7. SPOJ:Robot(数学期望)

    There is a robot on the 2D plane. Robot initially standing on the position (0, 0). Robot can make a ...

  8. POJ3304:Segments (几何:求一条直线与已知线段都有交点)

    Given n segments in the two dimensional space, write a program, which determines if there exists a l ...

  9. 【HDU 3555】 Bomb

    [题目链接] 点击打开链接 [算法] 数位DP [代码] #include<bits/stdc++.h> using namespace std; #define MAXL 15 type ...

  10. vue项目中的路径别名

    每次写引入组件的路径,如果路径嵌套比较深,那么会比较麻烦,我们可以在webpack.base.conf.js,中设置路径的别名,默认webpack设置src的别名为@ 建议配置src下一级目录的别名, ...