Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; map<int, int> a, b; int x, y, n, m; char ch; int main() { scanf("%d%d", &n, &m); while (m--) { scanf("%d%d %c", &x, &y, &ch); int ans = 0; map<int, int>::iterator it; if (ch == 'U') { it = a.lower_bound(x); if (a.count(x)) { puts("0"); continue; } if (it == a.end()) ans = y; else ans = y - (it->second); b[y] = x; a[x] = it->second; printf("%d\n", ans); } else if (ch == 'L') { it = b.lower_bound(y); if (b.count(y)) { puts("0"); continue; } if (it == b.end()) ans = x; else ans = x - (it->second); a[x] = y; b[y] = it->second; printf("%d\n", ans); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> int n, m; int ax[200100]; int ay[200100]; int az[200100]; char c[200100][10]; struct qry { int loc, orig; bool operator<(const qry& r) const { return loc < r.loc; } }; qry b[200100]; int it[1 << 19]; int left[200100]; int up[200100]; void push(int x) { x += 3; int val = x; x += 1 << 18; while (x != 0) { if (val > it[x]) { it[x] = val; } x /= 2; } } int calc(int x) { x += 3; int res = -99999; x += 1 << 18; int y = 1 << 18; while (x >= y) { if (x % 2 == 0) { if (it[x] > res) { res = it[x]; } x--; } x /= 2; y /= 2; } return res - 3; } int main() { int i, j, k; scanf("%d%d", &n, &m); for (i = 0; i < m; i++) { scanf("%d%d%s", &ax[i], &ay[i], c[i]); if (c[i][0] == 'U') ay[i] = 1; else ay[i] = 0; b[i].orig = i; b[i].loc = ax[i]; } push(0); std::sort(b, b + m); k = 1; for (i = 0; i < m; i++) { az[b[i].orig] = k; if (b[i].loc != b[i + 1].loc) k++; } left[0] = 0; up[0] = 0; for (i = 0; i < m; i++) { j = calc(az[i]); if (j == az[i]) { printf("0\n"); } else if (ay[i] == 1) { printf("%d\n", n + 1 - ax[i] - up[j]); up[az[i]] = up[j]; left[az[i]] = ax[i]; } else { printf("%d\n", ax[i] - left[j]); up[az[i]] = up[j]; left[az[i]] = left[j]; up[j] = n + 1 - ax[i]; } push(az[i]); } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
//package codeforces; import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeSet; public class C implements Closeable { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); StringTokenizer stringTokenizer; C() throws IOException { // reader = new BufferedReader(new FileReader("input.txt")); // writer = new PrintWriter(new FileWriter("bridges.out")); } String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } double nextDouble() throws IOException { return Double.parseDouble(next()); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } final int MOD = 1000 * 1000 * 1000 + 7; int sum(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } int product(int a, int b) { return (int) (1l * a * b % MOD); } int pow(int x, int k) { int result = 1; while (k > 0) { if (k % 2 == 1) { result = product(result, x); } x = product(x, x); k /= 2; } return result; } int pow(int x, long k) { int result = 1; while (k > 0) { if (k % 2 == 1) { result = product(result, x); } x = product(x, x); k /= 2; } return result; } int inv(int x) { return pow(x, MOD - 2); } void solve() throws IOException { int realN = nextInt(); final int n = nextInt(); int[] x = new int[n]; char[] d = new char[n]; Set<Integer> set = new TreeSet<>(); for(int i = 0; i < n; i++) { x[i] = nextInt(); set.add(x[i]); next(); d[i] = next().charAt(0); } class SegmentTree { int q = 1; { while(q <= n + 1) { q *= 2; } } int[] a = new int[2 * q]; void setMax(int l, int r, int x) { for(l += q, r += q; l < r; l /= 2, r /= 2) { if(l % 2 == 1) { a[l] = Math.max(a[l], x); l++; } if(r % 2 == 1) { r--; a[r] = Math.max(a[r], x); } } } int getMax(int i) { int max = 0; for(i += q; i > 0; i /= 2) { max = Math.max(max, a[i]); } return max; } } Map<Integer, Integer> map = new HashMap<>(); int[] realColumn = new int[n + 2]; int[] realRow = new int[n + 2]; for (Integer i : set) { int col = map.size() + 1; realColumn[col] = i; int row = n - col + 1; realRow[row] = realN - realColumn[col] + 1; map.put(i, map.size() + 1); } for(int i = 0; i < n; i++) { x[i] = map.get(x[i]); } SegmentTree rows = new SegmentTree(), cols = new SegmentTree(); Set<Integer> eaten = new HashSet<>(); for(int i = 0; i < n; i++) { if(eaten.contains(x[i])) { writer.println(0); continue; } eaten.add(x[i]); if(d[i] == 'U') { int row = n - x[i] + 1; int maxRow = cols.getMax(x[i]); writer.println(realRow[row] - realRow[maxRow]); rows.setMax(maxRow, row + 1, x[i]); } else { int row = n - x[i] + 1; int maxCol = rows.getMax(row); writer.println(realColumn[x[i]] - realColumn[maxCol]); cols.setMax(maxCol, x[i] + 1, row); } } } public static void main(String[] args) throws IOException { try (C c = new C()) { c.solve(); } } @Override public void close() throws IOException { reader.close(); writer.close(); } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, m, i, j, lx, ly, lval; pair<int, int> htree[800005], vtree[800005]; char s[10]; void push(int ind, pair<int, int>* tree) { if (tree[ind].second == 0) return; tree[ind * 2].first = max(tree[ind * 2].first, tree[ind].second); tree[ind * 2].second = max(tree[ind * 2].second, tree[ind].second); tree[ind * 2 + 1].first = max(tree[ind * 2 + 1].first, tree[ind].second); tree[ind * 2 + 1].second = max(tree[ind * 2 + 1].second, tree[ind].second); tree[ind].second = 0; } void query(int l, int r, int ind, pair<int, int>* tree) { if (l == r) { lval = tree[ind].first; return; } push(ind, tree); int mid = (l + r) / 2; if (lx <= mid) query(l, mid, ind * 2, tree); else query(mid + 1, r, ind * 2 + 1, tree); tree[ind].first = max(tree[ind * 2].first, tree[ind * 2 + 1].first); } void update(int l, int r, int ind, pair<int, int>* tree) { if (lx <= l && r <= ly) { tree[ind].first = max(tree[ind].first, lval); tree[ind].second = max(tree[ind].second, lval); return; } push(ind, tree); int mid = (l + r) / 2; if (lx <= mid) update(l, mid, ind * 2, tree); if (ly > mid) update(mid + 1, r, ind * 2 + 1, tree); tree[ind].first = max(tree[ind * 2].first, tree[ind * 2 + 1].first); } struct dat { int x, y; char t; }; vector<dat> q; int main() { scanf("%d%d", &n, &m); vector<int> tmp; vector<int> tmp1; for (i = 0; i < m; i++) { int x, y; scanf("%d%d%s", &x, &y, s); q.push_back({x, y, s[0]}); tmp.push_back(x); tmp1.push_back(y); } tmp.push_back(0); tmp1.push_back(0); sort(tmp.begin(), tmp.end()); sort(tmp1.begin(), tmp1.end()); tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin()); tmp1.resize(unique(tmp1.begin(), tmp1.end()) - tmp1.begin()); for (i = 0; i < m; i++) { int x = q[i].x; int y = q[i].y; q[i].x = lower_bound(tmp.begin(), tmp.end(), q[i].x) - tmp.begin() + 1; q[i].y = lower_bound(tmp1.begin(), tmp1.end(), q[i].y) - tmp1.begin() + 1; if (q[i].t == 'U') { lx = q[i].x; lval = 0; query(1, tmp.size(), 1, htree); printf("%d\n", y - lval); lx = lower_bound(tmp1.begin(), tmp1.end(), lval) - tmp1.begin() + 2; ly = q[i].y; lval = x; if (lx <= ly) update(1, tmp1.size(), 1, vtree); lx = q[i].x; ly = q[i].x; lval = y; update(1, tmp.size(), 1, htree); } else { lx = q[i].y; lval = 0; query(1, tmp1.size(), 1, vtree); printf("%d\n", x - lval); lx = lower_bound(tmp.begin(), tmp.end(), lval) - tmp.begin() + 2; ly = q[i].x; lval = y; if (lx <= ly) update(1, tmp.size(), 1, htree); lx = q[i].y; ly = q[i].y; lval = x; update(1, tmp1.size(), 1, vtree); } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.io.File; import java.io.FileNotFoundException; import java.lang.reflect.Array; import java.util.*; public class Test4A { static int n = 0; public static class Border{ int left; int upper; public Border(int left, int upper) { this.left = left; this.upper = upper; // TODO Auto-generated constructor stub } } public static void main(String[] args) throws FileNotFoundException { TreeMap<Integer, Border> map = new TreeMap<Integer, Border>(); int x[] = new int[1000000]; int y[] = new int[1000000]; char action[] = new char[1000000]; Scanner input = new Scanner (System.in); //Scanner input = new Scanner (new File("input.txt")); n = input.nextInt(); int q = input.nextInt(); for (int i = 0 ; i < q; i++){ x[i] = input.nextInt(); y[i] = input.nextInt(); action[i] = (char) input.next().charAt(0); } map.put(0,new Border(0, 0)); map.put(n+1, new Border(0,0)); for (int i = 0; i < q; i++){ if (action[i] == 'U'){ if (map.ceilingKey(x[i]) == x[i]){ System.out.println (0); continue; } int minY = (map.get(map.ceilingKey(x[i]))).upper; System.out.println (y[i]-minY); map.put(x[i], new Border(x[i], minY)); } else if (action[i] == 'L'){ if (map.floorKey(x[i]) == x[i]){ System.out.println (0); continue; } int minX = (map.get(map.floorKey(x[i]))).left; System.out.println (x[i]-minX); map.put(x[i], new Border(minX, y[i])); } } } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.util.TreeSet; import java.util.TreeMap; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.Map; import java.math.BigInteger; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.Set; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author shu_mj @ http://shu-mj.com */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int n = in.nextInt(); int q = in.nextInt(); TreeMap<Integer, Integer> X = new TreeMap<>(); TreeMap<Integer, Integer> Y = new TreeMap<>(); X.put(0, 0); Y.put(0, 0); Set<Integer> set = new TreeSet<>(); while (q-- > 0) { int x = in.nextInt(); int y = in.nextInt(); char c = in.next().charAt(0); if (set.contains(x)) { out.println(0); continue; } set.add(x); if (c == 'U') { Map.Entry<Integer, Integer> le = Y.floorEntry(y); out.println(y - le.getValue()); X.put(n - le.getValue() + 1, X.floorEntry(n - le.getValue() + 1).getValue()); X.put(x, x); } else { Map.Entry<Integer, Integer> ue = X.floorEntry(x); out.println(x - ue.getValue()); Y.put(n - ue.getValue() + 1, Y.floorEntry(n - ue.getValue() + 1).getValue()); Y.put(y, y); } } } } class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class Problem555C { private static class Tree { public Tree(int maxNodes) { value = new int[maxNodes]; leftIndex = new int[maxNodes]; value[0] = 0; leftIndex[0] = -1; curNode = 1; } public int getValue(int node, int low, int hi, int pos) { if (pos < low || pos > hi) { throw new Error(); } int answer = value[node]; int middle = (low + hi) / 2; if (leftIndex[node] != -1 && pos <= middle) { answer = Math.max(answer, getValue(leftIndex[node], low, middle, pos)); } if (leftIndex[node] != -1 && pos >= middle + 1) { answer = Math.max(answer, getValue(leftIndex[node] + 1, middle + 1, hi, pos)); } return answer; } public void set(int node, int low, int hi, int from, int to, int v) { if (to < low || from > hi) { return; } if (from <= low && hi <= to) { value[node] = Math.max(value[node], v); return; } int middle = (low + hi) / 2; if (leftIndex[node] == -1) { leftIndex[curNode] = leftIndex[curNode + 1] = -1; value[curNode] = value[curNode + 1] = value[node]; leftIndex[node] = curNode; curNode += 2; } set(leftIndex[node], low, middle, from, to, v); set(leftIndex[node] + 1, middle + 1, hi, from, to, v); } private int[] value; private int[] leftIndex; private int curNode; } private void solve() throws IOException { int n = nextInt(); int q = nextInt(); Tree up = new Tree(6100000); Tree left = new Tree(6100000); Set<Integer> taken = new HashSet<>(); for (int i = 0; i < q; i++) { int x = nextInt() - 1; int y = nextInt() - 1; if (next().charAt(0) == 'U') { if (!taken.contains(x)) { int r = up.getValue(0, 0, n, x); left.set(0, 0, n, r, y, x + 1); out.println(y - r + 1); } else { out.println(0); } taken.add(x); } else { if (!taken.contains(n - y - 1)) { int r = left.getValue(0, 0, n, y); up.set(0, 0, n, r, x, y + 1); out.println(x - r + 1); } else { out.println(0); } taken.add(n - y - 1); } } } public static void main(String[] args) { new Problem555C().run(); } private void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); in.close(); out.close(); } catch (IOException e) { System.exit(1); } } private int nextInt() throws IOException { return Integer.parseInt(next()); } private String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { String line = in.readLine(); if (line == null) { throw new IllegalStateException("EOF"); } stringTokenizer = new StringTokenizer(line); } return stringTokenizer.nextToken(); } private BufferedReader in; private PrintWriter out; private StringTokenizer stringTokenizer; }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 2000005; int val[1 << 19][2]; int tag[1 << 19][2]; int x[200005]; int x_val[200005]; int y[200005]; int y_val[200005]; int oper[200005]; struct node { int val; int id; friend bool operator<(const node &a, const node &b) { return a.val < b.val; } }; node a[200005]; map<int, bool> visit; void push_down(int num, int x) { if (tag[num][x] < val[num][x]) return; if (tag[num][x] > val[num * 2 + 1][x]) { val[num * 2 + 1][x] = tag[num][x]; tag[num * 2 + 1][x] = tag[num][x]; } if (tag[num][x] > val[num * 2 + 2][x]) { val[num * 2 + 2][x] = tag[num][x]; tag[num * 2 + 2][x] = tag[num][x]; } tag[num][x] = 0; } int query(int num, int l, int r, int k, int x) { if (l == r - 1) { return val[num][x]; } push_down(num, x); int mid = (l + r) / 2; if (k < mid) return query(num * 2 + 1, l, mid, k, x); else return query(num * 2 + 2, mid, r, k, x); } void change(int num, int l, int r, int l0, int r0, int t, int x) { if ((l0 <= l) && (r <= r0)) { tag[num][x] = max(tag[num][x], t); val[num][x] = max(val[num][x], t); return; } push_down(num, x); int mid = (l + r) / 2; if (l0 < mid) change(num * 2 + 1, l, mid, l0, r0, t, x); if (mid < r0) change(num * 2 + 2, mid, r, l0, r0, t, x); val[num][x] = min(val[num * 2 + 1][x], val[num * 2 + 2][x]); } int m; inline int finds(int x) { node b; b.val = x; return lower_bound(a, a + m, b) - a; } int main() { int n, q; scanf("%d%d", &n, &q); int i; for (i = 0; i < q; i++) { static char ope[5]; scanf("%d%d", &x[i], &y[i]); scanf("%s", ope); if (ope[0] == 'U') { oper[i] = 0; } else { oper[i] = 1; } if (!visit[x[i]]) { visit[x[i]] = true; a[i].val = x[i]; a[i].id = i; } else { a[i].val = n + 1; } } sort(a, a + q); memset(val, 0, sizeof(val)); memset(tag, 0, sizeof(tag)); for (i = 0; i < q; i++) { if (a[i].val > n) break; x_val[a[i].id] = i; y_val[a[i].id] = i; } m = i; for (i = 0; i < q; i++) { y_val[i] = m - y_val[i] - 1; } visit.clear(); for (i = 0; i < q; i++) { if (visit[x[i]]) { puts("0"); continue; } visit[x[i]] = true; if (oper[i] == 0) { int t = query(0, 0, m, x_val[i], 0); printf("%d\n", y[i] - t); change(0, 0, m, max(0, m - finds(n - t + 1) - 1), y_val[i], x[i], 1); } else { int t = query(0, 0, m, y_val[i], 1); printf("%d\n", x[i] - t); change(0, 0, m, finds(t), x_val[i], y[i], 0); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; struct node { int l, d; } v[200010]; struct data1 { int max, po; } f2[1000000]; struct data2 { int min, po; } f1[1000000]; const int MAX = 1000000000; int n, q, l[200010], r[200010], k, w[200010], t, back[200010]; bool b[200010]; char c; int comp(node x, node y) { return x.l < y.l; } void build(int l, int r, int k) { if (l == r) { f1[k].min = MAX; f1[k].po = MAX; } else { int mid = (l + r) / 2; build(l, mid, k * 2); build(mid + 1, r, k * 2 + 1); f1[k].min = MAX; f1[k].po = MAX; } } void pushdown1(int k) { if (f1[k].po != MAX) { f1[k * 2].min = min(f1[k * 2].min, f1[k].po); f1[k * 2 + 1].min = min(f1[k * 2 + 1].min, f1[k].po); f1[k * 2].po = min(f1[k * 2].po, f1[k].po); f1[k * 2 + 1].po = min(f1[k * 2 + 1].po, f1[k].po); f1[k].po = MAX; } } void pushdown2(int k) { if (f2[k].po) { f2[k * 2].po = max(f2[k * 2].po, f2[k].po); f2[k * 2 + 1].po = max(f2[k * 2 + 1].po, f2[k].po); f2[k * 2].max = max(f2[k * 2].max, f2[k].po); f2[k * 2 + 1].max = max(f2[k * 2 + 1].max, f2[k].po); f2[k].po = 0; } } void update1(int k) { f1[k].min = min(f1[k * 2].min, f1[k * 2 + 1].min); } void update2(int k) { f2[k].max = max(f2[k * 2].max, f2[k * 2 + 1].max); } int find1(int l, int r, int k, int t) { if (l == r) { return f1[k].min; } else { int mid = (l + r) / 2; pushdown1(k); if (t <= mid) return find1(l, mid, k * 2, t); else return find1(mid + 1, r, k * 2 + 1, t); update1(k); } } int find2(int l, int r, int k, int t) { if (l == r) { return f2[k].max; } else { int mid = (l + r) / 2; pushdown2(k); if (t <= mid) return find2(l, mid, k * 2, t); else return find2(mid + 1, r, k * 2 + 1, t); update2(k); } } void change2(int l, int r, int k, int ll, int rr, int t) { if (l == ll && r == rr) { f2[k].max = max(f2[k].max, t); f2[k].po = max(f2[k].po, t); return; } else { int mid = (l + r) / 2; pushdown2(k); if (rr <= mid) change2(l, mid, k * 2, ll, rr, t); else if (ll > mid) change2(mid + 1, r, k * 2 + 1, ll, rr, t); else { change2(l, mid, k * 2, ll, mid, t); change2(mid + 1, r, k * 2 + 1, mid + 1, rr, t); } update2(k); } } void change1(int l, int r, int k, int ll, int rr, int t) { if (l == ll && r == rr) { f1[k].min = min(f1[k].min, t); f1[k].po = min(f1[k].po, t); } else { int mid = (l + r) / 2; pushdown1(k); if (rr <= mid) change1(l, mid, k * 2, ll, rr, t); else if (ll > mid) change1(mid + 1, r, k * 2 + 1, ll, rr, t); else { change1(l, mid, k * 2, ll, mid, t); change1(mid + 1, r, k * 2 + 1, mid + 1, rr, t); } update1(k); } } int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= q; i++) { scanf("%d%d", &l[i], &r[i]); c = getchar(); if (c != 'U' && c != 'L') c = getchar(); if (c == 'U') w[i] = 1; else w[i] = 2; v[i].l = l[i]; v[i].d = i; } sort(v + 1, v + q + 1, comp); v[0].l = -1; for (int i = 1; i <= q; i++) if (v[i].l != v[i - 1].l) { t++; l[v[i].d] = t; back[t] = v[i].l; } else l[v[i].d] = t; build(1, t, 1); for (int i = 1; i <= q; i++) if (w[i] == 1) { if (b[l[i]]) { printf("%d\n", 0); continue; } b[l[i]] = 1; k = find1(1, t, 1, l[i]); if (k == MAX) printf("%d\n", r[i]); else printf("%d\n", r[i] - (n + 1 - back[k])); if (k != MAX) change2(1, t, 1, l[i], k, l[i]); else change2(1, t, 1, l[i], t, l[i]); } else { if (b[l[i]]) { printf("%d\n", 0); continue; } b[l[i]] = 1; k = find2(1, t, 1, l[i]); printf("%d\n", back[l[i]] - back[k]); if (k == 0) change1(1, t, 1, 1, l[i], l[i]); else change1(1, t, 1, k, l[i], l[i]); } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; set<pair<int, int> > s; set<pair<int, int> >::iterator it; int n, m; int x[maxn]; int y[maxn]; char c[maxn]; int main() { while (scanf("%d%d", &n, &m) != EOF) { s.clear(); memset(x, 0, sizeof(x)); memset(y, 0, sizeof(y)); memset(c, 0, sizeof(c)); s.insert(make_pair(0, 0)); s.insert(make_pair(n + 1, m + 1)); for (int i = 1; i <= m; i++) { scanf("%d %d %c", &x[i], &y[i], &c[i]); if (c[i] == 'U') { it = s.lower_bound(make_pair(x[i], -1)); if (it->first == x[i]) printf("0\n"); else { s.insert(make_pair(x[i], i)); printf("%d\n", y[i] - y[it->second]); y[i] = y[it->second]; } } else { it = s.upper_bound(make_pair(x[i], m + 1)); it--; if (it->first == x[i]) printf("0\n"); else { s.insert(make_pair(x[i], i)); printf("%d\n", x[i] - x[it->second]); x[i] = x[it->second]; } } } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; char c; int x, y, n, m; map<int, int> f; map<int, int>::iterator t; int main() { scanf("%d%d", &n, &m); for (f[0] = 0, f[n + 1] = 1 << 31; m--;) { scanf("%d%d %c", &x, &y, &c); t = f.lower_bound(x); if (t->first == x) { puts("0"); continue; } if (c == 'L') --t, c = -1; else c = 0; n = abs(t->first - x); if (t->second >> 31 == c) n += t->second; printf("%d\n", n & 1073741823); if (c) n |= 1 << 31; f[x] = n; } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int mr[1000050]; int mc[1000050]; int qx[1000050], qy[1000050]; map<int, int> mpx, mpy; void build(int l, int r, int i) { mr[i] = mc[i] = -1; if (l != r) { int mid = (l + r) >> 1; build(l, mid, i << 1); build(mid + 1, r, i << 1 | 1); } } void update(int l, int r, int pl, int pr, int va, int ty, int i) { if (l >= pl && r <= pr) { if (ty == 0) { if (mr[i] == -1) mr[i] = va; else mr[i] = ((mr[i]) < (va) ? (mr[i]) : (va)); } else { mc[i] = ((mc[i]) > (va) ? (mc[i]) : (va)); } return; } int mid = (l + r) >> 1; if (pl <= mid) update(l, mid, pl, pr, va, ty, i << 1); if (pr > mid) update(mid + 1, r, pl, pr, va, ty, i << 1 | 1); } int ans; void query(int l, int r, int p, int ty, int i) { if (ty == 0) { if (ans == -1) ans = mr[i]; else if (mr[i] != -1 && mr[i] < ans) ans = mr[i]; } else { ans = ((ans) > (mc[i]) ? (ans) : (mc[i])); } if (l == r) { return; } int mid = (l + r) >> 1; if (p <= mid) query(l, mid, p, ty, i << 1); else query(mid + 1, r, p, ty, i << 1 | 1); } int n, q, m; struct node { int x, y; char c; } s[1000050]; void init() { int tailx = 0, taily = 0; scanf("%d%d", &n, &q); for (int i = 0; i < q; ++i) { scanf("%d%d %c", &s[i].y, &s[i].x, &s[i].c); qx[tailx++] = s[i].x; qy[taily++] = s[i].y; } sort(qx, qx + tailx); sort(qy, qy + taily); tailx = unique(qx, qx + tailx) - qx; taily = unique(qy, qy + taily) - qy; m = tailx; build(1, m, 1); } void work() { for (int i = 0; i < q; ++i) { int x, y; char c; x = s[i].x, y = s[i].y, c = s[i].c; if (c == 'U') { ans = -1; int xx = lower_bound(qx, qx + m, x) - qx; xx++; query(1, m, xx, 1, 1); if (ans == -1) printf("%d\n", x); else printf("%d\n", x - qx[ans - 1]); if (ans == -1) ans = 1; update(1, m, ans, xx, xx, 0, 1); update(1, m, xx, xx, xx, 1, 1); } else { ans = -1; int xx = lower_bound(qx, qx + m, x) - qx; xx++; query(1, m, xx, 0, 1); if (ans == -1) printf("%d\n", n - x + 1); else printf("%d\n", qx[ans - 1] - x); if (ans == -1) ans = m; update(1, m, xx, ans, xx, 1, 1); update(1, m, xx, xx, xx, 0, 1); } } } int main() { init(); work(); }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; map<int, int> xg, yg; map<int, int>::iterator it; int n, q; int x, y; string s; int main() { cin >> n >> q; for (int i = 1; i <= q; i++) { cin >> x >> y >> s; if (s == "U") { it = xg.lower_bound(x); if (it->first == x) { cout << "0" << endl; continue; } else if (it == xg.end()) { xg[x] = y; } else { xg[x] = it->first - x + it->second; } cout << xg[x] << endl; yg[y] = 0; } else { it = yg.lower_bound(y); if (it->first == y) { cout << "0" << endl; continue; } else if (it == yg.end()) { yg[y] = x; } else { yg[y] = it->first - y + it->second; } cout << yg[y] << endl; xg[x] = 0; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; template <typename T> class segtree { private: long long n, sz; vector<T> node, min_val, second_val, lazy; vector<long long> count; vector<bool> lazyFlag; void update(long long id) { node[id] = node[2 * id + 1] + node[2 * id + 2]; if (min_val[2 * id + 1] < min_val[2 * id + 2]) { min_val[id] = min_val[2 * id + 1]; second_val[id] = min(second_val[2 * id + 1], min_val[2 * id + 2]); count[id] = count[2 * id + 1]; } else if (min_val[2 * id + 1] > min_val[2 * id + 2]) { min_val[id] = min_val[2 * id + 2]; second_val[id] = min(min_val[2 * id + 1], second_val[2 * id + 2]); count[id] = count[2 * id + 2]; } else { min_val[id] = min_val[2 * id + 1]; second_val[id] = min(second_val[2 * id + 1], second_val[2 * id + 2]); count[id] = count[2 * id + 1] + count[2 * id + 2]; } } public: segtree(vector<T>& v) : sz((long long)v.size()) { n = 1; while (n < sz) { n *= 2; } node.resize(2 * n, 0); lazy.resize(2 * n, 0); lazyFlag.resize(2 * n, false); min_val.resize(2 * n, numeric_limits<T>::max()); second_val.resize(2 * n, numeric_limits<T>::max()); count.resize(2 * n, 1); for (long long i = 0; i < sz; i++) { node[i + n - 1] = min_val[i + n - 1] = v[i]; } for (long long i = n - 2; i >= 0; i--) { update(i); } } void eval(long long k, long long l, long long r) { if (lazyFlag[k]) { if (lazy[k] > min_val[k]) { node[k] += (lazy[k] - min_val[k]) * count[k]; min_val[k] = lazy[k]; if (r - l > 1) { lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k]; lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true; } } lazyFlag[k] = false; } } void range(long long a, long long b, T x, long long k = 0, long long l = 0, long long r = -1) { if (r < 0) r = n; eval(k, l, r); if (b <= l || r <= a || min_val[k] >= x) { return; } if (a <= l && r <= b && second_val[k] > x) { lazy[k] = x; lazyFlag[k] = true; eval(k, l, r); } else { range(a, b, x, 2 * k + 1, l, (l + r) / 2); range(a, b, x, 2 * k + 2, (l + r) / 2, r); update(k); } } T query(long long a, long long b, long long k = 0, long long l = 0, long long r = -1) { if (r < 0) r = n; eval(k, l, r); if (b <= l || r <= a) { return 0; } if (a <= l && r <= b) { return node[k]; } T vl = query(a, b, 2 * k + 1, l, (l + r) / 2); T vr = query(a, b, 2 * k + 2, (l + r) / 2, r); return vl + vr; } void print() { for (long long i = 0; i < sz; i++) { cout << query(i, i + 1) << " "; } cout << endl; } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); long long N, Q; cin >> N >> Q; vector<long long> V(Q + 3, 0); segtree<long long> X(V); segtree<long long> Y(V); vector<long long> res; vector<pair<pair<long long, long long>, char> > A(Q); for (long long i = 0; i < Q; i++) { cin >> A[i].first.first >> A[i].first.second >> A[i].second; } long long x, y; char d; set<long long> s; s.insert(0); map<long long, long long> mpX; for (long long i = 0; i < Q; i++) { s.insert(A[i].first.first); } long long z = 0; vector<long long> revX; for (auto i : s) { mpX[i] = z; revX.push_back(i); z++; } for (long long i = 0; i < Q; i++) { A[i].first.first = mpX[A[i].first.first]; } s.clear(); s.insert(0); map<long long, long long> mpY; for (long long i = 0; i < Q; i++) { s.insert(A[i].first.second); } z = 0; vector<long long> revY; for (auto i : s) { mpY[i] = z; revY.push_back(i); z++; } for (long long i = 0; i < Q; i++) { A[i].first.second = mpY[A[i].first.second]; } for (long long i = 0; i < Q; i++) { x = A[i].first.first; y = A[i].first.second; d = A[i].second; if (d == 'U') { long long z = X.query(x, x + 1); res.push_back(revY[y] - revY[z]); Y.range(z, y + 1, x); X.range(x, x + 1, y); } else { long long z = Y.query(y, y + 1); res.push_back(revX[x] - revX[z]); X.range(z, x + 1, y); Y.range(y, y + 1, x); } } for (long long i = 0; i < Q; i++) { cout << res[i] << endl; } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 250000; struct node { node *l, *r; int ll, rr, m, pr; }; struct node *new_tree(int l, int r) { struct node *a; a = new (struct node); a->m = 0; a->pr = 0; a->ll = l; a->rr = r; if (r - l > 1) { a->l = new_tree(l, (r + l) / 2); a->r = new_tree((r + l) / 2, r); } return a; }; void push(struct node *a) { a->m = max(a->m, a->pr); if (a->rr - a->ll > 1) { a->l->pr = max(a->l->pr, a->pr); a->r->pr = max(a->r->pr, a->pr); } } void update(struct node *a, int l, int r, int c) { push(a); if (a->rr == r && a->ll == l) { a->pr = c; return; } int m = (a->ll + a->rr) / 2; if (r <= m) update(a->l, l, r, c); if (m <= l) update(a->r, l, r, c); if (r > m && m > l) { update(a->l, l, m, c); update(a->r, m, r, c); } } int get(struct node *a, int p) { push(a); if (a->rr - a->ll == 1) return a->m; int m = (a->ll + a->rr) / 2; if (p < m) return get(a->l, p); else return get(a->r, p); } int main() { int n, q; scanf("%d %d", &n, &q); int x[MAXN], y[MAXN]; char d[MAXN]; pair<int, int> t[MAXN]; for (int i = 0; i < q; i++) { scanf("%d %d %c", &x[i], &y[i], &d[i]); t[i] = make_pair(x[i], i); } t[q] = make_pair(0, q); t[q + 1] = make_pair(n + 1, q + 1); sort(t, t + q + 2); map<int, int> u; int v[MAXN]; for (int i = 0; i < q + 2; i++) { u[t[i].first] = i; v[i] = t[i].first; } node *treev, *treeh; treev = new_tree(0, q + 2); treeh = new_tree(0, q + 2); int a, b, c; for (int i = 0; i < q; i++) { a = u[x[i]]; b = q - a + 1; if (d[i] == 'L') { c = get(treeh, b); printf("%d\n", v[a] - v[c]); if (c != a) update(treev, c + 1, a + 1, b); update(treeh, b, b + 1, a); } else { c = get(treev, a); printf("%d\n", v[q + 1 - c] - v[a]); if (c != b) update(treeh, c + 1, b + 1, a); update(treev, a, a + 1, b); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int t[400100], sz; int e[400100][3]; int n, q; map<int, int> vis; struct Seg { int v[400100 << 2]; void up(int o) { v[o] = min(v[(o << 1)], v[((o << 1) | 1)]); } void build(int o, int l, int r) { v[o] = 0x3f3f3f3f; if (l == r) return; build((o << 1), l, ((l + r) >> 1)); build(((o << 1) | 1), ((l + r) >> 1) + 1, r); } void update(int o, int l, int r, int pos, int val) { if (l == r) { v[o] = val; return; } if (pos <= ((l + r) >> 1)) update((o << 1), l, ((l + r) >> 1), pos, val); else update(((o << 1) | 1), ((l + r) >> 1) + 1, r, pos, val); up(o); } int query(int o, int l, int r, int y1, int y2) { if (y1 <= l && y2 >= r) return v[o]; int ans = 0x3f3f3f3f; if (y1 <= ((l + r) >> 1)) ans = min(ans, query((o << 1), l, ((l + r) >> 1), y1, y2)); if (y2 > ((l + r) >> 1)) ans = min(ans, query(((o << 1) | 1), ((l + r) >> 1) + 1, r, y1, y2)); return ans; } } sg[2]; int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= q; ++i) { scanf("%d%d", &e[i][0], &e[i][1]); t[++sz] = e[i][0]; t[++sz] = e[i][1]; char typ[5]; scanf("%s", typ); e[i][2] = (*typ == 'U'); } sort(t + 1, t + sz + 1); sz = unique(t + 1, t + sz + 1) - t - 1; sg[0].build(1, 1, sz); sg[1].build(1, 1, sz); for (int i = 1; i <= q; ++i) { if (vis[e[i][0]]) { puts("0"); continue; } vis[e[i][0]] = 1; int x = lower_bound(t + 1, t + sz + 1, e[i][0]) - t; int y = lower_bound(t + 1, t + sz + 1, e[i][1]) - t; if (e[i][2]) { int lb = 0, ub = y; while (ub > lb + 1) { int MID = (lb + ub) >> 1; if (sg[0].query(1, 1, sz, MID, y) > e[i][0]) ub = MID; else lb = MID; } printf("%d\n", e[i][1] - t[lb]); sg[1].update(1, 1, sz, x, t[lb] + 1); } else { int lb = 0, ub = x; while (ub > lb + 1) { int MID = (lb + ub) >> 1; if (sg[1].query(1, 1, sz, MID, x) > e[i][1]) ub = MID; else lb = MID; } printf("%d\n", e[i][0] - t[lb]); sg[0].update(1, 1, sz, y, t[lb] + 1); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; typedef struct node { int leftEnd; int rightEnd; int ans; node* leftChild; node* rightChild; } node; int a[2 * 200001]; pair<pair<int, int>, int> queries[200001]; vector<int> compressedValues; int CompressCoordinate(int a[], int n) { vector<int> temp; for (int i = 1; i <= n; i++) temp.push_back(a[i]); sort(temp.begin(), temp.end()); compressedValues.push_back(0); compressedValues.push_back(temp[0]); for (int i = 1; i < temp.size(); i++) if (temp[i] != temp[i - 1]) compressedValues.push_back(temp[i]); return compressedValues.size() - 1; } int GetCompressedValue(int a) { return lower_bound(compressedValues.begin(), compressedValues.end(), a) - compressedValues.begin(); } int GetOriginalValue(int a) { return compressedValues[a]; } node* CreateNode(int leftEnd, int rightEnd, int ans) { node* temp = new node; temp->leftEnd = leftEnd; temp->rightEnd = rightEnd; temp->ans = ans; return temp; } node* BuildSkeletonSegmentTree(int leftEnd, int rightEnd) { node* temp = CreateNode(leftEnd, rightEnd, 0); if (leftEnd != rightEnd) { int mid = (leftEnd + rightEnd) / 2; temp->leftChild = BuildSkeletonSegmentTree(leftEnd, mid); temp->rightChild = BuildSkeletonSegmentTree(mid + 1, rightEnd); } else temp->leftChild = temp->rightChild = NULL; return temp; } void TraverseSegmentTree(node* root) { if (root != NULL) { TraverseSegmentTree(root->leftChild); TraverseSegmentTree(root->rightChild); printf("%d %d\n", root->leftEnd, root->rightEnd); } } void UpdateNode(node* currentNode, int l, int r, int x) { if (r < currentNode->leftEnd || currentNode->rightEnd < l) { return; } else { l = max(l, currentNode->leftEnd); r = min(r, currentNode->rightEnd); if (l == currentNode->leftEnd && r == currentNode->rightEnd) currentNode->ans = max(x, currentNode->ans); else { UpdateNode(currentNode->leftChild, l, r, x); UpdateNode(currentNode->rightChild, l, r, x); } } } int QueryNode(node* currentNode, int l, int r, int x) { if (r < currentNode->leftEnd || currentNode->rightEnd < l) { currentNode->ans = max(currentNode->ans, x); return 0; } else { l = max(l, currentNode->leftEnd); r = min(r, currentNode->rightEnd); if (l == currentNode->leftEnd && r == currentNode->rightEnd) return currentNode->ans = max(x, currentNode->ans); else return max( QueryNode(currentNode->leftChild, l, r, max(x, currentNode->ans)), QueryNode(currentNode->rightChild, l, r, max(x, currentNode->ans))); } } int main() { int n, m, q, i, j, k, x, y, z, t, x1, y1, x2, y2, ans; set<int> b; char s[2]; node *root1, *root2; scanf("%d%d", &n, &q); for (i = 1, j = 1; i <= q; i++) { scanf("%d%d", &queries[i].first.first, &queries[i].first.second); scanf("%s", s); queries[i].second = (s[0] == 'U'); a[j++] = queries[i].first.first; a[j++] = queries[i].first.second; } a[j] = n + 1; m = CompressCoordinate(a, 2 * q + 1); root1 = BuildSkeletonSegmentTree(0, m); root2 = BuildSkeletonSegmentTree(0, m); for (x = 1; x <= q; x++) { i = GetCompressedValue(queries[x].first.first); j = GetCompressedValue(queries[x].first.second); t = queries[x].second; if (t == 1) { k = QueryNode(root1, j, j, 0); x1 = GetCompressedValue(n + 1 - GetOriginalValue(j)); x2 = GetCompressedValue(n + 1 - GetOriginalValue(k)); UpdateNode(root2, x1, x2, i); ans = GetOriginalValue(j) - GetOriginalValue(k); } else { k = QueryNode(root2, i, i, 0); y1 = GetCompressedValue(n + 1 - GetOriginalValue(i)); y2 = GetCompressedValue(n + 1 - GetOriginalValue(k)); UpdateNode(root1, y1, y2, j); ans = GetOriginalValue(i) - GetOriginalValue(k); } if (b.find(i) != b.end()) ans = 0; else b.insert(i); printf("%d\n", ans); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const long long N = 400010; char t[10]; map<long long, long long> mp[3]; long long n, q, col; long long s[N], xx[N], yy[N], op[N], ff[N], gg[N]; struct Point { long long x, op, id; } X[N], Y[N]; inline bool cmp(Point A, Point B) { return A.x < B.x; } struct SegmentTree { long long tag[N << 2], mx[N << 2]; inline void pushdown(long long u) { if (!tag[u]) return; mx[(u << 1)] = max(mx[(u << 1)], tag[u]); mx[(u << 1 | 1)] = max(mx[(u << 1 | 1)], tag[u]); tag[(u << 1)] = max(tag[(u << 1)], tag[u]); tag[(u << 1 | 1)] = max(tag[(u << 1 | 1)], tag[u]), tag[u] = 0; } inline void change(long long u, long long l, long long r, long long L, long long R, long long v) { if (l >= L && r <= R) { mx[u] = max(mx[u], v); tag[u] = max(tag[u], v); return; } long long mid = l + r >> 1; pushdown(u); if (L <= mid) change((u << 1), l, mid, L, R, v); if (mid < R) change((u << 1 | 1), mid + 1, r, L, R, v); mx[u] = max(mx[(u << 1)], mx[(u << 1 | 1)]); } inline long long query(long long u, long long l, long long r, long long pos) { if (l == r) return mx[u]; long long mid = l + r >> 1; pushdown(u); if (pos <= mid) return query((u << 1), l, mid, pos); else return query((u << 1 | 1), mid + 1, r, pos); } } R, C; inline void calcX(long long x) { xx[X[x].id] = col; op[X[x].id] = X[x].op; ff[col] = X[x].x; } inline void calcY(long long x) { yy[Y[x].id] = col; op[Y[x].id] = Y[x].op; gg[col] = Y[x].x; } signed main() { cin >> n >> q; for (long long i = 1; i <= q; i++) { long long x, y; char t; cin >> x >> y >> t; if (t == 'U') X[i] = (Point){x, 1, i}, Y[i] = (Point){y, 1, i}; if (t == 'L') X[i] = (Point){x, 2, i}, Y[i] = (Point){y, 2, i}; } sort(X + 1, X + q + 1, cmp), col++, calcX(1); for (long long i = 2; i <= q; i++) { if (X[i].x > X[i - 1].x) ++col; calcX(i); } long long m = col; col = 0; sort(Y + 1, Y + q + 1, cmp), col++, calcY(1); for (long long i = 2; i <= q; i++) { if (Y[i].x > Y[i - 1].x) col++; calcY(i); } m = max(m, col) + 100000; for (long long i = 1; i <= q; i++) { if (op[i] == 1) { if (mp[1][xx[i]]) { puts("0"); continue; } long long ls = R.query(1, 1, m, xx[i]); mp[1][xx[i]] = 1; C.change(1, 1, m, ls + 1, yy[i], xx[i]); cout << n - ff[xx[i]] + 1 - gg[ls] << endl; } if (op[i] == 2) { if (mp[2][yy[i]]) { puts("0"); continue; } long long ls = C.query(1, 1, m, yy[i]); mp[2][yy[i]] = 1; R.change(1, 1, m, ls + 1, xx[i], yy[i]); cout << n - gg[yy[i]] + 1 - ff[ls] << endl; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const int B = 262144; struct BIT { int tr[2 * B]; int modified[2 * B]; int mod_val[2 * B]; void push(int nd) { if (!modified[nd]) return; tr[nd] = max(tr[nd], mod_val[nd]); int mv = mod_val[nd]; mod_val[nd] = modified[nd] = 0; if (nd >= B) return; mod_val[2 * nd] = max(mod_val[2 * nd], mv); mod_val[2 * nd + 1] = max(mod_val[2 * nd + 1], mv); modified[2 * nd] = modified[2 * nd + 1] = 1; return; } void upd(int nd, int ndl, int ndr, int l, int r, int val) { if (l > r || ndl > r || l > ndr) return; else if (l <= ndl && ndr <= r) { modified[nd] = 1; mod_val[nd] = max(mod_val[nd], val); return; } else { tr[nd] = max(tr[nd], val); int mid = (ndl + ndr) / 2; upd(2 * nd, ndl, mid, l, r, val); upd(2 * nd + 1, mid + 1, ndr, l, r, val); } } void update_node(int nd) { if (nd > 1) update_node(nd / 2); push(nd); } int get(int i) { update_node(B + i); return tr[B + i]; } } R, C; set<int> Upos; map<int, int> Uind; set<int> Lpos; map<int, int> Lind; pair<pair<int, int>, char> query[B]; int zeroq[B]; int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 0; i < q; i++) { int x, y; char tp; scanf("%d %d %c", &x, &y, &tp); query[i] = make_pair(make_pair(x, y), tp); if (tp == 'U') { if (Upos.find(x) != Upos.end()) { zeroq[i] = 1; continue; } Upos.insert(x); } else { if (Lpos.find(y) != Lpos.end()) { zeroq[i] = 1; continue; } Lpos.insert(y); } } Upos.insert(0); Lpos.insert(0); int i = 0; for (auto &x : Upos) { Uind[x] = i; i++; } i = 0; for (auto &x : Lpos) { Lind[x] = i; i++; } for (int i = 0; i < q; i++) { if (zeroq[i]) { puts("0"); continue; } int x = query[i].first.first; int y = query[i].first.second; if (query[i].second == 'U') { int M = C.get(Uind[x]); printf("%d\n", y - M); auto it = Lind.upper_bound(y); if (it == Lind.begin()) continue; it--; R.upd(1, 0, B - 1, Lind[M], it->second, x); } else { int M = R.get(Lind[y]); printf("%d\n", x - M); auto it = Uind.upper_bound(x); if (it == Uind.begin()) continue; it--; C.upd(1, 0, B - 1, Uind[M], it->second, y); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.util.TreeSet; import java.util.HashSet; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Comparator; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author Artem Gilmudinov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Reader in = new Reader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { class Pair { public int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return String.format("{%d %d}", x, y); } } public void solve(int testNumber, Reader in, PrintWriter out) { int n, q; n = in.ni(); q = in.ni(); HashSet<Integer> used = new HashSet<>(); Comparator<Pair> cmp = new Comparator<Pair>() { public int compare(Pair o1, Pair o2) { return o1.x - o2.x; } }; TreeSet<Pair> setX = new TreeSet<Pair>(cmp); TreeSet<Pair> setY = new TreeSet<Pair>(cmp); for(int i = 0; i < q; i++) { int x, y; y = in.ni(); x = in.ni(); String dir = in.next(); if(used.contains(x)) { out.println(0); continue; } used.add(x); if(dir.equals("L")) { Pair cur = new Pair(x, 0); Pair pairDown = setY.lower(cur); Pair pairUp = setY.higher(cur); int border = 0; if(pairUp != null) { border = pairUp.y; } if(pairDown != null) { if(pairDown.y < y) { border = Math.max(border, pairDown.y); } } Pair hor = setX.lower(new Pair(y, 0)); if(hor != null) { if(pairUp != null) { if(hor.x > n - pairUp.x + 1) { border = Math.max(border, hor.x); } } else { border = Math.max(border, hor.x); } } out.println(y - border); setY.add(new Pair(x, border)); } else { Pair cur = new Pair(y, 0); Pair pairUp = setX.higher(cur); Pair pairDown = setX.lower(cur); int border = 0; if(pairUp != null) { border = pairUp.y; } if(pairDown != null) { if(pairDown.y < x) { border = Math.max(border, pairDown.y); } } Pair hor = setY.lower(new Pair(x, 0)); if(hor != null) { if(pairUp != null) { if(hor.x > n - pairUp.x + 1) { border = Math.max(border, hor.x); } } else { border = Math.max(border, hor.x); } } out.println(x - border); setX.add(new Pair(y, border)); } } } } class Reader { private BufferedReader in; private StringTokenizer st = new StringTokenizer(""); private String delim = " "; public Reader(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } public String next() { if (!st.hasMoreTokens()) { st = new StringTokenizer(rl()); } return st.nextToken(delim); } public String rl() { try { return in.readLine(); } catch(IOException e) { throw new RuntimeException(e); } } public int ni() { return Integer.parseInt(next()); } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, m; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; set<pair<int, pair<int, bool> > > eaten; set<pair<int, pair<int, bool> > >::iterator it; int x, y, ans; char d; bool dr; eaten.insert(make_pair(0, make_pair(n, 1))); eaten.insert(make_pair(n + 1, make_pair(n, 0))); map<int, bool> mark; while (m--) { cin >> x >> y >> d; if (mark[x]) { cout << 0 << '\n'; continue; } it = eaten.lower_bound(make_pair(x, make_pair(0, 0))); if (d == 'U') { if (it->second.second) ans = it->second.first + it->first - x; else ans = it->first - x; dr = 1; } else { it--; if (it->second.second) ans = x - it->first; else ans = it->second.first + x - it->first; dr = 0; } mark[x] = 1; cout << ans << '\n'; eaten.insert(make_pair(x, make_pair(ans, dr))); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, m; struct wen { int x, y, opt; } a[400005]; int u[400005]; struct aa { int l, r, x; } c[400005 << 2], d[400005 << 2]; void jianshu(aa *c, int i, int l, int r) { int mid = (l + r) >> 1; c[i].l = l; c[i].r = r; if (l == r) { return; } jianshu(c, (i << 1), l, mid); jianshu(c, ((i << 1) | 1), mid + 1, r); } namespace lsh { map<int, int> p; int t[400005], f[400005]; void QAQ() { int nn = 0; for (int i = 1; i <= m; i++) { nn++; t[nn] = a[i].x; nn++; t[nn] = a[i].y; } sort(t + 1, t + nn + 1); for (int i = 1; i <= nn; i++) if (t[i] == t[i - 1]) f[i] = f[i - 1]; else f[i] = f[i - 1] + 1; for (int i = 1; i <= nn; i++) { p[t[i]] = f[i]; u[f[i]] = t[i]; } n = f[nn]; int i = 1; for (i = 1; i <= m; i++) { a[i].x = p[a[i].x]; a[i].y = p[a[i].y]; } } } // namespace lsh void rd() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int x, y; string opt; scanf("%d%d", &x, &y); cin >> opt; a[i].x = x; a[i].y = y; if (opt == "U") a[i].opt = 1; else a[i].opt = 2; } lsh::QAQ(); } void gai(aa *c, int i, int l, int r, int x) { if (l <= c[i].l && c[i].r <= r) { c[i].x = max(c[i].x, x); return; } if (l > c[i].r || r < c[i].l) return; gai(c, (i << 1), l, r, x); gai(c, ((i << 1) | 1), l, r, x); } int qiu(aa *c, int i, int l) { if (c[i].l == c[i].r) return c[i].x; int mid = (c[i].l + c[i].r) >> 1; if (l > mid) return max(c[i].x, qiu(c, ((i << 1) | 1), l)); else return max(c[i].x, qiu(c, (i << 1), l)); } int main() { rd(); jianshu(c, 1, 1, n); jianshu(d, 1, 1, n); for (int i = 1; i <= m; i++) { if (a[i].opt == 1) { int p = qiu(d, 1, a[i].x); printf("%d\n", u[a[i].y] - u[p]); gai(c, 1, p + 1, a[i].y, a[i].x); gai(d, 1, a[i].x, a[i].x, a[i].y); } else { int p = qiu(c, 1, a[i].y); printf("%d\n", u[a[i].x] - u[p]); gai(d, 1, p + 1, a[i].x, a[i].y); gai(c, 1, a[i].y, a[i].y, a[i].x); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int BASE = 1 << 18; const int INF = 3e5 + 7; struct query { char direction; int x, y; int num, numX, numY; int answer; query(char c = ' ', int a = 0, int b = 0, int d = 0) { direction = c; x = a; y = b; num = d; numX = numY = answer = 0; } }; char c; int a, b, n, k; int X[1000000]; int Y[1000000]; int TreeX[2000000]; int TreeY[2000000]; vector<query> Queries; vector<query*> V; set<pair<int, int> > S; bool compX(query* q1, query* q2) { if (q1->x > q2->x) return 0; if (q1->x < q2->x) return 1; if (q1->num < q2->num) return 0; if (q1->num > q2->num) return 1; } bool compY(query* q1, query* q2) { if (q1->y > q2->y) return 0; if (q1->y < q2->y) return 1; if (q1->num < q2->num) return 0; if (q1->num > q2->num) return 1; } void Add(int, int, int, int, int, int, int*); int Result(int, int, int, int, int, int*); int main() { for (int i = 0; i < 1000000; i++) { TreeX[i] = TreeY[i] = INF; } scanf("%d %d ", &n, &k); for (int i = 1; i <= k; i++) { scanf(" %d %d %c", &a, &b, &c); Queries.push_back(query(c, a, b, i)); } for (int i = 0; i < k; i++) { V.push_back(&Queries[i]); } sort(V.begin(), V.end(), compX); for (int i = 0; i < k; i++) { V[i]->numX = i + 1; X[V[i]->numX] = V[i]->num; } sort(V.begin(), V.end(), compY); for (int i = 0; i < k; i++) { V[i]->numY = i + 1; Y[V[i]->numY] = V[i]->num; } for (int i = 0; i < k; i++) { int block = 0; int sz = (int)(S).size(); S.insert(make_pair(Queries[i].x, Queries[i].y)); if ((int)(S).size() == sz) { Queries[i].answer = 0; continue; } if (Queries[i].direction == 'U') { block = X[Result(1, 1, BASE, Queries[i].numX, Queries[i].numX, TreeX)]; if (block == 0) { Queries[i].answer = Queries[i].y; Add(1, 1, BASE, 1, Queries[i].numY, Queries[i].numY, TreeY); } else { Queries[i].answer = Queries[i].y - Queries[block - 1].y; Add(1, 1, BASE, Queries[block - 1].numY + 1, Queries[i].numY, Queries[i].numY, TreeY); } } else { block = Y[Result(1, 1, BASE, Queries[i].numY, Queries[i].numY, TreeY)]; if (block == 0) { Queries[i].answer = Queries[i].x; Add(1, 1, BASE, 1, Queries[i].numX, Queries[i].numX, TreeX); } else { Queries[i].answer = Queries[i].x - Queries[block - 1].x; Add(1, 1, BASE, Queries[block - 1].numX + 1, Queries[i].numX, Queries[i].numX, TreeX); } } } for (int i = 0; i < k; i++) { printf("%d\n", Queries[i].answer); } } void Add(int i, int l, int r, int a, int b, int val, int* Tree) { if (a <= l && r <= b) { Tree[i] = min(Tree[i], val); } else { int mid = (l + r) / 2; if (a <= mid) { Add(i * 2, l, mid, a, b, val, Tree); } if (mid + 1 <= b) { Add(i * 2 + 1, mid + 1, r, a, b, val, Tree); } } } int Result(int i, int l, int r, int a, int b, int* Tree) { if (a <= l && r <= b) { return Tree[i]; } else { int res = Tree[i]; int mid = (l + r) / 2; if (a <= mid) { res = min(res, Result(i * 2, l, mid, a, b, Tree)); } if (mid + 1 <= b) { res = min(res, Result(i * 2 + 1, mid + 1, r, a, b, Tree)); } return res; } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; inline long double min(long double a, long double b) { if (a < b) return a; return b; } inline long double max(long double a, long double b) { if (a < b) return b; return a; } int n, q; int x[200010]; int y[200010]; map<int, int> m; unordered_set<int> uset; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; char c; x[q] = y[q] = 0; m[0] = q; m[n + 1] = q; for (int(i) = (0); (i) < (q); ++(i)) { cin >> x[i] >> y[i] >> c; if (uset.count(x[i])) { cout << 0 << endl; continue; } uset.insert(x[i]); auto j = m.lower_bound(x[i]); m[x[i]] = i; if (c == 'U') { int index = j->second; int val = y[i] - y[index]; y[i] = y[index]; cout << val << endl; } else { j--; j--; int index = j->second; int val = x[i] - x[index]; x[i] = x[index]; cout << val << endl; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; struct tree { int bit[4 * 200111], u[4 * 200111], v[4 * 200111]; void init(int i, int x, int y) { bit[i] = 1000000007; u[i] = x; v[i] = y; if (x == y) return; int mid = (x + y) >> 1; init(i << 1, x, mid); init(i << 1 | 1, mid + 1, y); } int get(int i, int x) { if (u[i] > x || v[i] < x) return 1000000007; if (u[i] == x && v[i] == x) return bit[i]; return min(bit[i], min(get(i << 1, x), get(i << 1 | 1, x))); } void up(int i, int x, int y, int t) { if (u[i] > y || v[i] < x) return; if (x <= u[i] && v[i] <= y) { bit[i] = min(bit[i], t); return; } up(i << 1, x, y, t); up(i << 1 | 1, x, y, t); } } A, B; int u[200111], v[200111]; char c[200111]; vector<int> luu; map<int, int> re; int main() { int n, m; scanf("%d%d\n", &n, &m); for (int i = 0, _b = (m); i < _b; ++i) { scanf("%d %d %c\n", &u[i], &v[i], &c[i]); luu.push_back(u[i]); } sort(luu.begin(), luu.end()); int N = 0; re[luu[0]] = ++N; for (int i = (1), _b = (m - 1); i <= _b; ++i) if (luu[i] != luu[i - 1]) re[luu[i]] = ++N; A.init(1, 1, N); B.init(1, 1, N); int x, y, tam, a, b; A.up(1, 1, N, n + 1); B.up(1, 1, N, n + 1); for (int i = 0, _b = (m); i < _b; ++i) { if (c[i] == 'U') { y = re[u[i]]; tam = max(B.get(1, y), u[i]); printf("%d\n", tam - u[i]); b = N - y + 1; if (tam == n + 1) a = 1; else a = N + 1 - re[tam]; B.up(1, y, y, 0); A.up(1, a, b, n + 1 - u[i]); } else { y = N + 1 - re[u[i]]; tam = max(A.get(1, y), n + 1 - u[i]); printf("%d\n", tam - n + u[i] - 1); b = N - y + 1; if (tam == n + 1) a = 1; else a = re[n + 1 - tam]; A.up(1, y, y, 0); B.up(1, a, b, u[i]); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.util.*; import java.io.*; public class CaseOfChocolate { public static void main(String[] args) throws Exception { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int q = in.nextInt(); TreeSet<Integer> tsc = new TreeSet<Integer>(); TreeSet<Integer> tsr = new TreeSet<Integer>(); int[] xs = new int[q]; int[] ys = new int[q]; char[] dirs = new char[q]; for(int x = 0; x < q; x++) { xs[x] = in.nextInt(); ys[x] = in.nextInt(); dirs[x] = in.next().charAt(0); tsc.add(xs[x]); tsr.add(ys[x]); } ArrayList<Integer> cols = new ArrayList<Integer>(); for(int v : tsc) { cols.add(v); } ArrayList<Integer> rows = new ArrayList<Integer>(); for(int v : tsr) { rows.add(v); } SegmentTree left = new SegmentTree(rows.size()); SegmentTree up = new SegmentTree(cols.size()); boolean[] used = new boolean[cols.size()]; for(int y = 0; y < q; y++) { if(used[Collections.binarySearch(cols, xs[y])]) { out.println(0); } else { if(dirs[y] == 'L') { int end = left.query(Collections.binarySearch(rows, ys[y])); out.println(xs[y] - end); up.modify(Collections.binarySearch(cols, end) + 1, Collections.binarySearch(cols, xs[y]), ys[y]); } else { int end = up.query(Collections.binarySearch(cols, xs[y])); out.println(ys[y] - end); left.modify(Collections.binarySearch(rows, end) + 1, Collections.binarySearch(rows, ys[y]), xs[y]); } used[Collections.binarySearch(cols, xs[y])] = true; } } out.close(); } static class SegmentTree { int n; int[] max; int[] delta; public SegmentTree(int n) { this.n = n; max = new int[n << 2 | 1]; delta = new int[n << 2 | 1]; } public void modify(int left, int right, int val) { modify(1, 0, n - 1, left, right, val); } public void modify(int i, int l, int r, int left, int right, int val) { if(r < left || l > right) { return; } else if(l >= left && r <= right) { apply(i, l, r, val); } else { prop(i, l, r); int mid = (l + r) >> 1; modify(i << 1, l, mid, left, right, val); modify(i << 1 | 1, mid + 1, r, left, right, val); update(i, l, r); } } public int query(int index) { return query(1, 0, n - 1, index, index); } public int query(int i, int l, int r, int left, int right) { if(r < left || l > right) { return 0; } else if(l >= left && r <= right) { return max[i]; } else { prop(i, l, r); int mid = (l + r) >> 1; return Math.max(query(i << 1, l, mid, left, right), query(i << 1 | 1, mid + 1, r, left, right)); } } public void apply(int i, int l, int r, int val) { delta[i] = Math.max(delta[i], val); max[i] = Math.max(max[i], val); } public void prop(int i, int l, int r) { int mid = (l + r) >> 1; apply(i << 1, l, mid, delta[i]); apply(i << 1 | 1, mid + 1, r, delta[i]); delta[i] = 0; } public void update(int i, int l, int r) { max[i] = Math.max(max[i << 1], max[i << 1 | 1]); } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream input) { br = new BufferedReader(new InputStreamReader(input)); st = new StringTokenizer(""); } public String next() throws IOException { if(st.hasMoreTokens()) { return st.nextToken(); } else { st = new StringTokenizer(br.readLine()); return next(); } } public int nextInt() throws IOException { return Integer.parseInt(next()); } } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int qmax = 2e5 + 42; int n, q; struct query { int x, y; char type; }; query inp[qmax]; int u_n = 0, u_values[qmax], u_tree[4 * qmax]; int l_n = 0, l_values[qmax], l_tree[4 * qmax]; set<int> u_, l_; int ROT(int q) { return n + 1 - q; } int where_l(int val, bool up) { if (up == 0) { int p = upper_bound(l_values + 1, l_values + 1 + l_n, val) - l_values; return p - 1; } int p = lower_bound(l_values + 1, l_values + 1 + l_n, val) - l_values; return p; } int where_u(int val, bool up) { if (up == 0) { int p = upper_bound(u_values + 1, u_values + 1 + u_n, val) - u_values; return p - 1; } int p = lower_bound(u_values + 1, u_values + 1 + u_n, val) - u_values; return p; } int query_u(int node, int l, int r, int pos) { if (l == r) return u_tree[node]; int av = (l + r) / 2, ans = u_tree[node]; if (pos <= av) ans = max(ans, query_u(node * 2, l, av, pos)); else ans = max(ans, query_u(node * 2 + 1, av + 1, r, pos)); return ans; } int query_l(int node, int l, int r, int pos) { if (l == r) return l_tree[node]; int av = (l + r) / 2, ans = l_tree[node]; if (pos <= av) ans = max(ans, query_l(node * 2, l, av, pos)); else ans = max(ans, query_l(node * 2 + 1, av + 1, r, pos)); return ans; } void u_update(int node) { u_tree[node * 2] = max(u_tree[node * 2], u_tree[node]); u_tree[node * 2 + 1] = max(u_tree[node * 2 + 1], u_tree[node]); u_tree[node] = 0; } void update_u(int node, int l, int r, int lq, int rq, int val) { if (lq > rq) return; if (l == lq && r == rq) { u_tree[node] = max(u_tree[node], val); return; } u_update(node); int av = (l + r) / 2; if (lq <= av) update_u(node * 2, l, av, lq, min(av, rq), val); if (av < rq) update_u(node * 2 + 1, av + 1, r, max(av + 1, lq), rq, val); } void l_update(int node) { l_tree[node * 2] = max(l_tree[node * 2], l_tree[node]); l_tree[node * 2 + 1] = max(l_tree[node * 2 + 1], l_tree[node]); l_tree[node] = 0; } void update_l(int node, int l, int r, int lq, int rq, int val) { if (lq > rq) return; if (l == lq && r == rq) { l_tree[node] = max(l_tree[node], val); return; } l_update(node); int av = (l + r) / 2; if (lq <= av) update_l(node * 2, l, av, lq, min(av, rq), val); if (av < rq) update_l(node * 2 + 1, av + 1, r, max(av + 1, lq), rq, val); } int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); cin >> n >> q; for (int i = 1; i <= q; i++) { cin >> inp[i].x >> inp[i].y >> inp[i].type; swap(inp[i].x, inp[i].y); } for (int i = 1; i <= q; i++) { u_.insert(inp[i].x); l_.insert(inp[i].y); } for (auto k : u_) { u_n++; u_values[u_n] = k; } for (auto k : l_) { l_n++; l_values[l_n] = k; } for (int i = 1; i <= q; i++) { if (inp[i].type == 'U') { int ans = inp[i].x - query_u(1, 1, u_n, where_u(inp[i].x, 0)); cout << ans << endl; if (ans) { if (l_n) update_l(1, 1, l_n, where_l(ROT(inp[i].x), 1), where_l(ROT(inp[i].x - (ans - 1)), 0), inp[i].y); if (u_n) update_u(1, 1, u_n, where_u(inp[i].x, 0), where_u(inp[i].x, 0), inp[i].x); } } else { int ans = inp[i].y - query_l(1, 1, l_n, where_l(inp[i].y, 0)); cout << ans << endl; if (ans) { if (u_n) update_u(1, 1, u_n, where_u(ROT(inp[i].y), 1), where_u(ROT(inp[i].y - (ans - 1)), 0), inp[i].x); if (l_n) update_l(1, 1, l_n, where_l(inp[i].y, 0), where_l(inp[i].y, 0), inp[i].y); } } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; long long lazy[800200 * 4][2], seg[800200 * 4][2]; void updLazy(int node, int ini, int fim, int k) { if (lazy[node][k] == 0) return; seg[node][k] = max(seg[node][k], lazy[node][k]); if (ini != fim) { lazy[node * 2][k] = max(lazy[node * 2][k], lazy[node][k]); lazy[node * 2 + 1][k] = max(lazy[node * 2 + 1][k], lazy[node][k]); } lazy[node][k] = 0; } void update(int node, int ini, int fim, int x, int y, long long val, int k) { updLazy(node, ini, fim, k); if (ini > y || fim < x) return; if (ini >= x && fim <= y) { lazy[node][k] = max(lazy[node][k], val); updLazy(node, ini, fim, k); } else { int fe = node * 2, fd = fe + 1, mid = (ini + fim) / 2; update(fe, ini, mid, x, y, val, k); update(fd, mid + 1, fim, x, y, val, k); seg[node][k] = max(seg[fe][k], seg[fd][k]); } } long long query(int node, int ini, int fim, int x, int y, int k) { updLazy(node, ini, fim, k); if (ini > y || fim < x) return 0; if (ini >= x && fim <= y) return seg[node][k]; int fe = node * 2, fd = fe + 1, mid = (ini + fim) / 2; return max(query(fe, ini, mid, x, y, k), query(fd, mid + 1, fim, x, y, k)); } int n, q; int a[800200], b[800200]; char c[800200]; map<int, int> mapa, rmapa; vector<int> v; void compress() { v.push_back(n); v.push_back(1); for (int i = 0; i < q; i++) { v.push_back(a[i]); v.push_back(b[i]); v.push_back(a[i] + 1); v.push_back(b[i] + 1); } sort((v).begin(), (v).end()); int cnt = 1; for (int i = 0; i < ((int)(v).size()); i++) { if (i == 0 || v[i] != v[i - 1]) { mapa[v[i]] = cnt; rmapa[cnt] = v[i]; cnt++; } } n = mapa[n]; } int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> q; for (int i = 0; i < q; i++) { cin >> a[i] >> b[i] >> c[i]; } compress(); update(1, 1, n, 1, n, 1, 0); update(1, 1, n, 1, n, 1, 1); for (int i = 0; i < q; i++) { int x, y; char d; x = mapa[a[i]]; y = mapa[b[i]]; d = c[i]; swap(x, y); int limite; if (d == 'U') { limite = query(1, 1, n, y, y, 0); update(1, 1, n, y, y, x + 1, 0); update(1, 1, n, limite, x, y + 1, 1); cout << rmapa[x] - rmapa[limite] + 1 << endl; } else { limite = query(1, 1, n, x, x, 1); update(1, 1, n, x, x, y + 1, 1); update(1, 1, n, limite, y, x + 1, 0); cout << rmapa[y] - rmapa[limite] + 1 << endl; } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; struct Segment { long long l; long long r; long long h; long long w; Segment(long long l = 0, long long r = 0, long long h = 0, long long w = 0) : l(l), r(r), h(h), w(w) {} bool operator<(const Segment a) const { if (r != a.r) return r < a.r; if (l != a.l) return l < a.l; if (h != a.h) return h < a.h; return w < a.w; } }; long long upd(const Segment& t, Segment& l, Segment& r, long long pos, int up) { if (up == true) { l = Segment(t.l, pos - 1, t.h + t.r - pos + 1, t.w); r = Segment(pos + 1, t.r, t.h, 1); return t.h + t.r - pos; } else { l = Segment(t.l, pos - 1, 1, t.w); r = Segment(pos + 1, t.r, t.h, t.w + pos + 1 - t.l); return t.w + pos - t.l; } } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); long long n; cin >> n; long long q; cin >> q; set<Segment> seg; seg.insert(Segment(1, n, 1, 1)); for (; q > 0; q--) { long long x, y; string way; cin >> x >> y >> way; bool up = true; if (way == "L") up = false; Segment l, r, tmp; tmp = Segment(-INF, x, -INF, -INF); auto it = seg.lower_bound(tmp); if (it == seg.end() || it->l > x) { cout << "0\n"; continue; } cout << upd(*it, l, r, x, up) << "\n"; seg.erase(it); if (l.l <= l.r) seg.insert(l); if (r.l <= r.r) seg.insert(r); } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 200020; int n, q; set<pair<pair<int, int>, pair<int, int> > > s; map<int, bool> mark; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> q; s.insert(make_pair(make_pair(1, n), make_pair(1, 1))); while (q--) { int a, b; char Q; cin >> a >> b >> Q; b = n - b + 1; int x = (a + b) / 2; if (mark[x]) { cout << 0 << endl; continue; } mark[x] = 1; set<pair<pair<int, int>, pair<int, int> > >::iterator it = s.upper_bound(make_pair(make_pair(x, 1e9), make_pair(1e9, 1e9))); it--; pair<pair<int, int>, pair<int, int> > pp = *it; if (Q == 'U') { int len = pp.first.second - x + pp.second.second; cout << len << endl; s.erase(pp); s.insert(make_pair(make_pair(pp.first.first, x - 1), make_pair(pp.second.first, len + 1))); s.insert(make_pair(make_pair(x + 1, pp.first.second), make_pair(1, pp.second.second))); } else { int len = x - pp.first.first + pp.second.first; cout << len << endl; s.erase(pp); s.insert(make_pair(make_pair(pp.first.first, x - 1), make_pair(pp.second.first, 1))); s.insert(make_pair(make_pair(x + 1, pp.first.second), make_pair(len + 1, pp.second.second))); } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import sys from bisect import bisect def input(): return sys.stdin.readline().strip() def solve(): n, q = map(int, input().split()) was = set() Q = [None]*q all = [0]*(2*q) for i in range(q): x, y, t = input().split() x, y = int(x), int(y) Q[i] = (x, y, t) all[2*i] = x all[2*i+1] = y all.sort() i = 0 p = -1 for j in range(2*q): v = all[j] if v != p: all[i] = v i += 1 p = v sz = i all = all[:sz] V = [0]*(2*sz) H = [0]*(2*sz) for x, y, t in Q: if (x,y) in was: print(0) else: was.add((x,y)) if t == 'L': TA = H TB = V else: x, y = y, x TA = V TB = H v = bisect(all, y) - 1 + sz r = 0 while v > 0: r = max(r, TA[v]) v //= 2 c = x - r print(c) r = bisect(all, x) - 1 + sz l = bisect(all, x - c) + sz while l <= r: if l % 2 == 1: TB[l] = max(TB[l], y) if r % 2 == 0: TB[r] = max(TB[r], y) l = (l+1)//2 r = (r-1)//2 solve()
PYTHON3
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; struct SegmentTree { int maxv[210000 << 2], pushdowntag[210000 << 2]; void pushdown(int o) { maxv[o << 1] = max(maxv[o << 1], pushdowntag[o]); maxv[o << 1 | 1] = max(maxv[o << 1 | 1], pushdowntag[o]); pushdowntag[o << 1] = max(pushdowntag[o], pushdowntag[o << 1]); pushdowntag[o << 1 | 1] = max(pushdowntag[o], pushdowntag[o << 1 | 1]); } void pushup(int o) { maxv[o] = max(maxv[o << 1], maxv[o << 1 | 1]); } void update(int o, int L, int R, int ql, int qr, int val) { if (ql <= L && R <= qr) { maxv[o] = max(maxv[o], val); pushdowntag[o] = max(pushdowntag[o], val); return; } pushdown(o); int M = (L + R) >> 1; if (ql <= M) update(o << 1, L, M, ql, qr, val); if (qr > M) update(o << 1 | 1, M + 1, R, ql, qr, val); pushup(o); } int query(int o, int L, int R, int ql, int qr) { if (ql <= L && R <= qr) return maxv[o]; pushdown(o); int M = (L + R) >> 1, ans = 0; if (ql <= M) ans = max(ans, query(o << 1, L, M, ql, qr)); if (qr > M) ans = max(ans, query(o << 1 | 1, M + 1, R, ql, qr)); pushup(o); return ans; } } segtreeCol, segtreeRow; struct Query { int x, y; char cmd[5]; } query[210000]; int n, q; map<int, int> idOfRow, idOfCol; int stackOfCol[210000], topCol, stackOfRow[210000], topRow; int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= q; i++) { scanf("%d%d%s", &query[i].x, &query[i].y, query[i].cmd); stackOfCol[++topCol] = query[i].x; stackOfRow[++topRow] = query[i].y; } topCol = unique(stackOfCol + 1, stackOfCol + topCol + 1) - stackOfCol; topRow = unique(stackOfRow + 1, stackOfRow + topRow + 1) - stackOfRow; sort(stackOfCol + 1, stackOfCol + topCol + 1); sort(stackOfRow + 1, stackOfRow + topRow + 1); for (int i = 1; i <= topCol; i++) idOfCol[stackOfCol[i]] = i; for (int i = 1; i <= topRow; i++) idOfRow[stackOfRow[i]] = i; for (int i = 1; i <= q; i++) { if (query[i].cmd[0] == 'U') { int tmp = segtreeCol.query(1, 1, topCol, idOfCol[query[i].x], idOfCol[query[i].x]); printf("%d\n", query[i].y - tmp); if (!(query[i].y - tmp)) continue; segtreeCol.update(1, 1, topCol, idOfCol[query[i].x], idOfCol[query[i].x], query[i].y); segtreeRow.update(1, 1, topRow, idOfRow[tmp], idOfRow[query[i].y], query[i].x); } else { int tmp = segtreeRow.query(1, 1, topRow, idOfRow[query[i].y], idOfRow[query[i].y]); printf("%d\n", query[i].x - tmp); if (!(query[i].x - tmp)) continue; segtreeRow.update(1, 1, topRow, idOfRow[query[i].y], idOfRow[query[i].y], query[i].x); segtreeCol.update(1, 1, topCol, idOfCol[tmp], idOfCol[query[i].x], query[i].y); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; int add(int a, int b) { if ((a += b) >= P) a -= P; return a; } int sub(int a, int b) { if ((a -= b) < 0) a += P; return a; } int mul(int a, int b) { return 1ll * a * b % P; } int kpow(int a, int b) { int r = 1; for (; b; b >>= 1, a = mul(a, a)) { if (b & 1) r = mul(r, a); } return r; } const int N = 202020; int n, m; vector<int> V; struct Q { int x, y; char o; } q[N]; int find(int x) { return lower_bound(V.begin(), V.end(), x) - V.begin() + 1; } struct Seg { static const int N = ::N << 3; int ma[N]; void upd(int L, int R, int c, int l, int r, int rt) { if (L <= l && r <= R) { ma[rt] = max(ma[rt], c); return; } int mid = l + r >> 1; if (L <= mid) upd(L, R, c, l, mid, rt << 1); if (R >= mid + 1) upd(L, R, c, mid + 1, r, rt << 1 | 1); } int qry(int p, int l, int r, int rt) { if (l == r) return ma[rt]; int mid = l + r >> 1, ans = ma[rt]; if (p <= mid) ans = max(ans, qry(p, l, mid, rt << 1)); else ans = max(ans, qry(p, mid + 1, r, rt << 1 | 1)); return ans; } } t1, t2; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> n >> m; for (int i = (1); i < (m + 1); i++) { string s; cin >> q[i].x >> q[i].y >> s; q[i].o = s[0]; V.push_back(q[i].x); V.push_back(q[i].y); } V.push_back(0); sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end()); map<pair<int, int>, bool> vis; for (int i = (1); i < (m + 1); i++) { int x = q[i].x, y = q[i].y; int px = find(x), py = find(y); char o = q[i].o; if (vis[make_pair(x, y)]) { cout << 0 << "\n"; continue; } vis[make_pair(x, y)] = 1; if (o == 'L') { int t = t2.qry(py, 1, (int)V.size(), 1); cout << x - t << "\n"; int pt = find(t); t1.upd(pt + 1, px, y, 1, (int)V.size(), 1); } else { int t = t1.qry(px, 1, (int)V.size(), 1); cout << y - t << "\n"; int pt = find(t); t2.upd(pt + 1, py, x, 1, (int)V.size(), 1); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int maxq = 200005; int ans, x, y, neo; int n, q, a, b, sizedesu; char c; struct node { int l, r, Mw, lz, mw; } allcl[maxq * 4], allcu[maxq * 4]; set<int> checkpie; vector<int> allpie; pair<int, char> allcmd[maxq]; void buildl(int l, int r, int k) { allcl[k].l = l; allcl[k].r = r; allcl[k].mw = 0; allcl[k].Mw = 0; allcl[k].lz = 0; if (l + 1 == r) return; int m = (l + r) / 2; buildl(l, m, 2 * k); buildl(m, r, 2 * k + 1); } void getlm(int k) { if (allcl[k].mw == allcl[k].Mw) { ans = allcl[k].mw; return; } int l = allcl[k].l, r = allcl[k].r; int m = (l + r) / 2; if (allcl[k].lz) { allcl[k].lz = 0; allcl[2 * k].lz = m - l - 1; allcl[2 * k + 1].mw = allcl[2 * k].mw = allcl[k].mw; allcl[2 * k + 1].lz = r - m - 1; allcl[2 * k + 1].Mw = allcl[2 * k].Mw = allcl[k].Mw; } if (x <= allpie[m]) getlm(2 * k); else getlm(2 * k + 1); } void changelm(int l, int r, int k) { if (allcl[k].mw >= neo) return; if (neo > allcl[k].Mw && allpie[l] >= x && allpie[r] <= y) { allcl[k].Mw = allcl[k].mw = neo; allcl[k].lz = r - l - 1; return; } else if (l + 1 == r) return; int m = (l + r) / 2; if (allcl[k].lz) { allcl[k].lz = 0; allcl[2 * k].lz = m - l - 1; allcl[2 * k + 1].mw = allcl[2 * k].mw = allcl[k].mw; allcl[2 * k + 1].Mw = allcl[2 * k].Mw = allcl[k].Mw; allcl[2 * k + 1].lz = r - m - 1; } if (x < allpie[m]) changelm(l, m, 2 * k); if (y > allpie[m]) changelm(m, r, 2 * k + 1); allcl[k].mw = min(allcl[2 * k].mw, allcl[2 * k + 1].mw); allcl[k].Mw = max(allcl[2 * k].Mw, allcl[2 * k + 1].Mw); } void buildu(int l, int r, int k) { allcu[k].l = l; allcu[k].r = r; allcu[k].Mw = allcu[k].mw = n + 1; allcu[k].lz = 0; if (l + 1 == r) return; int m = (l + r) / 2; buildu(l, m, 2 * k); buildu(m, r, 2 * k + 1); } void getum(int k) { if (allcu[k].mw == allcu[k].Mw) { ans = allcu[k].mw; return; } int l = allcu[k].l, r = allcu[k].r; int m = (l + r) / 2; if (allcu[k].lz) { allcu[k].lz = 0; allcu[2 * k].lz = m - l - 1; allcu[2 * k + 1].mw = allcu[2 * k].mw = allcu[k].mw; allcu[2 * k + 1].lz = r - m - 1; allcu[2 * k + 1].Mw = allcu[2 * k].Mw = allcu[k].Mw; } if (x <= allpie[m]) getum(2 * k); else getum(2 * k + 1); } void changeum(int l, int r, int k) { if (allcu[k].Mw <= neo) return; if (neo < allcu[k].mw && allpie[l] >= x && allpie[r] <= y) { allcu[k].Mw = allcu[k].mw = neo; allcu[k].lz = r - l - 1; return; } else if (l + 1 == r) return; int m = (l + r) / 2; if (allcu[k].lz) { allcu[k].lz = 0; allcu[2 * k].lz = m - l - 1; allcu[2 * k + 1].mw = allcu[2 * k].mw = allcu[k].mw; allcu[2 * k + 1].Mw = allcu[2 * k].Mw = allcu[k].Mw; allcu[2 * k + 1].lz = r - m - 1; } if (x < allpie[m]) changeum(l, m, 2 * k); if (y > allpie[m]) changeum(m, r, 2 * k + 1); allcu[k].mw = min(allcu[2 * k].mw, allcu[2 * k + 1].mw); allcu[k].Mw = max(allcu[2 * k].Mw, allcu[2 * k + 1].Mw); } int main(int argc, char *argv[]) { scanf("%d%d", &n, &q); allpie.push_back(0); for (int i = 0; i < q; i++) { scanf("%d%d", &a, &b); getchar(); c = getchar(); allpie.push_back(a); allcmd[i] = make_pair(a, c); } allpie.push_back(n + 1); sort(allpie.begin(), allpie.end()); allpie.erase(unique(allpie.begin(), allpie.end()), allpie.end()); sizedesu = allpie.size() - 1; buildl(0, sizedesu, 1); buildu(0, sizedesu, 1); for (int i = 0; i < q; i++) { x = allcmd[i].first; if (checkpie.count(x) == 0) { checkpie.insert(x); switch (allcmd[i].second) { case 'L': getlm(1); printf("%d\n", x - ans); neo = x; y = x; x = ans; changeum(0, sizedesu, 1); break; case 'U': getum(1); printf("%d\n", ans - x); neo = x; y = ans; changelm(0, sizedesu, 1); break; } } else printf("0\n"); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> #pragma GCC optimize("inline,Ofast", 3) #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma GCC optimize("-fpeephole2") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-fsched-spec") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-falign-jumps") #pragma GCC optimize("-falign-loops") #pragma GCC optimize("-falign-labels") #pragma GCC optimize("-fdevirtualize") #pragma GCC optimize("-fcaller-saves") #pragma GCC optimize("-fcrossjumping") #pragma GCC optimize("-fthread-jumps") #pragma GCC optimize("-funroll-loops") #pragma GCC optimize("-fwhole-program") #pragma GCC optimize("-freorder-blocks") #pragma GCC optimize("-fschedule-insns") #pragma GCC optimize("inline-functions") #pragma GCC optimize("-fschedule-insns2") #pragma GCC optimize("-fstrict-aliasing") #pragma GCC optimize("-fstrict-overflow") #pragma GCC optimize("-falign-functions") #pragma GCC optimize("-fcse-skip-blocks") #pragma GCC optimize("-fcse-follow-jumps") #pragma GCC optimize("-fsched-interblock") #pragma GCC optimize("-fpartial-inlining") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("-freorder-functions") #pragma GCC optimize("-findirect-inlining") #pragma GCC optimize("inline-small-functions") #pragma GCC optimize("-finline-small-functions") #pragma GCC optimize("-ftree-switch-conversion") #pragma GCC optimize("-foptimize-sibling-calls") #pragma GCC optimize("-fexpensive-optimizations") #pragma GCC optimize("-funsafe-loop-optimizations") #pragma GCC optimize("inline-functions-called-once") #pragma GCC optimize("-fdelete-null-pointer-checks") using namespace std; int x[200200], y[200200]; map<int, int> mp; int main() { int n, q; cin >> n >> q; mp[0] = mp[n + 1] = q; for (int i = q - 1; i >= 0; i--) { char c; cin >> x[i] >> y[i] >> c; if (c == 'U') { map<int, int>::iterator it; it = mp.lower_bound(x[i]); int u = x[i]; if (it->first == x[i]) { cout << '0' << endl; continue; } else { int ans; ans = y[i] - y[it->second]; cout << ans << endl; y[i] = y[it->second]; } mp[u] = i; } else { map<int, int>::iterator it; it = mp.lower_bound(x[i]); int u = x[i]; if (it->first == x[i]) { cout << '0' << endl; continue; } else { it--; int ans; ans = x[i] - x[it->second]; cout << ans << endl; x[i] = x[it->second]; } mp[u] = i; } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; map<int, int> X, Y; int n, q; set<int> s; int main() { cin >> n >> q; X[0] = Y[0] = 0; for (int i = 0; i < q; i++) { int x, y; char d; cin >> x >> y >> d; if (s.find(x) != s.end()) cout << "0\n"; else { s.insert(x); auto a = X.lower_bound(x); a--; auto b = Y.lower_bound(y); b--; X[x] = a->second; Y[y] = b->second; if (d == 'U') { Y[b->first] = x; cout << y - X[x] << '\n'; } else { X[a->first] = y; cout << x - Y[y] << '\n'; } } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, q; vector<pair<pair<int, int>, char> > V; int revR[800010]; map<int, int> R; set<int> setR; int tree[2000010][2]; int lazy[2000010][2]; void upd(int idx, int l, int r, int a, int b, int x, bool k) { if (idx * 2 + 1 < 2000010) lazy[idx * 2 + 1][k] = max(lazy[idx * 2 + 1][k], lazy[idx][k]); if (idx * 2 + 2 < 2000010) lazy[idx * 2 + 2][k] = max(lazy[idx * 2 + 2][k], lazy[idx][k]); tree[idx][k] = max(tree[idx][k], lazy[idx][k]); lazy[idx][k] = 0; if (l > b || r < a) return; if (l >= a && r <= b) { if (idx * 2 + 1 < 2000010) lazy[idx * 2 + 1][k] = max(lazy[idx * 2 + 1][k], x); if (idx * 2 + 2 < 2000010) lazy[idx * 2 + 2][k] = max(lazy[idx * 2 + 2][k], x); tree[idx][k] = max(tree[idx][k], x); return; } int mid = (l + r) / 2; upd(idx * 2 + 1, l, mid, a, b, x, k); upd(idx * 2 + 2, mid + 1, r, a, b, x, k); tree[idx][k] = max(tree[idx * 2 + 1][k], tree[idx * 2 + 2][k]); } int qu(int idx, int l, int r, int a, int b, int k) { if (idx * 2 + 1 < 2000010) lazy[idx * 2 + 1][k] = max(lazy[idx * 2 + 1][k], lazy[idx][k]); if (idx * 2 + 2 < 2000010) lazy[idx * 2 + 2][k] = max(lazy[idx * 2 + 2][k], lazy[idx][k]); tree[idx][k] = max(tree[idx][k], lazy[idx][k]); lazy[idx][k] = 0; if (l > b || r < a) return 0; if (l >= a && r <= b) { return tree[idx][k]; } int mid = (l + r) / 2; return max(qu(idx * 2 + 1, l, mid, a, b, k), qu(idx * 2 + 2, mid + 1, r, a, b, k)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q; int x, y; char c; for (int i = 0; i < q; i++) { cin >> x >> y >> c; V.push_back({{x, y}, c}); setR.insert(x); setR.insert(y); } set<int>::iterator it; it = setR.begin(); for (int i = 0;; i++) { if (setR.end() == it) break; R[*it] = i; revR[i] = *it; it++; } int tmpX, tmpY, m; for (int i = 0; i < q; i++) { x = V[i].first.first; y = V[i].first.second; c = V[i].second; if (c == 'U') { tmpX = R[x]; tmpY = R[y]; m = qu(0, 0, 400009, tmpX, tmpX, 0); upd(0, 0, 400009, R[m], tmpY, x, 1); upd(0, 0, 400009, tmpX, tmpX, y, 0); cout << y - m << "\n"; } else { tmpX = R[x]; tmpY = R[y]; m = qu(0, 0, 400009, tmpY, tmpY, 1); upd(0, 0, 400009, R[m], tmpX, y, 0); upd(0, 0, 400009, tmpY, tmpY, x, 1); cout << x - m << "\n"; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; template <class T, class BASE, T (*op)(T, T), T (*multop)(T, BASE), T nonMod> struct segtree { vector<BASE> pts; vector<pair<BASE, BASE> > segment; vector<T> val, addval; vector<bool> lazy; size_t sz; void push_back(BASE pt) { pts.push_back(pt); } void initialize() { sz = 1; while (sz < pts.size() - 1) sz *= 2; sz *= 2; segment.resize(sz); val.resize(sz, nonMod); addval.resize(sz, nonMod); lazy.resize(sz, false); for (size_t i = 0; i < sz / 2; i++) segment[sz / 2 + i] = make_pair(pts[min(i, pts.size() - 1)], pts[min(i + 1, pts.size() - 1)]); for (int i = sz / 2 - 1; i > 0; i--) segment[i] = make_pair(segment[i * 2].first, segment[i * 2 + 1].second); }; inline BASE len(int pos) { return segment[pos].second - segment[pos].first; } inline T posval(int pos) { if (lazy[pos]) return multop(val[pos], len(pos)); else if (addval[pos] != nonMod) return op(val[pos], multop(addval[pos], len(pos))); else return val[pos]; } template <int operation> T operate(int l, int r, T nval, int pos = 1) { if (segment[pos].second <= l || r <= segment[pos].first) return nonMod; if (lazy[pos]) { addval[pos] = nonMod; lazy[pos] = false; if (pos * 2 < sz) { val[pos * 2] = val[pos]; lazy[pos * 2] = true; val[pos * 2 + 1] = val[pos]; lazy[pos * 2 + 1] = true; } val[pos] = multop(val[pos], len(pos)); } if (addval[pos] != nonMod) { val[pos] = op(val[pos], multop(addval[pos], len(pos))); if (pos * 2 < sz) { addval[pos * 2] = op(addval[pos], addval[pos * 2]); addval[pos * 2 + 1] = op(addval[pos], addval[pos * 2 + 1]); } addval[pos] = nonMod; } if (l <= segment[pos].first && segment[pos].second <= r) switch (operation) { case 0: addval[pos] = op(addval[pos], nval); return nonMod; case 1: return val[pos]; case 2: lazy[pos] = true; val[pos] = nval; return nonMod; } T ret = op(operate<operation>(l, r, nval, pos * 2), operate<operation>(l, r, nval, pos * 2 + 1)); val[pos] = op(posval(pos * 2), posval(pos * 2 + 1)); return ret; } void addRange(int l, int r, T toadd) { operate<0>(l, r, toadd); } T getRange(int l, int r) { return operate<1>(l, r, nonMod); } void setRange(int l, int r, T nval) { return operate<2>(l, r, nval); } }; inline int segSame(int a, int b) { return a; } inline int segMax(int a, int b) { return max(a, b); } inline int segMin(int a, int b) { return min(a, b); } int main() { vector<pair<char, int> > query; segtree<int, int, segMax, segSame, -1000000010> leftb; segtree<int, int, segMin, segSame, 1000000010> upb; int n, q, p1, p2; char c; cin.sync_with_stdio(false); cin >> n >> q; vector<int> pts; pts.push_back(0); pts.push_back(1); pts.push_back(n + 1); for (int i = 0; i < q; i++) { cin >> p1 >> p2 >> c; pts.push_back(p1); pts.push_back(p1 + 1); pts.push_back(p2); pts.push_back(p2 + 1); query.push_back(make_pair(c, p1)); } sort(pts.begin(), pts.end()); pts.resize(unique(pts.begin(), pts.end()) - pts.begin()); for (size_t i = 0; i < pts.size(); i++) { leftb.push_back(pts[i]); upb.push_back(pts[i]); } leftb.initialize(); upb.initialize(); leftb.addRange(0, n + 1, 0); upb.addRange(0, n + 1, n + 1); for (auto op : query) { if (op.first == 'L') { int lb = leftb.getRange(op.second, op.second + 1); cout << op.second - lb << '\n'; upb.addRange(lb + 1, op.second + 1, op.second); leftb.addRange(op.second, op.second + 1, op.second); } if (op.first == 'U') { int ub = upb.getRange(op.second, op.second + 1); cout << ub - op.second << '\n'; leftb.addRange(op.second, ub, op.second); upb.addRange(op.second, op.second + 1, op.second); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; template <class T> void chmax(T &x, T y) { x = x > y ? x : y; return; } template <class T> void chmin(T &x, T y) { x = x < y ? x : y; return; } const int inf = 1000000000, mod = 1000000007; int n, q, X, Y; char c; set<pair<pair<int, int>, pair<int, int> > > s; set<pair<int, int> > t; int main() { cin >> n >> q; s.insert(make_pair(make_pair(1, n), make_pair(1, 1))); for (int i = 1; i <= q; i++) { cin >> X >> Y >> c; if (t.find(make_pair(X, Y)) != t.end()) { cout << 0 << endl; continue; } t.insert(make_pair(X, Y)); set<pair<pair<int, int>, pair<int, int> > >::iterator it = s.lower_bound(make_pair(make_pair(Y + 1, -1), make_pair(-1, -1))); it--; pair<pair<int, int>, pair<int, int> > p = *it; s.erase(it); if (c == 'U') { cout << Y - (p.second.second) + 1 << endl; if (Y + 1 <= p.first.second) s.insert(make_pair(make_pair(Y + 1, p.first.second), p.second)); if (Y - 1 >= p.first.first) s.insert(make_pair(make_pair(p.first.first, Y - 1), make_pair(X + 1, p.second.second))); } else { cout << X - (p.second.first) + 1 << endl; if (Y - 1 >= p.first.first) s.insert(make_pair(make_pair(p.first.first, Y - 1), p.second)); if (Y + 1 <= p.first.second) s.insert(make_pair(make_pair(Y + 1, p.first.second), make_pair(p.second.first, Y + 1))); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; typedef struct Query { int x, md; Query(int x = 0, int md = 0) : x(x), md(md) {} } Query; Query Q[maxn]; int bdes1[maxn], bdes2[maxn], n, q, c; map<int, int> mp1, mp2; map<int, int>::iterator it; typedef struct BiT { int t[maxn * 3]; int lazy[maxn * 3]; void clear() { memset(t, 0, sizeof(t)); memset(lazy, -1, sizeof(lazy)); } void build(int l, int r, int x) { if (l == r) { t[x] = 0; return; } int m = (l + r) / 2; build(l, m, x << 1); build(m + 1, r, x << 1 | 1); } void update(int L, int R, int a, int l, int r, int x) { if (L == l && R == r) { lazy[x] = max(lazy[x], a); t[x] = max(a, t[x]); return; } pushdown(l, r, x); int m = (l + r) / 2; if (L > m) update(L, R, a, m + 1, r, x << 1 | 1); else if (R <= m) update(L, R, a, l, m, x << 1); else { update(L, m, a, l, m, x << 1); update(m + 1, R, a, m + 1, r, x << 1 | 1); } } void pushdown(int l, int r, int x) { if (lazy[x] != -1) { lazy[x << 1] = max(lazy[x << 1], lazy[x]); lazy[x << 1 | 1] = max(lazy[x << 1 | 1], lazy[x]); t[x << 1] = max(t[x << 1], lazy[x]); t[x << 1 | 1] = max(t[x << 1 | 1], lazy[x]); lazy[x] = -1; } } int get(int p, int l, int r, int x) { if (l == r) { return t[x]; } int m = (l + r) / 2; pushdown(l, r, x); if (p > m) return get(p, m + 1, r, x << 1 | 1); else return get(p, l, m, x << 1); } } BiT; BiT T1, T2; int m1, m2; void des() { for (int i = 0; i < q; i++) { mp1[Q[i].x] = 0; mp2[n + 1 - Q[i].x] = 0; } c = 0; mp1[0] = 0; mp2[0] = 0; for (it = mp1.begin(); it != mp1.end(); it++) { (*it).second = c; bdes1[c++] = (*it).first; } c = 0; for (it = mp2.begin(); it != mp2.end(); it++) { (*it).second = c; bdes2[c++] = (*it).first; } m1 = mp1.size(); m2 = mp2.size(); } void input() { cin >> n >> q; char md; int b; for (int i = 0; i < q; i++) { scanf("%d%d %c", &Q[i].x, &b, &md); if (md == 'U') Q[i].md = 0; else Q[i].md = 1; } des(); } bool v1[maxn] = {0}, v2[maxn] = {0}; int main() { input(); T1.clear(); T2.clear(); T1.build(1, m1, 1); T2.build(1, m2, 1); for (int i = 0; i < q; i++) { if (!Q[i].md) { int u = mp1[Q[i].x], len; if (v1[u]) { cout << 0 << endl; continue; } len = T1.get(u, 1, m1, 1); cout << n + 1 - Q[i].x - bdes2[len] << endl; if (n + 1 - Q[i].x - bdes2[len]) T2.update(len + 1, mp2[n + 1 - Q[i].x], u, 1, m2, 1); v1[u] = 1; } else { int u = mp2[n + 1 - Q[i].x], len; if (v2[u]) { cout << 0 << endl; continue; } len = T2.get(u, 1, m2, 1); cout << Q[i].x - bdes1[len] << endl; if (Q[i].x - bdes1[len]) T1.update(len + 1, mp1[Q[i].x], u, 1, m1, 1); v2[u] = 1; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int N = 400005; struct SegmentTree { int tree[N << 2]; int lazy[N << 2]; inline void push_down(int rt) { if (lazy[rt] > tree[rt]) tree[rt] = lazy[rt]; lazy[rt << 1] = max(lazy[rt << 1], lazy[rt]); lazy[rt << 1 | 1] = max(lazy[rt << 1 | 1], lazy[rt]); lazy[rt] = 0; } void update(int v, int ll, int rr, int l, int r, int rt) { if (ll <= l && rr >= r) return void(lazy[rt] = max(lazy[rt], v)); if (lazy[rt]) push_down(rt); int m = (l + r) >> 1; if (ll <= m) update(v, ll, rr, l, m, rt << 1); if (rr > m) update(v, ll, rr, m + 1, r, rt << 1 | 1); } int query(int x, int l, int r, int rt) { if (lazy[rt]) push_down(rt); if (l == r) return tree[rt]; int m = (l + r) >> 1; if (x <= m) return query(x, l, m, rt << 1); else return query(x, m + 1, r, rt << 1 | 1); } } tx, ty; struct Q { int x, y; char d; void read() { cin >> x >> y >> d; } } q[N]; pair<int, int> x[N], y[N]; map<int, int> posx, posy; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { q[i].read(); x[i] = {q[i].x, i}; y[i] = {q[i].y, i}; } sort(x, x + m); sort(y, y + m); int cx = 1, cy = 1; for (int i = 0; i < m; i++) { posx[x[i].first] = cx; if (x[i].first != x[i + 1].first) cx++; posy[y[i].first] = cy; if (y[i].first != y[i + 1].first) cy++; } for (int i = 0; i < m; i++) { int ey = ty.query(posy[q[i].y], 1, m, 1); int ex = tx.query(posx[q[i].x], 1, m, 1); if (ey == q[i].x || ex == q[i].y) { cout << 0 << endl; continue; } if (q[i].d == 'U') { cout << q[i].y - ex << endl; ty.update(q[i].x, posy[ex] + 1, posy[q[i].y], 1, m, 1); } else { cout << q[i].x - ey << endl; tx.update(q[i].y, posx[ey] + 1, posx[q[i].x], 1, m, 1); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int N = 200000; int x[N + 2], y[N + 2], t[N + 2]; int main() { int n, q; cin >> n >> q; set<pair<int, int> > s; t[q] = 1; x[q] = 0; s.insert(make_pair(0, q)); t[q + 1] = 0; y[q + 1] = 0; s.insert(make_pair(n + 1, q + 1)); for (int i = 0; i < q; ++i) { char buffer[2]; scanf("%d%d%s", x + i, y + i, buffer); t[i] = *buffer == 'U'; set<pair<int, int> >::iterator it; if (t[i] == 0) { it = s.upper_bound(make_pair(x[i], INT_MAX)); it--; } else { it = s.lower_bound(make_pair(x[i], INT_MIN)); } int j = it->second; if (x[i] == it->first) { puts("0"); } else { s.insert(make_pair(x[i], i)); int x0 = x[i]; int y0 = y[i]; if (t[i] == 0) { x[i] = x[j]; } else { y[i] = y[j]; } printf("%d\n", x0 - x[i] + y0 - y[i]); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { cin.sync_with_stdio(0); cin.tie(0); int n, q; long long int k; cin >> n >> q; map<int, pair<int, int> > pieces; map<pair<int, int>, int> rev; pieces.clear(); rev.clear(); set<int> seen; seen.clear(); pieces[n + 1] = make_pair(0, 0); rev[make_pair(0, 0)] = n + 1; while (q--) { int x, y; char type; cin >> x >> y >> type; if (seen.count(x)) { cout << "0\n"; continue; } seen.insert(x); pair<int, int> original = pieces.upper_bound(x)->second; int index = rev[original]; if (type == 'U') { cout << y - original.first << "\n"; pair<int, int> New = original; pieces[x] = New; rev[New] = x; pieces[index].second = x; rev[pieces[index]] = index; } else { cout << x - original.second << "\n"; pair<int, int> New = original; New.first = y; pieces[x] = New; rev[New] = x; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.InputMismatchException; public class C555 { static class Solver { int N, Q, rmap[], cmap[]; Query[] queries; Node uMin, lMin; void solve(FastScanner s, PrintWriter out) { N = s.nextInt(); Q = s.nextInt(); queries = new Query[Q]; for(int i = 0; i < Q; i++) { int c = s.nextInt(), r = s.nextInt(), type = s.next().charAt(0) == 'U' ? 0 : 1; queries[i] = new Query(r, c, type, i); } Arrays.sort(queries, (a, b) -> a.r - b.r); rmap = new int[Q + 1]; for(int i = 0, li = 0, ix = 1; i < Q; i++) { if(queries[i].r != li) { queries[i].rid = ix; rmap[ix++] = li = queries[i].r; } else { queries[i].rid = ix - 1; } } Arrays.sort(queries, (a, b) -> a.c - b.c); cmap = new int[Q + 1]; for(int i = 0, li = 0, ix = 1; i < Q; i++) { if(queries[i].c != li) { queries[i].cid = ix; cmap[ix++] = li = queries[i].c; } else { queries[i].cid = ix - 1; } } lMin = new Node(0, Q); uMin = new Node(0, Q); lMin.set(0, 0); uMin.set(0, 0); Arrays.sort(queries, (a, b) -> a.qid - b.qid); for(Query q : queries) { int r = q.r, c = q.c, t = q.type, rid = q.rid, cid = q.cid; if(t == 0) { // up query int lo = 0, hi = rid, f = -1, m; while(lo <= hi) { m = lo + hi >> 1; if(lMin.min(m, rid) <= c) { f = m; lo = m + 1; } else hi = m - 1; } int far = r - rmap[f]; out.println(far); uMin.set(cid, rmap[f]); lMin.set(rid, c); } else { // left query int lo = 0, hi = cid, f = -1, m; while(lo <= hi) { m = lo + hi >> 1; if(uMin.min(m, cid) <= r) { f = m; lo = m + 1; } else hi = m - 1; } int far = c - cmap[f]; out.println(far); lMin.set(rid, cmap[f]); uMin.set(cid, r); } } } class Node { int l, m, r, mn; Node L, R; Node(int ll, int rr) { m = (l = ll) + (r = rr) >> 1; mn = N + 1; if(l != r) { L = new Node(l, m); R = new Node(m + 1, r); } } void set(int s, int x) { if(l == r) { if(x < mn) mn = x; return; } if(s <= m) L.set(s, x); else R.set(s, x); mn = L.mn < R.mn ? L.mn : R.mn; } int min(int s, int e) { if(s <= l && r <= e) return mn; int mn = N + 1; if(s <= m) mn = zz(mn, L.min(s, e)); if(m < e) mn = zz(mn, R.min(s, e)); return mn; } private int zz(int a, int b) { return a < b ? a : b; }; } class Query { int r, c, type, qid; int rid, cid; Query(int rr, int cc, int tt, int qi) { r = rr; c = cc; type = tt; qid = qi; } } } public static void main(String[] args) { FastScanner s = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); new Solver().solve(s, out); out.close(); s.close(); } static class FastScanner { private InputStream stream; private byte[] buf = new byte[1 << 22]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } public FastScanner(File f) throws FileNotFoundException { this(new FileInputStream(f)); } public FastScanner(String s) { this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)); } void close() { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } public int[] nextIntArray(int N) { int[] ret = new int[N]; for (int i = 0; i < N; i++) ret[i] = this.nextInt(); return ret; } public int[][] next2DIntArray(int N, int M) { int[][] ret = new int[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextIntArray(M); return ret; } public long[] nextLongArray(int N) { long[] ret = new long[N]; for (int i = 0; i < N; i++) ret[i] = this.nextLong(); return ret; } public long[][] next2DLongArray(int N, int M) { long[][] ret = new long[N][]; for (int i = 0; i < N; i++) ret[i] = nextLongArray(M); return ret; } public double[] nextDoubleArray(int N) { double[] ret = new double[N]; for (int i = 0; i < N; i++) ret[i] = this.nextDouble(); return ret; } public double[][] next2DDoubleArray(int N, int M) { double[][] ret = new double[N][]; for (int i = 0; i < N; i++) ret[i] = this.nextDoubleArray(M); return ret; } } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, m; map<int, pair<bool, int> > mp; int main() { scanf("%d%d", &n, &m); mp.insert(make_pair(n + 1, make_pair(0, n + 1))); mp.insert(make_pair(0, make_pair(1, n + 1))); int x, y; bool q; char ch[2]; for (int i = 1; i <= m; ++i) { scanf("%d%d%s", &x, &y, ch); q = ch[0] == 'U'; int res; if (q) { map<int, pair<bool, int> >::iterator it = mp.lower_bound(x); if (it->first == x) res = 0; else if (it->second.first) res = it->first + it->second.second - x; else res = it->first - x; } else { map<int, pair<bool, int> >::iterator it = mp.upper_bound(x); --it; if (it->first == x) res = 0; else if (it->second.first) res = x - it->first; else res = x - it->first + it->second.second; } printf("%d\n", res); mp.insert(make_pair(x, make_pair(q, res))); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; struct Node { Node(int s = 0, Node* left = nullptr, Node* right = nullptr) : val(s), L(left), R(right), lazy(s) {} Node* L; Node* R; int val; int lazy; }; struct DynamicSegmentTree { DynamicSegmentTree(int sz) : n(sz) { root = fetchNode(); } void upd(int left, int right, int v) { l = left; r = right; val = v; upd(root, 0, n); } int qwr(int left, int right) { l = left; r = right; return qwr(root, 0, n); } void push(Node* T) { if (T->L != nullptr) T->L->lazy = max(T->L->lazy, T->lazy); if (T->R != nullptr) T->R->lazy = max(T->R->lazy, T->lazy); T->val = max(T->val, T->lazy); } void merge(Node* T) { if (T->L != nullptr) T->val = max(T->val, T->L->val); if (T->R != nullptr) T->val = max(T->val, T->R->val); } void upd(Node* T, int low, int high) { if (l > high || r < low) { return; } if (l <= low && r >= high) { T->lazy = max(T->lazy, val); push(T); return; } int mid = ((low + high) >> 1); if (T->L != nullptr) push(T->L); if (T->R != nullptr) push(T->R); if (T->L == nullptr) { T->L = fetchNode(T->lazy); } if (l <= mid) { upd(T->L, low, mid); } if (T->R == nullptr) { T->R = fetchNode(T->lazy); } if (r >= mid + 1) { upd(T->R, mid + 1, high); } merge(T); } int qwr(Node* T, int low, int high) { if (l <= low && r >= high) { return T->val; } if (T->L == nullptr && T->R == nullptr) { return T->val; } int mid = ((low + high) >> 1); if (T->L != nullptr) push(T->L); if (T->R != nullptr) push(T->R); int resLeft = 0; int resRight = 0; if (l <= mid && T->L != nullptr) resLeft = qwr(T->L, low, mid); if (r >= mid + 1 && T->R != nullptr) resRight = qwr(T->R, mid + 1, high); return max(resLeft, resRight); } Node* fetchNode(int s = 0, Node* left = nullptr, Node* right = nullptr) { tree.push_back(Node(s, left, right)); return &tree.back(); } int n, l, r, val; Node* root; deque<Node> tree; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); DynamicSegmentTree DST_X(1e9 + 9); DynamicSegmentTree DST_Y(1e9 + 9); int n, q; cin >> n >> q; while (q--) { int x, y; char type; cin >> x >> y >> type; if (type == 'L') { int pos = DST_Y.qwr(y, y); if (pos > x || DST_X.qwr(x, x) == y) { cout << 0 << "\n"; continue; } cout << x - pos << "\n"; DST_X.upd(pos + 1, x, y); } else { int pos = DST_X.qwr(x, x); if (pos > y || DST_Y.qwr(y, y) == x) { cout << 0 << "\n"; continue; } cout << y - pos << "\n"; DST_Y.upd(pos + 1, y, x); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> long long mpow(long long a, long long n, long long mod) { long long ret = 1; long long b = a; while (n) { if (n & 1) ret = (ret * b) % mod; b = (b * b) % mod; n >>= 1; } return (long long)ret; } using namespace std; map<int, int> ma1; map<int, int> ma2; vector<pair<pair<int, int>, char> > v; vector<int> allQx, allQy; int n, q; int tree[4 * 200011][2]; int lazy[4 * 200011][2]; struct node { int l, r, id, k; node() {} node(int l_, int r_, int id_, int _k) { l = l_; r = r_; id = id_; k = _k; lazy_update(); } node left() { return node(l, (l + r) / 2, 2 * id, k); } node right() { return node((l + r) / 2 + 1, r, 2 * id + 1, k); } void lazy_update() { if (lazy[id][k] == 0) return; if (l != r) { lazy[2 * id][k] = max(lazy[id][k], lazy[2 * id][k]); lazy[2 * id + 1][k] = max(lazy[id][k], lazy[2 * id + 1][k]); } tree[id][k] = max(tree[id][k], lazy[id][k]); lazy[id][k] = 0; } int query(int s, int e, int k) { if (r < s or e < l) return 0; if (s <= l and r <= e) return tree[id][k]; return max(left().query(s, e, k), right().query(s, e, k)); } void update(int s, int e, int x, int k) { if (r < s or e < l) return; if (s <= l and r <= e) { lazy[id][k] = max(lazy[id][k], x); lazy_update(); return; } left().update(s, e, x, k); right().update(s, e, x, k); tree[id][k] = max(tree[2 * id][k], tree[2 * id + 1][k]); } }; void solve() { int c = 1; scanf("%d", &n); scanf("%d", &q); allQx.push_back(0); allQy.push_back(0); for (int i = 1; i <= q; i++) { int x, y; char c; cin >> x >> y >> c; v.push_back(make_pair(make_pair(x, y), c)); allQx.push_back(x); allQy.push_back(y); } allQx.push_back(n); allQy.push_back(n); sort(allQx.begin(), allQx.end()); sort(allQy.begin(), allQy.end()); for (int i = 0; i < allQx.size(); i++) { if (ma1.find(allQx[i]) == ma1.end()) { ma1[allQx[i]] = c++; } } c = 1; for (int i = 0; i < allQy.size(); i++) { if (ma2.find(allQy[i]) == ma2.end()) { ma2[allQy[i]] = c++; } } node R(1, q, 1, 0); node C(1, q, 1, 1); for (int i = 0; i < v.size(); i++) { char type = v[i].second; if (type == 'U') { int x = R.query(ma1[v[i].first.first], ma1[v[i].first.first], 0); C.update(ma2[x], ma2[v[i].first.second], v[i].first.first, 1); R.update(ma1[v[i].first.first], ma1[v[i].first.first], v[i].first.second, 0); printf("%d", v[i].first.second - x); printf("\n"); } else { int x = C.query(ma2[v[i].first.second], ma2[v[i].first.second], 1); R.update(ma1[x], ma1[v[i].first.first], v[i].first.second, 0); C.update(ma2[v[i].first.second], ma2[v[i].first.second], v[i].first.first, 1); printf("%d", v[i].first.first - x); printf("\n"); } } } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeMap; public class CF555C_caseOfChocolates { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int q = sc.nextInt(); PriorityQueue<Integer> col = new PriorityQueue<>(); PriorityQueue<Integer> row = new PriorityQueue<>(); action[] arr = new action[q]; for (int i = 0; i < q; i++) { arr[i] = new action(sc.nextInt(), sc.nextInt(), sc.next().charAt(0)); col.add(arr[i].x); row.add(arr[i].y); } TreeMap<Integer, Integer> cols = new TreeMap<>(); TreeMap<Integer, Integer> rows = new TreeMap<>(); int cc = 1; int cr = 1; while (!col.isEmpty()) { if (!cols.containsKey(col.peek())) cols.put(col.peek(), cc++); if (!rows.containsKey(row.peek())) rows.put(row.peek(), cr++); col.poll(); row.poll(); } segmentTree column = new segmentTree(cc); segmentTree rowSt = new segmentTree(cr); PrintWriter out = new PrintWriter(System.out); for (int i = 0; i < q; i++) { if (arr[i].c == 'U') { int lastY = column.query(cols.get(arr[i].x)); out.println(arr[i].y - lastY); rowSt.update(lastY == 0 ? 1 : rows.get(lastY), rows.get(arr[i].y), arr[i].x); column.update(cols.get(arr[i].x), cols.get(arr[i].x), arr[i].y); } else { int lastX = rowSt.query(rows.get(arr[i].y)); out.println(arr[i].x - lastX); column.update(lastX == 0 ? 1 : cols.get(lastX), cols.get(arr[i].x), arr[i].y); rowSt.update(rows.get(arr[i].y), rows.get(arr[i].y), arr[i].x); } } out.close(); } static class action { int x, y; char c; public action(int a, int b, char d) { x = a; y = b; c = d; } } static class segmentTree { int[] tree; int N; public segmentTree(int n) { N = 1; while (N < n) N <<= 1; tree = new int[N << 1]; } public void update(int i, int j, int val) { update(1, 1, N, i, j, val); } public void update(int id, int l, int r, int i, int j, int val) { if (i > r || j < l) return; if (i <= l && r <= j) { tree[id] = Math.max(tree[id], val); return; } int mid = (l + r) / 2; update(id << 1, l, mid, i, j, val); update(id << 1 | 1, mid + 1, r, i, j, val); // tree[id] = Math.max(tree[id << 1], tree[id << 1 | 1]); } public int query(int idx) { idx += N - 1; int max = tree[idx]; while (idx > 1) { idx /= 2; max = Math.max(max, tree[idx]); } return max; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String f) throws FileNotFoundException { br = new BufferedReader(new FileReader(f)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public int[] nextIntArray1(int n) throws IOException { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } public int[] shuffle(int[] a, int n) { int[] b = new int[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); int t = b[i]; b[i] = b[j]; b[j] = t; } return b; } public int[] nextIntArraySorted(int n) throws IOException { int[] a = nextIntArray(n); a = shuffle(a, n); Arrays.sort(a); return a; } public long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public long[] nextLongArray1(int n) throws IOException { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } public long[] newLongArrayC1(int n) throws IOException { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) { a[i] = a[i - 1] + nextLong(); } return a; } public long[] nextLongArraySorted(int n) throws IOException { long[] a = nextLongArray(n); Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); long t = a[i]; a[i] = a[j]; a[j] = t; } Arrays.sort(a); return a; } } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; set<pair<long long, int> > s; set<pair<long long, int> >::iterator it; long long n, q, x, y; char dir; map<long long, long long> m; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> q; s.insert(make_pair(0, 1)); s.insert(make_pair(n + 1, 0)); for (int i = 0; i < q; i++) { cin >> x >> y >> dir; it = s.lower_bound(make_pair(x, -1)); pair<long long, int> p = *it; if (p.first == x) { cout << 0 << endl; continue; } else if (dir == 'U') { m[x] = p.first - x + (p.second == 0 ? 0 : m[p.first]); s.insert(make_pair(x, 1)); } else if (dir == 'L') { it--; p = *it; m[x] = x - p.first + (p.second == 1 ? 0 : m[p.first]); s.insert(make_pair(x, 0)); } cout << m[x] << endl; } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; set<pair<int, int> > s; set<pair<int, int> >::iterator it; int x[N], y[N]; int main() { int n, m; scanf("%d%d", &n, &m); s.insert(make_pair(n + 1, m + 1)); s.insert(make_pair(0, 0)); for (int i = 1; i <= m; i++) { char c[5]; scanf("%d%d%s", &x[i], &y[i], c); if (c[0] == 'U') { it = s.lower_bound(make_pair(y[i], -1)); if (it->first > y[i]) it--; } else { it = s.upper_bound(make_pair(y[i], -1)); } if (it->first == y[i]) { printf("0\n"); continue; } s.insert(make_pair(y[i], i)); if (c[0] == 'U') { printf("%d\n", y[i] - y[it->second]); y[i] = y[it->second]; } else { printf("%d\n", x[i] - x[it->second]); x[i] = x[it->second]; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const long long N = 200001; struct SegmentTree { vector<long long> t; SegmentTree() { t.assign(4 * N, 0); } void update(long long v, long long tl, long long tr, long long pos, long long new_val) { if (tl == tr) { t[v] = new_val; } else { long long tm = (tl + tr) / 2; if (pos <= tm) update(v * 2, tl, tm, pos, new_val); else update(v * 2 + 1, tm + 1, tr, pos, new_val); t[v] = max(t[v * 2], t[v * 2 + 1]); } } long long get_first(long long v, long long lv, long long rv, long long l, long long r, long long x) { if (lv > r || rv < l) return -1; if (l <= lv && rv <= r) { if (t[v] <= x) return -1; while (lv != rv) { long long mid = lv + (rv - lv) / 2; if (t[2 * v] > x) { v = 2 * v; rv = mid; } else { v = 2 * v + 1; lv = mid + 1; } } return lv; } long long mid = lv + (rv - lv) / 2; long long rs = get_first(2 * v, lv, mid, l, r, x); if (rs != -1) return rs; return get_first(2 * v + 1, mid + 1, rv, l, r, x); } long long get_last(long long v, long long lv, long long rv, long long l, long long r, long long x) { if (lv > r || rv < l) return -1; if (l <= lv && rv <= r) { if (t[v] <= x) return -1; while (lv != rv) { long long mid = lv + (rv - lv) / 2; if (t[2 * v + 1] > x) { v = 2 * v + 1; lv = mid + 1; } else { v = 2 * v; rv = mid; } } return rv; } long long mid = lv + (rv - lv) / 2; long long rs = get_last(2 * v + 1, mid + 1, rv, l, r, x); if (rs != -1) return rs; return get_last(2 * v, lv, mid, l, r, x); } }; struct node { long long x, y; char d; }; signed main() { long long t; t = 1; while (t--) { long long n, q; cin >> n >> q; vector<long long> cy; map<long long, long long> my, vis; vector<node> a(q); for (long long i = 0; i < q; ++i) { cin >> a[i].y >> a[i].x >> a[i].d; if (a[i].d == 'U') a[i].d = 'R'; if (a[i].d == 'L') a[i].d = 'D'; cy.push_back(a[i].y); } sort(cy.begin(), cy.end()); unique(cy.begin(), cy.end()); for (long long i = 0; i < cy.size(); ++i) { my[cy[i]] = i; } SegmentTree sd, sr; long long n1 = cy.size(); for (long long i = 0; i < q; ++i) { if (a[i].d == 'D') { if (vis[a[i].y]) { cout << "0\n"; continue; } vis[a[i].y] = 1; long long pos = my[a[i].y]; long long z = sr.get_last(1, 0, n1 - 1, 0, pos, a[i].y - 1); if (z == -1) { cout << a[i].y << "\n"; sd.update(1, 0, n1 - 1, pos, n); } else { cout << a[i].y - cy[z] << "\n"; sd.update(1, 0, n1 - 1, pos, n - cy[z] + 1); } } else { if (vis[a[i].y]) { cout << "0\n"; continue; } vis[a[i].y] = 1; long long pos = my[a[i].y]; long long z = sd.get_first(1, 0, n1 - 1, pos, n1 - 1, n - a[i].y); if (z == -1) { cout << n + 1 - a[i].y << "\n"; sr.update(1, 0, n1 - 1, pos, n); } else { cout << cy[z] - a[i].y << "\n"; sr.update(1, 0, n1 - 1, pos, cy[z]); } } } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const double PI = acos(-1); const int N = 2e5 + 10; int n, q; set<int> cells, negcells; set<int>::iterator it; unordered_map<int, int> blocked; unordered_map<int, char> dir; int getUP(int at) { it = cells.lower_bound(at); if (it != cells.end()) { if (*it == at) return -1; return (dir[*it] == 'U') ? blocked[*it] : (n + 1 - *it); } return 0; } int getDOWN(int at) { it = negcells.lower_bound(-at); if (it != negcells.end()) { if (-*it == at) return -1; return (dir[-*it] == 'L') ? blocked[-*it] : -*it; } return 0; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q; for (int i = 0; i < (q); i++) { int x, y; char d; cin >> x >> y >> d; if (d == 'U') { int next = getUP(x); if (next != -1) { blocked[x] = next; dir[x] = 'U'; cout << (n + 1 - x) - next << endl; } else cout << 0 << endl; } else { int next = getDOWN(x); if (next != -1) { blocked[x] = next; dir[x] = 'L'; cout << x - next << endl; } else cout << 0 << endl; } cells.insert(x); negcells.insert(-x); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 9; set<pair<int, int> > vec; set<pair<int, int> >::iterator it; int ansx[MAX_N]; int ansy[MAX_N]; int main() { int N, M, T; while (scanf("%d%d", &N, &M) != EOF) { vec.clear(); memset(ansx, -1, sizeof(ansx)); memset(ansy, -1, sizeof(ansy)); ansx[0] = ansy[0] = 0; ansx[M + 1] = ansy[M + 1] = 0; vec.insert(make_pair(0, M + 1)); vec.insert(make_pair(N + 1, M + 1)); for (int i = 1; i <= M; i++) { char pos; cin >> ansx[i] >> ansy[i] >> pos; if (pos == 'U') { it = vec.lower_bound(make_pair(ansx[i], -1)); } else { it = vec.upper_bound(make_pair(ansx[i], M + 1)); it--; } if (it->first == ansx[i]) { puts("0"); continue; } vec.insert(make_pair(ansx[i], i)); if (pos == 'U') { printf("%d\n", ansy[i] - ansy[it->second]); ansy[i] = ansy[it->second]; } else { printf("%d\n", ansx[i] - ansx[it->second]); ansx[i] = ansx[it->second]; } } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; const double eps = 1e-15; const long long INF = (1LL << 60); const int N = 200000 + 100; int xis[N << 1]; bool vis[N << 1]; struct QUE { bool flag; int x, y; } que[N]; struct SegTree { int l, r; int val; } tree[2][N << 2]; int use[N << 2]; int k; void pushup(int p) { tree[k][p].val = min(tree[k][p << 1].val, tree[k][p << 1 | 1].val); } void build(int p, int l, int r) { tree[k][p].l = l; tree[k][p].r = r; if (l == r) { tree[k][p].val = inf; return; } int mid = (l + r) >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); pushup(p); } void update(int p, int pos, int val) { if (tree[k][p].l == tree[k][p].r) { tree[k][p].val = val; return; } int mid = (tree[k][p].l + tree[k][p].r) >> 1; if (pos <= mid) { update(p << 1, pos, val); } else { update(p << 1 | 1, pos, val); } pushup(p); } int tar, val, pos; int query(int p, int l, int r) { if (l <= tree[k][p].l && tree[k][p].r <= r) { return tree[k][p].val; } int mid = (tree[k][p].l + tree[k][p].r) >> 1; if (r <= mid) { return query(p << 1, l, r); } else if (l > mid) { return query(p << 1 | 1, l, r); } else { return min(query(p << 1, l, mid), query(p << 1 | 1, mid + 1, r)); } } void work1(int l, int r) { int L = l, R = r, mid; while (L <= R) { mid = (L + R) >> 1; int v = query(1, mid, r); if (v <= tar) { L = mid + 1; pos = mid; val = v; } else { R = mid - 1; } } } void work2(int l, int r) { int L = l, R = r, mid; while (L <= R) { mid = (L + R) >> 1; int v = query(1, l, mid); if (v <= tar) { R = mid - 1; pos = mid; val = v; } else { L = mid + 1; } } } char op[4]; int ret; int main() { int n, q; while (~scanf("%d%d", &n, &q)) { ret = 0; memset(vis, 0, sizeof(vis)); for (int i = 1; i <= q; ++i) { scanf("%d%d%s", &que[i].x, &que[i].y, op); if (op[0] == 'U') { que[i].flag = 0; } else { que[i].flag = 1; } swap(que[i].x, que[i].y); xis[++ret] = que[i].x; } sort(xis + 1, xis + 1 + ret); ret = unique(xis + 1, xis + 1 + ret) - xis - 1; k = 0; build(1, 1, ret); k = 1; build(1, 1, ret); for (int i = 1; i <= q; ++i) { int x = que[i].x; int y = que[i].y; int ans = 0; int indx = lower_bound(xis + 1, xis + 1 + ret, x) - xis; if (vis[indx]) { printf("0\n"); continue; } val = inf; vis[indx] = 1; if (que[i].flag == 0) { tar = y; if (indx == 1) { ans = x; } else { k = 0; work1(1, indx - 1); if (val <= tar) { ans = x - xis[pos]; } else { ans = x; } } k = 1; update(1, indx, x - ans + 1); } else { tar = x; if (indx == ret) { ans = y; } else { k = 1; work2(indx + 1, ret); if (val <= tar) { ans = y - (n + 1 - xis[pos]); } else { ans = y; } } k = 0; update(1, indx, y - ans + 1); } printf("%d\n", ans); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, m; map<int, pair<char, int> > mp; int x, y; char c; void query() { cin >> x >> y >> c; auto yy = mp.lower_bound(x); if (yy->first == x) { cout << "0" << endl; return; } if (c == 'L') { yy--; } int res = abs(yy->first - x); if (yy->second.first == c) { res += yy->second.second; } cout << res << endl; mp[x] = {c, res}; } int main() { cin >> n >> m; mp[0] = {'U', 0}; mp[n + 1] = {'L', 0}; for (int i = 0; i < m; i++) { query(); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.io.*; import java.util.*; public class CODEFORCES { @SuppressWarnings("rawtypes") static InputReader in; static PrintWriter out; public static class SegmentTree { int n; int arr[], lazy[]; SegmentTree(int n) { this.n = n; arr = new int[n << 2]; lazy = new int[n << 2]; } SegmentTree(int a[]) { this.n = a.length; arr = new int[n << 2]; lazy = new int[n << 2]; build(0, n - 1, 0, a); } void build(int l, int r, int c, int a[]) { if (l == r) { arr[c] = a[l]; return; } int mid = l + r >> 1; build(l, mid, (c << 1) + 1, a); build(mid + 1, r, (c << 1) + 2, a); arr[c] = merge(arr[(c << 1) + 1], arr[(c << 1) + 2]); } int merge(int a, int b) { return Math.max(a, b); } int lazymerge(int a, int b) { return Math.max(a, b); } boolean getInitialValue(long a) { return !(a == 0); } void check(int c, int l, int r) { if (getInitialValue(lazy[c])) { arr[c] = lazymerge(arr[c], lazy[c]); if (l != r) { lazy[(c << 1) + 1] = lazymerge(lazy[(c << 1) + 1], lazy[c]); lazy[(c << 1) + 2] = lazymerge(lazy[(c << 1) + 2], lazy[c]); } lazy[c] = 0; } } void update(int l, int r, int c, int x, int y, int val) { check(c, l, r); if (l > r || x > y || l > y || x > r) return; if (x <= l && y >= r) { arr[c] = lazymerge(arr[c], val); if (l != r) { lazy[(c << 1) + 1] = lazymerge(lazy[(c << 1) + 1], val); lazy[(c << 1) + 2] = lazymerge(lazy[(c << 1) + 2], val); } return; } int mid = l + r >> 1; update(l, mid, (c << 1) + 1, x, y, val); update(mid + 1, r, (c << 1) + 2, x, y, val); arr[c] = merge(arr[(c << 1) + 1], arr[(c << 1) + 2]); } void up(int x, int y, int val) { update(0, n - 1, 0, x, y, val); } int get(int l, int r, int c, int x, int y) { check(c, l, r); if (l > r || x > y || l > y || x > r) // return inverse value return 0; else if (x <= l && y >= r) return arr[c]; int mid = l + r >> 1; return merge(get(l, mid, (c << 1) + 1, x, y), get(mid + 1, r, (c << 1) + 2, x, y)); } int ans(int x, int y) { return get(0, n - 1, 0, x, y); } } static int n, m; static void solve() { n = in.ni(); m = in.ni(); int arr[][] = new int[m][2]; char c[] = new char[m]; TreeSet<Integer> set = new TreeSet<Integer>(); int p = n + 1; for (int i = 0; i < m; i++) { arr[i][0] = in.ni(); arr[i][1] = in.ni(); set.add(arr[i][0]); set.add(arr[i][1]); set.add(p - arr[i][0]); set.add(p - arr[i][1]); c[i] = in.readString().charAt(0); } set.add(0); set.add(n); int cnt = 0; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); HashMap<Integer, Integer> rev = new HashMap<Integer, Integer>(); for (int i : set) { rev.put(cnt, i); map.put(i, cnt++); } SegmentTree t[] = new SegmentTree[2]; for (int i = 0; i < 2; i++) t[i] = new SegmentTree(cnt + 1); for (int i = 0; i < m; i++) { int x = map.get(arr[i][0]), y = map.get(arr[i][1]); char ch = c[i]; if (ch == 'U') { int get1 = t[0].ans(y, y); int get = rev.get(get1); out.println(rev.get(y) - get); t[1].up(x, y - get1 + x, x); t[0].up(y, y, y); } else { int get1 = t[1].ans(x, x); int get = rev.get(get1); out.println(rev.get(x) - get); t[0].up(y, x + y - get1, y); t[1].up(x, x, x); } } } @SuppressWarnings("rawtypes") static void soln() { in = new InputReader(System.in); out = new PrintWriter(System.out); solve(); out.flush(); } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { try { soln(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } // To Get Input // Some Buffer Methods static class InputReader<SpaceCharFilter> { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nl(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; map<int, int> r; int x[200010], y[200010]; int main() { int n, q; cin >> n >> q; r[0] = r[n + 1] = q; x[q] = 0; y[q] = 0; while (q--) { char c; cin >> x[q] >> y[q] >> c; map<int, int>::iterator it = r.lower_bound(x[q]); if (it->first == x[q]) { puts("0"); continue; } r[x[q]] = q; if (c == 'U') { printf("%d\n", y[q] - y[it->second]); y[q] = y[it->second]; } else { it--; it--; printf("%d\n", x[q] - x[it->second]); x[q] = x[it->second]; } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, q, x, y, ans; char ch; map<int, pair<char, int> > mp; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; cin >> n >> q; mp[0] = {'U', 0}; mp[n + 1] = {'L', 0}; while (q--) { cin >> x >> y >> ch; auto it = mp.lower_bound(x); if (it->first == x) { cout << "0\n"; continue; } if (ch == 'L') --it; ans = abs(x - it->first); if (ch == it->second.first) ans += it->second.second; cout << ans << "\n"; mp[x] = {ch, ans}; } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, q; struct data1 { int x, ans; bool opt; bool operator<(const data1 &p) const { return x < p.x; } }; struct data2 { int x, ans; bool opt; bool operator<(const data2 &p) const { return x > p.x; } }; set<data1> s1; set<data2> s2; int main() { scanf("%d%d", &n, &q); data1 now1; now1.x = n + 1; now1.opt = 0; s1.insert(now1); data2 now2; now2.x = 0; now2.opt = 1; s2.insert(now2); for (int i = 1; i <= q; i++) { int x, y; char s0[3]; bool opt; scanf("%d%d%s", &x, &y, s0); if (s0[0] == 'U') opt = 1; else opt = 0; if (opt) { data1 nowx; nowx.x = x; data1 now = *s1.lower_bound(nowx); if (now.x == x) { printf("0\n"); continue; } data2 now2; if (now.opt) { int ans = now.ans + now.x - x; printf("%d\n", ans); now2.ans = now.ans = ans; now2.x = now.x = x; now2.opt = now.opt; s1.insert(now); s2.insert(now2); } else { int ans = now.x - x; printf("%d\n", ans); now2.ans = now.ans = ans; now2.x = now.x = x; now2.opt = now.opt = 1; s1.insert(now); s2.insert(now2); } } else { data2 nowx; nowx.x = x; data2 now = *s2.lower_bound(nowx); data1 now2; if (now.x == x) { printf("0\n"); continue; } if (!now.opt) { int ans = now.ans - now.x + x; printf("%d\n", ans); now2.ans = now.ans = ans; now2.x = now.x = x; now2.opt = now.opt; s1.insert(now2); s2.insert(now); } else { int ans = -now.x + x; printf("%d\n", ans); now2.ans = now.ans = ans; now2.x = now.x = x; now2.opt = now.opt = 0; s1.insert(now2); s2.insert(now); } } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, q, x, y; char c; map<int, int> M; pair<int, int> P, T; set<pair<int, int> > S; int main() { scanf("%d %d", &n, &q); T.first = 0; T.second = 2; S.insert(T); T.first = n + 1; T.second = 1; S.insert(T); for (int i = 1; i <= q; i++) { scanf("%d %d %c", &x, &y, &c); if (M.count(x) != 0) { printf("0\n"); continue; } T.first = x; T.second = 0; if (c == 'L') { P = *(--S.lower_bound(T)); if (P.second == 2) { M[x] = x - P.first; } else { M[x] = x - P.first + M[P.first]; } P.first = x; P.second = 1; S.insert(P); } else { P = *S.upper_bound(T); if (P.second == 1) { M[x] = P.first - x; } else { M[x] = M[P.first] + P.first - x; } P.first = x; P.second = 2; S.insert(P); } printf("%d\n", M[x]); } return (0); }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int read() { int w = 0, f = 1; char c = ' '; while (c < '0' || c > '9') c = getchar(), f = c == '-' ? -1 : f; while (c >= '0' && c <= '9') w = w * 10 + c - 48, c = getchar(); return w * f; } int readop() { char c = getchar(); while (c != 'U' && c != 'L') c = getchar(); return c == 'U'; } struct node { int x, y, op; } q[200005]; struct seg { int ls[200005 << 2], rs[200005 << 2], tag[200005 << 2]; void update(int p, int x, int y, int v, int l, int r) { if (x <= l && r <= y) return tag[p] = max(tag[p], v), void(); int mid = (l + r) >> 1; if (x <= mid) update(p << 1, x, y, v, l, mid); if (y > mid) update(p << 1 | 1, x, y, v, mid + 1, r); } int query(int p, int x, int l, int r) { if (l == r) return tag[p]; int mid = (l + r) >> 1; if (x <= mid) return max(tag[p], query(p << 1, x, l, mid)); return max(tag[p], query(p << 1 | 1, x, mid + 1, r)); } } s1, s2; bool vis[200005]; int n, m, w[200005], cnt; int work1(int x, int y) { if (vis[x]) return 0; vis[x] = 1; int v = s1.query(1, x, 1, cnt); s2.update(1, v + 1, y, x, 1, cnt); return w[cnt - v + 1] - w[x]; } int work2(int x, int y) { if (vis[x]) return 0; vis[x] = 1; int v = s2.query(1, y, 1, cnt); s1.update(1, v + 1, x, y, 1, cnt); return w[x] - w[v]; } signed main() { n = read(), m = read(); for (int i = 1; i <= m; i++) q[i].x = read(), q[i].y = read(), q[i].op = readop(), w[i] = q[i].x; sort(w + 1, w + m + 1); cnt = unique(w + 1, w + m + 1) - w - 1; for (int i = 1; i <= m; i++) q[i].x = lower_bound(w + 1, w + cnt + 1, q[i].x) - w, q[i].y = cnt - q[i].x + 1; w[cnt + 1] = n + 1; for (int i = 1; i <= m; i++) { printf("%d\n", q[i].op ? work1(q[i].x, q[i].y) : work2(q[i].x, q[i].y)); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int mod = 1000000000 + 7; int bs(int *st, int *en, int val, char c) { int k; if (c == 'l') k = lower_bound(st, en, val) - st; if (c == 'u') k = upper_bound(st, en, val) - st; return k; } set<int> sx; set<int> sy; int ans[200005]; map<int, int> mx, my, m; int xl, yl; struct inp { int x; int y; char c; }; inp in[2 * 100005]; struct st { int mx; bool is; }; vector<st> vx(4 * 8 * 100005); vector<st> vy(4 * 8 * 100005); void build(int lo, int hi, int at, vector<st> &v) { v[at].is = false; v[at].mx = 0; if (lo == hi) return; int mid = (lo + hi) / 2; build(lo, mid, 2 * at, v); build(mid + 1, hi, 2 * at + 1, v); } void undo_lazy(int at, vector<st> &v) { if (!v[at].is) return; v[at].is = false; v[2 * at].is = v[2 * at + 1].is = true; v[2 * at].mx = max(v[2 * at].mx, v[at].mx); v[2 * at + 1].mx = max(v[2 * at + 1].mx, v[at].mx); } void update(int lo, int hi, int at, int from, int to, vector<st> &v, int val) { if (lo != hi) undo_lazy(at, v); if (lo == from && hi == to) { v[at].is = true; v[at].mx = max(v[at].mx, val); return; } int mid = (lo + hi) / 2; if (to <= mid) { update(lo, mid, 2 * at, from, to, v, val); } else { if (from > mid) { update(mid + 1, hi, 2 * at + 1, from, to, v, val); } else { update(lo, mid, 2 * at, from, mid, v, val); update(mid + 1, hi, 2 * at + 1, mid + 1, to, v, val); } } } int q(int lo, int hi, int at, vector<st> &v, int pos) { if (lo != hi) undo_lazy(at, v); if (lo == hi) return v[at].mx; int mid = (lo + hi) / 2; if (pos <= mid) return q(lo, mid, 2 * at, v, pos); else return q(mid + 1, hi, 2 * at + 1, v, pos); } int main() { int a, b, c, d, e, x, y, z, n, qu; char ch; sx.clear(); sy.clear(); mx.clear(); my.clear(); sx.insert(0); sy.insert(0); scanf("%d %d", &n, &qu); set<int>::iterator i; for (a = 0; a < qu; a++) { scanf("%d %d %c", &in[a].x, &in[a].y, &in[a].c); sx.insert(in[a].x); sy.insert(in[a].y); } xl = yl = 0; for (i = sx.begin(); i != sx.end(); i++) { ++xl; mx[*i] = xl; } build(1, xl, 1, vx); for (i = sy.begin(); i != sy.end(); i++) { ++yl; my[*i] = yl; } build(1, yl, 1, vy); m.clear(); for (a = 0; a < qu; a++) { x = in[a].x; y = in[a].y; ch = in[a].c; if (m[x]) { printf("0\n"); continue; } m[x] = 1; if (ch == 'U') { z = q(1, xl, 1, vx, mx[x]); printf("%d\n", y - z); update(1, yl, 1, my[z], my[y], vy, x); } else { z = q(1, yl, 1, vy, my[y]); printf("%d\n", x - z); update(1, xl, 1, mx[z], mx[x], vx, y); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> const char en = '\n'; using namespace std; int main() { ios::sync_with_stdio(false); int n, q; cin >> n >> q; map<long long, long long> M; M[n + 1] = -n - 1; M[0] = n + 1; while (q--) { int x, y; char c; cin >> x >> y >> c; if (c == 'U') { auto it = M.lower_bound(x); int p = 0; if (it->second > 0) p = abs(it->first - x) + it->second; else p = abs(it->first - x); if (M.find(x) == M.end()) { M[x] = p; cout << abs(p) << en; } else cout << 0 << en; } else { auto it = M.upper_bound(x); it--; int p; if (it->second > 0) p = abs(it->first - x); else p = abs(it->first - x) + abs(it->second); if (M.find(x) == M.end()) { M[x] = -abs(p); cout << abs(p) << en; } else cout << 0 << en; } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.awt.Point; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.TreeMap; public class main { static class Area { Area(int row, int rows, int rowsize, int columnsize) { this.row = row; this.rows = rows; this.rowsize = rowsize; this.columnsize = columnsize; } int row; int rows; int rowsize; int columnsize; } public static void main(String[] args) { try { try (Scanner scanner = new Scanner(System.in)) { int n = scanner.nextInt(); int q=scanner.nextInt(); TreeMap<Integer, Area> map=new TreeMap<>(); map.put(1, new Area(1, n, n, n)); for (int i=0; i<q; i++) { int c=scanner.nextInt(); int r=scanner.nextInt(); String d=scanner.next(); Entry<Integer, Area> e = map.floorEntry(r); int p=0; if (e!=null) { Area a = e.getValue(); if (r>=a.row && r<a.row+a.rows) { if (d.charAt(0)=='U') { p=a.rowsize-(a.row+a.rows-1-r); if (r==a.row) { a.row++; a.rows--; a.columnsize--; } else if (r==a.row+a.rows-1) { a.rows--; a.rowsize--; a.columnsize=a.rows; } else { Area a2 = new Area(r+1, a.row+a.rows-r-1, a.rowsize, a.columnsize-(r-a.row+1)); map.put(a2.row, a2); a.rows=r-a.row; a.rowsize-=a2.rows+1; a.columnsize=a.rows; } } else { p=a.columnsize-(r-a.row); if (r==a.row) { a.row++; a.rows--; a.rowsize=a.rows; a.columnsize--; } else if (r==a.row+a.rows-1) { a.rows--; a.rowsize--; } else { Area a2 = new Area(r+1, a.row+a.rows-r-1, a.row+a.rows-r-1, a.columnsize-(r-a.row+1)); map.put(a2.row, a2); a.rows=r-a.row; a.rowsize-=a2.rows+1; } } if (a.rows==0) map.remove(e.getKey()); } } System.out.println(p); } } } catch (NullPointerException e) { } } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; struct Node { int l, r, len_u, len_l; inline Node() {} inline Node(int _l, int _r, int _len_u, int _len_l) : l(_l), r(_r), len_u(_len_u), len_l(_len_l) {} inline bool operator<(const Node &oth) const { return l < oth.l; } }; int n, q; set<Node> st; int main() { scanf("%d%d", &n, &q); st.insert(Node(1, n, 0, 0)); while (q--) { int x; char ty; scanf("%d%*d %c", &x, &ty); auto it = st.lower_bound(Node(x + 1, 0, 0, 0)); if (it == st.begin()) { puts("0"); continue; } --it; Node cur = *it; if (!(cur.l <= x && x <= cur.r)) { puts("0"); continue; } st.erase(it); if (ty == 'U') { printf("%d\n", cur.len_u + cur.r - x + 1); if (cur.l < x) { st.insert(Node(cur.l, x - 1, cur.len_u + cur.r - x + 1, cur.len_l)); } if (cur.r > x) { st.insert(Node(x + 1, cur.r, cur.len_u, 0)); } } else { printf("%d\n", cur.len_l + x - cur.l + 1); if (cur.l < x) { st.insert(Node(cur.l, x - 1, 0, cur.len_l)); } if (cur.r > x) { st.insert(Node(x + 1, cur.r, cur.len_u, cur.len_l + x - cur.l + 1)); } } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int INF = 0x7fFFffFF; struct MinTree { int n; vector<int> c; MinTree(int _n) : n(_n), c(4 * _n + 3, INF) {} int _query(int l, int r, int nl, int nr, int node) { if (l <= nl && nr <= r) return c[node]; if (nr < l || r < nl) return INF; return min(_query(l, r, nl, (nl + nr) / 2, node * 2), _query(l, r, (nl + nr) / 2 + 1, nr, node * 2 + 1)); } int query(int v, int r) { if (_query(1, r, 1, n, 1) > v) return 0; int lo = 1, hi = r; while (lo < hi) { int mid = (lo + hi + 1) / 2; if (_query(mid, r, 1, n, 1) <= v) { lo = mid; } else { hi = mid - 1; } } return lo; } void _update(int x, int v, int nl, int nr, int node) { if (nl <= x && x <= nr) { c[node] = min(c[node], v); } else return; if (nl < nr) { _update(x, v, nl, (nl + nr) / 2, node * 2); _update(x, v, (nl + nr) / 2 + 1, nr, node * 2 + 1); } } void update(int x, int v) { _update(x, v, 1, n, 1); } }; const int QMAX = 222222; int C[QMAX], R[QMAX]; char W[QMAX]; int n, q; int ans[QMAX]; void process() { unordered_map<int, int> M, Minv; set<int> s; for (int i = 0; i < q; i++) { s.insert(R[i]), s.insert(C[i]); } int idx = 1; for (auto x : s) M[x] = idx++; for (auto& kv : M) Minv[kv.second] = kv.first; Minv[0] = 0; int m = M.size(); MinTree col(m), row(m); for (int i = 1; i <= m; i++) col.update(i, m + 1 - i), row.update(i, m + 1 - i); vector<bool> visited(m + 1, false); for (int i = 0; i < q; i++) { if (visited[M[C[i]]]) cout << 0 << endl; else { visited[M[C[i]]] = true; int c = M[C[i]], r = M[R[i]]; if (W[i] == 'U') { int res = row.query(c - 1, r - 1); cout << Minv[r] - Minv[res] << endl; col.update(c, res); } else { int res = col.query(r - 1, c - 1); cout << Minv[c] - Minv[res] << endl; row.update(r, res); } } } } int main() { cin.sync_with_stdio(0), cout.sync_with_stdio(0); cin >> n >> q; for (int i = 0; i < q; i++) { int a, b; string c; cin >> a >> b >> c; C[i] = a, R[i] = b, W[i] = c[0]; } process(); }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; struct ask { int x, y; string xx; } ask[200001]; struct tree { int l, r; int ma; int tag; } tr[2][1600001]; inline void build(int p, int l, int r, int pp) { tr[pp][p].l = l; tr[pp][p].r = r; if (l != r) { int mid = (l + r) / 2; build(p * 2, l, mid, pp); build(p * 2 + 1, mid + 1, r, pp); } } inline void push(int p, int pp) { tr[pp][p * 2].ma = max(tr[pp][p * 2].ma, tr[pp][p].tag); tr[pp][p * 2 + 1].ma = max(tr[pp][p * 2 + 1].ma, tr[pp][p].tag); tr[pp][p * 2].tag = max(tr[pp][p * 2].tag, tr[pp][p].tag); tr[pp][p * 2 + 1].tag = max(tr[pp][p * 2 + 1].tag, tr[pp][p].tag); tr[pp][p].tag = 0; } inline void up(int p, int pp) { tr[pp][p].ma = max(tr[pp][p * 2].ma, tr[pp][p * 2 + 1].ma); } inline int findx(int p, int l, int r, int pp) { if (l <= tr[pp][p].l && tr[pp][p].r <= r) return tr[pp][p].ma; else { push(p, pp); int mid = (tr[pp][p].l + tr[pp][p].r) / 2; int ans = 0; if (l <= mid) ans = max(ans, findx(p * 2, l, r, pp)); if (r > mid) ans = max(ans, findx(p * 2 + 1, l, r, pp)); up(p, pp); return ans; } } inline void change(int p, int l, int r, int x, int pp) { if (l <= tr[pp][p].l && tr[pp][p].r <= r) { tr[pp][p].ma = max(tr[pp][p].ma, x); tr[pp][p].tag = max(tr[pp][p].tag, x); } else { push(p, pp); int mid = (tr[pp][p].l + tr[pp][p].r) / 2; int ans = 0; if (l <= mid) change(p * 2, l, r, x, pp); if (r > mid) change(p * 2 + 1, l, r, x, pp); up(p, pp); } } struct number { int x, p; bool operator<(number y) const { return x < y.x; } } p1[200001], p2[200001]; int f1[200001], f2[200001]; int fx1[200001], fx2[200001]; int sx1[200001], sx2[200001]; int main() { int n, q; scanf("%d%d", &n, &q); int i; int pp1 = 0, pp2 = 0; for (i = 1; i <= q; i++) { cin >> ask[i].x >> ask[i].y >> ask[i].xx; pp1++; p1[pp1].x = ask[i].x; p1[pp1].p = i; pp2++; p2[pp2].x = ask[i].y; p2[pp2].p = i; } sort(p1 + 1, p1 + 1 + pp1); sort(p2 + 1, p2 + 1 + pp1); int s1 = 1, s2 = 1; f1[s1] = p1[1].x; fx1[p1[1].p] = s1; for (i = 2; i <= pp1; i++) { if (p1[i].x != p1[i - 1].x) { s1++; f1[s1] = p1[i].x; } fx1[p1[i].p] = s1; } f2[s2] = p2[1].x; fx2[p2[1].p] = s2; for (i = 2; i <= pp2; i++) { if (p2[i].x != p2[i - 1].x) { s2++; f2[s2] = p2[i].x; } fx2[p2[i].p] = s2; } build(1, 1, s1, 0); build(1, 1, s2, 1); int ans; for (i = 1; i <= q; i++) { if (sx1[fx1[i]] == 1) { printf("0\n"); continue; } if (ask[i].xx == "U") { ans = findx(1, fx1[i], fx1[i], 0); printf("%d\n", ask[i].y - f2[ans]); change(1, ans, fx2[i], fx1[i], 1); } else { ans = findx(1, fx2[i], fx2[i], 1); printf("%d\n", ask[i].x - f1[ans]); change(1, ans, fx1[i], fx2[i], 0); } sx1[fx1[i]] = 1; sx2[fx2[i]] = 1; } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; struct t { int v; t() { v = 0; } t(int v_) { v = v_; } t operator+(const t &t_) { return t(max(v, t_.v)); } }; struct u { int v; u() { v = 0; } u(int v_) { v = v_; } t operator()(const t &t_) { return t(max(v, t_.v)); } u operator+(const u &u_) { return u(max(v, u_.v)); } }; template <typename T, typename U> struct seg_tree_lazy { int S, H; T zero; vector<T> value; U noop; vector<bool> dirty; vector<U> prop; seg_tree_lazy<T, U>(int _S, T _zero = T(), U _noop = U()) { zero = _zero, noop = _noop; for (S = 1, H = 1; S < _S;) S *= 2, H++; value.resize(2 * S, zero); dirty.resize(2 * S, false); prop.resize(2 * S, noop); } void set_leaves(vector<T> &leaves) { copy(leaves.begin(), leaves.end(), value.begin() + S); for (int i = S - 1; i > 0; i--) value[i] = value[2 * i] + value[2 * i + 1]; } void apply(int i, U &update) { value[i] = update(value[i]); if (i < S) { prop[i] = prop[i] + update; dirty[i] = true; } } void rebuild(int i) { for (int l = i / 2; l; l /= 2) { T combined = value[2 * l] + value[2 * l + 1]; value[l] = prop[l](combined); } } void propagate(int i) { for (int h = H; h > 0; h--) { int l = i >> h; if (dirty[l]) { apply(2 * l, prop[l]); apply(2 * l + 1, prop[l]); prop[l] = noop; dirty[l] = false; } } } void upd(int i, int j, U update) { i += S, j += S; propagate(i), propagate(j); for (int l = i, r = j; l <= r; l /= 2, r /= 2) { if ((l & 1) == 1) apply(l++, update); if ((r & 1) == 0) apply(r--, update); } rebuild(i), rebuild(j); } T query(int i, int j) { i += S, j += S; propagate(i), propagate(j); T res_left = zero, res_right = zero; for (; i <= j; i /= 2, j /= 2) { if ((i & 1) == 1) res_left = res_left + value[i++]; if ((j & 1) == 0) res_right = value[j--] + res_right; } return res_left + res_right; } }; int qu[200005][3]; int l[400005]; bool done[400005]; const int db = 0; int main() { int n, q, a, b; char c; set<int> coords; cin >> n >> q; for (int i = 0; i < q; i++) { cin >> a >> b >> c; coords.insert(a); coords.insert(b); if (c == 'U') { qu[i][0] = 0; qu[i][1] = a; qu[i][2] = b; } else { qu[i][0] = 1; qu[i][1] = a; qu[i][2] = b; } } if (db) cout << "hi" << endl; map<int, int> m; int cur = 1; for (auto i : coords) { m[i] = cur; l[cur++] = i; } if (db) cout << "hi" << endl; seg_tree_lazy<t, u> foru(400005), forl(400005); if (db) cout << "hi" << endl; for (int qi = 0; qi < q; qi++) { if (db) cout << "Current query: " << qi << endl; if (done[m[qu[qi][1]]]) { cout << 0 << endl; continue; } done[m[qu[qi][1]]] = 1; if (qu[qi][0] == 0) { if (db) cout << "UP" << endl; int curcu = qu[qi][1]; int curru = qu[qi][2]; if (db) cout << "cur*u: " << curcu << " " << curru << endl; int curcc = m[curcu]; int currc = m[curru]; if (db) cout << "cur*c: " << curcc << " " << currc << endl; int minrc = foru.query(curcc, curcc).v; if (db) cout << "minrc: " << minrc << endl; int minru = l[minrc]; if (db) cout << "minr*: " << minrc << " " << minru << endl; forl.upd(minrc, currc, u(curcc)); cout << (curru - minru) << endl; } else { if (db) cout << "LEFT" << endl; int curcu = qu[qi][1]; int curru = qu[qi][2]; if (db) cout << "cur*u: " << curcu << " " << curru << endl; int curcc = m[curcu]; int currc = m[curru]; if (db) cout << "cur*c: " << curcc << " " << currc << endl; int mincc = forl.query(currc, currc).v; if (db) cout << "mincc: " << mincc << endl; int mincu = l[mincc]; if (db) cout << "minc*: " << mincc << " " << mincu << endl; foru.upd(mincc, curcc, u(currc)); cout << (curcu - mincu) << endl; } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; map<int, pair<char, int> > inMap; map<int, pair<char, int> >::iterator itlow; int N, Q, i, x, y, ans; string type; int main() { cin >> N >> Q; inMap[0] = {'U', 0}; inMap[N + 1] = {'L', 0}; for (i = 1; i <= Q; ++i) { cin >> x >> y >> type; itlow = inMap.lower_bound(x); if ((*itlow).first == x) { cout << "0" << '\n'; continue; } if (type[0] == 'L') --itlow; ans = (*itlow).first - x; ans = (ans > 0) ? ans : -ans; if ((*itlow).second.first == type[0]) ans += (*itlow).second.second; inMap[x] = {type[0], ans}; cout << ans << '\n'; } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, ans, T; map<int, int> l; map<int, int> u; int main() { scanf("%d %d", &n, &T); while (T--) { int x, y; char ch; scanf("%d %d", &x, &y); ch = getchar(); while (ch != 'U' && ch != 'L') ch = getchar(); if (ch == 'U') { map<int, int>::iterator p; p = u.lower_bound(x); if (u.count(x)) { printf("0\n"); continue; } if (p == u.end()) ans = y; else ans = y - (p->second); printf("%d\n", ans); u[x] = y - ans; l[y] = x; } else { map<int, int>::iterator p; p = l.lower_bound(y); if (l.count(y)) { printf("0\n"); continue; } if (p == l.end()) ans = x; else ans = x - (p->second); printf("%d\n", ans); u[x] = y; l[y] = x - ans; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> struct Piece { int x0, x1, y0, y1, n; Piece left(int x) { return {x0, std::min(x - 1, x1), y0, y1, n}; } Piece right(int x) { return {std::max(x0, x + 1), x1, y0, y1, n}; } Piece up(int y) { return {x0, x1, y0, std::min(y - 1, y1), n}; } Piece down(int y) { return {x0, x1, std::max(y0, y + 1), y1, n}; } int diag() const { int x = std::max(x0, n - y1 + 1); return has(x, n - x + 1) ? x : -1; } bool operator<(const Piece &p) const { return diag() < p.diag(); } bool has(int x, int y) const { return x0 <= x && x <= x1 && y0 <= y && y <= y1; } void print() const { printf("[%d,%d][%d,%d] diag()=%d\n", x0, x1, y0, y1, diag()); } }; int main() { int n, q; scanf("%d%d", &n, &q); std::set<Piece> S; S.insert(Piece{1, n, 1, n, n}); while (q--) { int x, y; char d; scanf("%d%d %c", &x, &y, &d); auto it = S.upper_bound(Piece{x, x, y, y, n}); it--; if (!it->has(x, y)) { puts("0"); continue; } Piece p = *it; S.erase(it); if (d == 'U') { printf("%d\n", y - p.y0 + 1); S.insert(p.left(x)); S.insert(p.right(x)); } else { printf("%d\n", x - p.x0 + 1); S.insert(p.up(y)); S.insert(p.down(y)); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> const int md = 1e9 + 7; using namespace std; int n, q, x, y; char c; struct shape { int l, r, h, w; shape(int l = 0, int r = 0, int h = 0, int w = 0) : l(l), r(r), h(h), w(w){}; pair<shape, shape> cut(int x, char d) { int l1 = l, r1 = x - 1, l2 = x + 1, r2 = r, h1, w1, h2, w2; w1 = w, h2 = h; if (d == 'U') { h1 = h + r - r1; w2 = 0; } else { h1 = 0; w2 = w + l2 - l; } if (l1 > r1) l1 = r1 = h1 = w1 = -1; if (l2 > r2) l2 = r2 = h2 = w2 = -1; return make_pair(shape(l1, r1, h1, w1), shape(l2, r2, h2, w2)); } }; void show(map<int, shape> &m) { for (auto i = m.begin(); i != m.end(); i++) cout << i->second.l << " " << i->second.r << " " << i->second.h << " " << i->second.w << '\n'; cout << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> q; map<int, shape> m; m[1] = shape(1, n, 0, 0); while (q--) { cin >> x >> y >> c; auto it = m.upper_bound(x); if (it == m.begin()) cout << 0 << '\n'; else { it--; if (x > it->second.r) cout << 0 << '\n'; else { shape s = it->second; if (c == 'U') cout << s.h + s.r - x + 1 << '\n'; else cout << s.w + x - s.l + 1 << '\n'; pair<shape, shape> temp = s.cut(x, c); m.erase(it); if (temp.first.l != -1) m[temp.first.l] = temp.first; if (temp.second.l != -1) m[temp.second.l] = temp.second; } } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; inline int read() { register int x; register char c(getchar()); register bool k; while ((c < '0' || c > '9') && c ^ '-') if ((c = getchar()) == EOF) exit(0); if (c ^ '-') x = c & 15, k = 1; else x = 0, k = 0; while (c = getchar(), c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c & 15); return k ? x : -x; } void wr(register int a) { if (a < 0) putchar('-'), a = -a; if (a <= 9) putchar(a | '0'); else wr(a / 10), putchar((a % 10) | '0'); } int n, q, x, y, v; char c; struct T { int u, v; char c; inline bool operator<(T b) const { return u < b.u; } }; set<T> s; set<T>::iterator it; signed main() { (n = read()), (q = read()), s.insert({0, 0, 'U'}), s.insert({n + 1, 0, 'L'}); while (q--) { (x = read()), (y = read()), cin >> c; if (s.find({x, 0, 0}) != s.end()) wr(0); else if (c == 'U') { it = s.lower_bound({x, 0, 0}); v = it->c == 'U' ? it->v : n - (it->u) + 2; s.insert({x, v, 'U'}), wr(y - v + 1); } else { it = --s.lower_bound({x, 0, 0}); v = it->c == 'L' ? it->v : (it->u) + 1; s.insert({x, v, 'L'}), wr(x - v + 1); } putchar('\n'); } exit(0); }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; set<std::pair<int, int> > st; set<std::pair<int, int> >::iterator it; int n, m, x[M], y[M]; char str[2]; int main() { scanf("%d %d", &n, &m); st.insert(make_pair(0, 0)); st.insert(make_pair(n + 1, m + 1)); for (int i = 1; i <= m; i++) { scanf("%d%d%s", x + i, y + i, str); bool ok = *str == 'U'; if (ok) it = st.lower_bound(make_pair(x[i], 0)); else it = st.upper_bound(make_pair(x[i], m + 1)), it--; if (x[i] == it->first) { puts("0"); continue; } st.insert(make_pair(x[i], i)); int j = it->second; if (ok) { printf("%d\n", y[i] - y[j]); y[i] = y[j]; } else { printf("%d\n", x[i] - x[j]); x[i] = x[j]; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskE solver = new TaskE(); solver.solve(1, in, out); out.close(); } static class TaskE { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int q = in.nextInt(); Integer[] rows = new Integer[q]; Integer[] cols = new Integer[q]; char[] directions = new char[q]; for (int i = 0; i < q; i++) { cols[i] = in.nextInt(); rows[i] = in.nextInt(); directions[i] = in.next().toCharArray()[0]; } HashMap<Integer, Integer> rankValues = getRank(Arrays.copyOf(cols, cols.length), Arrays.copyOf(rows, rows.length)); MagicTree columnsTree = new MagicTree(rankValues.size()); MagicTree rowsTree = new MagicTree(rankValues.size()); StringBuilder res = new StringBuilder(); for (int i = 0; i < q; i++) { if (directions[i] == 'U') { int finishAt = columnsTree.getValue(rankValues.get(cols[i])); res.append(rows[i] - finishAt); res.append("\n"); rowsTree.update(rankValues.get(finishAt), rankValues.get(rows[i]), cols[i]); columnsTree.update(rankValues.get(cols[i]), rankValues.get(cols[i]), rows[i]); } else { int finishAt = rowsTree.getValue(rankValues.get(rows[i])); res.append(cols[i] - finishAt); res.append("\n"); columnsTree.update(rankValues.get(finishAt), rankValues.get(cols[i]), rows[i]); rowsTree.update(rankValues.get(rows[i]), rankValues.get(rows[i]), cols[i]); } } out.println(res); } HashMap<Integer, Integer> getRank(Integer[] a, Integer[] b) { Integer[] c = new Integer[a.length + b.length]; for (int i = 0; i < c.length; i++) { if (i < a.length) { c[i] = a[i]; } else { c[i] = b[i - a.length]; } } Arrays.sort(c); HashMap<Integer, Integer> rank = new HashMap<Integer, Integer>(); rank.put(0, 0); int r = 1; for (int i = 0; i < c.length; i++) { if (!rank.containsKey(c[i])) { rank.put(c[i], r++); } } return rank; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class MagicTree { int[] t; int[] lazy; int n; MagicTree(int n) { this.t = new int[4 * n + 10]; this.lazy = new int[this.t.length]; this.n = n; } void update(int from, int to, int value) { internalUpdate(0, n - 1, from, to, 0, value); } private void internalUpdate(int left, int right, int from, int to, int node, int value) { if (lazy[node] != 0) { t[node] = Math.max(t[node], lazy[node]); if (left != right) { lazy[2 * node + 1] = Math.max(lazy[2 * node + 1], lazy[node]); lazy[2 * node + 2] = Math.max(lazy[2 * node + 2], lazy[node]); } lazy[node] = 0; } if (right < from || left > to) return; if (from <= left && right <= to) { lazy[node] = Math.max(lazy[node], value); if (left != right) { lazy[2 * node + 1] = Math.max(lazy[2 * node + 1], lazy[node]); lazy[2 * node + 2] = Math.max(lazy[2 * node + 2], lazy[node]); } return; } int mid = (left + right) >> 1; internalUpdate(left, mid, from, to, 2 * node + 1, value); internalUpdate(mid + 1, right, from, to, 2 * node + 2, value); } int getValue(int index) { return interalQuery(0, n - 1, index, 0); } private int interalQuery(int left, int right, int index, int node) { if (lazy[node] != 0) { t[node] = Math.max(t[node], lazy[node]); if (left != right) { lazy[2 * node + 1] = Math.max(lazy[2 * node + 1], lazy[node]); lazy[2 * node + 2] = Math.max(lazy[2 * node + 2], lazy[node]); } lazy[node] = 0; } if (right < index || left > index) return 0; if (index <= left && right <= index) return t[node]; int mid = (left + right) >> 1; int leftNode = interalQuery(left, mid, index, 2 * node + 1); int rightNode = interalQuery(mid + 1, right, index, 2 * node + 2); return Math.max(leftNode, rightNode); } } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; long long int inline ipow(long long int a, long long int b, long long int m) { long long int val = 1; a %= m; while (b) { if (b & 01) val = (val * a) % m; b >>= 1; a = (a * a) % m; }; return val % m; } long long int inline ipow(long long int a, long long int b) { long long int val = 1; while (b) { if (b & 01) val = (val * a); b >>= 1; a = (a * a); }; return val; } map<int, pair<char, int> > m; void ans(int x, int y, char q) { map<int, pair<char, int> >::iterator it = m.lower_bound(x); if (it->first == x) { cout << 0 << endl; return; } if (q == 'L') it--; int ans1 = abs(it->first - x); if (it->second.first == q) ans1 += it->second.second; cout << ans1 << endl; m[x] = make_pair(q, ans1); } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); clock_t tStart = clock(); int n, x, y; char q; cin >> n; m[0] = make_pair('U', 0); m[n + 1] = make_pair('L', 0); int t; cin >> t; while (t--) { cin >> x >> y >> q; ans(x, y, q); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, q; const int N = (1 << 20) + 2, MOD = 1000000007LL; struct Query { int x, y; char typ; } queries[N]; struct segmentTree { int tree[N]; int lazy[N]; void push_down(int ni, int ns, int ne) { if (ns == ne || !lazy[ni]) return; int lf = 2 * ni + 1, rt = lf + 1; tree[lf] = max(lazy[ni], tree[lf]); tree[rt] = max(lazy[ni], tree[rt]); lazy[lf] = max(lazy[ni], lazy[lf]); lazy[rt] = max(lazy[ni], lazy[rt]); lazy[ni] = 0; } void update(int qs, int qe, int val, int ni = 0, int ns = 0, int ne = n - 1) { if (qs > ne || qe < ns) return; push_down(ni, ns, ne); if (ns >= qs && ne <= qe) { tree[ni] = max(tree[ni], val); lazy[ni] = val; return; } int lf = 2 * ni + 1, rt = lf + 1, mid = ns + (ne - ns) / 2; update(qs, qe, val, lf, ns, mid); update(qs, qe, val, rt, mid + 1, ne); tree[ni] = max(tree[lf], tree[rt]); } int query(int qs, int qe, int ni = 0, int ns = 0, int ne = n - 1) { if (qs > ne || qe < ns) return 0; push_down(ni, ns, ne); if (ns >= qs && ne <= qe) { return tree[ni]; } int lf = 2 * ni + 1, rt = lf + 1, mid = ns + (ne - ns) / 2; return max(query(qs, qe, lf, ns, mid), query(qs, qe, rt, mid + 1, ne)); } }; segmentTree a, b; int main() { scanf("%d%d", &n, &q); n = 4e5 + 2; vector<int> compo(q * 2); int idx = 0; for (int i = 0; i < q; i++) { scanf("%d%d %c", &queries[i].x, &queries[i].y, &queries[i].typ); compo[idx++] = queries[i].x; compo[idx++] = queries[i].y; } sort(compo.begin(), compo.end()); auto it = unique(compo.begin(), compo.end()); for (int i = 0; i < q; i++) { int X = lower_bound(compo.begin(), it, queries[i].x) - compo.begin(); int Y = lower_bound(compo.begin(), it, queries[i].y) - compo.begin(); if (queries[i].typ == 'U') { int ans = queries[i].y - a.query(X, X); cout << ans << endl; int st = lower_bound(compo.begin(), it, a.query(X, X)) - compo.begin(); a.update(X, X, queries[i].y); b.update(st, Y, queries[i].x); } else { int ans = queries[i].x - b.query(Y, Y); cout << ans << endl; int st = lower_bound(compo.begin(), it, b.query(Y, Y)) - compo.begin(); a.update(st, X, queries[i].y); b.update(Y, Y, queries[i].x); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int x[400010], y[400010], op[400010], hashx[400010], hashy[400010], cnt1 = 0, cnt2 = 0, Max[2][400010 << 2], lazy[2][400010 << 2]; map<pair<int, int>, bool> M; void pushdown(int i, int type) { if (lazy[type][i]) { lazy[type][i << 1] = max(lazy[type][i << 1], lazy[type][i]); lazy[type][(i << 1) | 1] = max(lazy[type][(i << 1) | 1], lazy[type][i]); Max[type][i << 1] = max(Max[type][i << 1], lazy[type][i]); Max[type][(i << 1) | 1] = max(Max[type][(i << 1) | 1], lazy[type][i]); lazy[type][i] = 0; } } void update(int i, int l, int r, int a, int b, int d, int type) { if (l == a && r == b) { Max[type][i] = max(Max[type][i], d); lazy[type][i] = max(lazy[type][i], d); } else { pushdown(i, type); int mid = (l + r) >> 1; if (b <= mid) update(i << 1, l, mid, a, b, d, type); else if (a > mid) update((i << 1) | 1, mid + 1, r, a, b, d, type); else { update(i << 1, l, mid, a, mid, d, type); update((i << 1) | 1, mid + 1, r, mid + 1, b, d, type); } Max[type][i] = max(Max[type][i << 1], Max[type][(i << 1) | 1]); } } int query(int i, int l, int r, int x, int type) { if (x < l || x > r) return 0; if (l == r) return Max[type][i]; pushdown(i, type); int mid = (l + r) >> 1; if (x <= mid) return query(i << 1, l, mid, x, type); return query((i << 1) | 1, mid + 1, r, x, type); } int main() { int n, q; char buf[5]; scanf("%d%d", &n, &q); for (int i = 1; i <= q; ++i) { scanf("%d%d%s", y + i, x + i, buf); if (buf[0] == 'U') op[i] = 1; else op[i] = 2; hashx[i] = x[i]; hashy[i] = y[i]; } sort(hashx + 1, hashx + q + 1); sort(hashy + 1, hashy + q + 1); cnt1 = unique(hashx + 1, hashx + q + 1) - hashx - 1; cnt2 = unique(hashy + 1, hashy + q + 1) - hashy - 1; for (int i = 1; i <= q; ++i) { x[i] = lower_bound(hashx + 1, hashx + cnt1 + 1, x[i]) - hashx; y[i] = lower_bound(hashy + 1, hashy + cnt2 + 1, y[i]) - hashy; } for (int i = 1; i <= q; ++i) if (M.find(make_pair(x[i], y[i])) != M.end()) puts("0"); else { M[make_pair(x[i], y[i])] = 1; if (op[i] == 1) { int xx = query(1, 1, cnt2, y[i], 0); printf("%d\n", hashx[x[i]] - hashx[xx]); if (xx == 0) ++xx; update(1, 1, cnt1, xx, x[i], y[i], 1); } else { int yy = query(1, 1, cnt1, x[i], 1); printf("%d\n", hashy[y[i]] - hashy[yy]); if (yy == 0) ++yy; update(1, 1, cnt2, yy, y[i], x[i], 0); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; void setup() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(15); } struct seg_tree { struct node { int val; node(int _val = 0x7f7f7f7f) { val = _val; } node operator+(const node &y) { return min(val, y.val); } }; int S; vector<node> arr; seg_tree(int _S) { assert(__builtin_popcount(_S) == 1); S = _S; arr.resize(2 * S); } void upd(int i, node v) { i += S + 1; arr[i] = v; while (i > 1) { i /= 2; arr[i] = arr[2 * i] + arr[2 * i + 1]; } } node query(int i, int j) { node res; for (i += S + 1, j += S + 1; i <= j; i /= 2, j /= 2) { if ((i & 1) == 1) res = res + arr[i++]; if ((j & 1) == 0) res = res + arr[j--]; } return res; } }; int find(seg_tree &seg, int i, int k) { int lo = 0, hi = i; int ans = 0; while (lo <= hi) { int mi = (lo + hi) / 2; int lv = seg.query(mi, i).val; if (lv <= k) { ans = mi; lo = mi + 1; } else hi = mi - 1; } return ans; } int look(vector<int> &all, int v) { return lower_bound(all.begin(), all.end(), v) - all.begin(); } const int MAXQ = 200005; int N, Q; int L[MAXQ], LC[MAXQ]; int R[MAXQ], RC[MAXQ]; char T[MAXQ]; vector<bool> ater, atec; int main() { setup(); cin >> N >> Q; vector<int> all; all.push_back(0); for (int i = 0; i < Q; i++) { cin >> L[i] >> R[i] >> T[i]; all.push_back(L[i]); all.push_back(R[i]); } sort(all.begin(), all.end()); all.resize(unique(all.begin(), all.end()) - all.begin()); ater.resize(all.size()); atec.resize(all.size()); int ts = 1; while (ts < all.size()) ts *= 2; seg_tree rows(ts), cols(ts); rows.upd(0, 0); cols.upd(0, 0); for (int i = 0; i < Q; i++) { int LC = look(all, L[i]); int RC = look(all, R[i]); if (T[i] == 'U') { if (atec[LC] || ater[RC]) { cout << 0 << "\n"; continue; } int b = find(rows, RC, LC); cols.upd(LC, b); cout << all[RC] - all[b] << "\n"; atec[LC] = true; } else { if (ater[RC] || atec[LC]) { cout << 0 << "\n"; continue; } int b = find(cols, LC, RC); rows.upd(RC, b); cout << all[LC] - all[b] << "\n"; ater[RC] = true; } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000,100000000000") using namespace std; const double eps = 1E-9; const double Exp = 2.718281828459045; const double Pi = 3.1415926535897932; const int NMAX = 200000 + 5; const int MMAX = 100000 + 5; const int INF = 1000000000; const int BS = 1000000007; template <typename T> inline T abs(const T a) { if (a < 0) return -a; return a; } template <typename T> inline T sqr(const T& a) { return a * a; } int _left[NMAX]; int top[NMAX]; int cs = 0; int main() { ios::sync_with_stdio(false); cin.tie(); int n, q; cin >> n >> q; int x, y; char c; set<pair<int, int> > st; _left[cs] = 0; top[cs] = 0; st.insert(make_pair(0, cs++)); st.insert(make_pair(n + 1, -1)); set<pair<int, int> >::iterator it, it2; while (q--) { cin >> x >> y >> c; it = (--st.upper_bound(make_pair(x, INF))); if (it->first == x) { cout << 0 << endl; continue; } if (c == 'U') { cout << y - top[it->second] << endl; st.insert(make_pair(x, cs)); _left[cs] = x; top[cs] = top[it->second]; cs++; } else { cout << x - _left[it->second] << endl; st.insert(make_pair(x, cs)); _left[cs] = _left[it->second]; top[cs] = top[it->second]; top[it->second] = y; cs++; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * @author pttrung */ public class C_Round_310_Div1 { public static long MOD = 1000000007; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); long n = in.nextLong(); int q = in.nextInt(); TreeSet<Seg> up = new TreeSet(); TreeSet<Seg> left = new TreeSet(); HashSet<Long> u = new HashSet(); HashSet<Long> l = new HashSet(); for (int i = 0; i < q; i++) { // System.out.println("Index " + i); // System.out.println(up); // System.out.println(left); long x = in.nextLong(); long y = in.nextLong(); char d = in.next().charAt(0); if (d == 'U') { if (u.contains(y)) { out.println(0); } else { out.println(update(y, x, up, left)); u.add(y); } } else { if (l.contains(x)) { out.println(0); } else { out.println(update(x, y, left, up)); l.add(x); } } } // System.out.println("LAST "); // System.out.println(up); // System.out.println(left); out.close(); } public static long update(long x, long y, TreeSet<Seg> t, TreeSet<Seg> o) { Seg tmp = o.ceiling(new Seg(y, y, 0)); long result = 0; long start; if (tmp == null || tmp.l > y) { start = 1; } else { start = tmp.v + 1; } result = x - start + 1; Seg up = t.floor(new Seg(x, x, 0)); if (up != null) { start = start < (up.r + 1) ? (up.r + 1) : start; } Seg down = t.ceiling(new Seg(x, x, 0)); if (down != null) { if (down.l <= x) { t.remove(down); if (down.l < start) { Seg other = new Seg(down.l, start - 1, down.v); t.add(other); } down.l = x + 1; if (down.r >= down.l) { t.add(down); } } } if (x >= start) { t.add(new Seg(start, x, y)); } return result; } static class Seg implements Comparable<Seg> { long l, r, v; public Seg(long l, long r, long v) { this.l = l; this.r = r; this.v = v; } @Override public String toString() { return "Seg{" + "l=" + l + ", r=" + r + ", v=" + v + '}'; } @Override public int compareTo(Seg o) { return Long.compare(r, o.r); } } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return x - o.x; } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, m; map<int, pair<char, int> > M; map<int, pair<char, int> >::iterator it; int x, y, tmd; char c[2]; int main() { scanf("%d%d", &n, &m); M[0] = {'U', 0}; M[n + 1] = {'L', 0}; while (m--) { scanf("%d%d%s", &x, &y, c); it = M.lower_bound(x); if (it->first == x) { puts("0"); continue; } if (c[0] == 'L') --it; tmd = abs(it->first - x); if (it->second.first == c[0]) tmd += it->second.second; printf("%d\n", tmd); M[x] = {c[0], tmd}; } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; struct pi { int lazy; int le, ri; } pp[2][maxn << 2]; void build(int u, int tot, int l, int r) { pp[u][tot].le = l; pp[u][tot].ri = r; pp[u][tot].lazy = 0; if (l == r) return; build(u, 2 * tot, l, (l + r) / 2); build(u, 2 * tot + 1, (l + r) / 2 + 1, r); } void merg(int u, int tot, int l, int r, int p) { if (pp[u][tot].le >= l && pp[u][tot].ri <= r) { pp[u][tot].lazy = max(pp[u][tot].lazy, p); return; } int mid = (pp[u][tot].le + pp[u][tot].ri) / 2; if (l <= mid) merg(u, 2 * tot, l, r, p); if (r > mid) merg(u, 2 * tot + 1, l, r, p); } int query(int u, int tot, int x) { if (pp[u][tot].le == pp[u][tot].ri) { return pp[u][tot].lazy; } int s = 0; int mid = (pp[u][tot].le + pp[u][tot].ri) / 2; s = pp[u][tot].lazy; if (x <= mid) s = max(s, query(u, 2 * tot, x)); if (x > mid) s = max(s, query(u, 2 * tot + 1, x)); return s; } struct ppi { int x; int y; int id; } pp1[maxn]; char c[100]; long long a[maxn]; int main() { int i, n, m; cin >> n >> m; for (i = 1; i <= m; i++) { scanf("%d%d", &pp1[i].x, &pp1[i].y); scanf("%s", c); if (c[0] == 'U') pp1[i].id = 0; else pp1[i].id = 1; a[i] = (long long)pp1[i].x * (n + 1) + pp1[i].y; } a[m + 1] = 0; sort(a + 1, a + 1 + m); build(0, 1, 1, m); build(1, 1, 1, m); for (i = 1; i <= m; i++) { int p = lower_bound(a + 1, a + 1 + m, (long long)pp1[i].x * (n + 1) + pp1[i].y) - a; if (pp1[i].id == 0) { int q = query(0, 1, p); printf("%d\n", (pp1[i].y - a[m + 1 - q] % (n + 1))); merg(0, 1, p, p, m + 1 - p); if (q != 0) merg(1, 1, p, m + 1 - q, p); else merg(1, 1, p, m, p); } else { int q = query(1, 1, p); printf("%d\n", pp1[i].x - a[q] / (n + 1)); merg(1, 1, p, p, p); if (q != 0) merg(0, 1, q, p, m + 1 - p); else merg(0, 1, 1, p, m + 1 - p); } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; template <class T> inline void amin(T &a, const T &b) { if (b < a) a = b; } template <class T> inline void amax(T &a, const T &b) { if (a < b) a = b; } struct Data { int sx, sy; int le, ri, to, bo; bool operator<(const Data &y) const { return sx < y.sx; } }; int N, Q; int main() { scanf("%d%d", &N, &Q); set<Data> S; S.insert((Data){1, 1, 1, N, 1, N}); Data seek; for (int $ = 0, $_len = (Q); $ < $_len; ++$) { int col, row; char buf[8], dir; scanf("%d%d%s", &col, &row, buf); dir = buf[0]; int ans = 0; seek.sx = col; set<Data>::iterator it = S.upper_bound(seek); if (it != S.begin()) { it--; if (it->sx <= col && col <= it->ri) { Data D = *it; S.erase(it); if (dir == 'U') { ans = row - D.to + 1; if (D.sx < col) S.insert((Data){D.sx, row + 1, D.le, col - 1, D.to, D.bo}); if (col < D.ri) S.insert((Data){col + 1, D.sy, col + 1, D.ri, D.to, row - 1}); } else { ans = col - D.le + 1; if (D.sy < row) S.insert((Data){col + 1, D.sy, D.le, D.ri, D.to, row - 1}); if (row < D.bo) S.insert((Data){D.sx, row + 1, D.le, col - 1, row + 1, D.bo}); } } else { ans = 0; } } else { ans = 0; } printf("%d\n", ans); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> template <class T> inline bool cmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <class T> inline bool cmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } using namespace std; const int N = 5e6 + 5; char s[5]; int ls[N], rs[N], mx[N], mn[N], nd, n, q, rt; inline int max(register int x, register int y) { return x > y ? x : y; } inline int min(register int x, register int y) { return x < y ? x : y; } void updmx(int &p, int l, int r, int ql, int qr, int x) { if (!p) p = ++nd, mx[p] = 0, mn[p] = n + 1; if (ql <= l && qr >= r) return cmax(mx[p], x), void(); int mid = (l + r) >> 1; if (ql <= mid) updmx(ls[p], l, mid, ql, qr, x); if (qr > mid) updmx(rs[p], mid + 1, r, ql, qr, x); } void updmn(int &p, int l, int r, int ql, int qr, int x) { if (!p) p = ++nd, mx[p] = 0, mn[p] = n + 1; if (ql <= l && qr >= r) return cmin(mn[p], x), void(); int mid = (l + r) >> 1; if (ql <= mid) updmn(ls[p], l, mid, ql, qr, x); if (qr > mid) updmn(rs[p], mid + 1, r, ql, qr, x); } int qrmx(int p, int l, int r, int x) { if (!p || l == r) return mx[p]; int mid = (l + r) >> 1; return max(mx[p], x <= mid ? qrmx(ls[p], l, mid, x) : qrmx(rs[p], mid + 1, r, x)); } int qrmn(int p, int l, int r, int x) { if (!p || l == r) return mn[p]; int mid = (l + r) >> 1; return min(mn[p], x <= mid ? qrmn(ls[p], l, mid, x) : qrmn(rs[p], mid + 1, r, x)); } map<int, int> mp; int main() { scanf("%d%d", &n, &q), mx[0] = 0, mn[0] = n + 1; ++nd, mx[1] = 0, mn[1] = n + 1, rt = 1; for (register int i = 1, x, y, lim; i <= q; ++i) { scanf("%d%d%s", &x, &y, s + 1); if (mp.count(x)) { puts("0"); continue; } mp[x] = 1; if (s[1] == 'U') { lim = qrmn(rt, 1, n, x); if (x <= lim - 2) updmx(rt, 1, n, x + 1, lim - 1, x); printf("%d\n", lim - x); } else { lim = qrmx(rt, 1, n, x); if (x >= lim + 2) updmn(rt, 1, n, lim + 1, x - 1, x); printf("%d\n", x - lim); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int N = 200005; struct Segment_Tree { struct Node { int v; bool Tag; int sl; }; Node tree[N << 2]; void Update(int root, int left, int right) { if (tree[root].Tag == false) return; tree[root].v = max(tree[root].v, tree[root].sl); tree[root].Tag = false; if (left != right) { tree[root << 1].Tag = true; tree[root << 1].sl = max(tree[root << 1].sl, tree[root].sl); tree[root << 1 | 1].Tag = true; tree[root << 1 | 1].sl = max(tree[root << 1 | 1].sl, tree[root].sl); } } void Add(int root, int left, int right, int l, int r, int cur) { Update(root, left, right); if (left == l && right == r) { tree[root].sl = max(tree[root].sl, cur), tree[root].Tag = true; Update(root, left, right); return; } int mid = (left + right) >> 1; if (r <= mid) Add(root << 1, left, mid, l, r, cur); else if (l > mid) Add(root << 1 | 1, mid + 1, right, l, r, cur); else { Add(root << 1, left, mid, l, mid, cur); Add(root << 1 | 1, mid + 1, right, mid + 1, r, cur); } Update(root << 1, left, mid), Update(root << 1 | 1, mid + 1, right); tree[root].v = max(tree[root << 1].v, tree[root << 1 | 1].v); } int Get_Min(int root, int left, int right, int x) { Update(root, left, right); if (left == x && right == x) return tree[root].v; int mid = (left + right) >> 1; if (x <= mid) return Get_Min(root << 1, left, mid, x); else return Get_Min(root << 1 | 1, mid + 1, right, x); } }; Segment_Tree T[2]; int G[N][3], n, q; void Init() { cin >> n >> q; char w[15]; for (int i = 1; i <= q; i++) { scanf("%d%d%s", &G[i][0], &G[i][1], w); if (w[0] == 'U') G[i][2] = 0; else G[i][2] = 1; } } vector<int> X, Y; void Discretize() { X.push_back(0), Y.push_back(0); for (int i = 1; i <= q; i++) X.push_back(G[i][0]), Y.push_back(G[i][1]); X.push_back(n), Y.push_back(n); sort(X.begin(), X.end()), sort(Y.begin(), Y.end()); X.erase(unique(X.begin(), X.end()), X.end()); Y.erase(unique(Y.begin(), Y.end()), Y.end()); for (int i = 1; i <= q; i++) { G[i][0] = lower_bound(X.begin(), X.end(), G[i][0]) - X.begin(); G[i][1] = lower_bound(Y.begin(), Y.end(), G[i][1]) - Y.begin(); } } void Work() { Discretize(); for (int i = 1; i <= q; i++) { if (G[i][2] == 0) { int tt = T[0].Get_Min(1, 0, X.size(), G[i][0]); cout << Y[G[i][1]] - Y[tt] << endl; T[1].Add(1, 0, Y.size(), tt, G[i][1], G[i][0]); T[0].Add(1, 0, X.size(), G[i][0], G[i][0], G[i][1]); } else { int tt = T[1].Get_Min(1, 0, Y.size(), G[i][1]); cout << X[G[i][0]] - X[tt] << endl; T[0].Add(1, 0, X.size(), tt, G[i][0], G[i][1]); T[1].Add(1, 0, Y.size(), G[i][1], G[i][1], G[i][0]); } } } int main() { Init(), Work(); return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int a; int answer(int l, int r, int b, int e, int i, const vector<int>& x) { if ((l == b) && (r == e)) return x[i]; int m = (e + b) / 2; if (m <= l) return max(x[i], answer(l, r, m, e, i * 2 + 2, x)); if (m >= r) return max(x[i], answer(l, r, b, m, i * 2 + 1, x)); return max(x[i], max(answer(l, m, b, m, i * 2 + 1, x), answer(m, r, m, e, i * 2 + 2, x))); } void mod(int l, int r, int b, int e, int i, vector<int>& x, int s) { if ((l == b) && (r == e)) { x[i] = max(x[i], s); return; } int m = (e + b) / 2; if (m <= l) { return mod(l, r, m, e, i * 2 + 2, x, s); } if (m >= r) { return mod(l, r, b, m, i * 2 + 1, x, s); } mod(l, m, b, m, i * 2 + 1, x, s); mod(m, r, m, e, i * 2 + 2, x, s); return; } int main() { int n, m; cin >> n; cin >> m; vector<pair<int, char> > z(m); vector<int> x(m), y(m); for (int i = 0; i < m; i++) { cin >> x[i]; z[i].first = x[i]; cin >> y[i]; cin >> z[i].second; } sort(x.begin(), x.end()); sort(y.begin(), y.end()); map<int, int> xx, yy, zz; for (int i = 0; i < m; i++) { xx[x[i]] = i; yy[y[i]] = i; x[i] = y[i] = 0; } vector<int> sx(4 * m, 0), sy(4 * m, 0); int s, p, k; for (int i = 0; i < m; i++) { if (zz[z[i].first] == -1) { cout << 0 << endl; continue; } if (z[i].second == 'U') { s = z[i].first; p = s = n + 1 - z[i].first; s = yy[s]; s = answer(s, s + 1, 0, m, 0, sy); cout << p - s << endl; if (s == 0) k = m; else { k = xx[n + 1 - s] + 1; } if (k != xx[z[i].first] + 1) mod(xx[z[i].first] + 1, k, 0, m, 0, sx, z[i].first); } else { p = s = z[i].first; s = xx[s]; s = answer(s, s + 1, 0, m, 0, sx); cout << p - s << endl; if (s == 0) k = m; else { k = yy[n + 1 - s] + 1; } if (k != yy[n + 1 - p] + 1) mod(yy[n + 1 - p] + 1, k, 0, m, 0, sy, n + 1 - z[i].first); } zz[z[i].first] = -1; } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = (1 << 19) + 10; char c; bool dir, mark[maxn]; int n, q, low, high, val, seg[2][maxn]; vector<int> v; vector<pair<int, bool>> folan; void shift(int& id, int& left, int& right) { if (dir) { seg[dir][left] = min(seg[dir][left], seg[dir][id]); seg[dir][right] = min(seg[dir][right], seg[dir][id]); seg[dir][id] = seg[1][0]; return; } seg[dir][left] = max(seg[dir][left], seg[dir][id]); seg[dir][right] = max(seg[dir][right], seg[dir][id]); seg[dir][id] = 0; } int query(int id = 1, int l = 0, int r = v.size()) { if (r <= low || l >= high) { if (dir) return seg[1][0]; return 0; } if (l >= low && r <= high) return seg[dir][id]; int mid = (l + r) >> 1, left = id << 1, right = left | 1; shift(id, left, right); if (dir) return min(query(left, l, mid), query(right, mid, r)); return max(query(left, l, mid), query(right, mid, r)); } void update(int id = 1, int l = 0, int r = v.size()) { if (r <= low || l >= high) return; if (l >= low && r <= high) { if (dir) seg[dir][id] = min(seg[dir][id], val); else seg[dir][id] = max(seg[dir][id], val); return; } int mid = (l + r) >> 1, left = id << 1, right = left | 1; shift(id, left, right); update(left, l, mid); update(right, mid, r); return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); memset(seg[1], 63, sizeof(seg[1])); cin >> n >> q; for (int i = 0, l, r; i < q; i++) { cin >> l >> r >> c; folan.push_back({l, c == 'U'}); v.push_back(l); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (auto [i, d] : folan) { int size = i; if (d) size = n + 1 - i; int ip = lower_bound(v.begin(), v.end(), i) - v.begin(); if (mark[ip]) { cout << "0\n"; continue; } mark[ip] = true; low = ip, high = low + 1, dir = d; int ans = query(); low = high, high = lower_bound(v.begin(), v.end(), ans) - v.begin(), dir = !dir, val = n + 1 - size; if (ans == seg[1][0]) ans = 0; else if (d) ans = n + 1 - ans; cout << size - ans << "\n"; if (!d) low = upper_bound(v.begin(), v.end(), ans) - v.begin(), high = ip, val = i; update(); } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; map<int, int> u, l; int main() { int n, t; scanf("%d%d", &n, &t); while (t--) { int x, y, saber = 0; char ch; cin >> x >> y >> ch; map<int, int>::iterator it; if (ch == 'U') { if (u.count(x)) { printf("0\n"); continue; } it = u.lower_bound(x); if (it == u.end()) { saber = y; } else { saber = y - (it->second); } cout << saber << endl; u[x] = y - saber; l[y] = x; } else { it = l.lower_bound(y); if (l.count(y)) { printf("0\n"); continue; } if (it == l.end()) { saber = x; } else { saber = x - (it->second); } cout << saber << endl; u[x] = y; l[y] = x - saber; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; const int N = 400000 + 123; class Cseg { public: int h, M; int n; int maxx[N * 4 * 2]; int flag[N * 4 * 2]; void ini(int n) { int i; this->n = n; h = ceil(log(n + 2.0) / log(2.0)) + 1; M = 1 << (h - 1); memset(flag, 0, sizeof(flag)); memset(maxx, 0, sizeof(maxx)); } void merge(const int &k) { maxx[k] = max(maxx[k << 1], maxx[k << 1 | 1]); } void updata(const int &k, const int &x) { maxx[k] = max(maxx[k], x); flag[k] = max(flag[k], x); } void down(const int &k) { updata(k << 1, flag[k]); updata(k << 1 | 1, flag[k]); flag[k] = 0; } void PID(const int &l, const int &r) { for (int i = h - 1; i > 0; --i) { int p1 = l >> i, p2 = r >> i; down(p1); if (p1 != p2) down(p2); } } void PID(const int &k) { for (int i = h - 1; i > 0; --i) down(k >> i); } void changein(int l, int r, int x) { for (l += M - 1, r += M + 1, PID(l, r); r > l + 1; r >>= 1, l >>= 1, merge(r), merge(l)) { if (!(l & 1)) updata(l + 1, x); if (r & 1) updata(r - 1, x); } while (l > 1) { l >>= 1, r >>= 1; merge(l); if (r != l) merge(r); } } int request(int k) { k += M; PID(k); return maxx[k]; } void changep(int k, int x) { k += M; PID(k); maxx[k] = max(maxx[k], x); } }; Cseg U, L; int query[N][3]; vector<int> mh; int n, q; int getN(int k) { return lower_bound(mh.begin(), mh.end(), k) - mh.begin() + 1; } int main() { scanf("%d%d", &n, &q); for (int i = 0; i < q; ++i) { char c; scanf("%d %d %c", &query[i][0], &query[i][1], &c); mh.push_back(query[i][0]); mh.push_back(query[i][1]); query[i][2] = (int)(c == 'U'); } mh.push_back(0); sort(mh.begin(), mh.end()); auto rt(unique(mh.begin(), mh.end())); mh.erase(rt, mh.end()); U.ini(mh.size()); L.ini(mh.size()); for (int i = 0; i < q; ++i) { int v1(getN(query[i][0])), v2(getN(query[i][1])); if (query[i][2]) { int m(U.request(v1)); printf("%d\n", query[i][1] - m); L.changein(getN(m) + 1, v2, query[i][0]); U.changep(v1, query[i][1]); } else { int m(L.request(v2)); printf("%d\n", query[i][0] - m); U.changein(getN(m) + 1, v1, query[i][1]); L.changep(v2, query[i][0]); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> const double PI = acos(-1.0); const int maxn = 1e5; const int INF = 1e9; using namespace std; int n, q; map<int, int> u; map<int, int> l; int main() { while (scanf("%d %d", &n, &q) != EOF) { u.clear(); l.clear(); while (q--) { map<int, int>::iterator p; int x, y; char op; scanf("%d %d", &x, &y); cin >> op; if (op == 'U') { int ans = 0; p = u.lower_bound(x); if (u.count(x)) { cout << 0 << endl; continue; } if (p == u.end()) ans = y; else ans = y - (p->second); cout << ans << endl; l[y] = x; u[x] = y - ans; } else { int ans = 0; p = l.lower_bound(y); if (l.count(y)) { cout << 0 << endl; continue; } if (p == l.end()) ans = x; else ans = x - (p->second); cout << ans << endl; u[x] = y; l[y] = x - ans; } } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> const double PI = acos(-1.0); using namespace std; struct Tree { int m, d; Tree *L, *R; Tree(int _m) : m(_m), d(0), L(0), R(0) {} }; void push(Tree *T) { T->m = max(T->m, T->d); if (T->L) T->L->d = max(T->L->d, T->d); if (T->R) T->R->d = max(T->R->d, T->d); } int get(Tree *T, int tl, int tr, int i) { push(T); int tx = (tl + tr) >> 1; if (T->L && i < tx) return max(get(T->L, tl, tx, i), T->m); if (T->R && i >= tx) return max(get(T->R, tx, tr, i), T->m); return T->m; } void upd(Tree *T, int tl, int tr, int l, int r, int d) { push(T); if (r <= tl || tr <= l) return; if (l <= tl && tr <= r) { T->d = d; return; } if (!T->L) { T->L = new Tree(T->m); T->R = new Tree(T->m); } int tx = (tl + tr) >> 1; upd(T->L, tl, tx, l, r, d); upd(T->R, tx, tr, l, r, d); } int n, q, x, y, z; char c; Tree *tv, *th; int main() { scanf("%d %d", &n, &q); tv = new Tree(0); th = new Tree(0); for (int qq = 0; qq < q; qq++) { scanf("%d %d %c", &x, &y, &c); if (c == 'U') { z = get(tv, 1, n + 1, x); printf("%d\n", y - z); upd(th, 1, n + 1, z + 1, y + 1, x); upd(tv, 1, n + 1, x, x + 1, y); } else { z = get(th, 1, n + 1, y); printf("%d\n", x - z); upd(tv, 1, n + 1, z + 1, x + 1, y); upd(th, 1, n + 1, y, y + 1, x); } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; int n, q; int x, y, z; int sum = 0; map<int, pair<char, int> > ans; map<int, pair<char, int> >::iterator t; int main() { scanf("%d%d", &n, &q); ans[0] = {'U', 0}; ans[n + 1] = {'L', 0}; for (int i = 1; i <= q; i++) { scanf("%d%d%c%c", &x, &y, &z, &z); t = ans.lower_bound(x); if (t->first == x) { printf("0\n"); continue; } if (z == 'L') t--; sum = abs(x - t->first); if (t->second.first == z) sum += t->second.second; ans[x] = {z, sum}; printf("%d\n", sum); } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; struct node { int maximum; int lazy; node *l, *r; node(int value) { maximum = value; lazy = 0; l = r = NULL; } }; node *tree[2]; void push(node *curr, int l, int r) { if (curr->lazy) { if (curr->l != NULL) { curr->l->lazy = max(curr->l->lazy, curr->lazy); curr->r->lazy = max(curr->r->lazy, curr->lazy); } curr->maximum = max(curr->maximum, curr->lazy); curr->lazy = 0; } } void update(node *curr, int l, int r, int qLow, int qHigh, int value) { if (r < qLow || qHigh < l) { return; } push(curr, l, r); if (qLow <= l && r <= qHigh) { curr->lazy = value; push(curr, l, r); } else { int mid = (l + r) / 2; if (curr->l == NULL) { curr->l = new node(curr->maximum); curr->r = new node(curr->maximum); } update(curr->l, l, mid, qLow, qHigh, value); update(curr->r, mid + 1, r, qLow, qHigh, value); } } int query(node *root, int l, int r, int index) { int mid = (l + r) / 2; push(root, l, r); if (root->l && index <= mid) { return query(root->l, l, mid, index); } else if (root->r) { return query(root->r, mid + 1, r, index); } return root->maximum; } int main() { int n, q, i, j, x, y, eaten; char c; scanf("%d %d", &n, &q); tree[0] = new node(0); tree[1] = new node(0); while (q--) { scanf("%d %d %c ", &x, &y, &c); if (c == 'L') { eaten = query(tree[0], 0, n, y); update(tree[0], 0, n, y, y, x); update(tree[1], 0, n, eaten, x, y); cout << x - eaten << "\n"; } else { eaten = query(tree[1], 0, n, x); update(tree[1], 0, n, x, x, y); update(tree[0], 0, n, eaten, y, x); cout << y - eaten << "\n"; } } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import jdk.nashorn.internal.ir.Assignment; import java.io.*; import java.util.Arrays; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeMap; public class Main { public static void main(String[] args) throws Exception { IOUtils ut = new IOUtils(System.in, System.out); new Solution(ut); ut.close(); } } class Solution { IOUtils io; final int mx = 400005; int n, q; int[] x = new int[mx]; int[] y = new int[mx]; int[] qx = new int[mx]; int[] qy = new int[mx]; String[] dir = new String[mx]; Map<Integer, Integer> coord_redn = new TreeMap<Integer, Integer>(); Map<Integer, Integer> coord_expn = new TreeMap<Integer, Integer>(); int[] rightmost_column = new int[4 * mx]; int[] bottommost_row = new int[4 * mx]; public Solution(IOUtils io) throws Exception { this.io = io; Arrays.fill(rightmost_column, 1); Arrays.fill(bottommost_row, 1); n = io.readInt(); q = io.readInt(); coord_redn.put(0, 1); for (int i = 0; i < q; i++) { qx[i] = io.readInt(); qy[i] = io.readInt(); dir[i] = io.read(); coord_redn.put(qx[i], 1); coord_redn.put(qy[i], 1); } int sz = 1; for (int e : coord_redn.keySet()) { coord_redn.put(e, sz); coord_expn.put(sz, e); sz++; } for (int ii = 0; ii < q; ii++) { if (dir[ii].charAt(0) == 'U') { int r_v = coord_redn.get(qx[ii]); int b_v = Get_Max(bottommost_row, 1, 1, sz, r_v); int b_v2 = coord_expn.get(b_v); io.writeln(qy[ii] - b_v2); int b_vv = coord_redn.get(qy[ii]); Assign_Boundary(rightmost_column, 1, 1, sz, b_v, b_vv, r_v); Assign_Boundary(bottommost_row, 1, 1, sz, r_v, r_v, b_vv); } else { int r_v = coord_redn.get(qy[ii]); int b_v = Get_Max(rightmost_column, 1, 1, sz, r_v); int b_v2 = coord_expn.get(b_v); io.writeln(qx[ii] - b_v2); int b_vv = coord_redn.get(qx[ii]); Assign_Boundary(bottommost_row, 1, 1, sz, b_v, b_vv, r_v); Assign_Boundary(rightmost_column, 1, 1, sz, r_v, r_v, b_vv); } } } private void Assign_Boundary(int[] arr, int ind, int l, int r, int x, int y, int val) { if (x > r || y < l) return; if (l >= x && r <= y) { arr[ind] = Math.max(arr[ind], val); return; } int m = (l + r) / 2; Assign_Boundary(arr, 2 * ind, l, m, x, y, val); Assign_Boundary(arr, 2 * ind + 1, m + 1, r, x, y, val); } private int Get_Max(int[] arr, int ind, int l, int r, int pos) { if (pos < l || pos > r) return 0; if (l == r) { return arr[ind]; } int ret = arr[ind]; int m = (l + r) / 2; ret = Math.max(ret, Get_Max(arr, 2 * ind, l, m, pos)); ret = Math.max(ret, Get_Max(arr, 2 * ind + 1, m + 1, r, pos)); return ret; } } class IOUtils { public BufferedReader in; public PrintWriter out; StringTokenizer st = null; public IOUtils(InputStream inStream, OutputStream outStream) { in = new BufferedReader(new InputStreamReader(inStream)); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outStream))); } private void read_input() throws Exception { while (st == null || !(st.hasMoreTokens())) { st = new StringTokenizer(in.readLine()); } } public void close() throws Exception { out.flush(); in.close(); out.close(); } public String read() throws Exception { read_input(); return st.nextToken(); } public int readInt() throws Exception { read_input(); return Integer.parseInt(st.nextToken()); } public long readLong() throws Exception { read_input(); return Long.parseLong(st.nextToken()); } public double readDouble() throws Exception { read_input(); return Double.parseDouble(st.nextToken()); } public void writeln(String i) throws Exception { out.println(i); } public void write(String i) throws Exception { out.print(i); } public void writeln() throws Exception { out.print("\n"); } public void write(int i) throws Exception { out.print(i); } public void writeln(int i) throws Exception { out.println(i); } public void write(long i) throws Exception { out.print(i); } public void writeln(long i) throws Exception { out.println(i); } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { public static void main(String[] args){ new TaskC().solve(); } } class TaskC { public void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int n=in.nextInt(); int q=in.nextInt(); int[] x=new int[q]; int[] y=new int[q]; String str; TreeSet<Integer> ts=new TreeSet<Integer>(); TreeSet<Pair> us=new TreeSet<Pair>(); for(int i=0;i<q;i++) { x[i]=in.nextInt(); y[i]=in.nextInt(); str=in.next(); if(ts.contains(x[i])){ out.println(0); continue; } ts.add(x[i]); us.add(new Pair(x[i],i)); if(str.equals("U")){ Pair p=us.higher(new Pair(x[i],i)); if(p==null){ out.println(y[i]); y[i]=0; } else{ out.println(y[i]-y[p.id]); y[i]=y[p.id]; } } else { Pair p=us.lower(new Pair(x[i],i)); if(p==null){ out.println(x[i]); x[i]=0; } else{ out.println(x[i]-x[p.id]); x[i]=x[p.id]; } } } out.close(); } class Pair implements Comparable<Pair> { int col; int id; public Pair(int col,int id) { this.col=col; this.id=id; } @Override public int compareTo(Pair o) { int z=Long.compare(col,o.col); if(z==0){ z=Long.compare(id,o.id); } return z; } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
JAVA
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b, long long m = 1000000007) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1; } return res; } long long inverse(long long a, long long m = 1000000007) { return power(a, m - 2, m); } struct LazySegmentTree { long long null = 0, lnull = 0, init = 0; long long combine(int p, int q) { return max(p, q); } long long getValue(int v) { if (lazy[v] != lnull) return max(st[v], lazy[v]); return st[v]; } void modifyLazy(int v, int x) { lazy[v] = max(lazy[v], 1ll * x); } vector<long long> st, lazy, len; int n; inline int left(int x) { return x << 1; } inline int right(int x) { return (x << 1) | 1; } inline int mid(int l, int r) { return (l + r) / 2; } LazySegmentTree() {} LazySegmentTree(int N) { n = N; st.assign(4 * n, 0); lazy.assign(4 * n, lnull); len.resize(4 * n); vector<long long> a(n, init); build(a, 1, 0, n - 1); } LazySegmentTree(vector<long long> &a) { n = a.size(); st.assign(4 * n, 0); lazy.assign(4 * n, lnull); len.resize(4 * n); build(a, 1, 0, n - 1); } void build(vector<long long> &a, int v, int tl, int tr) { len[v] = tr - tl + 1; if (tl == tr) { st[v] = a[tl]; return; } int tm = mid(tl, tr); build(a, left(v), tl, tm); build(a, right(v), tm + 1, tr); combine(v); } void combine(int v) { st[v] = combine(getValue(left(v)), getValue(right(v))); } void push(int v) { if (lazy[v] == lnull) return; st[v] = getValue(v); modifyLazy(left(v), lazy[v]); modifyLazy(right(v), lazy[v]); lazy[v] = lnull; } void update(int v, int tl, int tr, int l, int r, int x) { if (l > r) return; if (tl == l && tr == r) { modifyLazy(v, x); return; } push(v); int tm = mid(tl, tr); update(left(v), tl, tm, l, min(r, tm), x); update(right(v), tm + 1, tr, max(l, tm + 1), r, x); combine(v); } long long query(int v, int tl, int tr, int l, int r) { if (l > r) return null; if (tl == l && tr == r) return getValue(v); push(v); int tm = mid(tl, tr); long long resl = query(left(v), tl, tm, l, min(r, tm)); long long resr = query(right(v), tm + 1, tr, max(l, tm + 1), r); return combine(resl, resr); } void update(int pos, int x) { update(1, 0, n - 1, pos, pos, x); } void update(int l, int r, int x) { update(1, 0, n - 1, l, r, x); } long long query(int l, int r) { return query(1, 0, n - 1, l, r); } long long query(int x) { return query(x, x); } }; int main() { int n, q; cin >> n >> q; vector<pair<pair<int, int>, char>> Q(q); unordered_map<int, int> currX, currY; vector<set<int>> adjU(q + 5), adjL(q + 5); LazySegmentTree highX(q + 5), highY(q + 5); vector<int> X, Y; X.push_back(0); X.push_back(n + 1); Y.push_back(0); Y.push_back(n + 1); for (int i = 0; i < q; i++) { cin >> Q[i].first.first >> Q[i].first.second >> Q[i].second; if (currX[Q[i].first.first] != 1) X.push_back(Q[i].first.first); if (currY[Q[i].first.second] != 1) Y.push_back(Q[i].first.second); currX[Q[i].first.first] = 1; currY[Q[i].first.second] = 1; } sort(X.begin(), X.end()); sort(Y.begin(), Y.end()); for (int i = 0; i < X.size(); i++) currX[X[i]] = i; for (int i = 0; i < Y.size(); i++) currY[Y[i]] = i; adjU[0].insert(currX[n + 1]); adjL[0].insert(currY[n + 1]); vector<int> done(q + 5, 0); for (int i = 0; i < q; i++) { int XX = Q[i].first.first, YY = Q[i].first.second; int x = currX[XX]; int y = currY[YY]; char ch = Q[i].second; if (done[x]) { cout << 0 << endl; continue; } done[x] = 1; if (ch == 'U') { int hX = highX.query(y); int rX = *adjU[hX].lower_bound(x); int rY = currY[n + 1 - X[rX]]; printf("%d\n", Y[y] - Y[rY]); highX.update(rY, y, x); adjU[x].insert(rX); adjL[rY].insert(y); } else { int hY = highY.query(x); int rY = *adjL[hY].lower_bound(y); int rX = currX[n + 1 - Y[rY]]; printf("%d\n", X[x] - X[rX]); highY.update(rX, x, y); adjL[y].insert(rY); adjU[rX].insert(x); } } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; class Node { public: int val; Node *l, *r; Node(int v) { val = v; l = NULL; r = NULL; } }; int Q(Node *root, int ind, int beg, int end) { int mid = (beg + end) / 2; if (ind >= beg && ind <= mid) { if (root->l == NULL) return root->val; return max(root->val, Q(root->l, ind, beg, mid)); } if (root->r == NULL) return root->val; return max(root->val, Q(root->r, ind, mid + 1, end)); } void U(Node *root, int rbeg, int rend, int beg, int end, int nval) { if (rbeg == beg && rend == end) { root->val = max(root->val, nval); return; } int mid = (beg + end) / 2; if (rbeg <= mid) { if (root->l == NULL) root->l = new Node(root->val); if (rend <= mid) U(root->l, rbeg, rend, beg, mid, nval); else U(root->l, rbeg, mid, beg, mid, nval); } if (rend >= mid + 1) { if (root->r == NULL) root->r = new Node(root->val); if (rbeg >= mid + 1) U(root->r, rbeg, rend, mid + 1, end, nval); else U(root->r, mid + 1, rend, mid + 1, end, nval); } } int n, q; Node *r, *c; int main() { r = new Node(-1); c = new Node(-1); scanf("%d %d", &n, &q); long long result = 0; for (int i = 0; i < q; i++) { int rr, cc; char dir; scanf("%d %d %c", &cc, &rr, &dir); rr--; cc--; if (dir == 'U') { int val = Q(r, rr, 0, n - 1); result = rr - val; U(r, rr, rr, 0, n - 1, rr); if (result > 0) U(c, cc, cc + result - 1, 0, n - 1, cc); } else { int val = Q(c, cc, 0, n - 1); result = cc - val; U(c, cc, cc, 0, n - 1, cc); if (result > 0) U(r, rr, rr + result - 1, 0, n - 1, rr); } printf("%lld\n", result); } }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; map<long long, pair<char, long long> > m; long long query() { char c; long long x, y; cin >> x >> y >> c; map<long long, pair<char, long long> >::iterator b = m.lower_bound(x); if ((*b).first == x) { return 0; } if (c == 'L') { b--; } long long ans = abs((*b).first - x); if ((*b).second.first == c) { ans += (*b).second.second; } m[x] = make_pair(c, ans); return ans; } signed main() { ios::sync_with_stdio(false); long long n, q; m[0] = make_pair('U', 0); cin >> n >> q; m[n + 1] = make_pair('L', 0); while (q--) { cout << query() << endl; } return 0; }
CPP
555_C. Case of Chocolate
Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an n Γ— n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers n (1 ≀ n ≀ 109) and q (1 ≀ q ≀ 2Β·105) β€” the size of the chocolate bar and the number of actions. Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≀ xi, yi ≀ n, xi + yi = n + 1) β€” the numbers of the column and row of the chosen cell and the character that represents the direction (L β€” left, U β€” up). Output Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Output 4 3 2 1 2 Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Output 9 1 10 6 0 2 Note Pictures to the sample tests: <image> The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.
2
9
#include <bits/stdc++.h> using namespace std; char s[10]; int main() { map<int, int> A; map<int, char> B; int n, m; scanf("%d%d", &n, &m); while (m--) { int x, y, z = 0; scanf("%d%d%s", &x, &y, s); if (A.find(x) != A.end()) { puts("0"); continue; } if (s[0] == 'L') { auto it = A.lower_bound(x); if (it == A.begin()) { z = x; } else { --it; if (B[it->first] == 'L') { z = x - it->first + it->second; } else { z = x - it->first; } } } else { auto it = A.lower_bound(x); if (it == A.end()) { z = n + 1 - x; } else { if (B[it->first] == 'U') { z = it->first - x + it->second; } else { z = it->first - x; } } } printf("%d\n", z); A[x] = z; B[x] = s[0]; } return 0; }
CPP