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. xcode注释

    新开的项目需要先开发iOS版本,所以又把好久没写的iOS捡起来了,之前都是手动注释,最近是越来越懒了,所以在网上找了一个自动注释的插件,啊哈,其实有时候还真的挺怀念用Eclipse的时候,不过不用羡慕 ...

  2. Lucene4.3开发之分词器总结

    Lucene4.3开发之分词器总结 http://java.chinaitlab.com/tools/940011.html

  3. PHP生成CSV文件

    CSV文件的定义这里就不多做介绍了,难能可贵的是用Excel可以直接打开CSV文件.用PHP输出CSV文件本身很简单,但是大家如果有业务需求,下面的代码可以作为参考. $tableheader = a ...

  4. C++简单使用Jsoncpp来读取写入json文件

    一.源码编译 C++操作json字符串最好的库应该就是jsoncpp了,开源并且跨平台.它可以从这里下载. 下载后将其解压到任意目录,它默认提供VS2003和VS2010的工程文件,使用VS2010可 ...

  5. 多层神经网络与C++实现

    BP理论部分参考:http://blog.csdn.net/itplus/article/details/11022243 参考http://www.cnblogs.com/ronny/p/ann_0 ...

  6. 【Other】千字文 硬笔 楷书 字帖

    <千字文>是我国最优秀的一篇训蒙教材,用一千个汉字勾划出一部完整的中国文化史的基本轮廓,代表了中国传统教育启蒙阶段的最高水平.<千字文>通篇首尾连贯,音韵谐美,读起来朗朗上口, ...

  7. FileOutputStream与FileInputStream互相转换

    List<InstorageNoticeDto> noticeList = null; FileOutputStream fos = null; FileInputStream is = ...

  8. 【转】Spring Quartz的原理

    Quartz是一个大名鼎鼎的Java版开源定时调度器,功能强悍,使用方便. 一.核心概念 Quartz的原理不是很复杂,只要搞明白几个概念,然后知道如何去启动和关闭一个调度程序即可. 1.Job 表示 ...

  9. Java for LeetCode 141 Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  10. ios获取一个文件夹下的文件(夹)列表

    NSArray* ary=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:[[NSBundle mainBundle] pathFo ...