# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
647709 | DylanSmith | Monkey and Apple-trees (IZhO12_apple) | Java | 667 ms | 67632 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.util.*;
import java.io.*;
public class apple {
static int[] cnt = new int[3000000], left = new int[3000000], right = new int[3000000];
static boolean[] upd = new boolean[3000000];
static int size = 1;
public static void main(String[] args) throws IOException {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
Arrays.fill(left, -1);
Arrays.fill(right, -1);
int Q = in.nextInt();
int C = 0;
for (int q = 0; q < Q; q++) {
if (in.nextInt() == 1) {
int l = in.nextInt() + C, r = in.nextInt() + C;
int res = query(l, r, 0, 0, (1 << 30) - 1);
out.println(res);
C = res;
} else {
int l = in.nextInt() + C, r = in.nextInt() + C;
update(l, r, 0, 0, (1 << 30) - 1);
}
}
out.close();
}
static int query(int L, int R, int current, int l, int r) {
if (l > r) {
return 0;
}
if (left[current] == -1) {
left[current] = size++;
}
if (right[current] == -1) {
right[current] = size++;
}
push(current, l, r);
if (R < l || r < L) {
return 0;
}
if (L <= l && r <= R) {
return cnt[current];
}
int res = query(L, R, left[current], l, (l + r) / 2) + query(L, R, right[current], (l + r) / 2 + 1, r);
merge(current, l, r);
return res;
}
static void update(int L, int R, int current, int l, int r) {
if (l > r) {
return;
}
if (left[current] == -1) {
left[current] = size++;
}
if (right[current] == -1) {
right[current] = size++;
}
push(current, l, r);
if (R < l || r < L) {
return;
}
if (L <= l && r <= R) {
upd[current] = true;
push(current, l, r);
return;
}
update(L, R, left[current], l, (l + r) / 2);
update(L, R, right[current], (l + r) / 2 + 1, r);
merge(current, L, R);
}
static void push(int current, int l, int r) {
if (upd[current]) {
cnt[current] = r - l + 1;
upd[left[current]] = true;
upd[right[current]] = true;
upd[current] = false;
}
}
static void merge(int current, int l, int r) {
cnt[current] = cnt[left[current]] + cnt[right[current]];
}
static class Reader {
BufferedReader in;
StringTokenizer st;
public Reader() {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
public String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
public static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |