4003: [JLOI2015] Fortress conquest
Time Limit:10 Sec Memory limit:128 MB
submit:206 solved:89
[Submit] [Status] [Discuss]
Description
Xiao Ming has recently won a new board game, in which it is necessary to occupy N cities with M knights.
These n cities are represented by integers of 1 to N. In addition to the city of 1th, the fortress I will be governed by another town,
Where Fi is less than I. In other words, all the cities constitute a tree with a root. This m knight is represented by an integer of 1 to M, whose
In the first Knight of the initial combat capacity of SI, the number one attack of the castle for CI.
Each castle has a defensive value hi, if a knight's combat effectiveness is greater than the value of the castle's health, then the knight can
To occupy the city, or the conquest fails, and the knight will die in this city. After occupying a castle, the Knight's Fighting power
Will change, and then continue to attack the city that governs it until it occupies 1th, or at the expense of the fortress.
In addition to the 1th cities, each of the cities I will give a combat effectiveness change parameter ai;vi. If Ai = 0, after the conquest of the castle I will increase the effectiveness of the Knight VI; if AI = 1, after the conquest of the fortress I, the combat effectiveness will be multiplied by VI. Note that each knight is counted separately. That means a knight attacking a city, no matter what the result, will not affect the results of other knights attacking the city.
The question now is, for each of the cities, how many knights are sacrificed here, and for each knight, the number of cities he has captured.
Input
The 1th line contains two positive integer n;m, indicating the number of castles and the number of knights.
Line 2nd contains n integers, where the number of I is Hi, which represents the defensive value of the fortress I.
3rd through n + 1 lines, each line containing three integers. Where the three number of lines I + 1 is Fi;ai;vi, respectively, represents the jurisdiction
The city number and the two combat change parameters of the fortress.
Nth +2 to n + M + 1 lines, each line containing two integers. Where the two digits of the Nth + I row are si;ci, respectively, the table
The first attack of the castle is shown in the initial battle.
Output
Outputs n + m rows with one non-negative integer per line. The first n rows represent the Knights who sacrificed in the fortress 1 to N, respectively.
Number, the rear m lines represent the number of cities the knight has captured from 1 to M.
Sample Input
5 5
50 20 10) 10 30
1 1 2
2 0 5
2 0-10
1 0 10
20 2
10 3
40 4
20 4
35 5
Sample Output
2
2
0
0
0
1
1
3
1
1
HINT
For 100% data, 1 <= n;m <= 300000; 1 <= ci <= n; -10^18 <= hi,vi,si <= 10^18;ai equals 1 or 2; when ai = 1 o'clock, vi > 0; the absolute value of the knight's combat effectiveness is guaranteed to be no more than 10^18 at any time.
Idea question + Left bias tree + tag pass.
You can see that the modification operation does not change the size relationship between Knight Combat effectiveness.
Then we can start processing from the leaves, merging his son with each node and the knight starting from him (building the small Gan), culling out the combat effectiveness ≤ This node defends the value of the person (death), and then marks the change of combat effectiveness after this node.
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#define PB Push_back#define M 300005#define LL Long Longusing namespace Std;intC;struct qishi{intAns LLs;}Q[m]; struct Cq{int f,a,ans,root; LL h,v;}C[M];intNm, tot=0, H[m];struct edge{int y,NE;} E[m *];struct ltree{LL add,mul,v;intAa,ans,l,r,dis,root,id;} A[m];voidRead(int&TMP) {tmp=0; Char Ch=getchar ();intfu=1; for(;ch<' 0 '|| Ch>' 9 '; Ch=getchar ())if(ch=='-') fu=-1; for(; ch>=' 0 '&&ch<=' 9 '; Ch=getchar ()) tmp=tmp*10+ch-' 0 '; Tmp*=Fu;} void Read (LL &tmp) {tmp=0; Char Ch=getchar (); LL fu=1; for(;ch<' 0 '|| Ch>' 9 '; Ch=getchar ())if(ch=='-') fu=-1; for(; ch>=' 0 '&&ch<=' 9 '; Ch=getchar ()) tmp=tmp*10LL+ch-' 0 '; Tmp*=Fu;} void Addedge (int x,int y) {E[++tot].y=y; E[tot].NE=h[x]; h[x]=tot;} void Modify (int x,intAa,llm, LL D) {if(!aa&&!d&&m==1)return; a[x].aa+=aa,a[x].ANS+=AA; a[x].v=a[x].v*m+d; a[x].add=a[x].add*m+d; a[x].mul*=m;} void Push_down (int x){intl=a[x].l,r=a[x].R; Modify (l,a[x].aa,a[x].mul,a[x].add); Modify (r,a[x].aa,a[x].mul,a[x].add); a[x].aa=a[x].add=0, a[x].mul=1;}intMerge (int x,int y){if(!x||!y)return x+y; Push_down (x), Push_down (y);if(a[x].v>a[y].V) Swap (x,y); a[x].r=merge (a[x].R,y);if(a[a[x].l].dis<a[a[x].r].dis) Swap (a[x].l,a[x].R); a[x].dis=a[a[x].r].dis+1;return x;}intDel (int x){returnMerge (a[x].l,a[x].R);} void Dfs (int x,intFA) { for(inti=h[x];i;i=e[i].NE) {int y=e[i].y; Dfsy,x); c[x].root=merge (c[x].root,c[y].root); } while(c[x].root) {Push_down (c[x].root);if(a[c[x].root].v<c[x].h) {c[x].ans++;Q[a[c[x]. root].id].ans=a[c[x].root].ans; c[x].root=del (c[x].root); }Else Break; }intr=c[x].root;if(c[x].A) A[R].V*=c[x].v,a[r].mul*=c[x].v,a[r].add*=c[x].V;Elsea[r].v+=c[x].v,a[r].add+=c[x].V; a[r].ans++,a[r].aa++;}intMain () {Read(n),Read(m); for(intI=1; i<=n;i++) Read (c[i].h); for(intI=2; i<=n;i++) {Read(C[I].F),Read(C[I].A), Read (C[I].V); Addedge (C[i].f,i); } for(intI=1; i<=m; i++) {Read (Q[i].s),Read(C); a[i].root=i,a[i].v=Q[i].s, a[i].mul=1, a[i].add=0; A[i].aa=0, a[i].ans=0, A[i].id=i;if(c[c].root) C[c].root=merge (c[c].root,i);Elsec[c].root=i; } DFS (1,0); for(intI=1; i<=n;i++)printf("%d\ n", C[i].ans); while(c[1].root) {Push_down (c[1].root);Q[a[c[1]. root].id].ans=a[c[1].root].ans; c[1].root=del (c[1].root); } for(intI=1; i<=m; i++)printf("%d\ n",Q[i]. ans);/*if (n<=3000&&m<=3000) below is the violent {for (int i=1;i<=m;i++) {int now=q[i].c; while (now) {if (q[i].s<c[now].h) {C[now].ans ++; Break } else {if (c[now].a) q[i].s*=c[now].v; else Q[I].S+=C[NOW].V; NOW=C[NOW].F; q[i].ans++; }}} for (int i=1;i<=n;i++) printf ("%d\n", C[i].ans); for (int i=1;i<=m;i++) printf ("%d\n", Q[i].ans); return 0; }*/ return 0;}
"Bzoj 4003" [JLOI2015] Fortress conquest