-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathL.java
More file actions
171 lines (151 loc) · 5.17 KB
/
Copy pathL.java
File metadata and controls
171 lines (151 loc) · 5.17 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package atcoder.acl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public final class L {
private static class Node {
final long count0, count1, count01, count10;
Node(long count0, long count1, long count01, long count10) {
this.count0 = count0;
this.count1 = count1;
this.count01 = count01;
this.count10 = count10;
}
Node flip() {
return new Node(count1, count0, count10, count01);
}
}
private static class SegTree {
int leftMost, rightMost;
SegTree left, right;
Node node;
long operation = Long.MAX_VALUE;
SegTree(int leftMost, int rightMost, int[] arr) {
this.leftMost = leftMost;
this.rightMost = rightMost;
if (leftMost == rightMost) {
if (arr[leftMost] == 0) {
node = new Node(1, 0, 0, 0);
} else {
node = new Node(0, 1, 0, 0);
}
} else {
final int mid = leftMost + rightMost >>> 1;
left = new SegTree(leftMost, mid, arr);
right = new SegTree(mid + 1, rightMost, arr);
recalc();
}
}
private void recalc() {
if (leftMost == rightMost) {
return;
}
node = merge(left.node, right.node);
}
private static Node merge(Node left, Node right) {
final long count0 = left.count0 + right.count0;
final long count1 = left.count1 + right.count1;
final long count01 = left.count01 + right.count01 + left.count0 * right.count1;
final long count10 = left.count10 + right.count10 + left.count1 * right.count0;
return new Node(count0, count1, count01, count10);
}
private static long operation(long a, long b) {
if (b == Long.MAX_VALUE) {
return a;
}
if (a == Long.MAX_VALUE) {
return b;
}
return a + b;
}
private void propagate() {
if (leftMost == rightMost) {
return;
}
left.operation = operation(left.operation, operation);
right.operation = operation(right.operation, operation);
if (operation != Long.MAX_VALUE && operation % 2 != 0) {
left.node = left.node.flip();
right.node = right.node.flip();
node = merge(left.node, right.node);
}
operation = Long.MAX_VALUE;
}
private Node query(int l, int r) {
propagate();
if (r < leftMost || l > rightMost) {
return new Node(0, 0, 0, 0);
}
if (l <= leftMost && rightMost <= r) {
return node;
}
return merge(left.query(l, r), right.query(l, r));
}
private void add(int l, int r, int v) {
propagate();
if (r < leftMost || l > rightMost) {
return;
}
if (l <= leftMost && rightMost <= r) {
operation = v;
node = node.flip();
return;
}
left.add(l, r, v);
right.add(l, r, v);
node = merge(left.node, right.node);
}
}
public static void main(String[] args) {
final FastScanner fs = new FastScanner();
final PrintWriter pw = new PrintWriter(System.out);
final int n = fs.nextInt();
final int q = fs.nextInt();
final int[] arr = fs.nextIntArray(n);
final SegTree st = new SegTree(0, n - 1, arr);
for (int i = 0; i < q; i++) {
final int type = fs.nextInt();
final int l = fs.nextInt() - 1;
final int r = fs.nextInt() - 1;
if (type == 1) {
st.add(l, r, 1);
} else {
pw.println(st.query(l, r).count10);
}
}
pw.close();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
private String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] nextIntArray(int n) {
final int[] a = new int[n];
for (int i = 0; i < n; i++) { a[i] = nextInt(); }
return a;
}
long[] nextLongArray(int n) {
final long[] a = new long[n];
for (int i = 0; i < n; i++) { a[i] = nextLong(); }
return a;
}
}
}