编程介的小学生 2017-10-30 08:16 采纳率: 20.5%
浏览 810
已采纳

Bribery

Problem Description
ZJiaQ is a solider. In his army,every solider have a boss who is also a solider . Now, AFA want to bribe some solider to attack ZJiaQ .Every solider have a ID, and evry day ,they will get a new ID.ZJiaQ want to avoid been attacked. The only way is that he bribe the boss of the solider or the boss of the boss of the solider or...
Once he bribe a boss ,the boss can stop one solider who is branch of him.In this army,everybody only can send money to his boss,if ZJiaQ want to send money to the boss of his boss.The money should pass through his boss and his boss will get 1 coin.
Now ZJiaQ want to know how much will he cost.

Input
There are multiply test case . For each case , first line contains a integer n (3 < = n < = 40000) the number of node is from 0 to n-1. next line contains n integers a0,a2…an-1 ( 1<= ai <= 100000 ) . Ai is the money of bribery for the solider whose ID is i . next line contains n-1 integers f1, f2 …fn-1 ( 0 <= fi < n ) . fi is the boss of solider i. next line contains a integer m (1<= m<=40000). Next q lines contain 3 integers l ,r ,p (0 < l < = r < n,p < l or p>r),l,r means that AFA bribe soliders whose Id are between l and r.p is ZJiaQ's ID Now.

Output
Output the Ans mod 10000007 of each query.

Sample Input
3
2 2 2
0 0
2
1 1 2
2 2 1

10
52 30 56 13 98 53 75 80 7 34
3 0 0 3 2 2 9 1 0
5
2 9 1
5 5 8
2 2 1
2 6 7
2 7 1

Sample Output
2
2

331
54
53
265
278

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能