Home BOJ. Prefix Product (11505)
Post
Cancel

BOJ. Prefix Product (11505)

[Link] https://www.acmicpc.net/problem/11505


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.*;
import java.io.*;

public class Main {
	static int NUM = 1000000007;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String[] line = br.readLine().split(" ");
		int n = toi(line[0]), m = toi(line[1]), k = toi(line[2]);
		int[] arr = new int[n];
		int[] seg = new int[1 << (int)Math.ceil(Math.log(n) / Math.log(2) + 1)];

		for(int i = 0; i < n; i++) arr[i] = toi(br.readLine());

		init(seg, arr, 0, n - 1, 1);

		for(int i = 0, il = k + m; i < il; i++) {
			line = br.readLine().split(" ");
			int b = toi(line[1]), c = toi(line[2]);
			if(line[0].equals("1")) update(seg, 0, n - 1, 1, b - 1, c);
			else sb.append(get(seg, 0, n - 1, 1, b - 1, c - 1)).append("\n");
		}
		print(sb);
	}

	static int init(int[] seg, int[] arr, int start, int end, int idx) {
		if(start == end) return seg[idx] = arr[start];
		int mid = (start + end) / 2;
		return seg[idx] = (int)((long) init(seg, arr, start, mid, 2 * idx) *
			init(seg, arr, mid + 1, end, 2 * idx + 1) % NUM) ;
	}

	static int update(int[] seg, int start, int end, int idx, int targetIdx, int val) {
		if(targetIdx < start || end < targetIdx) return seg[idx];
		if(start == end) {
			seg[idx] = val;
			return seg[idx];
		}
		int mid = (start + end) / 2;
		return seg[idx] = (int)((long)update(seg, start, mid, 2 * idx, targetIdx, val)
			* update(seg, mid + 1, end, 2 * idx + 1, targetIdx, val) % NUM) ;
	}

	static int get(int[] seg, int start, int end, int idx, int targetL, int targetR) {
		if(end < targetL || targetR < start) return 1;
		if(targetL <= start && end <= targetR) return seg[idx];
		int mid = (start + end) / 2;
		return (int)((long)get(seg, start, mid, 2 * idx, targetL, targetR) * get(seg, mid + 1, end, 2 * idx + 1, targetL, targetR) % NUM);
	}

	// static int rev(int n) {
	// 	int remainder = 1, store = n, iter = NUM - 2;
	// 	while(iter > 0) {
	// 		if((iter & 1) == 1) remainder = (int)((long)remainder * store % NUM);
	// 		store = (int)((long)store * store % NUM);
	// 		iter = iter >> 1;
	// 	}
	// 	return remainder;
	// }

	static int toi(String s) { return Integer.parseInt(s); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}


failed code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
import java.io.*;

public class Main {
	static int NUM = 1000000007;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String[] line = br.readLine().split(" ");
		int n = toi(line[0]), m = toi(line[1]), k = toi(line[2]);
		int[] arr = new int[n];
		Arrays.fill(arr, 1);
		int[] seg = new int[1 << (int)Math.ceil(Math.log(n) / Math.log(2) + 1)];
		boolean[] isZero = new boolean[1 << (int)Math.ceil(Math.log(n) / Math.log(2) + 1)];

		for(int i = 0; i < n; i++) {
			arr[i] = toi(br.readLine());
		}

		init(seg, arr, 0, n - 1, 1);
		init(isZero, arr, 0, n - 1, 1);

		for(int i = 0, il = k + m; i < il; i++) {
			line = br.readLine().split(" ");
			int b = toi(line[1]), c = toi(line[2]);
			if(line[0].equals("1")) {
					if(c != 0) {
						update(seg, 0, n - 1, 1, b - 1, arr[b - 1], c);
						arr[b - 1] = c;
					}
					update(isZero, 0, n - 1, 1, b - 1, c);
			}
			else sb.append(get(seg, isZero, 0, n - 1, 1, b - 1, c - 1)).append("\n");
		}
		print(sb);
	}

	static int init(int[] seg, int[] arr, int start, int end, int idx) {
		if(start == end) return arr[start] == 0 ? (seg[idx] = 1) : (seg[idx] = arr[start]);
		int mid = (start + end) / 2;
		return seg[idx] = (int)((long) init(seg, arr, start, mid, 2 * idx) *
			init(seg, arr, mid + 1, end, 2 * idx + 1) % NUM);
	}

	static boolean init(boolean[] isZero, int[] arr, int start, int end, int idx) {
		if(start == end) return arr[start] == 0 ? (isZero[idx] = true) : false;
		int mid = (start + end) / 2;
		return isZero[idx] = init(isZero, arr, start, mid, 2 * idx) || init(isZero, arr, mid + 1, end, 2 * idx + 1);
	}

	static void update(int[] seg, int start, int end, int idx, int targetIdx, int div, int mul) {
		if(targetIdx < start || end < targetIdx) return;
		if(start == end) {
			seg[idx] = mul;
			return;
		}
		int mid = (start + end) / 2;
		seg[idx] = (int)(((long)seg[idx]) * (div == 0 ? 1 : rev(div)) * mul % NUM);
		if(targetIdx <= mid) update(seg, start, mid, 2 * idx, targetIdx, div, mul);
		else update(seg, mid + 1, end, 2 * idx + 1, targetIdx, div, mul);
	}

	static boolean update(boolean[] isZero, int start, int end, int idx, int targetIdx, int mul) {
		if(targetIdx < start || end < targetIdx) return isZero[idx];
		if(start == end) return mul == 0 ? (isZero[idx] = true) : (isZero[idx] = false);
		int mid = (start + end) / 2;
		return isZero[idx] =
			update(isZero, start, mid, 2 * idx, targetIdx, mul) || update(isZero, mid + 1, end, 2 * idx + 1, targetIdx, mul);
	}

	static int get(int[] seg, boolean[] isZero, int start, int end, int idx, int targetL, int targetR) {
		if(end < targetL || targetR < start) return 1;
		if(targetL <= start && end <= targetR) return isZero[idx] ? 0 : seg[idx];
		int mid = (start + end) / 2;
		return (int)((long)get(seg, isZero, start, mid, 2 * idx, targetL, targetR) * get(seg, isZero, mid + 1, end, 2 * idx + 1, targetL, targetR) % NUM);
	}

	static int rev(int n) {
		int remainder = 1, store = n, iter = NUM - 2;
		while(iter > 0) {
			if((iter & 1) == 1) remainder = (int)((long)remainder * store % NUM);
			store = (int)((long)store * store % NUM);
			iter = iter >> 1;
		}
		return remainder;
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.