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

ZOJ 3910 Market ZOJ Monthly, October 2015 - H的更多相关文章

  1. 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 ...

  2. 思维+multiset ZOJ Monthly, July 2015 - H Twelves Monkeys

    题目传送门 /* 题意:n个时刻点,m次时光穿梭,告诉的起点和终点,q次询问,每次询问t时刻t之前有多少时刻点是可以通过两种不同的路径到达 思维:对于当前p时间,从现在到未来穿越到过去的是有效的值,排 ...

  3. ZOJ 3910 Market

    Market Time Limit: 2 Seconds      Memory Limit: 65536 KB There's a fruit market in Byteland. The sal ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. [Effective JavaScript 笔记]第23条:永远不要修改arguments对象

    arguments对象并不是标准的Array类型的实例.arguments对象不能直接调用Array方法. arguments对象的救星call方法 使得arguments可以品尝到数组方法的美味,知 ...

  2. Stanford机器学习---第六讲. 怎样选择机器学习方法、系统

    原文:http://blog.csdn.net/abcjennifer/article/details/7797502 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  3. hiho #1288 微软2016.4校招笔试题 Font Size

    #1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...

  4. lucas定理,组合数学问题

    对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...

  5. Python Django 的 django templatedoesnotexist

    django 1.8版本的解决方案 在  setting.py 这个文件里 TEMPLATES = [ ...... #原来的 #'DIRS': [ ], //  这个 列表里添加 template路 ...

  6. Launchpad添加openPGP keys

    转自: https://help.ubuntu.com/community/GnuPrivacyGuardHowto mac下: http://notes.jerzygangi.com/the-bes ...

  7. 【OpenStack】OpenStack系列12之OpenStack自动化测试详解

    参考文档: https://github.com/yongluo2013/osf-openstack-training/blob/master/installation/How-to-setup-op ...

  8. 【Spring】Spring系列3之Spring AOP

    3.Spring AOP 3.1.AOP概述 3.2.前置通知 3.3.后置通知 3.4.返回通知.异常通知.环绕通知 3.5.指定切面优先级 3.6.重用切入点表达式 3.7.引入通知 3.8.基于 ...

  9. JavaScript设计模式 - 代理模式

    代理模式是为一个对象提供一个代用品或占位符,以便控制对它的访问 代理模式的用处(个人理解):为了保障当前对象的单一职责(相对独立性),而需要创建另一个对象来处理调用当前对象之前的一些逻辑以提高代码的效 ...

  10. java关闭流,解压缩后的清除

    关闭流文件和file文件的时候,先打开的后关闭,后打开的先关闭,实在不行调用system.jc()方法