标签:define cut images meta sample nod represent and red
题目意思:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 49065 Accepted Submission(s): 23200
package Combat.com; import java.util.Arrays; import java.util.Scanner; public class Main { static final int MAX = 100005; static int lazy[] = new int[4*MAX]; static int node[] = new int[4*MAX]; public static void main(String []args) { Scanner cin = new Scanner(System.in); int T = cin.nextInt(); for(int i = 1; i <= T; i++) { int n = cin.nextInt(); Arrays.fill(lazy, -1); buildBinaryTree(1,n,1); int q = cin.nextInt(); for(int j = 0; j < q; j++) { int x = cin.nextInt(); int y = cin.nextInt(); int z = cin.nextInt(); update(x,y,z,1,n,1); } System.out.println("Case " + i + ": The total value of the hook is " + node[1]+"."); } } static void update(int x,int y,int z,int L,int R,int num) { if(x <= L && R <= y) { node[num] = (R-L+1)*z; lazy[num] = z; return; } int mid = (L+R)>>1; pushDown(L,R,num); if(x <= mid) { update(x,y,z,L,mid,num<<1); } if(y > mid) { update(x,y,z,mid+1,R,num<<1|1); } node[num] = node[num<<1] + node[num<<1|1]; } static void pushDown(int L,int R,int num) { if(lazy[num] != -1) { int mid = (L+R)>>1; lazy[num<<1] = lazy[num]; lazy[num<<1|1] = lazy[num]; node[num<<1] = (mid-L+1)*lazy[num]; node[num<<1|1] = (R-mid)*lazy[num]; lazy[num] = -1; } } static void buildBinaryTree(int L,int R,int num) { if(L == R) { node[num] = 1; return; } int mid = (L+R)>>1; buildBinaryTree(L,mid,num<<1); buildBinaryTree(mid+1,R,num<<1|1); node[num] = node[num<<1]+node[num<<1|1]; } }
做这道题目时,我主要遇到的问题是:1.数组开得太小了,直接报错。2.lazy数组的用法,没有理解好!
总之,这些都是模板题,做多点就容易上手了。
标签:define cut images meta sample nod represent and red
原文地址:https://www.cnblogs.com/674001396long/p/10809779.html