-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentTree3.java
More file actions
73 lines (63 loc) · 2.4 KB
/
SegmentTree3.java
File metadata and controls
73 lines (63 loc) · 2.4 KB
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
import java.util.Scanner;
public class SegmentTree3 {
private int[] arr; // To keep track of who defeated whom
private int[] seg; // Segment tree array
public SegmentTree3(int n) {
arr = new int[n + 1]; // 1-based indexing
seg = new int[n * 4]; // Segment tree size
}
public void build(int ind, int low, int high) {
if (low == high) {
seg[ind] = 0; // No winner initially
return;
}
int mid = (low + high) / 2;
build(2 * ind + 1, low, mid);
build(2 * ind + 2, mid + 1, high);
seg[ind] = 0; // Initialize node
}
public void update(int ind, int low, int high, int l, int r, int winner) {
// No overlap
if (r < low || l > high) {
return;
}
// Full overlap
if (l <= low && high <= r) {
for (int start = low; start <= high; start++) {
if (arr[start] == 0 && start != winner) {
arr[start] = winner; // Mark defeated by winner
}
}
seg[ind] = winner; // Set the winner for this segment
return;
}
// Partial overlap
int mid = (low + high) / 2;
update(2 * ind + 1, low, mid, l, r, winner);
update(2 * ind + 2, mid + 1, high, l, r, winner);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // number of knights
int m = sc.nextInt(); // number of fights
SegmentTree3 segmentTree = new SegmentTree3(n);
segmentTree.build(0, 1, n); // Build segment tree (1-based index)
int lastWinner = 0; // Variable to hold the last winner
for (int i = 0; i < m; i++) {
int l = sc.nextInt(); // start index of knights in this fight
int r = sc.nextInt(); // end index of knights in this fight
int x = sc.nextInt(); // winner of this fight
segmentTree.update(0, 1, n, l, r, x); // Update the segment tree
lastWinner = x; // Update last winner
}
// After processing all fights, set the last winner's value to 0
if (lastWinner > 0) {
segmentTree.arr[lastWinner] = 0;
}
// Output the results for knights 1 to n
for (int i = 1; i <= n; i++) {
System.out.print(segmentTree.arr[i] + " ");
}
sc.close();
}
}