ZOJ 3910 Market ZOJ Monthly, October 2015 - H

时间:2022-06-12 17:50:14

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 ≤ nm ≤ 10000) — the number of salesmen and the number of buyers.

The second line contains n integers w1w2, ..., 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 cixi 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 ;
}