ZOJ 3910 Market ZOJ Monthly, October 2015 - H
Market
Time Limit: 2 Seconds Memory Limit: 65536 KB
There's a fruit market in Byteland. The salesmen there only sell apples.
There are n salesmen in the fruit market and the i-th salesman will sell at most wi apples. Every salesman has an immediate manager pi except one salesman who is the boss of the market. A salesman A is said to be the superior of another salesman B if at least one of the followings is true:
- Salesman A is the immediate manager of salesman B.
- Salesman B has an immediate manager salesman C such that salesman A is the superior of salesman C.
The market will not have a managerial cycle. That is, there will not exist a salesman who is the superior of his/her own immediate manager.
We will call salesman x a subordinate of another salesman y, if either y is an immediate manager of x, or the immediate manager of x is a subordinate to salesman y. In particular, subordinates of the boss are all other salesmen of the market. Let the degree of the boss be 0. Then if the degree of i-th salesman is k, the immediate subordinates of i-th salesman will have degree k + 1.
Today, m buyers come to market for apples. The i-th buyer will buy at most ci apples only from the xi-th salesman and his subordinates whose degree is no larger than xi-th salesman's degree plus di.
The boss wants to know how many apples can be sold in salesmen's best effort (i.e. the maximum number).
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers n and m (1 ≤ n, m ≤ 10000) — the number of salesmen and the number of buyers.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 105). Every wi denotes the number of apples that i-th salesman can sell.
The next line contains n integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th salesman. If pi is -1, that means that the i-th salesman does not have an immediate manager.
Each of the next m lines contains three integers ci, xi and di (1 ≤ ci ≤ 105, 1 ≤ xi ≤ n, 0 ≤ di ≤ n) — the information of i-th buyer.
It is guaranteed that the total number of salesmen in the input doesn't exceed 105, and the total number of buyers also doesn't exceed 105. The number of test cases in the input doesn't exceed 500.
Output
For each test case, output a single integer denoting the maximum number of apples can be sold.
Sample Input
1
4 2
1 2 3 4
-1 1 2 3
3 2 1
5 1 1
Sample Output
6
Author: LIN, Xi
题意:给出一棵n个点的树,然后每个点有个权值,给出m次操作,每次操作让你从第i个点以及其下面d层的所有点中 最多减去c的权值(可以分开减),问最后最多划去多少权值?
分析:这题明显就是贪心,想啊想啊就容易想到平衡术合并维护,贪心优先划掉层数较大的权值
就是每个点都有一个平衡树,代表这个点为根的子树已经处理完下面的操作了之后,所有有权值的点,
然后把这个平衡树向上启发式合并
整个过程是从下往上的
c++可以使用set做这题,pascal就悲剧了。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name) {
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} inline int Getint() {
int Ret = ;
bool Flag = ;
char Ch = ' ';
while(!(Ch >= '' && Ch <= '')) {
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '') {
Ret = Ret*+Ch-'';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
int n, m, Arr[N];
int Fa[N], First[N], To[N], Next[N], Tot;
int Root, Que[N], Degree[N], Ans;
typedef pair<int, int> II;
vector<II> Salesman[N];
set<II> Splay[N];
int Set[N]; inline void Insert(int u, int v) {
Tot++;
To[Tot] = v, Next[Tot] = First[u];
First[u] = Tot;
} inline void Init() {
For(i, , n) {
Splay[i].clear(), Salesman[i].clear();
Set[i] = i, First[i] = ;
}
Tot = ;
} inline void Solve(); inline void Input() {
int Test;
//scanf("%d", &Test);
Test = Getint();
while(Test--) {
//scanf("%d%d", &n, &m);
n = Getint();
m = Getint();
For(i, , n) Arr[i] = Getint(); Init(); Tot = ;
For(i, , n) {
Fa[i] = Getint();
if(Fa[i] != -) Insert(Fa[i], i);
else Root = i;
} For(i, , m) {
int C, x, D;
C = Getint();
x = Getint();
D = Getint();
Salesman[x].pub(mk(C, D));
} Solve();
}
} inline void Bfs(int Start) {
static int Head, Tail;
Head = Tail = , Que[] = Start, Degree[Start] = ;
while(Head <= Tail) {
int u = Que[Head++];
for(int Tab = First[u], v; Tab; Tab = Next[Tab]) {
v = To[Tab];
Degree[v] = Degree[u]+, Que[++Tail] = v;
}
}
} inline void Merge(int x, int y) {
for(set<II>::iterator It = Splay[Set[y]].begin(); It != Splay[Set[y]].end(); It++)
Splay[Set[x]].insert(*It);
} inline int Work(int x) {
int Ret = ;
Splay[Set[x]].insert(mk(Degree[x], x)); int Len = sz(Salesman[x]), Size = sz(Splay[Set[x]]);
set<II>::iterator It;
Rep(i, Len) {
int Buy = Salesman[x][i].ft, D = Salesman[x][i].sd, T;
while(Buy && Size) {
It = Splay[Set[x]].upper_bound(mk(Degree[x]+D, INF));
if(It == Splay[Set[x]].begin()) break;
It--;
T = min(Arr[It->sd], Buy);
Arr[It->sd] -= T, Buy -= T;
if(Arr[It->sd] == ) {
Splay[Set[x]].erase(It);
Size--;
}
}
Ret += Salesman[x][i].ft-Buy;
} if(Fa[x] != -) {
Size = sz(Splay[Set[x]]);
int FaSize = sz(Splay[Set[Fa[x]]]);
if(FaSize < Size) swap(Set[x], Set[Fa[x]]);
Merge(Fa[x], x);
} return Ret;
} inline void Solve() {
Bfs(Root); Ans = ;
Ford(i, n, )
Ans += Work(Que[i]); printf("%d\n", Ans);
} int main() {
SetIO("H");
Input();
//Solve();
return ;
}
ZOJ 3910 Market ZOJ Monthly, October 2015 - H的更多相关文章
- ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H
Bob wants to pour water Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge There i ...
- 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys
题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...
- ZOJ 3910 Market
Market Time Limit: 2 Seconds Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...
- 143 - ZOJ Monthly, October 2015 I Prime Query 线段树
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F
Number Game Time Limit: 2 Seconds Memory Limit: 65536 KB The bored Bob is playing a number game ...
- ZOJ 3905 Cake ZOJ Monthly, October 2015 - C
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
- ZOJ 3903 Ant ZOJ Monthly, October 2015 - A
Ant Time Limit: 1 Second Memory Limit: 32768 KB There is an ant named Alice. Alice likes going ...
- Twelves Monkeys (multiset解法 141 - ZOJ Monthly, July 2015 - H)
Twelves Monkeys Time Limit: 5 Seconds Memory Limit: 32768 KB James Cole is a convicted criminal ...
随机推荐
- codeforces 258div2 A Game With Sticks(DP)
题目链接:http://codeforces.com/contest/451/problem/A 解题报告:有n跟红色的棍子横着放,m根蓝色的棍子竖着放,它们形成n*m个交点,两个人轮流在里面选择交点 ...
- NGUI例子Scroll View场景中item添加点击后自动滑到终点
http://blog.csdn.net/luyuncsd123/article/details/22914497 最近在做一个项目的UI,需求是1.拖动items后当永远有一个item保存在中间位置 ...
- python 异步线程简单实现
import threading def foo(): with open(r'./result.log','wb') as f: f.write('=some logs here ==') t = ...
- 【原创】sql:慎用【数字字段1 - 数字字段2】这样的sql(10-null = null)mysql
如果只有一个表的情况下 a表: id num1,num2 1 10 5 2 10 0 3 20 0 select id, num1,num2,num1 - num2 AS subNum from a; ...
- 2013 ACM/ICPC 长春网络赛F题
题意:两个人轮流说数字,第一个人可以说区间[1~k]中的一个,之后每次每人都可以说一个比前一个人所说数字大一点的数字,相邻两次数字只差在区间[1~k].谁先>=N,谁输.问最后是第一个人赢还是第 ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- NEFU 2016省赛演练一 F题 (高精度加法)
Function1 Problem:F Time Limit:1000ms Memory Limit:65535K Description You know that huicpc0838 has b ...
- 借助magicwindow sdk plugin快速集成sdk
到目前为止,Android Studio已经是开发原生Android App的主流IDE,它是由Google官方设计并基于JetBrains的IntelliJ IDEA.我们魔窗开发的sdk也是使用此 ...
- .NET的堆和栈01,基本概念、值类型内存分配
当我们对.NET Framework的一些基本面了解之后,实际上,还是很有必要了解一些更底层的知识.比如.NET Framework是如何进行内存管理的,是如何垃圾回收的......这样,我们才能写出 ...
- Freemarker中遍历List以及内置函数使用
在Freemarker应用中经常会遍历List获取需要的数据,并对需要的数据进行排序加工后呈现给用户. 那么在Freemarker中如何遍历List,并对List中数据进行适当的排序呢?一. Free ...