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
import java.io.*; import java.util.*; public class C implements Runnable{ public static void main (String[] args) {new Thread(null, new C(), "_cf", 1 << 28).start();} public void run() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println("Go!"); int n = fs.nextInt(); int q = fs.nextInt(); TreeSet<Integer> row = new TreeSet<>(); TreeSet<Integer> col = new TreeSet<>(); int[][] queries = new int[q][3]; for(int i = 0; i < q; i++) { queries[i][0] = fs.nextInt(); queries[i][1] = fs.nextInt(); queries[i][2] = fs.next().charAt(0) == 'U' ? 0 : 1; row.add(queries[i][0]); col.add(queries[i][1]); } HashMap<Integer, Integer> rows = new HashMap<>(); HashMap<Integer, Integer> cols = new HashMap<>(); HashMap<Integer, Integer> Rrows = new HashMap<>(); HashMap<Integer, Integer> Rcols = new HashMap<>(); while(!row.isEmpty()) { int x = row.pollFirst(); rows.put(x, rows.size()); Rrows.put(rows.size()-1, x); } while(!col.isEmpty()) { int x = col.pollFirst(); cols.put(x, cols.size()); Rcols.put(cols.size()-1, x); } ST forRow = new ST(rows.size()); ST forCol = new ST(cols.size()); HashSet<Long> seen = new HashSet<>(); for(int i = 0; i < q; i++) { int x = queries[i][0]; int y = queries[i][1]; if(seen.contains(getMask(x, y, queries[i][2]))) { out.println(0); continue; } seen.add(getMask(x, y, queries[i][2])); if(queries[i][2] == 0) { //up int spot = cols.get(queries[i][1]); int lo = 0, hi = spot; int res = -1; for(int bs = 0; bs < 50 && lo <= hi; bs++) { int mid = (lo+hi)/2; int min = forCol.query(mid, spot, true); if(min <= queries[i][0]) { res = mid; lo = mid+1; } else { hi = mid-1; } } int gets = 0; if(res == -1) { gets = queries[i][1]; } else { gets = queries[i][1] - Rcols.get(res); } out.println(gets); spot = rows.get(queries[i][0]); forRow.upd(spot, queries[i][1] - gets + 1); } else { //left int spot = rows.get(queries[i][0]); int lo = 0, hi = spot; int res = -1; for(int bs = 0; bs < 50 && lo <= hi; bs++) { int mid = (lo + hi) / 2; int min = forRow.query(mid, spot, true); if(min <= queries[i][1]) { res = mid; lo = mid+1; } else { hi = mid-1; } } int gets = 0; if(res == -1) { gets = queries[i][0]; } else { gets = queries[i][0] - Rrows.get(res); } out.println(gets); spot = cols.get(queries[i][1]); forCol.upd(spot, queries[i][0] - gets + 1); } } out.close(); } long getMask(int x, int y, int d) { long res = x + ((long)y << 30L); return res + ((long)d << 60L); } class ST { int n, oo = (int)2e9; int[] min, max; ST(int a) { n = 2*a; min = new int[n]; max = new int[n]; Arrays.fill(min, oo); } void upd(int idx, int value) { idx += n/2; min[idx] = value; max[idx] = value; for(; idx > 1; idx >>= 1) { min[idx >> 1] = Math.min(min[idx], min[idx^1]); max[idx >> 1] = Math.max(max[idx], max[idx^1]); } } int query(int left, int right, boolean type) { int res = type ? oo : -oo; left += n/2; right += n/2; for(; left <= right; left = (left+1) >> 1, right = (right-1) >> 1) { if((left & 1) != 0) { if(type) res = Math.min(res, min[left]); else res = Math.max(res, max[left]); } if((right & 1) == 0) { if(type) res = Math.min(res, min[right]); else res = Math.max(res, max[right]); } } return res; } } class FastScanner { public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } } }
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.Scanner; import java.util.TreeMap; public class P556Ef { static long get_val(TreeMap<Long, Long> map, long query){ return map.ceilingEntry(query).getValue()- query + 1; } static boolean has_val(TreeMap<Long, Long> map, long query){ return map.containsKey(query); } static void add_vals(TreeMap<Long, Long> map, long bound, long cap){ long b = bound-cap; map.put(b, b-1+get_val(map, b)); map.put(bound, bound-1); } public static void main(String [] args){ Scanner sc = new Scanner(System.in); long n = sc.nextInt(); int q = sc.nextInt(); TreeMap<Long, Long> column_map = new TreeMap<Long, Long>(); TreeMap<Long, Long> row_map = new TreeMap<Long, Long>(); column_map.put(n+1, n); row_map.put(n+1,n); for (int i=0; i<q; i++){ int c = sc.nextInt(); int r = sc.nextInt(); String S = sc.next(); long h = 0; if (S.charAt(0)=='U') { if(!has_val(row_map, r)) { h = get_val(column_map, c); if (h > 0){ add_vals(row_map,r,h); } } } else if (S.charAt(0)=='L') { if(!has_val(column_map, c)) { h = get_val(row_map, r); if (h > 0){ add_vals(column_map,c,h); } } } System.out.println(h); } sc.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, q; map<int, int> r; map<int, int> l; int main() { scanf("%d%d", &n, &q); map<int, int>::iterator it; for (int i = 0; i < q; i++) { int x, y; char cm[2]; scanf("%d%d%s", &x, &y, cm); if (cm[0] == 'U') { if (r.count(x)) { printf("0\n"); continue; } it = r.lower_bound(x); int res; if (it == r.end()) { res = y; } else { res = y - it->second; } printf("%d\n", res); r[x] = y - res; l[y] = x; } else { if (l.count(y)) { printf("0\n"); continue; } int res; it = l.lower_bound(y); if (it == l.end()) { res = x; } else { res = x - it->second; } printf("%d\n", res); l[y] = x - res; r[x] = 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 N = 400010; struct tree { int l, r, minn, tag; } tl[4 * N], tu[4 * N]; int n, qq; map<int, map<int, int> > mp; struct question { int x, y, id; char c; } q[N], aq[N]; int lsh[2 * N], cnt, px[N]; void update(int i) { tl[i].minn = max(tl[i << 1].minn, tl[i << 1 | 1].minn); tu[i].minn = max(tu[i << 1].minn, tu[i << 1 | 1].minn); } void build(int i, int l, int r) { tl[i].l = l; tu[i].l = l; tl[i].r = r; tu[i].r = r; tl[i].minn = 0; tu[i].minn = 0; if (l == r) { } else { int mid = l + r >> 1; build(i << 1, l, mid); build(i << 1 | 1, mid + 1, r); update(i); } } void add_tag(int i, int x) { tu[i].minn = max(tu[i].minn, x); } void pushdown(int i) { if (tu[i].tag) { if (tu[i].l == tu[i].r) return; tu[i << 1].tag = max(tu[i << 1].tag, tu[i].tag); tu[i << 1 | 1].tag = max(tu[i << 1 | 1].tag, tu[i].tag); add_tag(i << 1, tu[i << 1].tag); add_tag(i << 1 | 1, tu[i << 1 | 1].tag); tu[i].tag = 0; } } void add_tag2(int i, int x) { tl[i].minn = max(tl[i].minn, x); } void pushdown2(int i) { if (tl[i].tag) { if (tl[i].l == tl[i].r) return; tl[i << 1].tag = max(tl[i << 1].tag, tl[i].tag); tl[i << 1 | 1].tag = max(tl[i << 1 | 1].tag, tl[i].tag); add_tag2(i << 1, tl[i << 1].tag); add_tag2(i << 1 | 1, tl[i << 1 | 1].tag); tl[i].tag = 0; } } int search(int i, int x) { int l = tl[i].l; int r = tl[i].r; pushdown2(i); if (l == x && l == r) return tl[i].minn; int mid = l + r >> 1; if (mid < x) return search(i << 1 | 1, x); else return search(i << 1, x); } void add(int i, int l, int r, int x) { int L = tu[i].l; int R = tu[i].r; pushdown(i); if (R < l || L > r) return; if (l <= L && R <= r) { tu[i].tag = max(tu[i].tag, x); add_tag(i, x); } else { int mid = l + r >> 1; add(i << 1, l, r, x); add(i << 1 | 1, l, r, x); update(i); } } int search2(int i, int x) { int l = tu[i].l; int r = tu[i].r; pushdown(i); if (l == x && l == r) return tu[i].minn; int mid = l + r >> 1; if (mid < x) return search2(i << 1 | 1, x); else return search2(i << 1, x); } void add2(int i, int l, int r, int x) { int L = tl[i].l; int R = tl[i].r; pushdown2(i); if (R < l || L > r) return; if (l <= L && R <= r) { tl[i].tag = max(tl[i].tag, x); add_tag2(i, x); } else { int mid = l + r >> 1; add2(i << 1, l, r, x); add2(i << 1 | 1, l, r, x); update(i); } } void print_tree(int i, int l, int r) { pushdown(i); printf("%d %d ", l, r); if (l == r) { printf("%d\n", tl[i].minn); return; } printf("%d\n", tl[i].minn); int mid = l + r >> 1; print_tree(i << 1, l, mid); print_tree(i << 1 | 1, mid + 1, r); } int main() { scanf("%d%d", &n, &qq); for (int i = 1; i <= qq; i++) { scanf("%d%d%c%c", &q[i].x, &q[i].y, &q[i].c, &q[i].c); lsh[++cnt] = q[i].x; lsh[++cnt] = q[i].y; } sort(lsh + 1, lsh + cnt + 1); cnt = unique(lsh + 1, lsh + cnt + 1) - lsh - 1; for (int i = 1; i <= qq; i++) { aq[i].x = lower_bound(lsh + 1, lsh + cnt + 1, q[i].x) - lsh; aq[i].y = lower_bound(lsh + 1, lsh + cnt + 1, q[i].y) - lsh; aq[i].id = i; aq[i].c = q[i].c; } build(1, 1, cnt); for (int i = 1; i <= qq; i++) { if (mp[aq[i].x][aq[i].y]) { printf("0\n"); continue; } if (aq[i].c == 'L') { int _ans = search(1, aq[i].y); printf("%d\n", q[i].x - _ans); if (_ans < lsh[1]) add(1, 1, aq[i].x, q[i].y); else { _ans = lower_bound(lsh + 1, lsh + cnt + 1, _ans + 1) - lsh; add(1, _ans, aq[i].x, q[i].y); } } else { int _ans = search2(1, aq[i].x); printf("%d\n", q[i].y - _ans); if (_ans < lsh[1]) add2(1, 1, aq[i].y, q[i].x); else { _ans = lower_bound(lsh + 1, lsh + cnt + 1, _ans + 1) - lsh; add2(1, _ans, aq[i].y, q[i].x); } } mp[aq[i].x][aq[i].y] = 1; } }
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 x, up, left; node(int x, int up, int left) { this->x = x; this->up = up; this->left = left; } bool operator<(const node &cmp) const { return x < cmp.x; } }; set<node> a; int main() { int n, q; scanf("%d%d", &n, &q); a.insert(node(0, 0, 0)); for (int i = 1; i <= q; i++) { int x, y; char s[5]; scanf("%d%d%s", &x, &y, s); set<node>::iterator p = a.upper_bound(node(x, 0, 0)); p--; int px = p->x, pup = p->up, pleft = p->left; if (px == x) { printf("0\n"); continue; } if (s[0] == 'U') { printf("%d\n", y - pup); a.insert(node(x, pup, x)); } if (s[0] == 'L') { printf("%d\n", x - pleft); a.erase(p); a.insert(node(px, y, pleft)); a.insert(node(x, pup, pleft)); } } 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 inft = 1000000009; const int MAXN = 1000006; const int T = 1024 * 1024; int drz[2][2 * T]; vector<int> X; pair<int, int> Q[MAXN]; char C[MAXN]; bool used[MAXN]; int ask(int a, int nr) { int ans = -1; a += T; while (a) { ans = max(ans, drz[nr][a]); a /= 2; } return ans; } void add(int a, int b, int value, int nr) { a += T; b += T; while (a <= b) { if (a % 2 == 1) { drz[nr][a] = max(drz[nr][a], value); a++; } if (b % 2 == 0) { drz[nr][b] = max(drz[nr][b], value); b--; } a /= 2; b /= 2; } } void solve() { int n, m; scanf("%d%d", &n, &m); X.push_back(0); X.push_back(n + 1); for (int i = 0; i < (m); ++i) { int a, b; char c; scanf("%d%d %c", &a, &b, &c); X.push_back(a); X.push_back(b); Q[i] = pair<int, int>(a, b); C[i] = c; } sort((X).begin(), (X).end()); X.resize(unique((X).begin(), (X).end()) - X.begin()); for (int j = 0; j < (2); ++j) for (int i = 0; i < (2 * T); ++i) drz[j][i] = -1; add(0, T - 1, 0, 0); add(0, T - 1, 0, 1); for (int i = 0; i < (m); ++i) { int aa = lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin(); if (used[aa]) { printf("0\n"); continue; } used[aa] = 1; if (C[i] == 'L') { int nr = lower_bound((X).begin(), (X).end(), Q[i].second) - X.begin(); int ans = ask(nr, 0); printf("%d\n", Q[i].first - X[ans]); add(ans, lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin(), nr, 1); } else { int nr = lower_bound((X).begin(), (X).end(), Q[i].first) - X.begin(); int ans = ask(nr, 1); printf("%d\n", Q[i].second - X[ans]); add(ans, lower_bound((X).begin(), (X).end(), Q[i].second) - X.begin(), nr, 0); } } } int main() { int t = 1; for (int i = 0; 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.*; import java.util.Scanner; import java.util.TreeSet; public class Main { public static void main(String[] args){ new TaskC().solve(); } } class TaskC { public void solve() { Scanner in=new Scanner(new BufferedInputStream(System.in)); long n=in.nextLong(); int q=in.nextInt(); long[] x=new long[q]; long[] y=new long[q]; String str; TreeSet<Long> ts=new TreeSet<Long>(); TreeSet<Pair> us=new TreeSet<Pair>(); for(int i=0;i<q;i++) { x[i]=in.nextLong(); y[i]=in.nextLong(); str=in.next(); if(ts.contains(x[i])){ System.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){ System.out.println(y[i]); y[i]=0; } else{ System.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){ System.out.println(x[i]); x[i]=0; } else{ System.out.println(x[i]-x[p.id]); x[i]=x[p.id]; } } } } class Pair implements Comparable<Pair> { long col; int id; public Pair(long 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; } } }
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> #pragma comment(linker, "/stack:20000000") using namespace std; using mt = int32_t; using fl = double; template <typename T> constexpr mt infValue = std::numeric_limits<T>::max() - 1000; template <typename T> constexpr mt maxValue = std::is_same<T, int>::value ? 1000000007 : 1000000000000000001ll; constexpr mt INF = infValue<mt>; constexpr int64_t MOD = 1000000007ll; constexpr double EPS = 1e-6; constexpr mt MAX = maxValue<mt>; using pr = pair<mt, mt>; constexpr auto N = 2000001; constexpr auto K = 6; constexpr auto P = 19; constexpr auto M = 200111; struct tree { mt l = -1; mt r = -1; mt val = 0; } * s; mt nextIdx; mt lineTop = -1, columnTop = -1; void apply(mt &v, mt val = 0) { if (v == -1) v = nextIdx++; s[v].val = max(val, s[v].val); } void push(mt v) { if (s[v].val != -1) { apply(s[v].l, s[v].val); apply(s[v].r, s[v].val); s[v].val = -1; } } mt get(mt &v, mt tl, mt tr, mt pos) { if (v == -1) return 0; if ((pos > (tl + tr >> 1) && s[v].r == -1) || s[v].l == -1) return s[v].val; apply(v); if (tl == tr) return s[v].val; push(v); mt md = tl + tr >> 1; if (pos > md) return get(s[v].r, md + 1, tr, pos); return get(s[v].l, tl, md, pos); } void update(mt &v, mt tl, mt tr, mt l, mt r, mt val) { apply(v); if (l > r) return; if (tl != tr) push(v); if (tl == l && tr == r) { s[v].val = max(s[v].val, val); return; } mt md = tl + tr >> 1; update(s[v].l, tl, md, l, min(md, r), val); update(s[v].r, md + 1, tr, max(md + 1, l), r, val); } mt n, q; int main(void) { scanf("%d %d", &n, &q); s = new tree[N * 8]; while (q--) { mt l, r; char t; scanf("%d %d %c\n", &r, &l, &t); if (t == 'L') { mt idx = get(lineTop, 1, n, l); printf("%d\n", r - idx); update(columnTop, 1, n, idx + 1, r, l); update(lineTop, 1, n, l, l, r); } else { mt idx = get(columnTop, 1, n, r); printf("%d\n", l - idx); update(lineTop, 1, n, idx + 1, l, r); update(columnTop, 1, n, r, r, l); } } 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); } const int MN = 1 << 19; struct seg_tree { vector<int> arr; seg_tree() { arr = vector<int>(2 * MN + 100, INF); } int upd(int i, int v, int loc = 1, int x = 0, int y = MN - 1) { if (y < i || i < x) return arr[loc]; if (x == y) { return arr[loc] = v; } return arr[loc] = min(upd(i, v, 2 * loc, x, x + (y - x + 1) / 2 - 1), upd(i, v, 2 * loc + 1, x + (y - x + 1) / 2, y)); } int rmq(int i, int j, int loc = 1, int x = 0, int y = MN - 1) { if (y < i || x > j) return INF; if (i <= x && y <= j) return arr[loc]; return min(rmq(i, j, 2 * loc, x, x + (y - x + 1) / 2 - 1), rmq(i, j, 2 * loc + 1, x + (y - x + 1) / 2, y)); } }; 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.rmq(mi, i); 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; seg_tree rows, cols; int L[MAXQ], LC[MAXQ]; int R[MAXQ], RC[MAXQ]; char T[MAXQ]; vector<bool> ater, atec; int main() { setup(); cin >> N >> Q; rows.upd(0, 0); cols.upd(0, 0); 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()); 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> 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) { 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; seg_tree rows(1 << 19), cols(1 << 19); int L[MAXQ], LC[MAXQ]; int R[MAXQ], RC[MAXQ]; char T[MAXQ]; vector<bool> ater, atec; int main() { setup(); cin >> N >> Q; rows.upd(0, 0); cols.upd(0, 0); 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()); 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 GCC optimize("O3") using namespace std; map<int, pair<char, int> > ma; map<int, pair<char, int> >::iterator it; int ans = 0, n, m, x, y; string s; int main() { ios::sync_with_stdio(false); cin >> n >> m; ma[0] = pair<char, int>('U', 0); ma[n + 1] = pair<char, int>('L', 0); while (m--) { cin >> x >> y >> s; ans = 0; it = ma.lower_bound(x); if (it->first == x) { cout << "0\n"; continue; } if (s[0] == 'L') it--; ans = abs((it->first) - x); if (it->second.first == s[0]) ans += it->second.second; cout << ans << endl; ma[x] = pair<char, int>(s[0], 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> #pragma comment(linker, "/STACK:100000000") using namespace std; const double eps = 1.0e-11; const double pi = acos(-1.0); using namespace std; struct el { int x, y; char c; int old; int idx; bool operator<(const el& temp) const { if (x != temp.x) return x < temp.x; return old < temp.old; } }; int main() { int n, q; cin >> n >> q; vector<el> v; vector<int> res(q + 2); set<pair<int, int> > used; for (int i = 0; i < q; i++) { el temp; char tc; scanf("%c%d %d %c", &tc, &temp.x, &temp.y, &temp.c); temp.old = i; temp.idx = i + 1; if (used.count(make_pair(temp.x, temp.y))) { res[i] = 0; continue; } used.insert(make_pair(temp.x, temp.y)); v.push_back(temp); } v.push_back(el()); v.back().x = 0; v.back().y = n + 1; v.back().c = 'U'; v.back().old = -1; v.back().idx = 0; v.push_back(el()); v.back().x = n + 1; v.back().y = 0; v.back().c = 'L'; v.back().old = -1; v.back().idx = n + 1; sort(v.begin(), v.end()); list<el> lst(v.begin(), v.end()); list<el>::iterator cur = lst.begin(); while (lst.size() != 2) { if (cur->c == 'U') { list<el>::iterator next = cur; next++; if (next->c == 'U') cur = next; else { if (cur->old > next->old) { res[cur->idx] = next->x - cur->x; lst.erase(cur); cur = next; } else { res[next->idx] = next->x - cur->x; lst.erase(next); } } } else { list<el>::iterator prev = cur; prev--; if (prev->c == 'L') cur = prev; else { if (cur->old > prev->old) { res[cur->idx] = cur->x - prev->x; lst.erase(cur); cur = prev; } else { res[prev->idx] = cur->x - prev->x; lst.erase(prev); } } } } for (int i = 1; i <= q; i++) printf("%d\n", res[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 cnt = 0; char buf[100000000]; class Tree { public: int from, to; int value; Tree *L, *R; Tree() { cnt += 1; }; Tree(int from, int to) : from(from), to(to), value(0), L(nullptr), R(nullptr) { cnt += 1; } void modify(int pos, int val); int query(int l, int r); }; void Tree::modify(int pos, int val) { if (pos == from && pos == to - 1) { value = val; return; } int mid = from + (to - from) / 2; if (pos < mid) { if (L == nullptr) L = new (buf + cnt * sizeof(Tree)) Tree(from, mid); L->modify(pos, val); value = L->value; if (R != nullptr) value = max(value, R->value); } else { if (R == nullptr) R = new (buf + cnt * sizeof(Tree)) Tree(mid, to); R->modify(pos, val); value = R->value; if (L != nullptr) value = max(value, L->value); } } int Tree::query(int l, int r) { if (l == from && r == to) return value; int mid = from + (to - from) / 2; int ans = 0; if (L != nullptr && l < mid) ans = max(ans, L->query(l, min(r, mid))); if (R != nullptr && r > mid) ans = max(ans, R->query(max(mid, l), r)); return ans; } int n, q; Tree *L, *U; int search_l(int x, int y) { int lo = 0, hi = x; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (U->query(mid, x) >= n - y) lo = mid + 1; else hi = mid; } return x - lo + 1; } int search_u(int x, int y) { if (L->query(x, n) < n - x) return n - x; int lo = x, hi = n; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (L->query(x, mid) >= n - x) hi = mid; else lo = mid + 1; } return lo - 1 - x; } int main() { int x, y; char t; scanf("%d%d", &n, &q); L = new (buf + cnt * sizeof(Tree)) Tree(0, n); U = new (buf + cnt * sizeof(Tree)) Tree(0, n); set<int> used; while (q--) { scanf("%d%d %c", &x, &y, &t); x -= 1; y -= 1; if (used.count(x)) { puts("0"); continue; } used.insert(x); int d; if (t == 'L') { d = search_l(x, y); L->modify(x, n - 1 - x + d); } else { d = search_u(x, y); U->modify(x, n - 1 - y + d); } printf("%d\n", d); } }
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 = 0x3f3f3f3f3f3f3f3f; const long long mod = 998244353; int n, m; int x, y; char c; struct Tree { int cnt = 0, root = 0; struct Node { int ls = 0, rs = 0, ma = 0; } t[4 * 200010]; void add(int &o, int l, int r, int ql, int qr, int v) { if (o == 0) { o = ++cnt; } if (ql <= l && r <= qr) { t[o].ma = max(t[o].ma, v); return; } if (r < ql || qr < l) return; int mid = (l + r) >> 1; if (ql <= mid) add(t[o].ls, l, mid, ql, qr, v); if (qr > mid) add(t[o].rs, mid + 1, r, ql, qr, v); } int qry(int &o, int l, int r, int ql, int qr) { if (o == 0) return 0; if (ql <= l && r <= qr) { return t[o].ma; } if (r < ql || qr < l) return 0; int mid = (l + r) >> 1, ans = t[o].ma; if (ql <= mid) ans = max(ans, qry(t[o].ls, l, mid, ql, qr)); if (qr > mid) ans = max(ans, qry(t[o].rs, mid + 1, r, ql, qr)); return ans; } } L, R; map<int, int> tt; int xx[200010], yy[200010], cc[200010]; int st[3 * 200010], ccnt; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d %c", &xx[i], &yy[i], &cc[i]); st[++ccnt] = xx[i]; st[++ccnt] = yy[i]; } sort(st + 1, st + ccnt + 1); n = 0; for (int i = 1; i <= ccnt; i++) { if (st[i] != st[n]) { st[++n] = st[i]; } } for (int i = 1; i <= n; i++) tt[st[i]] = i; for (int i = 1; i <= m; i++) { x = tt[xx[i]]; y = tt[yy[i]]; c = cc[i]; int tmp; if (c == 'L') { tmp = L.qry(L.root, 1, n, x, x); printf("%d\n", st[x] - st[tmp]); if (tmp + 1 <= x) R.add(R.root, 1, n, tmp + 1, x, y); L.add(L.root, 1, n, x, x, x); } else { tmp = R.qry(R.root, 1, n, x, x); printf("%d\n", st[y] - st[tmp]); if (x <= n - tmp) L.add(L.root, 1, n, x, n - tmp, x); R.add(R.root, 1, n, x, x, 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; int n; int q; set<pair<pair<int, int>, pair<int, int> > > s; set<int> bio; int main() { cin >> n >> q; s.insert(make_pair(pair<int, int>(n, 1), pair<int, int>(1, n))); for (int i = 0; i < q; i++) { int bla, row; string dir; cin >> bla >> row >> dir; if (bio.count(row) != 0) { cout << "0" << endl; continue; } bio.insert(row); set<pair<pair<int, int>, pair<int, int> > >::iterator it = s.lower_bound(make_pair(pair<int, int>(row, 0), pair<int, int>(0, 0))); pair<pair<int, int>, pair<int, int> > dl = *it; int hi = dl.first.first; int lo = dl.first.second; int najvisi = dl.second.first; int nalevo = dl.second.second; if (dir[0] == 'U') { cout << row - najvisi + 1 << endl; if (row < hi) { s.insert(make_pair(pair<int, int>(hi, row + 1), pair<int, int>(najvisi, nalevo))); } if (row > lo) { s.insert(make_pair(pair<int, int>(row - 1, lo), pair<int, int>(najvisi, row - 1))); } } else { cout << nalevo - row + 1 << endl; if (row < hi) { s.insert(make_pair(pair<int, int>(hi, row + 1), pair<int, int>(row + 1, nalevo))); } if (row > lo) { s.insert(make_pair(pair<int, int>(row - 1, lo), pair<int, int>(najvisi, nalevo))); } } s.erase(it); } 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; #pragma warning(disable : 4996) const int INF = 1 << 30; const int N = 200000 + 10; set<pair<int, int> > s; set<pair<int, int> >::iterator it; int x[N], y[N]; int main() { int n, q; char d[2]; scanf("%d%d", &n, &q); s.insert(make_pair(0, q + 1)); s.insert(make_pair(n + 1, q + 1)); x[0] = y[0] = 0; for (int i = 1; i <= q; ++i) { scanf("%d%d%s", &x[i], &y[i], d); if (d[0] == 'U') { it = s.lower_bound(make_pair(x[i], -1)); } else { it = s.upper_bound(make_pair(x[i], q + 1)); it--; } int first = it->first; if (it->first == x[i]) { printf("0\n"); continue; } s.insert(make_pair(x[i], i)); int idx = it->second; if (d[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
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; import java.util.Map.Entry; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Main2 { static int oo = 2*(int)1e9; public static void main(String[] args) throws Throwable { Scanner sc = new Scanner((System.in)); int n = sc.nextInt() , q = sc.nextInt(); HashMap<Integer, Integer> iToX = new HashMap<Integer, Integer>() , vToI = new HashMap<Integer, Integer>() , iToY = new HashMap<Integer, Integer>(); TreeMap<Integer,Integer> set = new TreeMap<Integer,Integer>(); int[][] query = new int[q][3]; for (int i = 0; i < q; i++) { for (int j = 0; j < 2; j++) query[i][j] = sc.nextInt(); if(sc.next().equals("U")) { query[i][2] = 1; } set.put(query[i][0],query[i][1]); } int ind = 1; for(Entry<Integer, Integer> e : set.entrySet()) { if(!vToI.containsKey(e.getKey())) { iToY.put(ind, e.getValue()); iToX.put(ind, e.getKey()); vToI.put(e.getKey(), ind++); } } VerticalTree verticalTree = new VerticalTree(q); HorizTree horizontalTree = new HorizTree(q); HashSet<Integer> taken = new HashSet<Integer>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < q; i++) { if(query[i][2] == 1) // up { if(taken.contains(query[i][0])) { sb.append(0+"\n"); continue; } taken.add(query[i][0]); int index = horizontalTree.query(vToI.get(query[i][0]) , query[i][0]); if(index!=oo) { int val = iToY.get(index) + 1; verticalTree.update(vToI.get(query[i][0]) , val); sb.append(query[i][1] - val + 1 +"\n"); } else { verticalTree.update(vToI.get(query[i][0]), 1); sb.append(query[i][1]+"\n"); } } else { if(taken.contains(query[i][0])) { sb.append(0+"\n"); continue; } taken.add(query[i][0]); int depth = verticalTree.query(vToI.get(query[i][0]), query[i][1]); if(depth!=-1) { int val = iToX.get(depth) + 1; horizontalTree.update(vToI.get(query[i][0]), val ); sb.append(query[i][0] - val + 1+"\n"); } else { horizontalTree.update(vToI.get(query[i][0]), 1); sb.append(query[i][0] + "\n"); } } } System.out.print(sb); } static class VerticalTree { // l & r = x-pos int n,arr[]; public VerticalTree(int n) { this.n = n; arr = new int[n<<2]; if(n==0)return; Arrays.fill(arr, oo); } int left(int cur){return cur<<1;} int right(int cur){return (cur<<1)|1;} int query(int x,int y) { if(n==0)return -1; return query(1, 1, n, x, y); } int query(int cur,int l,int r,int xInd,int realY) { if(l > xInd || arr[cur] > realY ) return -1; if(l==r) { return arr[cur] <= realY ? l : -1; } int mid = (l+r)>>1; if(arr[right(cur)] <= realY && r <= xInd) { return query(right(cur), mid+1, r, xInd, realY); } return Math.max(query(right(cur), mid+1, r, xInd, realY), query(left(cur), l, mid, xInd, realY)); } void update(int x,int h) { if(n==0)return; update(1, 1, n, x, h); } void update(int cur,int l,int r,int xInd,int h) { if(l > xInd || r < xInd) return; if(l==r) { if(l==xInd) arr[cur] = h; return; } int mid = (l+r)>>1; update(left(cur), l, mid, xInd, h); update(right(cur), mid+1, r, xInd, h); arr[cur] = Math.min(arr[left(cur)], arr[right(cur)]); } } static class HorizTree { // l & r = y-pos int n, arr[]; public HorizTree(int n) { this.n = n; arr = new int[n<<2]; if(n==0)return; Arrays.fill(arr, oo); } int left(int cur){return cur<<1;} int right(int cur){return (cur<<1)|1;} int query(int x,int ux) { if(n==0)return oo; return query(1, 1, n, x, ux); } int query(int cur,int l,int r,int xInd,int realx) { if(r < xInd || arr[cur] > realx ) return oo; if(l==r) { return arr[cur] <= realx ? l : oo; } int mid = (l+r)>>1; if(arr[left(cur)] <= realx && l >= xInd) { return query(left(cur), l, mid, xInd, realx); } return Math.min(query(right(cur), mid+1, r, xInd, realx), query(left(cur), l, mid, xInd, realx)); } void update(int y,int w) { if(n==0)return; update(1, 1, n, y, w); } void update(int cur,int l,int r,int xInd,int w) { if(l>xInd || r<xInd)return; if(l==r) { if(l==xInd) arr[cur] = w; return; } int mid = (l+r)>>1; update(left(cur), l, mid, xInd, w); update(right(cur), mid+1, r, xInd, w); arr[cur] = Math.min(arr[left(cur)], arr[right(cur)]); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public Scanner(FileReader r){ br = new BufferedReader(r);} 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 String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} } }
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, first, second; char w; map<int, pair<int, int>> z; int main() { cin >> n >> m; z[n + 1]; while (m--) { cin >> first >> second >> w; auto i = *z.lower_bound(first); cout << (i.first - first ? (w & 1 ? second - i.second.second : first - i.second.first) : 0) << endl; if (i.first - first) z[first] = i.second, w & 1 ? z[i.first].first = first : z[first].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
#include <bits/stdc++.h> using namespace std; char s[20]; map<int, int> row, col; map<int, int>::iterator it; int main() { int n, q, x, y, ans; scanf("%d%d", &n, &q); while (q--) { scanf("%d%d%s", &x, &y, s); if (s[0] == 'U') { it = col.lower_bound(x); if (it == col.end()) ans = y; else if (it->first == x) { printf("0\n"); continue; } else ans = y - it->second; col[x] = y - ans; row[y] = x; } else { it = row.lower_bound(y); if (it == row.end()) ans = x; else if (it->first == y) { printf("0\n"); continue; } else ans = x - it->second; col[x] = y; row[y] = x - ans; } 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 int MN = 13000009, INF = 1196969696; int MIN[MN], MAX[MN], lewy[MN], prawy[MN]; int P2 = 1, lastNode; map<int, int> bylo[2]; void ustawMin(int x, int v, int curr, int l, int p) { if (l == p && l == x) { MIN[curr] = v; return; } int mid = (l + p) >> 1; if (x <= mid) { if (!lewy[curr]) lewy[curr] = ++lastNode; ustawMin(x, v, lewy[curr], l, mid); } else { if (!prawy[curr]) prawy[curr] = ++lastNode; ustawMin(x, v, prawy[curr], mid + 1, p); } MIN[curr] = min(MIN[lewy[curr]], MIN[prawy[curr]]); } int mini(int a, int b, int curr, int l, int p) { if (l == a && p == b) return MIN[curr]; int mid = (l + p) >> 1; int res = INF; if (a <= mid && lewy[curr]) res = min(res, mini(a, min(b, mid), lewy[curr], l, mid)); if (b > mid && prawy[curr]) res = min(res, mini(max(a, mid + 1), b, prawy[curr], mid + 1, p)); return res; } void ustawMax(int a, int b, int v, int curr, int l, int p) { if (a > b) return; if (a == l && b == p) { MAX[curr] = max(MAX[curr], v); } else { int mid = (l + p) >> 1; if (a <= mid) { if (!lewy[curr]) lewy[curr] = ++lastNode; ustawMax(a, min(b, mid), v, lewy[curr], l, mid); } if (b > mid) { if (!prawy[curr]) prawy[curr] = ++lastNode; ustawMax(max(a, mid + 1), b, v, prawy[curr], mid + 1, p); } } } int maxi(int x, int curr, int l, int p) { if (l == x && p == x) return MAX[curr]; int mid = (l + p) >> 1; int res = MAX[curr]; if (x <= mid && lewy[curr]) res = max(res, maxi(x, lewy[curr], l, mid)); if (x > mid && prawy[curr]) res = max(res, maxi(x, prawy[curr], mid + 1, p)); return res; } int BS(int a, int b) { int zd = 1, zg = a; if (mini(a, a, 2, 0, P2 - 1) <= b) return a + 1; while (zd != zg) { int mid = (zd + zg) >> 1; if (mini(mid, a, 2, 0, P2 - 1) <= b) zd = mid; else zg = mid; if (zd + 1 == zg) { if (mini(zd, a, 2, 0, P2 - 1) > b) zg = zd; else zd = zg; } } return zd; } int main() { MIN[0] = INF; lastNode = 2; int n, q; scanf("%d%d", &n, &q); while (P2 <= n) P2 <<= 1; while (q--) { int a, b; char co[3]; scanf("%d%d%s", &a, &b, co); if (co[0] == 'L') { if (bylo[0][a]) { printf("0\n"); continue; } bylo[0][a] = 1; int g = BS(a, b); printf("%d\n", a - g + 1); ustawMax(g, a, b, 1, 0, P2 - 1); } else { if (bylo[1][a]) { printf("0\n"); continue; } bylo[1][a] = 1; int g = maxi(a, 1, 0, P2 - 1); printf("%d\n", b - g); ustawMin(a, g + 1, 2, 0, P2 - 1); } } }
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 nextInt() { int x = 0, p = 1; char ch; do { ch = getchar(); } while (ch <= ' '); if (ch == '-') { p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getchar(); } return x * p; } const int N = 100500; const int INF = (int)1e9; int n, q; class SegmentTree { struct Node { Node *l, *r; int val; Node() : val(-INF), l(0), r(0) {} Node(int val) : val(val), l(0), r(0) {} void push() { if (!l) l = new Node(0); if (!r) r = new Node(0); } void modify(int l, int r, int x, int tl, int tr) { if (l > r) return; if (tl >= l && tr <= r) { val = max(val, x); return; } push(); int tm = (tl + tr) / 2; if (l > tm) { this->r->modify(l, r, x, tm + 1, tr); return; } if (r <= tm) { this->l->modify(l, r, x, tl, tm); return; } this->l->modify(l, r, x, tl, tm); this->r->modify(l, r, x, tm + 1, tr); } int get(int ps, int tl, int tr) { if (!this) return -INF; if (tl == tr) return val; push(); int tm = (tl + tr) / 2; if (ps <= tm) { return max(val, l->get(ps, tl, tm)); } return max(val, r->get(ps, tm + 1, tr)); } }; Node *root; public: SegmentTree() { root = new Node(0); } void modify(int l, int r, int x) { root->modify(l, r, x, 0, n + 1); } void modify(int ps, int x) { root->modify(ps, ps, x, 0, n + 1); } int get(int ps) { return root->get(ps, 0, n + 1); } }; int main() { n = nextInt(); q = nextInt(); SegmentTree *hor = new SegmentTree(); SegmentTree *ver = new SegmentTree(); for (int qq = 0; qq < q; ++qq) { int x, y; char d; x = nextInt(); y = nextInt(); d = getchar(); if (d == 'U') { int res = hor->get(x); hor->modify(x, y); ver->modify(res + 1, y, x); printf("%d\n", y - res); } else { int res = ver->get(y); ver->modify(y, x); hor->modify(res + 1, x, y); printf("%d\n", x - 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; const int maxn = 200000 + 100; struct T { long long r, c; int opr; }; struct Node { long long num; int id; Node(long long num, int id = 0) : num(num), id(id) {} bool operator<(const Node& rhs) const { return num < rhs.num; } }; T save[maxn]; int IDup[maxn], IDleft[maxn]; vector<Node> Vup, Vleft; long long m; int n; struct SegTree { int num[maxn << 2]; SegTree() { memset(num, 0, sizeof(num)); } void pushdown(int u) { int L = u << 1, R = u << 1 | 1; num[L] = max(num[L], num[u]); num[R] = max(num[R], num[u]); } void pushup(int u) { num[u] = max(num[u << 1], num[u << 1 | 1]); } void update(int cL, int cR, int val, int L, int R, int u) { if (cL <= L && R <= cR) { num[u] = max(num[u], val); } else { pushdown(u); int m = (L + R) / 2; if (cL <= m) update(cL, cR, val, L, m, u << 1); if (cR > m) update(cL, cR, val, m + 1, R, u << 1 | 1); } } int query(int p, int L, int R, int u) { if (L == R) return num[u]; else { int m = (L + R) / 2; pushdown(u); if (p <= m) return query(p, L, m, u << 1); else return query(p, m + 1, R, u << 1 | 1); } } }; SegTree Up, Left; int Max; long long Numup[maxn], Numleft[maxn]; void getID(int ID[], vector<Node>& V, long long Num[]) { int idx = 0; for (int i = 0; i < V.size(); ++i) if (i == 0 || V[i].num != V[i - 1].num) { Num[idx] = V[i].num; ID[V[i].id] = idx++; } else ID[V[i].id] = idx - 1; Max = idx; } int main() { scanf("%lld %d", &m, &n); for (int i = 1; i <= n; ++i) { char buf[5]; scanf("%lld %lld %s", &save[i].c, &save[i].r, buf); save[i].opr = buf[0] == 'L'; Vup.push_back(Node(save[i].r, i)); Vleft.push_back(Node(save[i].c, i)); } Vup.push_back(Node(0, 0)); Vleft.push_back(Node(0, 0)); Vup.push_back(Node(m + 1, n + 1)); Vleft.push_back(Node(m + 1, n + 1)); sort(Vup.begin(), Vup.end()); sort(Vleft.begin(), Vleft.end()); getID(IDup, Vup, Numup); getID(IDleft, Vleft, Numleft); for (int i = 1; i <= n; ++i) { if (save[i].opr == 0) { int tR = IDup[i]; int tL = Up.query(tR, 0, Max - 1, 1); int L = Max - 1 - tR; int R = Max - 1 - tL; Left.update(L, R, L, 0, Max - 1, 1); Up.update(tR, tR, tR, 0, Max - 1, 1); printf("%lld\n", Numup[tR] - Numup[tL]); } else { int tR = IDleft[i]; int tL = Left.query(tR, 0, Max - 1, 1); int L = Max - 1 - tR; int R = Max - 1 - tL; Up.update(L, R, L, 0, Max - 1, 1); Left.update(tR, tR, tR, 0, Max - 1, 1); printf("%lld\n", Numleft[tR] - Numleft[tL]); } } 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 val; int lzy; }; struct Data { int n; Node T[4000000]; vector<pair<int, int> > segs; void Init(vector<pair<int, int> > d) { segs = d; n = segs.size(); for (int i = 0; i <= n; i++) T[i].val = 0, T[i].lzy = 0; } void push(int node, int tl, int tr) { T[node].val = max(T[node].val, T[node].lzy); if (tl != tr) { T[node * 2].lzy = max(T[node * 2].lzy, T[node].lzy); T[node * 2 + 1].lzy = max(T[node * 2 + 1].lzy, T[node].lzy); } T[node].lzy = 0; } void update(int node, int cl, int cr, int tl, int tr, int vl) { push(node, cl, cr); if (cr < tl) return; if (cl > tr) return; if (cl >= tl && cr <= tr) { T[node].lzy = vl; push(node, cl, cr); return; } int md = (cl + cr) / 2; update(node * 2, cl, md, tl, tr, vl); update(node * 2 + 1, md + 1, cr, tl, tr, vl); T[node].val = max(T[node * 2].val, T[node * 2 + 1].val); } int qry(int node, int cl, int cr, int tl, int tr) { push(node, cl, cr); if (cr < tl) return 0; if (cl > tr) return 0; if (cl >= tl && cr <= tr) { return T[node].val; } int md = (cl + cr) / 2; return max(qry(node * 2, cl, md, tl, tr), qry(node * 2 + 1, md + 1, cr, tl, tr)); } int ask(int l, int r) { l = lower_bound(segs.begin(), segs.end(), make_pair(l, -1)) - segs.begin(); r = lower_bound(segs.begin(), segs.end(), make_pair(r, -1)) - segs.begin(); return qry(1, 0, n - 1, l, r); } void upd(int l, int r, int d) { l = lower_bound(segs.begin(), segs.end(), make_pair(l, -1)) - segs.begin(); r = lower_bound(segs.begin(), segs.end(), make_pair(r, -1)) - segs.begin(); update(1, 0, n - 1, l, r, d); } }; Data rows, cols; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int nn, q; cin >> nn >> q; int x[q], y[q]; char d[q]; vector<int> xp, yp; vector<pair<int, int> > xseg, yseg; xp.push_back(1); yp.push_back(1); for (int i = 0; i < q; i++) { cin >> x[i] >> y[i] >> d[i]; xp.push_back(x[i]); yp.push_back(y[i]); if (x[i] + 1 <= nn) xp.push_back(x[i] + 1); if (y[i] + 1 <= nn) yp.push_back(y[i] + 1); } sort(xp.begin(), xp.end()); sort(yp.begin(), yp.end()); xp.resize(unique(xp.begin(), xp.end()) - xp.begin()); yp.resize(unique(yp.begin(), yp.end()) - yp.begin()); for (int i = 0; i < xp.size(); i++) { xseg.push_back(make_pair(xp[i], xp[i])); if (i + 1 < xp.size()) { if (xp[i] + 1 < xp[i + 1] - 1) { xseg.push_back(make_pair(xp[i] + 1, xp[i + 1] - 1)); } } } for (int i = 0; i < yp.size(); i++) { yseg.push_back(make_pair(yp[i], yp[i])); if (i + 1 < yp.size()) { if (yp[i] + 1 < yp[i + 1] - 1) { yseg.push_back(make_pair(yp[i] + 1, yp[i + 1] - 1)); } } } rows.Init(yseg); cols.Init(xseg); set<pair<int, int> > asked; int mx; for (int i = 0; i < q; i++) { if (asked.count(make_pair(x[i], y[i]))) { cout << 0 << "\n"; continue; } asked.insert(make_pair(x[i], y[i])); if (d[i] == 'U') { mx = cols.ask(x[i], x[i]); cout << y[i] - mx << "\n"; mx++; rows.upd(mx, y[i], x[i]); } else { mx = rows.ask(y[i], y[i]); cout << x[i] - mx << "\n"; mx++; cols.upd(mx, x[i], 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; struct edge { int u, v; long long w; }; long long MOD = 1000000007; long long _MOD = 1000000009; double EPS = 1e-10; void f(vector<pair<int, int> >& e, vector<int>& a) { sort(e.begin(), e.end()); stack<int> I, X; I.push(-1); X.push(0); long long sum = 0; for (int k = 0; k < e.size(); k++) { int x = e[k].first, i = e[k].second; if (i >= 0) { I.push(i); X.push(x); } else { i = ~i; while (I.top() > i) { I.pop(); X.pop(); } a[i] = x - X.top(); } } } int main() { int n, q; cin >> n >> q; vector<pair<int, int> > e(q), _e(q); set<int> s; for (int i = 0; i < q; i++) { int x, y; char c; scanf("%d%d %c", &x, &y, &c); if (s.count(x)) continue; s.insert(x); e[i] = pair<int, int>(x, c == 'U' ? i : ~i); _e[i] = pair<int, int>(y, c == 'L' ? i : ~i); } vector<int> a(q); f(e, a); f(_e, a); for (int i = 0; i < q; i++) cout << a[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 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) { S = _S; arr.resize(2 * S + 1); } void upd(int i, node v) { i += S; 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, j += S; 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 = all.size(); 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> using namespace std; map<int, int> h; map<int, int> l; map<int, int>::iterator it; int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= q; i++) { int x, y; char op[5]; scanf("%d%d%s", &x, &y, op); if (op[0] == 'U') { int ans = 0; it = h.lower_bound(x); if (it->first == x) { printf("0\n"); continue; } if (it == h.end()) ans = y; else ans = y - it->second; printf("%d\n", ans); l[y] = x; h[x] = y - ans; } if (op[0] == 'L') { int ans = 0; it = l.lower_bound(y); if (it->first == y) { printf("0\n"); continue; } if (it == l.end()) ans = x; else ans = x - it->second; printf("%d\n", ans); h[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
import java.io.*; import java.util.*; import java.util.List; public class C { private static StringTokenizer st; private static BufferedReader br; public static long MOD = 1000000007; private static double EPS = 0.0000001; public static void print(Object x) { System.out.println(x + ""); } public static void printArr(long[] x) { StringBuilder s = new StringBuilder(); for (int i = 0; i < x.length; i++) { s.append(x[i] + " "); } print(s); } public static void printArr(int[] x) { StringBuilder s = new StringBuilder(); for (int i = 0; i < x.length; i++) { s.append(x[i] + " "); } print(s); } public static void printArr(char[] x) { StringBuilder s = new StringBuilder(); for (int i = 0; i < x.length; i++) { s.append(x[i] + ""); } print(s); } public static String join(Collection<?> x, String space) { if (x.size() == 0) return ""; StringBuilder sb = new StringBuilder(); boolean first = true; for (Object elt : x) { if (first) first = false; else sb.append(space); sb.append(elt); } return sb.toString(); } public static String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = br.readLine(); st = new StringTokenizer(line.trim()); } return st.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public static List<Integer> nextInts(int N) throws IOException { List<Integer> ret = new ArrayList<Integer>(); for (int i = 0; i < N; i++) { ret.add(nextInt()); } return ret; } public static class Chunk { long left; long top; public Chunk(long left, long top) { this.left = left; this.top = top; } } public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); int n = nextInt(); int q = nextInt(); TreeMap<Long, Chunk> chunks = new TreeMap<Long, Chunk>(); Set<Long> done = new HashSet<Long>(); List<Long> ans = new ArrayList<Long>(); chunks.put(1L, new Chunk(0, n+1)); for (int i = 0; i < q; i++) { long x = nextLong(); nextLong(); String s = nextToken(); if (done.contains(x)) { ans.add(0L); continue; } done.add(x); Long key = chunks.lowerKey(x+1); Chunk c = chunks.remove(key); if (s.equals("L")) { ans.add(x - c.left); chunks.put(key, new Chunk(c.left, x)); chunks.put(x+1, new Chunk(c.left, c.top)); } else { ans.add(c.top - x); chunks.put(key, new Chunk(c.left, c.top)); chunks.put(x+1, new Chunk(x, c.top)); } } for (Long x : ans) print(x); } }
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.Scanner; import java.util.TreeMap; import java.util.TreeSet; public class Main { public void solve() { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int Q = scanner.nextInt(); TreeSet<Integer> xRecord = new TreeSet<>();// 前に出てきたx // 自分よりx座標の小さいクエリが来た時にどこが壁になるか、を記録する TreeMap<Integer, Wall> wallMap = new TreeMap<>(); wallMap.put(N + 1, new Wall(0, 0)); StringBuilder builder = new StringBuilder(); for (int q = 0; q < Q; q++) { int x = scanner.nextInt(); int y = scanner.nextInt(); String s = scanner.next(); if (xRecord.contains(x)) { // 処理したことのあるクエリなら、チョコは残っていないはず builder.append(0 + "\n"); continue; } xRecord.add(x); // 自分より右側にある食べられ情報を読む Wall higherWall = wallMap.get(wallMap.higherKey(x)); if (s.equals("U")) { // 上向き食べる時 builder.append(y - higherWall.y + "\n"); wallMap.put(x, new Wall(higherWall.y, higherWall.x)); // 今後xとhigherEaten.xの間で左に行こうとすると、xが壁になる higherWall.x = x; } else { // 左向きに食べる時 builder.append(x - higherWall.x + "\n"); wallMap.put(x, new Wall(y, higherWall.x)); } } System.out.println(builder.toString()); scanner.close(); } private class Wall { int y, x; public Wall(int y, int x) { this.x = x; this.y = y; } } public static void main(String[] args) { new Main().solve(); } }
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.*; /** * Created by sbabkin on 9/14/2015. */ public class SolverE { public static void main(String[] args) throws IOException { new SolverE().Run(); } BufferedReader br; PrintWriter pw; StringTokenizer stok; private String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { stok = new StringTokenizer(br.readLine()); } return stok.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private long nextLong() throws IOException { return Long.parseLong(nextToken()); } private double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } class Tree { Tree left = null, right = null; int toSet = -1; public void push() { if (toSet >= 0 && left != null) { if (left.toSet == -1 || left.toSet < toSet) { left.toSet = toSet; } if (right.toSet == -1 || right.toSet < toSet) { right.toSet = toSet; } toSet = -1; } } public int getMax(int ind, int curL, int curR) { push(); if (toSet >= 0) { return toSet; } int mid = (curL + curR) >> 1; if (ind <= mid) { return left.getMax(ind, curL, mid); } else { return right.getMax(ind, mid + 1, curR); } } public void setMax(int max, int curL, int curR, int l, int r) { if (l > r) { return; } if (toSet>=max){ return; } if (l == curL && r == curR) { if (toSet == -1 || toSet < max) { toSet = max; } return; } int mid = (curL + curR) >> 1; if (left == null) { left = new Tree(); right = new Tree(); } push(); left.setMax(max, curL, mid, l, Math.min(r, mid)); right.setMax(max, mid + 1, curR, Math.max(l, mid + 1), r); } } int n; int kolq; // Tree hor = new Tree(); // Tree vert = new Tree(); int[] toSet; int[] leftInd, rightInd; int masSize; public void push(int pos) { if (toSet[pos] >= 0 && leftInd[pos] >=0) { if (toSet[leftInd[pos]] == -1 || toSet[leftInd[pos]] < toSet[pos]) { toSet[leftInd[pos]] = toSet[pos]; } if (toSet[rightInd[pos]] == -1 || toSet[rightInd[pos]] < toSet[pos]) { toSet[rightInd[pos]] = toSet[pos]; } toSet[pos] = -1; } } public int getMax(int pos, int ind, int curL, int curR) { push(pos); if (toSet[pos] >= 0) { return toSet[pos]; } int mid = (curL + curR) >> 1; if (ind <= mid) { return getMax(leftInd[pos], ind, curL, mid); } else { return getMax(rightInd[pos], ind, mid + 1, curR); } } public void setMax(int pos, int max, int curL, int curR, int l, int r) { if (l > r) { return; } if (toSet[pos]>=max){ return; } if (l == curL && r == curR) { if (toSet[pos] == -1 || toSet[pos] < max) { toSet[pos] = max; } return; } int mid = (curL + curR) >> 1; if (leftInd[pos] < 0) { leftInd[pos]=masSize; rightInd[pos]=masSize+1; masSize+=2; } push(pos); setMax(leftInd[pos], max, curL, mid, l, Math.min(r, mid)); setMax(rightInd[pos], max, mid + 1, curR, Math.max(l, mid + 1), r); } private void Run() throws IOException { // br = new BufferedReader(new FileReader("input.txt")); // pw = new PrintWriter("output.txt"); br=new BufferedReader(new InputStreamReader(System.in)); pw=new PrintWriter(new OutputStreamWriter(System.out)); solve(); pw.flush(); pw.close(); } private void solve() throws IOException { n = nextInt(); kolq = nextInt(); // hor.toSet = 0; // vert.toSet = 0; toSet =new int[65*kolq]; leftInd=new int[65*kolq]; rightInd=new int[65*kolq]; Arrays.fill(toSet, -1); Arrays.fill(leftInd, -1); Arrays.fill(rightInd, -1); masSize=2; toSet[0]=0; toSet[1]=0; //vert - 0 , hor - 1 Set<Integer> setUsed = new HashSet<Integer>(); for (int i = 0; i < kolq; i++) { int x = nextInt(); int y = nextInt(); String type = nextToken(); if (type.equals("L")) { if (setUsed.contains(x)){ pw.println(0); } else { int start = getMax(0, y, 1, n); pw.println(x - start); setMax(1, y, 1, n, start + 1, x); // setMax(0, x, 1, n, y, y); setUsed.add(x); } } else { if (setUsed.contains(x)){ pw.println(0); } else { int start = getMax(1, x, 1, n); pw.println(y - start); setMax(0, x, 1, n, start + 1, y); // setMax(1, y, 1, n, x, x); setUsed.add(x); } } } } }
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 Q = 200100; int x[Q], y[Q], pos[Q]; char d[Q]; struct Node { int l, r, left, up; int lazy_left, lazy_up; Node() {} Node(int l, int r) : l(l), r(r), left(0), up(0), lazy_left(0), lazy_up(0) {} } node[4 * Q]; void build_tree(int seg, int l, int r) { node[seg] = Node(l, r); if (l != r) { int mid = (l + r) / 2; build_tree(2 * seg, l, mid); build_tree(2 * seg + 1, mid + 1, r); } } void update(int seg) { bool leaf = (node[seg].l == node[seg].r); if (node[seg].lazy_left) { if (leaf) node[seg].left = max(node[seg].left, node[seg].lazy_left); else { node[2 * seg].lazy_left = max(node[2 * seg].lazy_left, node[seg].lazy_left); node[2 * seg + 1].lazy_left = max(node[2 * seg + 1].lazy_left, node[seg].lazy_left); } } if (node[seg].lazy_up) { if (leaf) node[seg].up = max(node[seg].up, node[seg].lazy_up); else { node[2 * seg].lazy_up = max(node[2 * seg].lazy_up, node[seg].lazy_up); node[2 * seg + 1].lazy_up = max(node[2 * seg + 1].lazy_up, node[seg].lazy_up); } } node[seg].lazy_left = node[seg].lazy_up = 0; } void modify(int seg, int l, int r, bool left, int v) { update(seg); if (node[seg].l > r || node[seg].r < l) return; if (node[seg].l >= l && node[seg].r <= r) { if (left) node[seg].lazy_left = v; else node[seg].lazy_up = v; update(seg); return; } modify(2 * seg, l, r, left, v); modify(2 * seg + 1, l, r, left, v); } int query(int seg, int p, bool left) { update(seg); if (node[seg].l > p || node[seg].r < p) return INT_MAX; if (node[seg].l == p && node[seg].r == p) return left ? node[seg].left : node[seg].up; return min(query(2 * seg, p, left), query(2 * seg + 1, p, left)); } int main() { int n, q; scanf("%d %d", &n, &q); vector<pair<int, int> > arr; for (int i = 1; i <= q; i++) { scanf("%d %d %c", &x[i], &y[i], &d[i]); swap(x[i], y[i]); arr.push_back(make_pair(y[i], i)); } sort(arr.begin(), arr.end()); map<int, int> compX, compY; for (int i = 0; i < arr.size(); i++) { if (i > 0 && y[arr[i - 1].second] == arr[i].first) { arr[i].first = arr[i - 1].first; } else arr[i].first = i + 1; pos[arr[i].second] = arr[i].first; compX[x[arr[i].second]] = compY[y[arr[i].second]] = pos[arr[i].second]; } build_tree(1, 1, q); for (int i = 1; i <= q; i++) { if (d[i] == 'L') { int last = query(1, pos[i], true); printf("%d\n", y[i] - last); modify(1, compY[last] + 1, pos[i], false, x[i]); modify(1, pos[i], pos[i], true, y[i]); } else { int last = query(1, pos[i], false); printf("%d\n", x[i] - last); if (!last) modify(1, pos[i], q, true, y[i]); else modify(1, pos[i], compX[last] - 1, true, y[i]); modify(1, pos[i], pos[i], false, x[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; set<int> in; map<int, int> re, d; int N, M, X[200005], R[200005], V[200005], S[200005]; char D[200005]; int main() { scanf("%d %d", &N, &M); for (int i = 1; i <= M; i++) { scanf("%d %*d %c", &X[i], &D[i]); re[X[i]] = 0; } X[M + 1] = N + 1; D[0] = 'U'; D[M + 1] = 'L'; int c = 1; for (auto &p : re) p.second = c++; map<int, int> cnt; for (int i = 1; i <= M; i++) { if (cnt[X[i]]++ > 0) { R[i] = -1; } else R[i] = re[X[i]], V[R[i]] = i; } in.insert(0); in.insert(c); V[c] = M + 1; R[M + 1] = c; for (int i = 1; i <= M; i++) { if (R[i] == -1) { printf("0\n"); continue; } if (D[i] == 'U') { int r = *(in.lower_bound(R[i])); if (D[V[r]] == 'U') S[i] = S[V[r]]; else S[i] = N + 1 - X[V[r]]; printf("%d\n", (N + 1 - X[i]) - S[i]); } else { int l = *(in.upper_bound(R[i]).operator--()); if (D[V[l]] == 'L') S[i] = S[V[l]]; else S[i] = X[V[l]]; printf("%d\n", X[i] - S[i]); } in.insert(R[i]); d[R[i]] = D[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.io.*; import java.text.DecimalFormat; import java.util.*; public class Main { static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static Scanner in = new Scanner(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void main(String[] args) { TreeSet<Integer> setx = new TreeSet<Integer>(); setx.add(0); int len = in.nextInt(); setx.add(len+1); int n = in.nextInt(); Map<Integer,Integer> up = new HashMap<Integer,Integer>(); Map<Integer,Integer> left = new HashMap<Integer,Integer>(); Map<Integer,Integer> op = new HashMap<Integer,Integer>(); up.put(len+1, 1); left.put(0, 1); op.put(len+1, 1); op.put(0, 2); for(int i = 0;i<n;i++){ int x = in.nextInt(); int y = in.nextInt(); String s = in.next(); setx.add(x); if(op.containsKey(x)){ out.println(0); continue; }else{ if(s.equals("U")){ op.put(x, 1); int upper = setx.higher(x); if(op.get(upper)==1){ up.put(x, up.get(upper)); out.println(y-up.get(x)+1); }else{ up.put(x, len+1-upper+1) ; out.println(y-up.get(x)+1); } }else{ int lower = setx.lower(x); op.put(x,2); if(op.get(lower)==1){ left.put(x, lower+1); out.println(x-left.get(x)+1); }else{ left.put(x, left.get(lower)); out.println(x-left.get(x)+1); } } } } out.close(); } static void debug(int a[]) { out.println("----------debug----------"); for (int i = 0; i < a.length; i++) { out.print(a[i] + " "); } out.println(); out.println("----------debug----------"); } } 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()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(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 main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; map<int, pair<char, int>> mp; mp[0] = make_pair('U', 0); mp[n + 1] = make_pair('L', 0); while (q--) { int x, y; char cc; cin >> x >> y >> cc; auto it = mp.lower_bound(x); if (it->first == x) { cout << 0 << '\n'; continue; } if (cc == 'L') it--; int ans = abs(it->first - x); if (it->second.first == cc) ans += it->second.second; mp[x] = make_pair(cc, 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; struct meo { int x, y, x1, y1, p; }; const int N = 200010; int n, q, dir[N], X, Y; meo a[N]; bool comparex(meo i, meo j) { if (i.x == j.x) { if (i.y == j.y) return i.p < j.p; else return i.y < j.y; } return i.x < j.x; } bool comparey(meo i, meo j) { return i.y < j.y; } bool comparep(meo i, meo j) { return i.p < j.p; } int T[2][8 * N] = {0}, F[2][8 * N] = {0}, res, L, R, v, x[N], y[N], fr[N] = {0}; void maximize(int c, int &i, int j) { if (c == 1) { if (a[j].x > a[i].x) i = j; } else if (a[j].y > a[i].y) i = j; } void Get(int c, int k, int l, int r) { if (l == r) { maximize(c, T[c][k], F[c][k]); F[c][k] = 0; } else if (F[c][k]) maximize(c, F[c][k * 2], F[c][k]), maximize(c, F[c][k * 2 + 1], F[c][k]), F[c][k] = 0; if (l > r || v < l || v > r) return; if (l == r) { res = T[c][k]; return; } int m = (l + r) >> 1; Get(c, k * 2, l, m); Get(c, k * 2 + 1, m + 1, r); } void Update(int c, int k, int l, int r) { if (l == r) maximize(c, T[c][k], F[c][k]), F[c][k] = 0; else if (F[c][k]) maximize(c, F[c][k * 2], F[c][k]), maximize(c, F[c][k * 2 + 1], F[c][k]), F[c][k] = 0; if (l > r || l > R || r < L) return; if (L <= l && r <= R) { if (l == r) maximize(c, T[c][k], v); else maximize(c, F[c][k * 2], v), maximize(c, F[c][k * 2 + 1], v); return; } int m = (l + r) >> 1; Update(c, k * 2, l, m); Update(c, k * 2 + 1, m + 1, r); } int main(void) { scanf("%d%d", &n, &q); char c; for (int i = 1; i <= q; ++i) { scanf("%d%d", &a[i].y, &a[i].x); scanf("%c", &c); scanf("%c", &c); dir[i] = c == 'U'; a[i].p = i; } a[0].x = a[0].y = a[0].x1 = a[0].y1 = 0; x[0] = y[0] = 0; sort(a + 1, a + 1 + q, comparex); for (int i = 1, cnt = 0, prev = 0; i <= q; ++i) { if (a[i].x == prev) a[i].x1 = cnt; else cnt++, a[i].x1 = cnt, prev = a[i].x, x[cnt] = a[i].x; if (a[i].x == a[i - 1].x && a[i].y == a[i - 1].y) fr[a[i].p] = 1; } X = a[q].x1; sort(a + 1, a + 1 + q, comparey); for (int i = 1, cnt = 0, prev = 0; i <= q; ++i) if (a[i].y == prev) a[i].y1 = cnt; else cnt++, a[i].y1 = cnt, prev = a[i].y, y[cnt] = a[i].y; Y = a[q].y1; sort(a + 1, a + 1 + q, comparep); for (int i = 1; i <= q; ++i) { if (fr[i]) { printf("0\n"); continue; } res = 0; v = dir[i] * a[i].y1 + (1 - dir[i]) * a[i].x1; Get(dir[i], 1, 1, dir[i] * Y + (1 - dir[i]) * X); v = i; if (dir[i]) { printf("%d\n", n - a[i].y + 1 - x[a[res].x1]); L = a[res].x1 + 1; R = a[i].x1 - 1; if (L > R) continue; Update(0, 1, 1, X); } else { printf("%d\n", n - a[i].x + 1 - y[a[res].y1]); L = a[res].y1 + 1; R = a[i].y1 - 1; if (L > R) continue; Update(1, 1, 1, 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; struct Nod { int val; Nod *L, *R; Nod(int i) : val(i), L(NULL), R(NULL) {} }; int x[200005]; char c[200005]; int t[200005]; map<int, int> u; int v[200005]; void rfs(Nod *a) { if (a->L != NULL) { a->L->val = max(a->L->val, a->val); a->R->val = max(a->R->val, a->val); } } int get(Nod *a, int s, int d, int x) { rfs(a); int mid = (s + d) >> 1; if (x <= mid && a->L != NULL) return get(a->L, s, mid, x); if (x > mid && a->R != NULL) return get(a->R, mid + 1, d, x); return a->val; } void upd(Nod *a, int s, int d, int l, int r, int x) { rfs(a); if (l > d || s > r || l > r) return; if (s >= l && r >= d) { a->val = max(a->val, x); return; } int mid = (s + d) >> 1; if (a->L == NULL) { a->L = new Nod(a->val); a->R = new Nod(a->val); } upd(a->L, s, mid, l, r, x); upd(a->R, mid + 1, d, l, r, x); } void parc(Nod *a) { if (a != NULL) { parc(a->L); cout << a->val << ' '; parc(a->R); } } int main() { ios_base::sync_with_stdio(false); int n, q, y; cin >> n >> q; for (int i = 0; i < q; ++i) { cin >> x[i] >> y >> c[i]; t[i] = x[i]; } t[q] = 0; t[q + 1] = n + 1; sort(t, t + q + 2); for (int i = 0; i < q + 2; ++i) { u[t[i]] = i; v[i] = t[i]; } Nod *treev, *treeh; treev = new Nod(0); treeh = new Nod(0); int a, b; for (int i = 0; i < q; ++i) { a = u[x[i]]; b = q - a + 1; if (c[i] == 'L') { y = get(treeh, 1, q, b); cout << v[a] - v[y] << '\n'; upd(treev, 1, q, y + 1, a, b); upd(treeh, 1, q, b, b, a); } else { y = get(treev, 1, q, a); cout << v[q - y + 1] - v[a] << '\n'; upd(treeh, 1, q, y + 1, b, a); upd(treev, 1, q, a, a, 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
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class E556 { public static void main(String[] args) throws IOException { initReader(); int n = nextInt(); int q = nextInt(); TreeSet<Value> set = new TreeSet<Value>(); TreeSet<Value> row = new TreeSet<Value>(); set.add(new Value(0, 0, 0)); set.add(new Value(n + 1, 0, 0)); PrintWriter writer = new PrintWriter(System.out); for (int i = 0; i < q; ++i) { int a = nextInt(); int b = nextInt(); String d = next(); if (d.equals("U")) { Value temp = new Value(a - 1, 0, 0); Value higher = set.higher(temp); if (higher.colIndex == a) { writer.println(0); continue; } int z = higher.colIndex - a + higher.colHeight; writer.println(z); temp.colIndex = a; temp.colHeight = z; set.add(temp); } else { Value temp = new Value(a + 1, 0, 0); Value higher = set.lower(temp); if (higher.colIndex == a) { writer.println(0); continue; } int z = a - higher.colIndex + higher.rowLong; writer.println(z); temp.colIndex = a; temp.rowLong = z; set.add(temp); } } // System.out.println(time); writer.close(); } static BufferedReader reader; static StringTokenizer tokenizer; public static void initReader() throws FileNotFoundException { // reader = new BufferedReader(new InputStreamReader(new // FileInputStream( // new File("in.in")))); reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static String nextLine() throws IOException { return reader.readLine(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static Double nextDouble() throws IOException { return Double.parseDouble(next()); } } class Value implements Comparable<Value> { int colIndex; int colHeight; int rowLong; public Value(int colIndex, int colHeight, int rowLong) { super(); this.colIndex = colIndex; this.colHeight = colHeight; this.rowLong = rowLong; } @Override public int compareTo(Value v) { return this.colIndex - v.colIndex; } }
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, q, x, y, hor, ver; char z; set<pair<int, pair<int, int> > > S; set<pair<int, pair<int, int> > >::iterator it; set<int> B; int main() { scanf("%d%d", &n, &q); S.insert(make_pair(0, make_pair(0, 0))); while (q--) { scanf("%d%d", &y, &x); cin >> z; if (B.find(y) != B.end()) { printf("0\n"); continue; } B.insert(y); it = S.upper_bound(make_pair(-y, make_pair(0, 0))); if (it != S.end()) { hor = it->second.first; ver = it->second.second; if (z == 'U') { S.insert(make_pair(-y, make_pair(hor, y))); printf("%d\n", x - hor); } else { S.insert(make_pair(-y, make_pair(hor, ver))); printf("%d\n", y - ver); S.insert(make_pair(it->first, make_pair(x, it->second.second))); S.erase(it); } } else assert(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) { 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; seg_tree rows((1 << 19) + 23923), cols(1 << 19); int L[MAXQ], LC[MAXQ]; int R[MAXQ], RC[MAXQ]; char T[MAXQ]; vector<bool> ater, atec; int main() { setup(); cin >> N >> Q; rows.upd(0, 0); cols.upd(0, 0); 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()); 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> using namespace std; int gi() { int a; scanf("%d", &a); return a; } long long gli() { long long a; scanf("%lld", &a); return a; } struct bar_ { int a; int b; int left; int top; }; map<int, bar_> m; map<int, bar_>::iterator it; void ins(bar_ &b) { if (b.a <= b.b) m[b.b] = b; } int main() { int n = gi(); int q = gi(); bar_ bar = {1, n, 1, n}; bar_ b1, b2; ins(bar); for (int i = 0; i < q; i++) { int x = gi(); int dummy = gi(); char c; scanf(" %c", &c); it = m.lower_bound(x); if (it == m.end() || (bar = it->second).a > x) { printf("0\n"); continue; } m.erase(it); b1 = bar; b2 = bar; b1.b = x - 1; b2.a = x + 1; if (c == 'U') { printf("%d\n", bar.top - x + 1); b2.left = x + 1; } else { printf("%d\n", x - bar.left + 1); b1.top = x - 1; } ins(b1); ins(b2); } 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 = 200100; int t[2 * N][2]; int sz[2]; void build(int i) { for (int p = sz[i] - 1; p > 0; p--) { t[p][i] = min(t[p << 1][i], t[p << 1 | 1][i]); } } void update(int i, int p, int v) { for (t[p += sz[i]][i] = v; p > 1; p >>= 1) { t[p >> 1][i] = min(t[p][i], t[p ^ 1][i]); } } int query(int i, int l, int r) { int ans = INT_MAX; for (l += sz[i], r += sz[i]; l < r; l >>= 1, r >>= 1) { if (l & 1) ans = min(ans, t[l++][i]); if (r & 1) ans = min(ans, t[--r][i]); } return ans; } bool done[N]; int main() { memset(t, 0x3f3f3f3f, sizeof t); int n, q; scanf("%d %d", &n, &q); vector<int> compr_x, compr_y; vector<tuple<int, int, bool>> ql; while (q--) { int x, y; char c; scanf("%d %d %c", &x, &y, &c); ql.emplace_back(x, y, c == 'U'); compr_x.push_back(x); compr_y.push_back(y); } sort(begin(compr_x), end(compr_x)); compr_x.resize(unique(begin(compr_x), end(compr_x)) - begin(compr_x)); sort(begin(compr_y), end(compr_y)); compr_y.resize(unique(begin(compr_y), end(compr_y)) - begin(compr_y)); sz[0] = compr_x.size(); sz[1] = compr_y.size(); build(0), build(1); int x, y; bool up; for (auto& t : ql) { tie(x, y, up) = t; int xc, yc; xc = lower_bound(begin(compr_x), end(compr_x), x) - begin(compr_x); yc = lower_bound(begin(compr_y), end(compr_y), y) - begin(compr_y); if (done[xc]) printf("0\n"); else if (up) { int lo = 0, hi = yc + 1; while (lo < hi) { int mid = (lo + hi) >> 1; if (query(1, yc - mid, yc + 1) <= x) { hi = mid; } else { lo = mid + 1; } } int ans = (yc - lo == -1) ? y : (y - compr_y[yc - lo]); printf("%d\n", ans); update(0, xc, y - ans + 1); } else { int lo = 0, hi = xc + 1; while (lo < hi) { int mid = (lo + hi) >> 1; if (query(0, xc - mid, xc + 1) <= y) { hi = mid; } else { lo = mid + 1; } } int ans = (xc - lo == -1) ? x : (x - compr_x[xc - lo]); printf("%d\n", ans); update(1, yc, x - ans + 1); } done[xc] = true; } 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 val; node *l, *r; node() { val = 0; l = NULL; r = NULL; } }; node *segr, *segc; void update(node *cur, int l, int r, int x, int y, int val) { if (l > r || l > y || r < x) return; if (x <= l && r <= y) { cur->val = max(cur->val, val); return; } int mid = (l + r) >> 1; if (cur->l == NULL) cur->l = new node(); if (cur->r == NULL) cur->r = new node(); update(cur->l, l, mid, x, y, val); update(cur->r, mid + 1, r, x, y, val); } int query(node *cur, int l, int r, int idx) { if (cur == NULL || l > idx || l > r || r < idx) return 0; if (l == r && l == idx) { return cur->val; } int mid = (l + r) >> 1; return max( {cur->val, query(cur->l, l, mid, idx), query(cur->r, mid + 1, r, idx)}); } const int maxn = (int)(1e9); int main() { segr = new node(); segc = new node(); int n, q; scanf("%d %d", &n, &q); for (int i = 0; i < q; i++) { int u, v; char c; scanf("%d %d %c", &u, &v, &c); if (c == 'U') { int ma = query(segc, 1, maxn, u); printf("%d\n", v - ma); update(segr, 1, maxn, ma, v, u); update(segc, 1, maxn, u, u, v); } else { int ma = query(segr, 1, maxn, v); printf("%d\n", u - ma); update(segc, 1, maxn, ma, u, v); update(segr, 1, maxn, v, v, u); } } 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 = 4e5 + 9; int n, q, tree[2][4 * MAX], lazy[2][4 * MAX], l, r, idx, val; vector<pair<pair<int, int>, char> > v; map<int, int> encode, decode; void push(int id, int low, int high, int pos) { tree[id][pos] = max(tree[id][pos], lazy[id][pos]); if (low != high) { lazy[id][(pos << 1)] = max(lazy[id][(pos << 1)], lazy[id][pos]); lazy[id][(pos << 1) + 1] = max(lazy[id][(pos << 1) + 1], lazy[id][pos]); } } void upd(int id, int low, int high, int pos) { push(id, low, high, pos); if (l > high || r < low) { return; } if (l <= low && r >= high) { lazy[id][pos] = max(lazy[id][pos], val); return; } int mid = ((low + high) >> 1); upd(id, low, mid, (pos << 1)); upd(id, mid + 1, high, (pos << 1) + 1); tree[id][pos] = max(tree[id][(pos << 1)], tree[id][(pos << 1) + 1]); } int qwr(int id, int low, int high, int pos) { push(id, low, high, pos); if (low == high) { return tree[id][pos]; } int mid = ((low + high) >> 1); if (idx <= mid) { return qwr(id, low, mid, (pos << 1)); } else { return qwr(id, mid + 1, high, (pos << 1) + 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q; for (int i = 1; i <= q; ++i) { int x, y; char type; cin >> x >> y >> type; encode[x]; encode[y]; v.push_back({{x, y}, type}); } int cnt = 0; for (auto& element : encode) { element.second = ++cnt; decode[cnt] = element.first; } for (int i = 0; i < q; ++i) { int newX = encode[v[i].first.first], newY = encode[v[i].first.second]; char type = v[i].second; if (type == 'L') { idx = newY; int lastX = qwr(1, 1, (q << 1), 1); cout << decode[newX] - decode[lastX] << "\n"; l = lastX + 1; r = newX; val = newY; upd(0, 1, (q << 1), 1); l = newY; r = newY, val = newX; upd(1, 1, (q << 1), 1); } else { idx = newX; int lastY = qwr(0, 1, (q << 1), 1); cout << decode[newY] - decode[lastY] << "\n"; l = lastY + 1; r = newY; val = newX; upd(1, 1, (q << 1), 1); l = newX; r = newX, val = newY; upd(0, 1, (q << 1), 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.TreeSet; import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction; public class C { static int n, q; static class Query implements Comparable<Query> { public int row, col, time, id; public boolean up; public Query(int r, int c, int t, boolean up, int id) { this.row = r; this.col = c; this.time = t; this.up = up; this.id = id; } public Query() { this.row = -1; this.col = -1; this.time = -1; this.id = -1; } public int compareTo(Query a) { return Integer.compare(this.col, a.col); } } public static void main(String[] args) throws IOException { int[] l = readIntArray(); n = l[0]; q = l[1]; int[][] queries = new int[q][3]; HashSet<Integer> seen = new HashSet<Integer>(); List<int[]> tosolve = new ArrayList<int[]>(); List<Query> tosolveleft = new ArrayList<C.Query>(); List<Query> tosolveup = new ArrayList<C.Query>(); List<Query> tosolveall = new ArrayList<C.Query>(); for (int i = 0; i < q; i++) { String[] vals = br.readLine().split(" "); queries[i][0] = Integer.parseInt(vals[0]); queries[i][1] = Integer.parseInt(vals[1]); queries[i][2] = vals[2].equals("U") ? 0 : 1; if (!seen.contains(queries[i][1])) { seen.add(queries[i][1]); tosolve.add(queries[i]); Query q = new Query(queries[i][1], queries[i][0], i, queries[i][2] == 0, tosolve.size() - 1); tosolveall.add(q); if (queries[i][2] == 1) { tosolveleft.add(q); } else { tosolveup.add(q); } } } int[] solution = new int[tosolve.size()]; Arrays.fill(solution, -1); Collections.sort(tosolveleft, new Comparator<Query>() { public int compare(Query q1, Query q2) { return Integer.compare(q1.col, q2.col); } }); TreeSet<Query> upTree2 = new TreeSet<C.Query>(); for (Query q : tosolveup) { upTree2.add(q); } Segment upTree = new Segment(tosolveup.size()); for (Query q : tosolveup) { upTree.add(q); } for (int i = 0; i < tosolveleft.size(); i++) { Query cq = tosolveleft.get(i); for (;;) { Query q = upTree2.floor(cq); if (q == null) { solution[cq.id] = cq.col; break; } else { if (q.time > cq.time) { upTree2.remove(q); solution[q.id] = cq.col - q.col; } else { solution[cq.id] = cq.col - q.col; break; } } } } for (int i = 0; i < solution.length; i++) { if (solution[i] == -1) { solution[i] = n + 1 - tosolveall.get(i).col; } } StringBuilder ans = new StringBuilder(); for (int i = 0, j = 0; i < queries.length; i++) { if (j < tosolveall.size() && queries[i][0] == tosolveall.get(j).col) { append(ans, solution[j++]); } else { append(ans, 0); } } System.out.println(ans); } static void append(StringBuilder sb, int val) { if (sb.length() > 0) { sb.append('\n'); } sb.append(val); } static class Segment { public Query[] data; public int size; public Segment(int size) { this.data = new Query[size * 5]; this.size = size; } public Query getMax(int l, int r) { return getMax(0, 0, size - 1, l, r); } Query getMax(int root, int rl, int rr, int l, int r) { if (rl > rr || r < rl || l > rr) return null; if (rl >= l && rr <= r) { return data[root]; } else { int mid = (rl + rr) / 2; int lch = 2 * root + 1; int rch = 2 * root + 2; Query ml = getMax(lch, rl, mid, l, r); Query mr = getMax(rch, mid + 1, rr, l, r); if (ml == null) return mr; if (mr == null) return ml; return ml.col > mr.col ? ml : mr; } } public void remove(Query q) { remove(0, 0, size - 1, q); } void remove(int root, int rl, int rr, Query q) { if (rl > rr || q.time < rl || q.time > rr) return; if (rl == rr) { this.data[root] = null; } else { int mid = (rl + rr) / 2; int lch = 2 * root + 1; int rch = 2 * root + 2; remove(lch, rl, mid, q); remove(rch, mid + 1, rr, q); this.data[root] = data[lch]; if (data[rch] != null) { if (data[root] == null || data[root].col < data[rch].col) { data[root] = data[rch]; } } } } public void add(Query q) { add(0, 0, size - 1, q); } void add(int root, int rl, int rr, Query q) { if (rl > rr || q.time < rl || q.time > rr) return; if (rl == rr) { this.data[root] = q; } else { int mid = (rl + rr) / 2; int lch = 2 * root + 1; int rch = 2 * root + 2; add(lch, rl, mid, q); add(rch, mid + 1, rr, q); this.data[root] = data[lch]; if (data[rch] != null) { if (data[root] == null || data[root].col < data[rch].col) { data[root] = data[rch]; } } } } } static InputStreamReader isr = new InputStreamReader(System.in); static BufferedReader br = new BufferedReader(isr); static int[] readIntArray() throws IOException { String[] v = br.readLine().split(" "); int[] ans = new int[v.length]; for (int i = 0; i < ans.length; i++) { ans[i] = Integer.valueOf(v[i]); } return ans; } static long[] readLongArray() throws IOException { String[] v = br.readLine().split(" "); long[] ans = new long[v.length]; for (int i = 0; i < ans.length; i++) { ans[i] = Long.valueOf(v[i]); } return ans; } static <T> void print(List<T> v) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < v.size(); i++) { if (sb.length() > 0) { sb.append(' '); } sb.append(v.get(i)); } System.out.println(sb); } }
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 query { int x, y; char d; }; struct node { int rmq; node() : rmq(2000000000) {} void merge(node& ls, node& rs) { rmq = min(ls.rmq, rs.rmq); } }; struct segtree { node seg[1000000]; node segQuery(int n, int l, int r, int i, int j) { if (i <= l && r <= j) return seg[n]; int m = (l + r) / 2; if (m < i) return segQuery(2 * n + 2, m + 1, r, i, j); if (m >= j) return segQuery(2 * n + 1, l, m, i, j); node ls = segQuery(2 * n + 1, l, m, i, j); node rs = segQuery(2 * n + 2, m + 1, r, i, j); node a; a.merge(ls, rs); return a; } void segUpdate(int n, int l, int r, int i, int v) { if (i < l || i > r) return; if (i == l && l == r) { seg[n].rmq = v; return; } int m = (l + r) / 2; segUpdate(2 * n + 1, l, m, i, v); segUpdate(2 * n + 2, m + 1, r, i, v); seg[n].merge(seg[2 * n + 1], seg[2 * n + 2]); } int findit(int x, int e, int MM) { int a = 0, b = e; while (b - a > 1) { int c = (a + b) / 2; node n = segQuery(0, 0, MM, c, e); if (n.rmq <= x) a = c; else b = c; } node n = segQuery(0, 0, MM, a, e); int ans = (n.rmq <= x) ? a : -1; return ans; } }; int N, Q; vector<int> xmap, ymap; vector<query> queries; segtree xtree, ytree; int revmap(vector<int> const& m, int cd) { return lower_bound(begin(m), end(m), cd) - begin(m); } int main() { scanf("%d%d", &N, &Q); for (int i = 0; i < int(Q); i++) { query q; scanf("%d%d %c", &q.x, &q.y, &q.d); xmap.push_back(q.x); ymap.push_back(q.y); queries.push_back(q); } sort(begin(xmap), end(xmap)); sort(begin(ymap), end(ymap)); xmap.erase(unique(begin(xmap), end(xmap)), end(xmap)); ymap.erase(unique(begin(ymap), end(ymap)), end(ymap)); set<pair<int, int> > pts; for (query& q : queries) { pair<int, int> pt(q.x, q.y); if (pts.count(pt)) { puts("0"); continue; } pts.insert(pt); int ci = revmap(xmap, q.x); int ri = revmap(ymap, q.y); int ans; if (q.d == 'L') { int uci = xtree.findit(q.y, ci, xmap.size() - 1); ans = q.x; if (uci != -1) ans -= xmap[uci]; ytree.segUpdate(0, 0, ymap.size() - 1, ri, q.x - ans + 1); } else { int uri = ytree.findit(q.x, ri, ymap.size() - 1); ans = q.y; if (uri != -1) ans -= ymap[uri]; xtree.segUpdate(0, 0, xmap.size() - 1, ci, q.y - ans + 1); } printf("%d\n", ans); } }
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 val; int lazy; node *l; node *r; node(node *x, node *y, int v, int ll) { l = x; r = y; val = v; lazy = ll; } }; int gv(node *x) { if (x == NULL) return 0; return x->val; } int gl(node *x) { if (x == NULL) return 0; return x->lazy; } void updatecol(node *&x, int start, int end, int i, int j, int val) { if (start > j || end < i) return; if (x == NULL) { x = new node(NULL, NULL, 0, 0); } if (start >= i && end <= j) { x->lazy = max(x->lazy, val); return; } updatecol(x->l, start, (start + end) / 2, i, j, val); updatecol(x->r, (start + end) / 2 + 1, end, i, j, val); x->val = max(gv(x->l), gv(x->r)); x->val = max(x->val, gl(x->l)); x->val = max(x->val, gl(x->r)); } void updaterow(node *&x, int start, int end, int i, int j, int val) { if (start > j || end < i) return; if (x == NULL) { x = new node(NULL, NULL, 0, 0); } if (start >= i && end <= j) { x->lazy = max(x->lazy, val); return; } updaterow(x->l, start, (start + end) / 2, i, j, val); updaterow(x->r, (start + end) / 2 + 1, end, i, j, val); x->val = max(gv(x->l), gv(x->r)); x->val = max(x->val, gl(x->l)); x->val = max(x->val, gl(x->r)); } int queryrow(node *&x, int start, int end, int i) { if (start > i || end < i) return 0; if (x == NULL) return 0; if (start == end) return max(x->lazy, x->val); return max(x->lazy, queryrow(x->l, start, (start + end) / 2, i) + queryrow(x->r, (start + end) / 2 + 1, end, i)); } int querycol(node *&x, int start, int end, int i) { if (start > i || end < i) return 0; if (x == NULL) return 0; if (start == end) return max(x->lazy, x->val); return max(x->lazy, querycol(x->l, start, (start + end) / 2, i) + querycol(x->r, (start + end) / 2 + 1, end, i)); } node *rows; node *cols; map<long long, int> vis; int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; for (int g = 0; g < q; g++) { int x, y; cin >> x >> y; swap(x, y); char t; cin >> t; if (vis[1LL * (1e9 + 1) * (int)(t - 'A') + x]) { cout << 0 << '\n'; continue; } vis[1LL * (1e9 + 1) * (int)(t - 'A') + x] = 1; if (t == 'L') { int o = queryrow(rows, 1, n, x); if (o >= y) { cout << 0 << '\n'; continue; } cout << y - o << '\n'; updaterow(rows, 1, n, x, x, y); updatecol(cols, 1, n, o, y, x); } else { int o = querycol(cols, 1, n, y); if (o >= x) { cout << 0 << '\n'; continue; } cout << x - o << '\n'; updatecol(cols, 1, n, y, y, x); updaterow(rows, 1, n, o, x, 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 long long MOD = 1e9 + 7; long long n, q, row[200010]; vector<pair<long long, long long> > que; vector<long long> v, r; map<long long, long long> mp; map<pair<long long, long long>, long long> res; struct segTree { long long n2, dat[800010]; void Init() { n2 = 1; while (n2 < v.size()) n2 *= 2; for (int i = 0; i < n2 * 2 + 5; i++) dat[i] = 1; } void pushDown(long long k) { dat[k * 2 + 1] = max(dat[k * 2 + 1], dat[k]); dat[k * 2 + 2] = max(dat[k * 2 + 2], dat[k]); } void upd(long long k, long long lb, long long ub, long long tlb, long long tub, long long val) { if (ub < tlb || tub < lb) return; if (tlb <= lb && ub <= tub) { dat[k] = max(dat[k], val); return; } pushDown(k); upd(k * 2 + 1, lb, (lb + ub) / 2, tlb, tub, val); upd(k * 2 + 2, (lb + ub) / 2 + 1, ub, tlb, tub, val); } long long get(long long k, long long lb, long long ub, long long to) { if (lb == ub) return dat[k]; pushDown(k); long long mid = (lb + ub) / 2; if (to <= mid) return get(k * 2 + 1, lb, mid, to); return get(k * 2 + 2, mid + 1, ub, to); } } up, lft; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> q; long long x, xx; char y; for (int i = 0; i < q; i++) { cin >> x >> xx; r.push_back(-xx); row[i] = xx; cin >> y; que.push_back(make_pair(x, (y == 'U' ? 0 : 1))); v.push_back(x); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (int i = 0; i < v.size(); i++) mp[v[i]] = i; sort(r.begin(), r.end()); r.erase(unique(r.begin(), r.end()), r.end()); up.Init(); lft.Init(); for (int i = 0; i < q; i++) { if (res[que[i]] > 0) { puts("0"); continue; } int pos = mp[que[i].first]; if (que[i].second == 0) { int to = up.get(0, 0, up.n2 - 1, pos); printf("%I64d\n", row[i] - to + 1); res[que[i]] = 1; int ub = lower_bound(r.begin(), r.end(), -to + 1) - r.begin() - 1; lft.upd(0, 0, lft.n2 - 1, pos, ub, que[i].first + 1); } else { int to = lft.get(0, 0, lft.n2 - 1, pos); printf("%I64d\n", que[i].first - to + 1); res[que[i]] = 1; int lb = lower_bound(v.begin(), v.end(), to) - v.begin(); up.upd(0, 0, up.n2 - 1, lb, pos, row[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; 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; int yy = lower_bound(qy, qy + m, y) - qy; yy++; 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; int yy = lower_bound(qy, qy + m, y) - qy; xx++; yy++; 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); } ans = -1; query(1, m, 5, 1, 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> #pragma comment(linker, "/STACK:256000000") using namespace std; struct State { int l, r; int addH, addV; State() { l = r = addH = addV = 0; } State(int l, int r, int addH, int addV) : l(l), r(r), addH(addH), addV(addV) {} bool operator<(const State &other) const { return l < other.l; } }; set<State> S; unordered_set<int> used; char s[10]; void add(State s) { if (s.l <= s.r) { S.insert(s); } } int process(int x, int y, char s) { set<State>::iterator it = S.upper_bound(State(y, y, 0, 0)); State current; if (it == S.end()) { current = *S.rbegin(); } else { --it; current = *it; } S.erase(current); if (s == 'U') { State L(current.l, y, current.addH + (current.r - y), current.addV); State R(y + 1, current.r, current.addH, 0); add(L); add(R); return current.r - y + 1 + current.addH; } else { State L(current.l, y - 1, 0, current.addV); State R(y, current.r, current.addH, current.addV + (y - current.l)); add(L); add(R); return y - current.l + 1 + current.addV; } } int main() { int n, q; scanf("%d%d", &n, &q); S.insert(State(1, n, 0, 0)); for (int i = 0; i < q; ++i) { int x, y; scanf("%d%d", &y, &x); scanf("%s", &s); if (used.count(x)) { printf("0\n"); continue; } used.insert(x); int res = process(x, y, s[0]); printf("%d\n", 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; int n, Q; map<int, pair<int, char> > Map; int main() { scanf("%d%d", &n, &Q); Map[0] = make_pair(n + 1, 'U'); Map[n + 1] = make_pair(n + 1, 'L'); while (Q--) { int x, y; char d; scanf("%d%d %c", &x, &y, &d); map<int, pair<int, char> >::iterator k = Map.lower_bound(x); if (k->first == x) { puts("0"); continue; } if (d == 'L') --k; int cnt = abs(x - k->first) + ((d != k->second.second) ? (0) : (k->second.first)); printf("%d\n", cnt); Map[x] = make_pair(cnt, d); } 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.StringTokenizer; import java.util.TreeSet; public class ProblemE { public static void main(String[] args) { int testMode = 0; String testInput = "15 7\n" + "8 8 U\n" + "6 10 L\n" + "9 7 L\n" + "3 13 L\n" + "15 1 L\n" + "13 3 U\n" + "1 15 L\n"; /*-------------------------------------------------------------------------------------------------------*/ FastReader in = new FastReader(testMode == 0 ? System.in : new ByteArrayInputStream(testInput.getBytes())); PrintWriter out = new PrintWriter(System.out); new ProblemSolver().solve(in, out); out.close(); } } class ProblemSolver { void solve(FastReader in, PrintWriter out) { int n = in.nextInt(); int q = in.nextInt(); TreeSet<Pair> chunks = new TreeSet<Pair>(); chunks.add(new Pair(1, n, n, 1)); for (int i = 0; i < q; i++) { int x = in.nextInt(); int y = in.nextInt(); char c = in.nextToken().charAt(0); Pair p = chunks.floor(new Pair(x, x, x, x)); if (p != null && p.x1 <= x && x <= p.x2) { chunks.remove(p); if (c == 'U') { int h = (p.ub - p.x1 + 1) - (x - p.x1); out.println(h); if (p.x1 < x) chunks.add(new Pair(p.x1, x - 1, p.ub, p.lb)); if (x < p.x2) chunks.add(new Pair(x + 1, p.x2, p.ub, x + 1)); } else { int w = x - p.lb + 1; out.println(w); if (p.x1 < x) chunks.add(new Pair(p.x1, x - 1, x - 1, p.lb)); if (x < p.x2) chunks.add(new Pair(x + 1, p.x2, p.ub, p.lb)); } } else { out.println(0); } } } } class Pair implements Comparable<Pair> { public int x1; public int x2; public int ub; public int lb; public Pair(int x1, int x2, int y, int lb) { this.x1 = x1; this.x2 = x2; this.ub = y; this.lb = lb; } public int compareTo(Pair o) { if (this.x1 < o.x1) return -1; if (this.x1 == o.x1) return 0; return 1; } } class FastReader extends BufferedReader { private StringTokenizer st; public FastReader(InputStream is) { super(new InputStreamReader(is)); } public String nextToken() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(readLine()); } return st.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public int[] nextArrayInt(int size) { int[] ret = new int[size]; for (int i = 0; i < size; i++) { ret[i] = nextInt(); } 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; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dc[] = {0, 1, 0, -1, 1, -1, -1, 1}; const double eps = 1e-9; const int INF = 0x7FFFFFFF; const long long INFLL = 0x7FFFFFFFFFFFFFFFLL; const double pi = acos(-1); template <class T> T take(queue<T> &O) { T tmp = O.front(); O.pop(); return tmp; } template <class T> T take(stack<T> &O) { T tmp = O.top(); O.pop(); return tmp; } template <class T> T take(priority_queue<T> &O) { T tmp = O.top(); O.pop(); return tmp; } template <class T> inline void getint(T &num) { bool neg = 0; num = 0; char c; while ((c = getchar_unlocked()) && (!isdigit(c) && c != '-')) ; if (c == '-') { neg = 1; c = getchar_unlocked(); } do num = num * 10 + c - '0'; while ((c = getchar_unlocked()) && isdigit(c)); if (neg) num = -num; } template <class T> inline bool inRange(T z, T a, T b) { return a <= z && z <= b; } void OPEN(string in = "input.txt", string out = "output.txt") { freopen(in.c_str(), "r", stdin); freopen(out.c_str(), "w", stdout); return; } struct Action { int x, y, t, dir, ans; inline void getData(int _t = -1) { cin >> x >> y; t = _t; string s; cin >> s; if (s[0] == 'L') dir = 0; else dir = 1; ans = 0; return; } inline bool operator<(const Action &O) const { return make_tuple(x, y, t) < make_tuple(O.x, O.y, O.t); } inline void calculate(int xkiri, int yatas) { if (dir == 0) ans = x - xkiri; else ans = y - yatas; return; } }; int rmq[20][200005]; int arr[200005], n, q; Action action[200005]; inline int query(int l, int r) { if (r - l + 1 <= 0) return -1; int loglen = (r - l + 1); loglen = 31 - __builtin_clz(loglen); ; return action[arr[rmq[loglen][l]]].t < action[arr[rmq[loglen][r - (1 << loglen) + 1]]].t ? rmq[loglen][l] : rmq[loglen][r - (1 << loglen) + 1]; } int cntt = 0; void rec(int idx, int l, int r, int xkiri, int yatas) { action[arr[idx]].calculate(xkiri, yatas); if (action[arr[idx]].dir == 0) { if (l <= idx - 1) rec(query(l, idx - 1), l, idx - 1, xkiri, max(yatas, action[arr[idx]].y)); if (idx + 1 <= r) rec(query(idx + 1, r), idx + 1, r, xkiri, yatas); } else { if (l <= idx - 1) rec(query(l, idx - 1), l, idx - 1, xkiri, yatas); if (idx + 1 <= r) rec(query(idx + 1, r), idx + 1, r, max(xkiri, action[arr[idx]].x), yatas); } return; } set<pair<int, int> > uda; int cntq; int main(int argc, const char *argv[]) { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> q; for (int(i) = (0), _t = (q); i < (_t); ++(i)) { action[i].getData(i); if (uda.count(make_pair(action[i].x, action[i].y))) action[i].ans = 0; else { uda.insert(make_pair(action[i].x, action[i].y)); arr[cntq] = i; ++cntq; } } sort(arr, arr + cntq, [](const int &A, const int &B) { return action[A] < action[B]; }); for (int(i) = (0), _t = (cntq); i < (_t); ++(i)) rmq[0][i] = i; for (int j = 1; (1 << j) <= cntq; ++j) for (int(i) = (0), _t = (cntq - (1 << j)); i <= (_t); ++(i)) rmq[j][i] = (action[arr[rmq[j - 1][i]]].t < action[arr[rmq[j - 1][i + (1 << (j - 1))]]].t ? rmq[j - 1][i] : rmq[j - 1][i + (1 << (j - 1))]); rec(query(0, cntq - 1), 0, cntq - 1, 0, 0); for (int(i) = (0), _t = (q); i < (_t); ++(i)) cout << action[i].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
import java.util.Scanner; import java.util.TreeMap; public class Main { public void solve() { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int Q = scanner.nextInt(); // 自分よりx座標の小さいクエリが来た時にどこが壁になるか、を記録する TreeMap<Integer, Wall> wallMap = new TreeMap<>(); wallMap.put(N + 1, new Wall(0, 0)); StringBuilder builder = new StringBuilder(); for (int q = 0; q < Q; q++) { int x = scanner.nextInt(); int y = scanner.nextInt(); String s = scanner.next(); if (wallMap.containsKey(x)) { // 処理したことのあるクエリなら、チョコは残っていないはず builder.append(0 + "\n"); continue; } // 自分より右側にある食べられ情報を読む Wall higherWall = wallMap.get(wallMap.higherKey(x)); if (s.equals("U")) { // 上向き食べる時 builder.append(y - higherWall.y + "\n"); wallMap.put(x, new Wall(higherWall.y, higherWall.x)); // 今後xとhigherWall.xの間で左に行こうとすると、xが壁になる higherWall.x = x; } else { // 左向きに食べる時 builder.append(x - higherWall.x + "\n"); wallMap.put(x, new Wall(y, higherWall.x)); } } System.out.println(builder.toString()); scanner.close(); } private class Wall { int y, x; public Wall(int y, int x) { this.x = x; this.y = y; } } public static void main(String[] args) { new Main().solve(); } }
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 INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const int MAX = 5e6; int N; namespace seg1 { int seg[MAX], lazy[MAX], R[MAX], L[MAX], ptr; int get_l(int i) { if (L[i] == 0) { seg[ptr] = seg[i]; L[i] = ptr++; } return L[i]; } int get_r(int i) { if (R[i] == 0) { seg[ptr] = seg[i]; R[i] = ptr++; } return R[i]; } void build() { ptr = 2; seg[1] = 0; } void prop(int p, int l, int r) { if (lazy[p] == 0) return; if (l != r) { lazy[get_l(p)] = max(lazy[get_l(p)], lazy[p]); lazy[get_r(p)] = max(lazy[get_r(p)], lazy[p]); seg[get_l(p)] = max(seg[get_l(p)], lazy[p]); seg[get_r(p)] = max(seg[get_r(p)], lazy[p]); } lazy[p] = 0; } int query(int a, int b, int p = 1, int l = 0, int r = N - 1) { if (b < l or r < a) return 0; if (a <= l and r <= b) return seg[p]; prop(p, l, r); int m = (l + r) / 2; return max(query(a, b, get_l(p), l, m), query(a, b, get_r(p), m + 1, r)); } int update(int a, int b, int val, int p = 1, int l = 0, int r = N - 1) { if (b < l or r < a) return seg[p]; if (a <= l and r <= b) { lazy[p] = max(lazy[p], val); seg[p] = max(seg[p], val); return seg[p]; } prop(p, l, r); int m = (l + r) / 2; return seg[p] = max(update(a, b, val, get_l(p), l, m), update(a, b, val, get_r(p), m + 1, r)); } }; // namespace seg1 namespace seg2 { int seg[MAX], lazy[MAX], R[MAX], L[MAX], ptr; int get_l(int i) { if (L[i] == 0) { seg[ptr] = seg[i]; L[i] = ptr++; } return L[i]; } int get_r(int i) { if (R[i] == 0) { seg[ptr] = seg[i]; R[i] = ptr++; } return R[i]; } void build() { ptr = 2; seg[1] = 0; } void prop(int p, int l, int r) { if (lazy[p] == 0) return; if (l != r) { lazy[get_l(p)] = max(lazy[get_l(p)], lazy[p]); lazy[get_r(p)] = max(lazy[get_r(p)], lazy[p]); seg[get_l(p)] = max(seg[get_l(p)], lazy[p]); seg[get_r(p)] = max(seg[get_r(p)], lazy[p]); } lazy[p] = 0; } int query(int a, int b, int p = 1, int l = 0, int r = N - 1) { if (b < l or r < a) return 0; if (a <= l and r <= b) return seg[p]; prop(p, l, r); int m = (l + r) / 2; return max(query(a, b, get_l(p), l, m), query(a, b, get_r(p), m + 1, r)); } int update(int a, int b, int val, int p = 1, int l = 0, int r = N - 1) { if (b < l or r < a) return seg[p]; if (a <= l and r <= b) { lazy[p] = max(lazy[p], val); seg[p] = max(seg[p], val); return seg[p]; } prop(p, l, r); int m = (l + r) / 2; return seg[p] = max(update(a, b, val, get_l(p), l, m), update(a, b, val, get_r(p), m + 1, r)); } }; // namespace seg2 int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; seg1::build(); seg2::build(); N = n + 3; set<int> usd; for (int i = 0; i < q; i++) { char c; int x, y; cin >> x >> y >> c; if (c == 'L') { if (usd.count(x)) { cout << 0 << '\n'; continue; } usd.insert(x); int maxi = seg2::query(y, y); cout << x - maxi << '\n'; seg1::update(maxi, x, y); } else { if (usd.count(x)) { cout << 0 << '\n'; continue; } usd.insert(x); int maxi = seg1::query(x, x); ; cout << y - maxi << '\n'; seg2::update(maxi, 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> using namespace std; struct section { int s, e; int lb, lr; section(int a, int b, int c, int d) { s = a; e = b; lb = c; lr = d; } bool operator<(const section aa) const { return e < aa.e; } }; set<section> v; map<int, bool> vis; int cut(int pos, char action, section aa) { v.erase(aa); if (action == 'U') { if (pos > aa.s) v.insert(section(aa.s, pos - 1, aa.lb, aa.lr)); if (pos < aa.e) v.insert(section(pos + 1, aa.e, pos, aa.lr)); return aa.lr - pos; } else { if (pos > aa.s) v.insert(section(aa.s, pos - 1, aa.lb, pos)); if (pos < aa.e) v.insert(section(pos + 1, aa.e, aa.lb, aa.lr)); return pos - aa.lb; } } int n, m; void solve() { cin >> n >> m; v.insert(section(1, n, 0, n + 1)); char action; int x, y; for (int i = 0; i < m; i++) { cin >> x >> y >> action; if (vis[x]) { cout << 0 << endl; continue; } vis[x] = 1; section tem(0, x, 0, 0); section p = *(v.lower_bound(tem)); cout << cut(x, action, p) << endl; } } int main() { 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
#include <bits/stdc++.h> using namespace std; map<long long, pair<char, long long> > mp; map<long long, pair<char, long long> >::iterator it; int main() { long long x, y, n, m, ans; char c; scanf("%lld %lld", &n, &m); mp[0] = make_pair('U', n); mp[n + 1] = make_pair('L', n); while (m--) { scanf("%lld %lld %c", &x, &y, &c); it = mp.lower_bound(x); if (it->first == x) { printf("0\n"); continue; } if (c == 'L') it--; ans = abs(it->first - x); if (c == it->second.first) ans += it->second.second; mp[x] = make_pair(c, ans); printf("%lld\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
import java.util.*; public final class Chocolate { public static void main(String arg[]) { Scanner br=new Scanner(System.in); int n=br.nextInt(); int q=br.nextInt(); int x[]=new int[200005]; int y[]=new int[200005]; ArrayList<Integer> g=new ArrayList<Integer>(); int j=0; x[0]=y[0]=0; x[q+1]=y[q+1]=0; TreeMap<Integer ,Integer > t=new TreeMap<Integer ,Integer >(); t.put(0,q+1); t.put(n+1,q+1); //Iterator<Pair> it=t.iterator(); Map.Entry<Integer ,Integer> it; for(int i=1;i<=q;i++) { x[i]=br.nextInt(); y[i]=br.nextInt(); //br.nextLine(); String s=new String(br.next()); if(s.charAt(0)=='U') { it=t.ceilingEntry(x[i]); } else { it=t.floorEntry(x[i]); } if(it.getKey()==x[i]) { g.add(0); continue; } t.put(x[i],i); if(s.charAt(0)=='U') { g.add(y[i]-y[it.getValue()]); y[i]=y[it.getValue()]; } else{ g.add(x[i]-x[it.getValue()]); x[i]=x[it.getValue()]; } } Iterator<Integer> itr=g.iterator(); while(itr.hasNext()) System.out.print(itr.next()+" "); } } final class MyEntry<K, V> implements Map.Entry<K, V> { private final K key; private V value; public MyEntry(K key, V value) { this.key = key; this.value = value; } @Override public K getKey() { return key; } @Override public V getValue() { return value; } @Override public V setValue(V value) { V old = this.value; this.value = value; return old; } }
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 sz = 1600006; int n, m; int co[sz]; typedef struct A { int x, y; char dir[5]; } AA; AA t[sz]; bool cmp(AA a, AA b) { if (a.x < b.x) return 1; return 0; } int get_idx(int x) { int l = 0, r = 2 * m; int mid; while (r != l) { mid = (l + r) >> 1; if (x <= co[mid]) r = mid; else l = mid + 1; } return l; } int max(int a, int b) { return (a > b) ? a : b; } struct SegTree { int tr[sz], lazy[sz]; SegTree() { memset(tr, 0, sizeof(tr)); memset(lazy, 0, sizeof(lazy)); } void up(int r) { tr[r] = max(tr[r << 1], tr[r << 1 | 1]); } void down(int L, int mid, int R, int r) { lazy[r << 1] = max(lazy[r << 1], lazy[r]); lazy[r << 1 | 1] = max(lazy[r << 1 | 1], lazy[r]); tr[r << 1] = max(tr[r << 1], lazy[r]); tr[r << 1 | 1] = max(tr[r << 1 | 1], lazy[r]); lazy[r] = 0; } void update(int x, int y, int z, int L, int R, int r) { if (x <= L && R <= y) { lazy[r] = max(lazy[r], z); tr[r] = max(tr[r], z); } else { int mid = (L + R) >> 1; if (lazy[r] != 0) { down(L, mid, R, r); } if (x <= mid) update(x, y, z, L, mid, r << 1); if (y > mid) update(x, y, z, mid + 1, R, r << 1 | 1); up(r); } } int query(int x, int y, int L, int R, int r) { if (x <= L && R <= y) return tr[r]; else { int maxi = 0; int mid = (L + R) >> 1; if (lazy[r] != 0) { down(L, mid, R, r); } if (x <= mid) maxi = max(maxi, query(x, y, L, mid, r << 1)); if (y > mid) maxi = max(maxi, query(x, y, mid + 1, R, r << 1 | 1)); return maxi; } } }; SegTree lf, up; int main() { int i, j, id, q; while (scanf("%d %d", &n, &m) != EOF) { co[0] = 0; j = 1; for (i = 0; i < m; i++) { scanf("%d %d %s", &t[i].x, &t[i].y, t[i].dir); co[j++] = t[i].x; co[j++] = t[i].y; } sort(co, co + 2 * m + 1); for (i = 0; i < m; i++) { if (t[i].dir[0] == 'U') { id = get_idx(t[i].x); q = up.query(id, id, 0, 2 * m, 1); printf("%d\n", t[i].y - co[q]); up.update(id, id, get_idx(t[i].y), 0, 2 * m, 1); lf.update(q, get_idx(t[i].y), id, 0, 2 * m, 1); } else if (t[i].dir[0] == 'L') { id = get_idx(t[i].y); q = lf.query(id, id, 0, 2 * m, 1); printf("%d\n", t[i].x - co[q]); lf.update(id, id, get_idx(t[i].x), 0, 2 * m, 1); up.update(q, get_idx(t[i].x), id, 0, 2 * 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; int N, Q; map<int, int> bio; map<int, pair<int, int> > vrhovi; int main(void) { scanf("%d%d", &N, &Q); vrhovi[N + 1] = make_pair(0, 0); for (int i = 0; i < Q; i++) { int x, y; string s; cin >> x >> y >> s; if (bio[x]) { printf("0\n "); continue; } bio[x] = 1; map<int, pair<int, int> >::iterator it = vrhovi.upper_bound(x); if (s[0] == 'L') { printf(" %d\n", x - (it->second).second); vrhovi[x].first = y; vrhovi[x].second = (it->second).second; } if (s[0] == 'U') { printf(" %d\n", y - (it->second).first); vrhovi[x].first = (it->second).first; vrhovi[x].second = (it->second).second; (it->second).second = 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; struct X { int orig; int other; int left; int top; X() {} X(int orig, int other, int left, int top) { this->orig = orig; this->other = other; this->left = left; this->top = top; } }; int main() { int n; cin >> n; int q; cin >> q; set<pair<int, int> > seen; map<int, X> mp; mp[n] = X(n, 1, 1, 1); for (int i = 0; i < q; ++i) { int c; cin >> c; int r; cin >> r; string lu; cin >> lu; pair<int, int> cr = make_pair(c, r); if (seen.find(cr) != seen.end()) { cout << "0" << endl; continue; } seen.insert(cr); map<int, X>::iterator it = mp.lower_bound(c); X old = it->second; X new1 = X(c - 1, old.other, old.left, r + 1); X new2 = X(old.orig, c + 1, old.left, old.top); if (lu == "L") cout << (c - old.left + 1) << endl; else { new1.top = old.top; new2.left = c + 1; cout << (r - old.top + 1) << endl; } mp.erase(it); if (new1.orig >= new1.other) mp[new1.orig] = new1; if (new2.orig >= new2.other) mp[new2.orig] = new2; } }
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; const int inv = 2e9; int n, q; map<int, int> vis; set<pair<int, int> > up, lt; set<pair<int, int> >::iterator it, ti; int main() { ios_base::sync_with_stdio(0); cin.tie(0); up.insert({-inv, -inv}); lt.insert({-inv, -inv}); cin >> n >> q; while (q--) { int x, y; char c; cin >> x >> y >> c; swap(x, y); if (vis[x]) { cout << 0 << endl; continue; } vis[x] = 1; int a, b; if (c == 'U') { it = up.lower_bound({y, -1}); if (it == up.end()) { it = lt.lower_bound({x, -1}); it--; if (it->first == -inv) { cout << x << endl; up.insert({y, x}); } else { cout << x - it->first << endl; up.insert({y, x - it->first}); } } else { a = n + 1 - it->first - it->second + 1; b = n + 1 - it->first; it = lt.lower_bound({x, -1}); it--; if (it->first > b) a = it->first + 1; cout << x - a + 1 << endl; up.insert({y, x - a + 1}); } } else { it = lt.lower_bound({x, -1}); if (it == lt.end()) { it = up.lower_bound({y, -1}); it--; if (it->first == -inv) { cout << y << endl; lt.insert({x, y}); } else { cout << y - it->first << endl; lt.insert({x, y - it->first}); } } else { a = n + 1 - it->first - it->second + 1; b = n + 1 - it->first; it = up.lower_bound({y, -1}); it--; if (it->first > b) a = it->first + 1; cout << y - a + 1 << endl; lt.insert({x, y - a + 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 NMAX = 200010, INF = 0x3f3f3f3f; int N, Q, X[NMAX], Y[NMAX], Aint[2][4 * NMAX], Cnt, Order[NMAX]; char T[NMAX]; map<int, int> Map; set<int> Used; void Update(int Index, int Node, int Left, int Right, int Pos, int Val) { if (Left == Right) { Aint[Index][Node] = Val; return; } int Mid = (Left + Right) / 2; if (Pos <= Mid) Update(Index, (2 * Node), Left, Mid, Pos, Val); else Update(Index, (2 * Node + 1), Mid + 1, Right, Pos, Val); Aint[Index][Node] = min(Aint[Index][(2 * Node)], Aint[Index][(2 * Node + 1)]); } int Query(int Index, int Node, int Left, int Right, int LeftB, int RightB) { if (LeftB <= Left && Right <= RightB) return Aint[Index][Node]; int Mid = (Left + Right) / 2, Now = INF; if (LeftB <= Mid) Now = min(Now, Query(Index, (2 * Node), Left, Mid, LeftB, RightB)); if (Mid < RightB) Now = min(Now, Query(Index, (2 * Node + 1), Mid + 1, Right, LeftB, RightB)); return Now; } int main() { scanf("%i %i", &N, &Q); for (int i = 1; i <= Q; ++i) scanf("%i %i %c", &X[i], &Y[i], &T[i]), swap(X[i], Y[i]); vector<int> V; for (int i = 1; i <= Q; ++i) V.push_back(X[i]); sort(V.begin(), V.end()); Map[V[0]] = (Cnt = 1); Order[1] = V[0]; for (int i = 1; i < V.size(); ++i) { if (V[i] == V[i - 1]) continue; Map[V[i]] = ++Cnt; Order[Cnt] = V[i]; } Order[Cnt + 1] = N + 1; for (int i = 1; i < 4 * NMAX; ++i) Aint[0][i] = Aint[1][i] = INF; for (int i = 1; i <= Q; ++i) { if (Used.count(X[i])) { printf("0\n"); continue; } Used.insert(X[i]); if (T[i] == 'U') { int CrtIndex = Map[X[i]]; int Left = 1, Right = CrtIndex, Mid, Up = 0; while (Left <= Right) { Mid = (Left + Right) / 2; if (Query(1, 1, 1, Cnt, Mid, CrtIndex) > Y[i]) Up = Mid, Right = Mid - 1; else Left = Mid + 1; } Up--; printf("%i\n", X[i] - Order[Up]); Update(0, 1, 1, Cnt, CrtIndex, Order[Up] + 1); } else { int CrtIndex = Map[X[i]]; int Left = CrtIndex, Right = Cnt, Mid, Lft = 0; while (Left <= Right) { Mid = (Left + Right) / 2; if (Query(0, 1, 1, Cnt, CrtIndex, Mid) > X[i]) Lft = Mid, Left = Mid + 1; else Right = Mid - 1; } Lft++; printf("%i\n", Y[i] - (N + 1 - Order[Lft])); Update(1, 1, 1, Cnt, CrtIndex, N + 1 - Order[Lft] + 1); } } }
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
import java.io.*; import java.util.*; import java.awt.Point; public class CodeForces { static boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; public class Intersect implements Comparable<Intersect> { public int x; public int y; public Intersect(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Intersect o) { if (x == o.x) { return y - o.y; } return x - o.x; } } void runCase(int caseNum) throws IOException { int n = nextInt(); int q = nextInt(); TreeMap<Integer, Integer> rowLimits = new TreeMap<>(); TreeMap<Integer, Integer> colLimits = new TreeMap<>(); Set<Point> taken = new HashSet<>(); rowLimits.put(n, 0); colLimits.put(n, 0); for (int i = 0; i < q; ++i) { int x = nextInt(); int y = nextInt(); String dir = next(); Point cur = new Point(x, y); if (taken.contains(cur)) { System.out.println(0); continue; } taken.add(cur); if (dir.equals("U")) { int row = rowLimits.ceilingEntry(x).getValue(); System.out.println(y - row); colLimits.put(y, x); rowLimits.put(x, row); } else { int col = colLimits.ceilingEntry(y).getValue(); System.out.println(x - col); rowLimits.put(x, y); colLimits.put(y, col); } } } public static void main(String[] args) throws IOException { if (ONLINE_JUDGE) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter(System.out); //out = new PrintWriter("output.txt"); } new CodeForces().runIt(); out.flush(); out.close(); return; } static BufferedReader in; private StringTokenizer st; static PrintWriter out; static int pos; static String curInput = ""; String next() throws IOException { while (!st.hasMoreTokens()) { String line = in.readLine(); if (line == null) { return null; } st = new StringTokenizer(line); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void runIt() throws IOException { st = new StringTokenizer(""); // int N = nextInt(); // for (int i = 0; i < N; i++) { // runCase(i + 1); // } runCase(0); out.flush(); } }
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 query { int x, height, tab; }; int max(int a, int b) { if (a < b) return b; else return a; } int min(int a, int b) { if (a < b) return a; else return b; } int maxTree[2][1200000]; int getmax(int tab, int id, int lb, int ub, int cell) { if (lb == cell and ub == cell + 1) return maxTree[tab][id]; else if ((lb + ub) / 2 > cell) return max(maxTree[tab][id], getmax(tab, 2 * id + 1, lb, (lb + ub) / 2, cell)); else return max(maxTree[tab][id], getmax(tab, 2 * id + 2, (lb + ub) / 2, ub, cell)); } void setmax(int tab, int id, int lb, int ub, int mlb, int mub, int val) { if (maxTree[tab][id] >= val) return; if (lb == mlb and ub == mub) { maxTree[tab][id] = val; return; } if (mlb < (lb + ub) / 2) setmax(tab, 2 * id + 1, lb, (lb + ub) / 2, mlb, min(mub, (lb + ub) / 2), val); if (mub > (lb + ub) / 2) setmax(tab, 2 * id + 2, (lb + ub) / 2, ub, max(mlb, (lb + ub) / 2), mub, val); } int main() { int n, q; scanf("%d %d", &n, &q); int pow2 = 1; while (pow2 < 2 * q + 2) pow2 *= 2; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2 * pow2; j++) maxTree[i][j] = 0; } query tabQ[q]; int x, height, tab; string dir; int m; vector<int> keys(2 * q + 2); unordered_map<int, int> assoc; for (int i = 0; i < q; i++) { cin >> x >> height >> dir; x--; height--; if (dir[0] == 'U') { tab = 0; } else { tab = 1; swap(x, height); } tabQ[i].x = x; tabQ[i].height = height; tabQ[i].tab = tab; keys[2 * i] = x; keys[2 * i + 1] = height; } keys[2 * q] = -1; keys[2 * q + 1] = 0; sort(keys.begin(), keys.end()); assoc[keys[0]] = 0; int cnt = 1; for (int i = 1; i < keys.size(); i++) { if (keys[i] != keys[i - 1]) { assoc[keys[i]] = cnt; cnt++; } } for (int i = 0; i < q; i++) { m = getmax(tabQ[i].tab, 0, 0, pow2, assoc[tabQ[i].x]); printf("%d\n", tabQ[i].height - m + 1); setmax(tabQ[i].tab, 0, 0, pow2, assoc[tabQ[i].x], assoc[tabQ[i].x] + 1, tabQ[i].height + 1); if (m < tabQ[i].height + 1) { setmax(1 - tabQ[i].tab, 0, 0, pow2, assoc[m - 1] + 1, assoc[tabQ[i].height] + 1, tabQ[i].x + 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
import java.util.Arrays; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author Aldo Culquicondor */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { int[][] ps; public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(), q = in.nextInt(); Query[] queries = new Query[q]; ps = new int[2][q]; for (int i = 0; i < q; ++i) { queries[i] = new Query(in.nextInt(), in.nextInt(), in.next().charAt(0) == 'U'); ps[0][i] = queries[i].p[0]; ps[1][i] = queries[i].p[1]; } Arrays.sort(ps[0]); Arrays.sort(ps[1]); Maximum[] maximum = new Maximum[2]; maximum[0] = new Maximum(q); maximum[1] = new Maximum(q); for (int i = 0; i < q; ++i) { Query query = queries[i]; int stId = ArrayUtils.lowerBound(ps[query.up], query.other()); long maxi = maximum[query.up].query(stId); out.println(query.dir() - maxi); maximum[query.up].update(stId, (long)query.dir()); maximum[1 - query.up].update( ArrayUtils.lowerBound(ps[1 - query.up], (int)maxi), ArrayUtils.upperBound(ps[1 - query.up], query.dir()) - 1, query.other()); } } } class Query { int[] p; int up; Query(int x, int y, boolean up) { p = new int[]{y, x}; this.up = up ? 1 : 0; } int dir() { return p[1 - up]; } int other() { return p[up]; } } class Maximum extends LazyPropagation { Maximum(int n) { super(n); } protected long merge(long x, long y) { return Math.max(x, y); } protected long nodeUpdate(long prev, long up, int size) { return Math.max(prev, up); } } class InputReader { private BufferedReader reader; private 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()); } } class ArrayUtils { public static int lowerBound(int[] A, int x) { int lo = -1, hi = A.length, mid; while (hi - lo > 1) { mid = (hi + lo) >> 1; if (A[mid] < x) lo = mid; else hi = mid; } return hi; } public static int upperBound(int[] A, int x) { int lo = -1, hi = A.length, mid; while (hi - lo > 1) { mid = (hi + lo) >> 1; if (A[mid] > x) hi = mid; else lo = mid; } return hi; } } abstract class SegmentTree { int n; long[] data; long defaultValue; static int upperPowTwo(int x) { return 1 << (32 - Integer.numberOfLeadingZeros(x - 1)); } static int center(int x, int y) { return (x + y) >> 1; } static int left(int x) { return (x << 1) + 1; } public SegmentTree(int n, long defaultValue) { this.n = n; this.defaultValue = defaultValue; data = new long[upperPowTwo(n) << 1]; Arrays.fill(data, defaultValue); } protected abstract long merge(long x, long y); public long query(int i) { return query(i, i, 0, 0, n - 1); } long query(int i, int j, int idx, int l, int r) { int L = left(idx), R = L + 1; if (r < i || l > j) return defaultValue; if (l >= i && r <= j) return data[idx]; int mid = center(l, r); return merge(query(i, j, L, l, mid), query(i, j, R, mid + 1, r)); } } abstract class LazyPropagation extends SegmentTree { long[] add; public LazyPropagation(int n, long defaultValue) { super(n, defaultValue); add = new long[upperPowTwo(n) << 1]; } public LazyPropagation(int n) { this(n, 0); } protected abstract long nodeUpdate(long prev, long up, int size); void push(int idx, int l, int r, int L, int R) { if (add[idx] > 0) { data[idx] = nodeUpdate(data[idx], add[idx], r - l + 1); if (l != r) { add[L] = merge(add[L], add[idx]); add[R] = merge(add[R], add[idx]); } add[idx] = 0; } } public void update(int i, long up) { update(i, i, up, 0, 0, n - 1); } public void update(int i, int j, long up) { update(i, j, up, 0, 0, n - 1); } void update(int i, int j, long up, int idx, int l, int r) { int L = left(idx), R = L + 1; if (l >= i && r <= j) add[idx] = merge(add[idx], up); push(idx, l, r, L, R); if ((l >= i && r <= j) || r < i || l > j) return; int mid = center(l, r); update(i, j, up, L, l, mid); update(i, j, up, R, mid + 1, r); data[idx] = merge(data[L], data[R]); } long query(int i, int j, int idx, int l, int r) { int L = left(idx), R = L + 1; push(idx, l, r, L, R); if (l >= i && r <= j) return data[idx]; if (r < i || l > j) return defaultValue; int mid = center(l, r); return merge(query(i, j, L, l, mid), query(i, j, R, mid + 1, r)); } }
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> const long long mod = 1000000007; const int maxn = 5e5 + 1; long long inf = 1LL << 60; using namespace std; int main() { ios_base::sync_with_stdio(0); int n, q; cin >> n >> q; map<int, pair<int, char>> used; used[0] = {0, 'U'}; used[n + 1] = {0, 'L'}; for (int i = 0; i < q; i++) { int ans = 0; int x, y; char d; cin >> x >> y >> d; auto it = used.lower_bound(x); if (it->first == x) puts("0"); else { if (d == 'L') it--; ans = abs(it->first - x); if (it->second.second == d) ans += it->second.first; printf("%d\n", ans); used[x] = {ans, d}; } } 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 line { int p, top; line(int p = 0, int top = 0) : p(p), top(top){}; }; bool operator<(const line& a, const line& b) { return a.p < b.p; } bool operator==(const line& a, const line& b) { return a.p == b.p; } set<line> up, fuck; int main(int argc, char* argv[]) { std::ios::sync_with_stdio(false); cin >> n >> q; for (int i = 1; i <= q; i++) { int x, y, ans; string way; cin >> x >> y >> way; if (up.find(line(x, -1)) != up.end() || fuck.find(line(y, -1)) != fuck.end()) { cout << "0" << endl; continue; } up.insert(line(0, 0)); fuck.insert(line(0, 0)); if (way == "U") { auto it = up.upper_bound(line(x, -1)); int top = -1, bot = -1; if (it == up.end()) { top = y; bot = y; } else { top = (*it).top; bot = n - (*it).p + 1; } auto upit = --fuck.lower_bound(line(top, -1)); auto boit = --fuck.lower_bound(line(y, -1)); if ((*boit).p <= bot) { ans = (*upit).p; } else { ans = (*boit).p; } up.insert(line(x, ans + 1)); cout << y - ans << endl; } if (way == "L") { auto it = fuck.upper_bound(line(y, -1)); int top = -1, bot = -1; if (it == fuck.end()) { top = x; bot = x; } else { top = (*it).top; bot = n - (*it).p + 1; } auto upit = --up.lower_bound(line(top, -1)); auto boit = --up.lower_bound(line(x, -1)); if ((*boit).p <= bot) { ans = (*upit).p; } else { ans = (*boit).p; } fuck.insert(line(y, ans + 1)); cout << x - ans << 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 segtree { int i, j; int minvalue; int pvalue; segtree *l, *r; segtree(vector<int> &v, int I, int J) : pvalue(-1), l(NULL), r(NULL) { i = v[I]; j = v[J]; minvalue = 0; if (I == J) { } else { int K = I + J >> 1; l = new segtree(v, I, K); r = new segtree(v, K + 1, J); } } void visit() { if (minvalue < pvalue) { minvalue = pvalue; if (l) { l->pvalue = max(l->pvalue, pvalue); r->pvalue = max(r->pvalue, pvalue); } } pvalue = -1; } void setmx(int I, int J, int v) { if (I <= i && j <= J) { pvalue = max(pvalue, v); visit(); } else { visit(); if (!(j < I || J < i)) { l->setmx(I, J, v); r->setmx(I, J, v); minvalue = min(l->minvalue, r->minvalue); } } } int get(int I) { visit(); if (i == j) { return minvalue; } else if (l->i <= I && I <= l->j) { return l->get(I); } else { return r->get(I); } } void prstate() { printf("[%d %d]:", i, j); printf("%d (%d)", minvalue, pvalue); printf("\n"); if (l) l->prstate(); if (r) r->prstate(); } }; struct qr { int x, y; bool left; }; qr qrs[201111]; vector<int> xs; vector<int> ys; char s[111]; int main() { int n, q; scanf("%d%d", &n, &q); for (int i = (0); i < (q); i++) { int x, y; scanf("%d%d%s", &x, &y, s); xs.push_back(x); ys.push_back(y); qrs[i].x = x; qrs[i].y = y; qrs[i].left = *s == 'L'; } sort(xs.begin(), xs.end()); sort(ys.begin(), ys.end()); xs.resize(distance(xs.begin(), unique(xs.begin(), xs.end()))); ys.resize(distance(ys.begin(), unique(ys.begin(), ys.end()))); segtree *tops = new segtree(xs, 0, xs.size() - 1); segtree *lefts = new segtree(ys, 0, ys.size() - 1); for (int i = (0); i < (q); i++) { int x = qrs[i].x; int y = qrs[i].y; int ans; if (qrs[i].left) { int X = lefts->get(y); lefts->setmx(y, y, x); if (X < x) tops->setmx(X + 1, x, y); ans = x - X; } else { int Y = tops->get(x); tops->setmx(x, x, y); if (Y < y) lefts->setmx(Y + 1, y, x); ans = y - Y; } printf("%d\n", ans); } }
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.*; import static java.lang.Math.max; public class _310C_Shrinked { public static void main(String[] args) throws IOException { InputReader scanner = new InputReader(System.in); PrintWriter writer = new PrintWriter(System.out); int n = scanner.nextInt(); int q = scanner.nextInt(); int[] x = new int[q]; char[] d = new char[q]; int[] t = new int[q + 2]; // holds all x coordinates used in action list for (int i = 0; i < q; i++) { x[i] = scanner.nextInt(); // y coordinate is read, but can omitted scanner.nextInt(); d[i] = scanner.next().charAt(0); t[i] = x[i]; } t[q] = 0; t[q + 1] = n + 1; Arrays.sort(t); Map<Integer, Integer> u = new HashMap<>(); // holds mappings from real coordinates to shrinked for (int i = 0; i < q + 2; i++) { u.put(t[i], i); } /* Using two separate segment trees for vertical and horizontal coordinates. We need to create segment trees for only the number of actions (+2), as there can be q different coordinates at max. */ LinkSegmentTree tv = new LinkSegmentTree(0, q + 2), th = new LinkSegmentTree(0, q + 2); // shrinked start coordinates int xS, yS; // closest coordinate (also shrinked) int cS; for (int i = 0; i < q; i++) { xS = u.get(x[i]); // y can be calculated from x and coordinates number (q) yS = q - xS + 1; if (d[i] == 'L') { cS = th.get(yS); writer.println(t[xS] - t[cS]); // eat row if (cS != xS) { tv.update(cS + 1, xS + 1, yS); } // eat start position th.update(yS, yS + 1, xS); } else { cS = tv.get(xS); writer.println(t[q + 1 - cS] - t[xS]); // eat column if (cS != yS) { th.update(cS + 1, yS + 1, xS); } // eat start position tv.update(xS, xS + 1, yS); } } writer.close(); } } class LinkSegmentTree { private LinkSegmentTree L, R; private int leftBound, rightBound, value, pendingValue; public LinkSegmentTree(int l, int r) { this.leftBound = l; this.rightBound = r; if (r - l > 1) { // if node is not a unit length segment - divide it and create children nodes recursively L = new LinkSegmentTree(l, (r + l) / 2); R = new LinkSegmentTree((r + l) / 2, r); } } private void updateNode() { if (value == pendingValue) { return; } value = max(value, pendingValue); // check if current node has children (is a segment) if (L != null) { L.pendingValue = max(L.pendingValue, pendingValue); R.pendingValue = max(R.pendingValue, pendingValue); } } public void update(int l, int r, int c) { updateNode(); if (rightBound == r && leftBound == l) { // found match for update segment pendingValue = c; return; } int m = (leftBound + rightBound) >> 1; if (r <= m) { // update segment is fully contained in left child L.update(l, r, c); } else if (m <= l) { // update segment is fully contained in right child R.update(l, r, c); } else if (r > m && m > l) { // update segment is distributed between left and right children L.update(l, m, c); R.update(m, r, c); } } public int get(int p) { updateNode(); // check is it is a unit-length segment if (L == null) { return value; } int middle = (leftBound + rightBound) >> 1; if (p < middle) { return L.get(p); } else { return R.get(p); } } } 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()); } }
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<int, int> > s; map<int, int> m; int main() { int i, n, q, x, y; char buf[5]; scanf("%d %d", &n, &q); s.insert(make_pair(0, 1)); s.insert(make_pair(n + 1, 0)); for (i = 0; i < q; i++) { scanf("%d %d", &x, &y); scanf("%s", buf); if (m.count(x)) { puts("0"); continue; } if (buf[0] == 'U') { pair<int, int> p = *s.lower_bound(pair<int, int>(x, -1)); if (p.second == 1) { m[x] = p.first - x + m[p.first]; } else { m[x] = p.first - x; } s.insert(pair<int, int>(x, 1)); } else { set<pair<int, int> >::iterator it = --s.lower_bound(pair<int, int>(x, -1)); if (it->second == 0) { m[x] = x - it->first + m[it->first]; } else { m[x] = x - it->first; } s.insert(pair<int, int>(x, 0)); } 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 n, q; const int N = (1 << 19) + 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 = q - 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 = q - 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; int a = query(qs, qe, lf, ns, mid), b = query(qs, qe, rt, mid + 1, ne); return max(a, b); } }; segmentTree a, b; int main() { scanf("%d%d", &n, &q); n = 2e5 + 2; vector<int> compo(q), compo2(q); int idx = 0, idx2 = 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; compo2[idx2++] = queries[i].y; } sort(compo.begin(), compo.end()); sort(compo2.begin(), compo2.end()); auto it = unique(compo.begin(), compo.end()); auto it2 = unique(compo2.begin(), compo2.end()); for (int i = 0; i < q; i++) { int X = lower_bound(compo.begin(), it, queries[i].x) - compo.begin(); int Y = lower_bound(compo2.begin(), it2, queries[i].y) - compo2.begin(); if (queries[i].typ == 'U') { int ans = a.query(X, X); int lo = lower_bound(compo2.begin(), it2, ans) - compo2.begin(); int hi = lower_bound(compo2.begin(), it2, queries[i].y) - compo2.begin(); a.update(X, X, queries[i].y); b.update(lo, hi, queries[i].x); cout << queries[i].y - ans << endl; } else { int ans = b.query(Y, Y); int lo = lower_bound(compo.begin(), it, ans) - compo.begin(); int hi = lower_bound(compo.begin(), it, queries[i].x) - compo.begin(); a.update(lo, hi, queries[i].y); b.update(Y, Y, queries[i].x); cout << queries[i].x - ans << 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 seg { seg(int a, int x, int y) : A(a), X(x), Y(y) {} int A; int X; int Y; }; bool operator<(const seg &M, const seg &N) { return M.A < N.A; } set<seg> segs; int main() { int N, Q; scanf("%d %d\n", &N, &Q); segs.insert(seg(0, 0, 0)); for (int i = 0; i < Q; ++i) { int x, y; char c; scanf("%d %d %c\n", &x, &y, &c); set<seg>::iterator it = segs.upper_bound(seg(x, 0, 0)); --it; if (x == it->A) { printf("0\n"); continue; } int fa = it->A; int fy = it->Y; int fx = it->X; if (c == 'U') { printf("%d\n", y - it->Y); segs.insert(seg(x, x, fy)); } else { printf("%d\n", x - it->X); segs.erase(it); segs.insert(seg(fa, fx, y)); segs.insert(seg(x, fx, fy)); } } 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.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; public class P556E { static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static void solve() throws Exception { Set<Integer> set = new HashSet<>(); TreeMap<Integer, List<Integer>> map = new TreeMap<>(); int n = nextInt(), q = nextInt(); List<Integer> list = new ArrayList<>(); list.add(0); list.add(0); map.put(n + 1, list); for (int i = 0; i < q; i++) { int x = nextInt(), y = nextInt(); String s = next(); if (set.contains(x)) { out.println(0); continue; } set.add(x); List<Integer> old = map.get(map.higherKey(x)); if (s.equals("U")) { out.println(y - old.get(0)); List<Integer> now = new ArrayList<Integer>(); now.add(old.get(0)); now.add(old.get(1)); map.put(x, now); old.set(1, x); } else { out.println(x - old.get(1)); List<Integer> now = new ArrayList<Integer>(); now.add(y); now.add(old.get(1)); map.put(x, now); } } } public static void main(String args[]) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } static int nextInt() throws IOException { return Integer.parseInt(next()); } static int[] nextIntArray(int len, int start) throws IOException { int[] a = new int[len]; for (int i = start; i < len; i++) a[i] = nextInt(); return a; } static long nextLong() throws IOException { return Long.parseLong(next()); } static long[] nextLongArray(int len, int start) throws IOException { long[] a = new long[len]; for (int i = start; i < len; i++) a[i] = nextLong(); return a; } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } }
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, q; set<pair<pair<int, int>, int>> yatay; set<pair<pair<int, int>, int>> dikey; void make(int x, int y, set<pair<pair<int, int>, int>> &yat, set<pair<pair<int, int>, int>> &dik) { set<pair<pair<int, int>, int>>::iterator it = --yat.upper_bound(make_pair(make_pair(x, INT_MAX / 2), INT_MAX / 2)); pair<pair<int, int>, int> s = *it; yat.erase(it); pair<pair<int, int>, int> s1 = make_pair(make_pair(s.first.first, x - 1), s.second); pair<pair<int, int>, int> s2 = make_pair(make_pair(x, x), y); pair<pair<int, int>, int> s3 = make_pair(make_pair(x + 1, s.first.second), s.second); if (s1.first.first <= s1.first.second) yat.insert(s1); if (s2.first.first <= s2.first.second) yat.insert(s2); if (s3.first.first <= s3.first.second) yat.insert(s3); int l = s.second + 1; pair<pair<int, int>, int> yeni = make_pair(make_pair(y, INT_MAX / 2), INT_MAX / 2); it = --dik.upper_bound(yeni); s = *it; dik.erase(it); s1 = make_pair(make_pair(s.first.first, y), x); s2 = make_pair(make_pair(y + 1, s.first.second), s.second); if (s1.first.first <= s1.first.second) dik.insert(s1); if (s2.first.first <= s2.first.second) dik.insert(s2); cout << y - l + 1 << " "; } int main() { cin >> n >> q; yatay.insert(make_pair(make_pair(1, n), 0)); dikey.insert(make_pair(make_pair(1, n), 0)); while (q--) { int x, y; char t; cin >> x >> y >> t; if (t == 'U') { make(x, y, yatay, dikey); } else { make(y, x, dikey, yatay); } } cout << 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
import java.io.*; import java.util.*; import static java.lang.Math.max; public class _310C_Shrinked { public static void main(String[] args) throws IOException { InputReader scanner = new InputReader(System.in); PrintWriter writer = new PrintWriter(System.out); int n = scanner.nextInt(); int q = scanner.nextInt(); int[] x = new int[q], y = new int[q]; char[] d = new char[q]; IntPair[] t = new IntPair[q + 2]; for (int i = 0; i < q; i++) { x[i] = scanner.nextInt(); y[i] = scanner.nextInt(); d[i] = scanner.next().charAt(0); t[i] = new IntPair(x[i], i); } t[q] = new IntPair(0, q); t[q + 1] = new IntPair(n + 1, q + 1); Arrays.sort(t, new Comparator<IntPair>() { @Override public int compare(IntPair o1, IntPair o2) { if (o1.fs > o2.fs) { return 1; } else if (o1.fs < o2.fs) { return -1; } else { // o1.fs == o2.fs if (o1.sc > o2.sc) { return 1; } else if (o1.sc < o2.sc) { return -1; } else { return 0; } } } }); Map<Integer, Integer> u = new HashMap<>(); int v[] = new int[q + 2]; for (int i = 0; i < q + 2; i++) { u.put(t[i].fs, i); v[i] = t[i].fs; } ShrinkedSegmentTree tv = new ShrinkedSegmentTree(0, q + 2), th = new ShrinkedSegmentTree(0, q + 2); int a, b, c; for (int i = 0; i < q; i++) { a = u.get(x[i]); b = q - a + 1; if (d[i] == 'L') { c = th.get(b); writer.println(v[a] - v[c]); if (c != a) tv.update(c + 1, a + 1, b); th.update(b, b + 1, a); } else { c = tv.get(a); writer.println(v[q + 1 - c] - v[a]); if (c != b) th.update(c + 1, b + 1, a); tv.update(a, a + 1, b); } } writer.close(); } } class ShrinkedSegmentTree { private ShrinkedSegmentTree L, R; private int ll, rr, m, pr; public ShrinkedSegmentTree(int l, int r) { this.ll = l; this.rr = r; if (r - l > 1) { L = new ShrinkedSegmentTree(l, (r + l) / 2); R = new ShrinkedSegmentTree((r + l) / 2, r); } } public void push() { m = max(m, pr); if (rr - ll > 1) { L.pr = max(L.pr, pr); R.pr = max(R.pr, pr); } } public void update(int l, int r, int c) { push(); if (rr == r && ll == l) { pr = c; return; } int m = (ll + rr) / 2; if (r <= m) L.update(l, r, c); if (m <= l) R.update(l, r, c); if (r > m && m > l) { L.update(l, m, c); R.update(m, r, c); } } public int get(int p) { push(); if (rr - ll == 1) return m; int m = (ll + rr) / 2; if (p < m) return L.get(p); else return R.get(p); } } 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()); } } class IntPair { public int fs; public int sc; public IntPair(int first, int second) { this.fs = first; this.sc = second; } public int hashCode() { int hashFirst = fs; int hashSecond = sc; return (hashFirst + hashSecond) * hashSecond + hashFirst; } public boolean equals(Object other) { if (other instanceof IntPair) { IntPair otherPair = (IntPair) other; return this.fs == otherPair.fs && this.sc == otherPair.sc; } return false; } public String toString() { return "(" + fs + ", " + sc + ")"; } }
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<int> done; map<int, pair<int, int> > pi; int n, q; int main() { scanf("%d%d", &n, &q); pi[n + 1] = pair<int, int>(0, 0); int x, y; char dir; while (q--) { scanf("%d %d %c", &x, &y, &dir); if (done.count(x)) { printf("0\n"); continue; } done.insert(x); pair<int, int> &old = pi.upper_bound(x)->second; if (dir == 'U') { printf("%d\n", y - old.first); pi[x] = pair<int, int>(old.first, old.second); old.second = x; } else { printf("%d\n", x - old.second); pi[x] = pair<int, int>(y, old.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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.UncheckedIOException; import java.util.TreeMap; import java.io.Closeable; import java.util.Map.Entry; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 29); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); CCaseOfChocolate solver = new CCaseOfChocolate(); solver.solve(1, in, out); out.close(); } } static class CCaseOfChocolate { public void solve(int testNumber, FastInput in, FastOutput out) { int n = in.ri(); int q = in.ri(); TreeMap<Integer, Cut> map = new TreeMap<>(); map.put(0, new Cut(n + 1, 0, 'U', 0)); map.put(n + 1, new Cut(0, n + 1, 'L', 0)); for (int i = 0; i < q; i++) { int y = in.ri(); int x = in.ri(); char dir = in.rc(); if (map.containsKey(y)) { out.println(0); continue; } Cut block = null; if (dir == 'L') { block = map.floorEntry(y).getValue(); } else { block = map.ceilingEntry(y).getValue(); } int to; if (block.dir == dir) { to = block.endPos; } else { if (dir == 'L') { to = block.y; } else { to = block.x; } } if (dir == 'L') { out.println(y - to); } else { out.println(x - to); } map.put(y, new Cut(x, y, dir, to)); } } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private static final int THRESHOLD = 1 << 13; private final Writer os; private StringBuilder cache = new StringBuilder(THRESHOLD * 2); public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } private void afterWrite() { if (cache.length() < THRESHOLD) { return; } flush(); } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); afterWrite(); return this; } public FastOutput append(int c) { cache.append(c); afterWrite(); return this; } public FastOutput append(String c) { cache.append(c); afterWrite(); return this; } public FastOutput println(int c) { return append(c).println(); } public FastOutput println() { return append(System.lineSeparator()); } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int ri() { return readInt(); } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public char rc() { return readChar(); } public char readChar() { skipBlank(); char c = (char) next; next = read(); return c; } } static class Cut { int x; int y; char dir; int endPos; public Cut(int x, int y, char dir, int endPos) { this.x = x; this.y = y; this.dir = dir; this.endPos = endPos; } } }
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; class Tree { public: int n; int *val; Tree(int n) { this->n = n; val = new int[4 * n + 5]; fill(val, val + 4 * n + 5, -1); } void update(int l, int r, int v) { internalUpdate(0, 0, n - 1, l, r, v); } int get(int x) { return internalGet(0, 0, n - 1, x); } void internalUpdate(int root, int rl, int rr, int l, int r, int v) { if (l > r) { return; } if (rl == l && rr == r) { val[root] = max(val[root], v); } else { int mid = rl + (rr - rl) / 2; internalUpdate(2 * root + 1, rl, mid, l, min(r, mid), v); internalUpdate(2 * root + 2, mid + 1, rr, max(l, mid + 1), r, v); } } int internalGet(int root, int rl, int rr, int p) { if (rl == rr) { return val[root]; } int res = val[root]; int mid = rl + (rr - rl) / 2; if (p <= mid) { return max(res, internalGet(2 * root + 1, rl, mid, p)); } else { return max(res, internalGet(2 * root + 2, mid + 1, rr, p)); } } ~Tree() { delete[] val; } }; int qx[200010], qy[200010]; int normalX[200010], normalY[200010]; int lenX, lenY; string qup[200010]; int N, Q; int normalize(int *data, int n, int *out) { int temp[n]; memcpy(temp, data, sizeof(int) * n); sort(temp, temp + n); out[0] = temp[0]; int ans = 0; for (int i = 1; i < n; ++i) { if (temp[i] == out[ans]) { continue; } out[++ans] = temp[i]; } return ans + 1; } int main() { cin >> N >> Q; for (int i = 0; i < Q; ++i) { scanf("%d%d", qx + i, qy + i); cin >> qup[i]; } lenX = normalize(qx, Q, normalX); lenY = normalize(qy, Q, normalY); Tree *left = new Tree(lenX), *up = new Tree(lenY); for (int i = 0; i < Q; ++i) { int x = lower_bound(normalX, normalX + lenX, qx[i]) - normalX; int y = lower_bound(normalY, normalY + lenY, qy[i]) - normalY; if (qup[i] == "U") { int stopAt = up->get(x); if (stopAt == -1) { printf("%d\n", normalY[y]); } else { printf("%d\n", normalY[y] - normalY[stopAt]); } up->update(x, x, y); left->update(stopAt + 1, y, x); } else { int stopAt = left->get(y); if (stopAt == -1) { printf("%d\n", normalX[x]); } else { printf("%d\n", normalX[x] - normalX[stopAt]); } left->update(y, y, x); up->update(stopAt + 1, x, 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; int n, q, x, y; char type; map<int, pair<char, int> > Data; int solve() { auto fi = Data.lower_bound(x); if (fi->first == x) return 0; if (type == 'L') fi--; int ans = abs(x - fi->first); if (fi->second.first == type) ans += fi->second.second; Data[x] = make_pair(type, ans); return ans; } int main() { scanf("%d %d", &n, &q); Data[0] = make_pair('U', 0); Data[n + 1] = make_pair('L', 0); while (q--) { cin >> x >> y >> type; printf("%d\n", solve()); } }
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 n, q, x[200100], y[200100]; long long lzy1[800200], lzy2[800200], seg1[800200], seg2[800200]; map<long long, long long> m1, m2; map<pair<long long, long long>, bool> vis; char d[200200]; void update1(int st, int en, int l, int r, int id, long long val) { if (st > r || en < l) return; if (l >= st && r <= en) { seg1[id] = max(val, seg1[id]); return; } int mid = (l + r) / 2; seg1[2 * id] = max(seg1[2 * id], seg1[id]); seg1[2 * id + 1] = max(seg1[2 * id + 1], seg1[id]); update1(st, en, l, mid, 2 * id, val); update1(st, en, mid + 1, r, 2 * id + 1, val); } long long query1(int idx, int l, int r, int id) { if (l > idx || r < idx) return 0; if (l == r && l == idx) return seg1[id]; int mid = (l + r) / 2; seg1[2 * id] = max(seg1[2 * id], seg1[id]); seg1[2 * id + 1] = max(seg1[2 * id + 1], seg1[id]); if (idx <= mid) { return query1(idx, l, mid, 2 * id); } return query1(idx, mid + 1, r, 2 * id + 1); } void update2(int st, int en, int l, int r, int id, long long val) { if (st > r || en < l) return; if (l >= st && r <= en) { seg2[id] = max(val, seg2[id]); return; } int mid = (l + r) / 2; seg2[2 * id] = max(seg2[2 * id], seg2[id]); seg2[2 * id + 1] = max(seg2[2 * id + 1], seg2[id]); update2(st, en, l, mid, 2 * id, val); update2(st, en, mid + 1, r, 2 * id + 1, val); } long long query2(int idx, int l, int r, int id) { if (l > idx || r < idx) return 0; if (l == r) return seg2[id]; int mid = (l + r) / 2; seg2[2 * id] = max(seg2[2 * id], seg2[id]); seg2[2 * id + 1] = max(seg2[2 * id + 1], seg2[id]); if (idx <= mid) { return query2(idx, l, mid, 2 * id); } return query2(idx, mid + 1, r, 2 * id + 1); } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> q; for (int i = 0; i < q; i++) { cin >> y[i] >> x[i] >> d[i]; m1[x[i]] = 1; m2[y[i]] = 1; } map<long long, long long>::iterator it; long long i = 1; for (it = m1.begin(); it != m1.end(); it++, i++) { it->second = i; } i = 1; for (it = m2.begin(); it != m2.end(); it++, i++) it->second = i; long long n1 = m1.size(); long long n2 = m2.size(); for (int i = 0; i < q; i++) { if (vis[{y[i], x[i]}]) { cout << "0\n"; continue; } vis[{y[i], x[i]}] = 1; if (d[i] == 'U') { int qe = query2(m2[y[i]], 1, n2, 1); cout << x[i] - qe << "\n"; update1(m1[qe], m1[x[i]], 1, n1, 1, y[i]); } else { int qe = query1(m1[x[i]], 1, n1, 1); cout << y[i] - qe << "\n"; update2(m2[qe], m2[y[i]], 1, n2, 1, x[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.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.util.TreeMap; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.TreeSet; import java.io.Writer; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Andrey Rechitsky ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, FastScanner in, FastPrinter out) { int n = in.nextInt(); int q = in.nextInt(); int[] qr = new int[q]; int[] qc = new int[q]; boolean[] qd = new boolean[q]; TreeSet<Integer> rmarks = new TreeSet<Integer>(), cmarks = new TreeSet<Integer>(); TreeMap<Integer,Integer> rows = new TreeMap<Integer,Integer>(), cols = new TreeMap<Integer,Integer>(); for (int i = 0; i < q; i++) { qc[i] = in.nextInt()-1; qr[i] = in.nextInt()-1; qd[i] = in.next().equals("U"); } for (int i = q-1; i >= 0; i--) { rmarks.add(qr[i]); cmarks.add(qc[i]); if (qd[i]){ cols.put(qc[i],i); } else{ rows.put(qr[i],i); } } int size = 1; while (size<q) size<<=1; size<<=1; int [] treer = new int[size], treec = new int[size]; Arrays.fill(treec, -1); Arrays.fill(treer, -1); int []rm = new int[q]; int []cm = new int[q]; Arrays.fill(rm, 0); Arrays.fill(cm, 0); TreeMap<Integer,Integer> rm2 = new TreeMap<Integer,Integer>(), cm2 = new TreeMap<Integer,Integer>(); int ind = 0; for (int k: rmarks){ rm[ind] = k; rm2.put(k, ind); ind++; } ind = 0; for (int k: cmarks){ cm[ind] = k; cm2.put(k, ind); ind++; } TreeMap<Integer,Integer> eatenR = new TreeMap<Integer, Integer>(), eatenC = new TreeMap<Integer, Integer>(); for (int i = 0; i <q; i++) { if (qd[i]){ if (cols.get(qc[i])<i) { out.printLine(0); continue; } Integer nextC = eatenC.higherKey(qc[i]); int minR = nextC==null?0:rm2.get(n-nextC-1)+1; int g = get(treer, minR, rm2.get(qr[i]) + 1); int tmp = nextC==null?qr[i]+1:eatenC.get(nextC)+nextC-qc[i]; tmp = Math.min(qr[i]-g, tmp); out.printLine(tmp); eatenC.put(qc[i], tmp); set(treec, cm2.get(qc[i]), qc[i]); } else{ if (rows.get(qr[i])<i) { out.printLine(0); continue; } Integer nextR = eatenR.higherKey(qr[i]); int minC = nextR==null?0:cm2.get(n-nextR-1)+1; int g = get(treec, minC, cm2.get(qc[i]) + 1); int tmp = nextR==null?qc[i]+1:eatenR.get(nextR)+nextR-qr[i]; tmp = Math.min(qc[i]-g, tmp); out.printLine(tmp); eatenR.put(qr[i], tmp); set(treer, rm2.get(qr[i]), qr[i]); } } } void set (int [] t, int pos, int val){ int size = t.length; pos += size/2; t[pos] = val; while (pos>1){ pos>>=1; t[pos] = Math.max(t[2*pos], t[2*pos+1]); } } int get (int []t, int cur, int l, int r, int L, int R){ if (l>=R) return -1; if (r<=L) return -1; if (l<=L && r>=R) return t[cur]; int mid = (L+R)>>1; return Math.max(get(t,cur*2, l, r, L, mid), get(t,cur*2+1, l, r, mid, R)); } int get (int []t, int l, int r){ return get(t, 1, l, r, 0, t.length/2); } } class FastScanner { private BufferedReader reader; private StringTokenizer st; public FastScanner(InputStream stream) { this.reader = new BufferedReader(new InputStreamReader(stream)); this.st = new StringTokenizer(""); } public int nextInt() { return Integer.parseInt(next()); } public String next(){ while (!st.hasMoreTokens()){ st = new StringTokenizer(readLine()); } return st.nextToken(); } private String readLine() { try { String line = reader.readLine(); if (line==null) throw new InputMismatchException(); return line; } catch (IOException e) { throw new InputMismatchException(); } } } class FastPrinter { private final PrintWriter writer; public FastPrinter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void close() { writer.close(); } public void printLine(long i) { writer.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
//package cf556; import java.util.*; public class E { private enum Direction { LEFT, UP } private static class Move { private final int x; private final int y; private final Direction direction; private Move(int x, int y, Direction direction) { this.x = x; this.y = y; this.direction = direction; } public int getX() { return x; } public int getY() { return y; } public Direction getDirection() { return direction; } @Override public String toString() { return x + " " + y + (direction == Direction.UP ? " up" : " left"); } } private static class Triangle { private final int xmin; private final int xmax; private final int leftBoost; private final int topBoost; private Triangle(int x) { this.xmin = x; this.xmax = x; this.leftBoost = 0; this.topBoost = 0; } private Triangle(int xmin, int xmax, int leftBoost, int topBoost) { this.xmin = xmin; this.xmax = xmax; this.leftBoost = leftBoost; this.topBoost = topBoost; } public int getXmin() { return xmin; } public int getXmax() { return xmax; } public int getLeftBoost() { return leftBoost; } public int getTopBoost() { return topBoost; } @Override public String toString() { return "[" + xmin + ", " + xmax + ") l" + leftBoost + " t" + topBoost; } } private static List<Integer> getEatenCount(int n, List<Move> moves) { TreeSet<Triangle> currentTriangles = new TreeSet<>((x, y) -> Integer.compare(x.getXmax(), y.getXmax())); currentTriangles.add(new Triangle(0, n, 0, 0)); List<Integer> result = new ArrayList<>(); for (Move move : moves) { Triangle target = new Triangle(move.getX()); Triangle triangle = currentTriangles.higher(target); if (triangle == null || triangle.getXmin() > move.getX()) { result.add(0); continue; } currentTriangles.remove(triangle); if (move.getDirection() == Direction.UP) { int eaten = triangle.getXmax() - move.getX() + triangle.getTopBoost(); addTriangle(currentTriangles, new Triangle(triangle.getXmin(), move.getX(), triangle.getLeftBoost(), eaten)); // 1 addTriangle(currentTriangles, new Triangle(move.getX() + 1, triangle.getXmax(), 0, triangle.getTopBoost())); // 2 result.add(eaten); } else { int eaten = move.getX() - triangle.getXmin() + 1 + triangle.getLeftBoost(); addTriangle(currentTriangles, new Triangle(triangle.getXmin(), move.getX(), triangle.getLeftBoost(), 0)); // 3 addTriangle(currentTriangles, new Triangle(move.getX() + 1, triangle.getXmax(), eaten, triangle.getTopBoost())); // 4 result.add(eaten); } } return result; } private static void addTriangle(Collection<Triangle> triangles, Triangle triangle) { if (triangle.getXmin() == triangle.getXmax()) { return; } triangles.add(triangle); } private static List<Integer> getEatenCountBrute(int n, List<Move> moves) { List<Integer> result = new ArrayList<>(); boolean[][] used = new boolean[n][n]; for (Move move : moves) { int x = move.getX(); int y = move.getY(); int count = 0; if (move.getDirection() == Direction.UP) { while(x >= 0 && y >= 0) { if (used[x][y]) { break; } used[x][y] = true; --y; ++count; } } else { while(x >= 0 && y >= 0) { if (used[x][y]) { break; } used[x][y] = true; --x; ++count; } } result.add(count); } return result; } private static List<Move> generateMoves(Random r, int n, int q) { List<Move> moves = new ArrayList<>(); for (int i = 0; i < q; ++i) { int x = r.nextInt(n); int y = n - x - 1; Direction direction = r.nextBoolean() ? Direction.LEFT : Direction.UP; moves.add(new Move(x, y, direction)); } return moves; } private static void test0(int n, int q, int testCount) { Random r = new Random(137); for (int i = 0; i < testCount; ++i) { List<Move> moves = generateMoves(r, n, q); List<Integer> expected = getEatenCountBrute(n, moves); List<Integer> actual = getEatenCount(n, moves); if (!expected.equals(actual)) { System.out.println("N " + n); System.out.println("Moves " + moves); System.out.println("Expected " + expected); System.out.println("Got" + actual); System.err.println("TEST FAILURE"); getEatenCount(n, moves); System.exit(1); } } } private static void test() { test0(5, 3, 1000); test0(5, 10, 1000); test0(10, 10, 100); test0(10, 20, 100); test0(100, 200, 100); System.out.println("TEST SUCCESS"); } private static void solve() { Scanner s = new Scanner(System.in); int n = s.nextInt(); int q = s.nextInt(); List<Move> moves = new ArrayList<>(); for (int i = 0; i < q; ++i) { int x = s.nextInt(); int y = s.nextInt(); Direction direction = s.next().equals("U") ? Direction.UP : Direction.LEFT; moves.add(new Move(x - 1, y - 1, direction)); } List<Integer> result = getEatenCount(n, moves); result.stream().forEach(System.out::println); } public static void main(String[] args) { solve(); } }
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 Q = 200020; class segment_tree { struct node { int l, r, maxv; } t[Q << 2]; void make_tree(int l, int r, int k) { if (l != r) { int mid = (l + r) >> 1; make_tree(l, mid, k << 1); make_tree(mid + 1, r, (k << 1) | 1); } t[k].l = l; t[k].r = r; t[k].maxv = 0; } int get_max(int pos, int k) { if (t[k].l == t[k].r) return t[k].maxv; if (t[k << 1].r >= pos) return max(t[k].maxv, get_max(pos, k << 1)); else return max(t[k].maxv, get_max(pos, (k << 1) | 1)); } void change(int l, int r, int c, int k) { if (t[k].l >= l && t[k].r <= r) { t[k].maxv = max(t[k].maxv, c); return; } if (t[k << 1].r >= l) change(l, r, c, k << 1); if (t[(k << 1) | 1].l <= r) change(l, r, c, (k << 1) | 1); } public: void init(int size) { make_tree(1, size, 1); } void change(int l, int r, int c) { change(l, r, c, 1); } int get_max(int pos) { return get_max(pos, 1); } } Tx, Ty; int n, q, tx = 0, ty = 0, vx[Q] = {}, vy[Q] = {}; int x[Q] = {}, y[Q] = {}; char type[Q] = {}; void init() { scanf("%d%d", &n, &q); for (int i = 1; i <= q; ++i) { scanf("%d%d\n%c", x + i, y + i, type + i); vx[++tx] = x[i]; vy[++ty] = y[i]; } sort(vx + 1, vx + tx + 1); sort(vy + 1, vy + ty + 1); Tx.init(q); Ty.init(q); for (int i = 1; i <= q; ++i) { x[i] = lower_bound(vx + 1, vx + tx + 1, x[i]) - vx; y[i] = lower_bound(vy + 1, vy + ty + 1, y[i]) - vy; } } void work() { for (int i = 1; i <= q; ++i) if (type[i] == 'U') { int tmp = Tx.get_max(x[i]); printf("%d\n", vy[y[i]] - vy[tmp]); Ty.change(tmp, y[i], x[i]); Tx.change(x[i], x[i], y[i]); } else { int tmp = Ty.get_max(y[i]); printf("%d\n", vx[x[i]] - vx[tmp]); Tx.change(tmp, x[i], y[i]); Ty.change(y[i], y[i], x[i]); } } 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[400010], cnt; int x[200010], y[200010], w[200010]; map<int, int> mp; int pre[400010]; struct Tree { int L, R, val, add; }; Tree seg[2][1600010]; void build(int pos, int L, int R, int id) { seg[id][pos].L = L; seg[id][pos].R = R; if (L != R) { int mid = (L + R) / 2; build(pos * 2, L, mid, id); build(pos * 2 + 1, mid + 1, R, id); } } void Down(int id, int pos) { seg[pos][id * 2].add = max(seg[pos][id * 2].add, seg[pos][id].add); seg[pos][id * 2 + 1].add = max(seg[pos][id * 2 + 1].add, seg[pos][id].add); seg[pos][id * 2].val = max(seg[pos][id * 2].val, seg[pos][id * 2].add); seg[pos][id * 2 + 1].val = max(seg[pos][id * 2 + 1].val, seg[pos][id * 2 + 1].add); seg[pos][id].add = -1; } int read(int pos, int id, int x) { if (seg[id][pos].L == seg[id][pos].R) { return seg[id][pos].val; } Down(pos, id); int mid = (seg[id][pos].L + seg[id][pos].R) / 2; if (x <= mid) { return read(pos * 2, id, x); } else { return read(pos * 2 + 1, id, x); } } void update(int pos, int L, int R, int val, int id) { int l = seg[id][pos].L, r = seg[id][pos].R; if (r < L || l > R) return; if (l >= L && r <= R) { seg[id][pos].add = max(seg[id][pos].add, val); seg[id][pos].val = max(seg[id][pos].val, seg[id][pos].add); return; } update(pos * 2, L, R, val, id); update(pos * 2 + 1, L, R, val, id); seg[id][pos].val = max(seg[id][pos * 2].val, seg[id][pos * 2 + 1].val); } int read(int id, int x) { return read(1, id, x); } void Up(int L, int R, int val, int id) { int idL = lower_bound(a, a + cnt, L) - a + 1; int idR = upper_bound(a, a + cnt, R) - a; if (idL <= idR) { update(1, idL, idR, val, id); } } int main() { int n, m; scanf("%d%d", &n, &m); cnt = 0; for (int i = 0; i < m; i++) { int tx, ty; char ope[10]; scanf("%d%d%s", &tx, &ty, ope); x[i] = tx; y[i] = ty; w[i] = (ope[0] == 'L' ? 1 : 0); a[cnt++] = tx; a[cnt++] = ty; } sort(a, a + cnt); cnt = unique(a, a + cnt) - a; for (int i = 0; i < cnt; i++) { mp[a[i]] = i + 1; pre[i + 1] = a[i]; } for (int i = 0; i < 2; i++) { build(1, 1, cnt, i); } for (int i = 0; i < m; i++) { int prex = x[i], prey = y[i]; x[i] = mp[prex]; y[i] = mp[prey]; if (w[i] == 0) { int num = read(w[i], x[i]); int pre = prey; pre -= num; printf("%d\n", pre); Up(prex, prex, prey, w[i]); int L = prey - pre + 1, R = prey; Up(L, R, prex, 1 - w[i]); } else { int num = read(w[i], y[i]); int pre = prex; pre -= num; printf("%d\n", pre); Up(prey, prey, prex, w[i]); int L = prex - pre + 1, R = prex; Up(L, R, prey, 1 - w[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> const int MAXN = 2e5 + 10; template <int N> struct SegmentTree { static const int M = N << 2; int tag[M], max[M]; inline void modify(int x, int val) { max[x] = std::max(max[x], val), tag[x] = std::max(tag[x], val); } inline void pushUp(int x) { max[x] = std::max(max[((x) << 1)], max[((x) << 1 | 1)]); } inline void pushDown(int x) { if (tag[x]) { modify(((x) << 1), tag[x]), modify(((x) << 1 | 1), tag[x]), tag[x] = 0; } } void update(int x, int L, int R, int ql, int qr, int val) { if (ql > qr) return; if (ql <= L && R <= qr) return modify(x, val); int mid = L + R >> 1; pushDown(x); if (ql <= mid) update(((x) << 1), L, mid, ql, qr, val); if (qr > mid) update(((x) << 1 | 1), mid + 1, R, ql, qr, val); return pushUp(x); } int query(int x, int L, int R, int pos) { if (L == R) return max[x]; int mid = L + R >> 1; pushDown(x); return pos <= mid ? query(((x) << 1), L, mid, pos) : query(((x) << 1 | 1), mid + 1, R, pos); } }; SegmentTree<MAXN> T1, T2; int n, Q, cnt1, cnt2; int x[MAXN], y[MAXN], op[MAXN], tmp[MAXN], rx[MAXN], ry[MAXN]; int main() { scanf("%d%d", &n, &Q); for (int i = 1; i <= Q; ++i) { scanf("%d%d", &x[i], &y[i]); char ch = getchar(); while (ch != 'L' && ch != 'U') ch = getchar(); op[i] = ch == 'L' ? 1 : 2; } std::copy(x + 1, x + Q + 1, tmp + 1), std::sort(tmp + 1, tmp + Q + 1); int m = std::unique(tmp + 1, tmp + Q + 1) - tmp - 1; for (int i = 1, awa; i <= Q; ++i) awa = std::lower_bound(tmp + 1, tmp + m + 1, x[i]) - tmp, rx[awa] = x[i], x[i] = awa; for (int i = 1; i <= Q; ++i) ry[m - x[i] + 1] = y[i], y[i] = m - x[i] + 1; for (int i = 1; i <= Q; ++i) { if (op[i] == 1) { int max = T1.query(1, 1, m, y[i]); printf("%d\n", rx[x[i]] - rx[max]); T1.update(1, 1, m, y[i], y[i], x[i]); T2.update(1, 1, m, max + 1, x[i], y[i]); } else { int max = T2.query(1, 1, m, x[i]); printf("%d\n", ry[y[i]] - ry[max]); T2.update(1, 1, m, x[i], x[i], y[i]); T1.update(1, 1, m, max + 1, y[i], x[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; map<int, int> rw, cl; map<pair<int, int>, bool> vis; int main() { int n, q; scanf("%d%d", &n, &q); rw[0] = 0; rw[n + 1] = 0; cl[0] = 0; cl[n + 1] = 0; int x, y; char ins[3]; for (int i = 0; i < q; ++i) { scanf("%d%d%s", &x, &y, ins); pair<int, int> p = make_pair(x, y); if (ins[0] == 'U') { if (vis[p]) printf("0\n"); else { map<int, int>::iterator it = rw.upper_bound(y), itc; cl[x] = x; --it; rw[y] = (*it).second; printf("%d\n", y - (*it).second); vis[p] = 1; } } else { if (vis[p]) printf("0\n"); else { map<int, int>::iterator it = cl.upper_bound(x), itc; rw[y] = y; --it; cl[x] = (*it).second; printf("%d\n", x - (*it).second); vis[p] = 1; } } } }
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; const long long mod = 1e9 + 7; int n, q, ans; map<int, int> u; map<int, int> l; int main() { scanf("%d%d", &n, &q); while (q--) { int x, y; char dir[5]; scanf("%d%d", &x, &y); scanf("%s", dir); if (u.count(x) || l.count(y)) { printf("0\n"); continue; } ans = 0; map<int, int>::iterator it; if (dir[0] == 'L') { it = l.lower_bound(y); int tmp = 0; if (it != l.end()) tmp = (it->second); ans = x - tmp; printf("%d\n", ans); l[y] = tmp; u[x] = y; } else { it = u.lower_bound(x); int tmp = 0; if (it != u.end()) tmp = (it->second); ans = y - tmp; printf("%d\n", ans); u[x] = tmp; l[y] = 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
import java.util.Arrays; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.text.DecimalFormat; import java.util.StringTokenizer; /** * Built using CHelper plug-in * Actual solution is at the top * @author Bat-Orgil */ 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { static DecimalFormat df = new DecimalFormat(); public void solve(int testNumber, InputReader in, PrintWriter out) { df.setMaximumFractionDigits(20); df.setMinimumFractionDigits(20); int N = in.nextInt(); int Q = in.nextInt(); Integer nums[] = new Integer[2*Q+1]; int X[] = new int[Q]; int Y[] = new int[Q]; char dir[] = new char[Q]; for (int i = 0; i < Q; i++) { X[i] = nums[i*2] = in.nextInt(); Y[i] = nums[i*2+1] = in.nextInt(); dir[i] = in.next().charAt(0); } nums[2*Q] = 0; Arrays.sort(nums); HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); for (int i = 0; i < Q*2+1; i++) { if (map.containsKey(nums[i])) continue; map.put(nums[i],i); } LAZY mxrow = new LAZY(Q*2+2); LAZY mxcol = new LAZY(Q*2+2); for (int i = 0; i < Q; i++) { int qr = map.get(Y[i]); int qc = map.get(X[i]); if (dir[i] == 'U') { int r = mxrow.q(qc); out.println(nums[qr] - nums[r]); mxcol.update(r, qr, qc); mxrow.update(qc, qc, qr); } else { int c = mxcol.q(qr); out.println(nums[qc] - nums[c]); mxrow.update(c, qc, qr); mxcol.update(qr, qr, qc); } } } public class LAZY { void update(int l, int r, int val){ update(l,r,val,1,N,1); } int q(int l){ return query(l,l,1,N,1); } int N; int[] A; int[] lazy; LAZY(int size){ N=size+10; A=new int[4*N]; lazy= new int[4*N]; } void upd(int val, int n){ A[n] = Math.max(A[n],val); lazy[n]= Math.max(lazy[n], val); } void clearLazy(int a, int b, int n){ if(lazy[n] ==0) return; if(a<b){ upd(lazy[n], 2 * n); upd(lazy[n], 2 * n + 1); } lazy[n]=0; } void update(int l, int r, int val, int a, int b, int n){ clearLazy(a,b,n); if(r < a || b<l) return; if(l<=a && r>=b){ upd(val,n); return; } int m =(a+b)/2; update(l,r,val,a,m,2*n); update(l,r,val,m+1,b,2*n+1); A[n] = Math.max(A[n*2], A[n*2+1]); } int query(int l, int r, int a, int b, int n){ clearLazy(a,b,n); if( r<a || l>b) return 0; if( l<=a && r>=b) return A[n]; int m= (a+b)/2; int m1= query(l,r,a,m,2*n); int m2= query(l,r,m+1,b,2*n+1); return Math.max(m1,m2); } } } 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()); } }
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 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; 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, j += S; 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> 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) { 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
import java.util.*; public class C { public C () { int N = sc.nextInt(); int Q = sc.nextInt(); int [] ZERO = { 0, 0 }; TreeMap<Integer, int[]> X = new TreeMap<>(); X.put(-1, ZERO); X.put(N+1, ZERO); for (int q : rep(Q)) { int x = sc.nextInt(), y = sc.nextInt(); char d = sc.nextChar(); if (X.containsKey(x)) print(0); else { if (d == 'U') { int [] v = X.ceilingEntry(x).getValue(); print(y - v[1]); X.put(x, new int [] { x, v[1] }); } if (d == 'L') { int [] v = X.floorEntry(x).getValue(); print(x - v[0]); X.put(x, new int [] { v[0], y }); } } } } private static int [] rep(int N) { return rep(0, N); } private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; } //////////////////////////////////////////////////////////////////////////////////// private final static IOUtils.MyScanner sc = new IOUtils.MyScanner(); private static void print (Object o, Object ... A) { IOUtils.print(o, A); } private static class IOUtils { public static class MyScanner { public String next() { newLine(); return line[index++]; } public char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error (e); } } private final java.io.BufferedReader r; private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); } private MyScanner (java.io.BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = split(readLine()); index = 0; } } private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; } } private static String build(Object o, Object ... A) { return buildDelim(" ", o, A); } private static String buildDelim(String delim, Object o, Object ... A) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : A) append(b, p, delim); return b.substring(delim.length()); } ////////////////////////////////////////////////////////////////////////////////// private static void start() { if (t == 0) t = millis(); } private static void append(StringBuilder b, Object o, String delim) { if (o.getClass().isArray()) { int len = java.lang.reflect.Array.getLength(o); for (int i = 0; i < len; ++i) append(b, java.lang.reflect.Array.get(o, i), delim); } else if (o instanceof Iterable<?>) for (Object p : (Iterable<?>) o) append(b, p, delim); else { if (o instanceof Double) o = new java.text.DecimalFormat("#.############").format(o); b.append(delim).append(o); } } private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out); private static void print(Object o, Object ... A) { pw.println(build(o, A)); } private static void err(Object o, Object ... A) { System.err.println(build(o, A)); } private static void exit() { IOUtils.pw.close(); System.out.flush(); err("------------------"); err(IOUtils.time()); System.exit(0); } private static long t; private static long millis() { return System.currentTimeMillis(); } private static String time() { return "Time: " + (millis() - t) / 1000.0; } } public static void main (String[] args) { new C(); IOUtils.exit(); } }
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 = 200009; int X[maxn << 2], Y[maxn << 2], lazyX[maxn << 2], lazyY[maxn << 2]; int x[maxn], y[maxn], m, n, p, a[maxn], b[maxn]; char dir[maxn]; void update(int T[], int lazy[], int o, int l, int r, int x, int y, int z) { if (l == x && y == r) { T[o] = max(T[o], z); lazy[o] = max(lazy[o], z); return; } T[(o << 1)] = max(T[(o << 1)], lazy[o]); lazy[(o << 1)] = max(lazy[(o << 1)], lazy[o]); T[(o << 1 | 1)] = max(T[(o << 1 | 1)], lazy[o]); lazy[(o << 1 | 1)] = max(lazy[(o << 1 | 1)], lazy[o]); if (x <= ((l + r) >> 1)) update(T, lazy, (o << 1), l, ((l + r) >> 1), x, min(((l + r) >> 1), y), z); if (((l + r) >> 1) + 1 <= y) update(T, lazy, (o << 1 | 1), ((l + r) >> 1) + 1, r, max(((l + r) >> 1) + 1, x), y, z); T[o] = max(T[(o << 1)], T[(o << 1 | 1)]); } int query(int T[], int lazy[], int o, int l, int r, int x) { if (l == r) return T[o]; T[(o << 1)] = max(T[(o << 1)], lazy[o]); lazy[(o << 1)] = max(lazy[(o << 1)], lazy[o]); T[(o << 1 | 1)] = max(T[(o << 1 | 1)], lazy[o]); lazy[(o << 1 | 1)] = max(lazy[(o << 1 | 1)], lazy[o]); if (x <= ((l + r) >> 1)) return query(T, lazy, (o << 1), l, ((l + r) >> 1), x); return query(T, lazy, (o << 1 | 1), ((l + r) >> 1) + 1, r, x); } int main() { scanf("%d%d", &m, &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &x[i], &y[i]); getchar(); dir[i] = getchar(); a[i] = x[i]; b[i] = y[i]; } sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) { x[i] = lower_bound(a + 1, a + n + 1, x[i]) - a; y[i] = lower_bound(b + 1, b + n + 1, y[i]) - b; } for (int i = 1; i <= n; i++) if (dir[i] == 'L') { p = query(X, lazyX, 1, 1, n, y[i]); printf("%d\n", a[x[i]] - a[p]); update(Y, lazyY, 1, 1, n, p + 1, x[i], y[i]); update(X, lazyX, 1, 1, n, y[i], y[i], x[i]); } else { p = query(Y, lazyY, 1, 1, n, x[i]); printf("%d\n", b[y[i]] - b[p]); update(X, lazyX, 1, 1, n, p + 1, y[i], x[i]); update(Y, lazyY, 1, 1, n, x[i], x[i], 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; set<int> U; map<int, int> mp; int main() { int n, q; scanf("%d%d", &n, &q); int i; map<int, int>::iterator it; for (i = 1; i <= q; i++) { int x, y; char s[2]; scanf("%d%d%s", &x, &y, s); int to; if (mp.find(x) != mp.end()) { printf("0\n"); continue; } if (s[0] == 'U') { it = mp.upper_bound(x); if (it == mp.end()) { to = 1; } else { if (U.find(it->first) != U.end()) { to = it->second; } else { to = n + 1 - it->first + 1; } } U.insert(x); mp[x] = to; printf("%d\n", y - to + 1); } else { it = mp.lower_bound(x); if (it == mp.begin()) { to = 1; } else { it--; if (U.find(it->first) != U.end()) { to = it->first + 1; } else { to = it->second; } } mp[x] = to; printf("%d\n", x - to + 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
import java.util.Arrays; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Random; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; 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); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { static class Tree { int[] arr; int n; public Tree(int n) { this.n = n; arr = new int[4 * n + 10]; Arrays.fill(arr, -1); } void update(int l, int r, int v) { internalUpd(0, 0, n - 1, l, r, v); } int get(int x) { return internalGet(0, 0, n - 1, x); } private int internalGet(int root, int rl, int rr, int x) { int res = arr[root]; if (rl == rr) return res; int rm = (rl + rr) / 2; if (x <= rm) return Math.max(res, internalGet(root * 2 + 1, rl, rm, x)); else return Math.max(res, internalGet(root * 2 + 2, rm + 1, rr, x)); } private void internalUpd(int root, int rl, int rr, int l, int r, int v) { if (l > r) return; if (rl == l && rr == r) { arr[root] = Math.max(arr[root], v); return; } int rm = (rl + rr) / 2; internalUpd(root * 2 + 1, rl, rm, l, Math.min(rm, r), v); internalUpd(root * 2 + 2, rm + 1, rr, Math.max(rm + 1, l), r, v); } } public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int q = in.nextInt(); int[] qx = new int[q]; int[] qy = new int[q]; boolean[] qup = new boolean[q]; for (int iq = 0; iq < q; ++iq) { qx[iq] = in.nextInt() - 1; qy[iq] = in.nextInt() - 1; qup[iq] = in.next().equals("U"); } int[] allX = normalize(qx); int[] allY = normalize(qy); Tree left = new Tree(allY.length); Tree up = new Tree(allX.length); for (int iq = 0; iq < q; ++iq) { int x = Arrays.binarySearch(allX, qx[iq]); int y = Arrays.binarySearch(allY, qy[iq]); if (qup[iq]) { int stopAt = up.get(x); if (stopAt < 0) { out.println(allY[y] + 1); } else { out.println(allY[y] - allY[stopAt]); } up.update(x, x, y); left.update(stopAt + 1, y, x); } else { int stopAt = left.get(y); if (stopAt < 0) { out.println(allX[x] + 1); } else { out.println(allX[x] - allX[stopAt]); } left.update(y, y, x); up.update(stopAt + 1, x, y); } } } private int[] normalize(int[] qx) { int[] allX = qx.clone(); shuffle(allX); Arrays.sort(allX); int xCnt = 1; for (int i = 1; i < allX.length; ++i) if (allX[i] > allX[i - 1]) { allX[xCnt++] = allX[i]; } allX = Arrays.copyOf(allX, xCnt); return allX; } Random random = new Random(5437534511L + System.currentTimeMillis()); private void shuffle(int[] allX) { for (int i = 0; i < allX.length; ++i) { int j = i + random.nextInt(allX.length - i); int tmp = allX[i]; allX[i] = allX[j]; allX[j] = tmp; } } } 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()); } }
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 main() { long long n, q; while (cin >> n >> q) { map<int, pair<char, long long> > mp; mp[0] = make_pair('U', 0); mp[n + 1] = make_pair('L', 0); for (int i = 0; i < q; i++) { long long x, y; char c; scanf("%I64d%I64d %c", &x, &y, &c); auto tmp = mp.lower_bound(x); if (tmp->first == x) { printf("0\n"); continue; } if (c == 'L') { tmp--; } long long ans = abs(tmp->first - x); if (tmp->second.first == c) { ans += tmp->second.second; } printf("%I64d\n", ans); mp[x] = make_pair(c, 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 x4[4] = {0, 0, -1, 1}; int y4[4] = {-1, 1, 0, 0}; int x8[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int y8[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; int xhorse[8] = {1, 2, 1, 2, -1, -2, -1, -2}; int yhorse[8] = {2, 1, -2, -1, 2, 1, -2, -1}; using namespace std; int n, q; map<int, pair<int, int> > mymap; set<pair<int, int> > in; int main() { scanf("%d %d", &n, &q); mymap[n + 5] = make_pair(0, 0); while (q--) { int a, b; char s[2]; scanf("%d %d %s", &a, &b, s); if (in.find(make_pair(a, b)) != in.end()) { printf("0\n"); continue; } in.insert(make_pair(a, b)); pair<int, int> &next = mymap.lower_bound(a)->second; if (s[0] == 'U') { printf("%d\n", b - next.second); mymap[a] = next; next.first = a; } else { printf("%d\n", a - next.first); mymap[a] = make_pair(next.first, 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 n, q, k, tmp; int x[1700007], y[1700007]; int R[1700007], C[1700007]; char dir[1700007]; map<int, int> M; set<pair<int, int> > seen; void upd(int san, int a, int b, int l, int r, int node, int *A) { if (a <= l && b >= r) { A[node] = max(A[node], san); return; } if (a > r || b < l) return; upd(san, a, b, l, (l + r) / 2, node * 2, A); upd(san, a, b, (l + r) / 2 + 1, r, node * 2 + 1, A); } void tap(int x, int l, int r, int node, int *A) { k = max(A[node], k); if (l == r) return; if (x <= (l + r) / 2) tap(x, l, (l + r) / 2, node * 2, A); else tap(x, (l + r) / 2 + 1, r, node * 2 + 1, A); } int main() { cin >> n >> q; for (int i = 0; i < q; i++) { cin >> x[i] >> y[i] >> dir[i]; M[x[i]] = 1; M[y[i]] = 1; } for (auto i : M) M[i.first] = ++tmp; for (int i = 0; i < q; i++) { swap(x[i], y[i]); auto j = seen.find({x[i], y[i]}); k = 0; if (j != seen.end() && j->first == x[i] && j->second == y[i]) { cout << "0\n"; continue; } if (dir[i] == 'U') { tap(M[y[i]], 1, tmp, 1, R); upd(y[i], M[k] + 1, M[x[i]], 1, tmp, 1, C); cout << x[i] - k << "\n"; } else { tap(M[x[i]], 1, tmp, 1, C); upd(x[i], M[k] + 1, M[y[i]], 1, tmp, 1, R); cout << y[i] - k << "\n"; } seen.insert({x[i], y[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 N = 1000000; int n; int t1[2 * N], t2[2 * N]; vector<int> u, v; map<int, int> lin, linrev, col, colrev; int nlin, ncol; map<int, int> usedlin, usedcol; int bbeglin(int i) { int beg = 0, end = nlin; while (beg < end) { int mid = (beg + end) / 2; if (linrev[mid] < i) beg = mid + 1; else end = mid; } return beg; } int bbegcol(int i) { int beg = 0, end = ncol; while (beg < end) { int mid = (beg + end) / 2; if (colrev[mid] < i) beg = mid + 1; else end = mid; } return beg; } void modify1(int l, int r, int value) { l = bbegcol(l); r = bbegcol(r + 1); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) { t1[l] = max(t1[l], value); l++; } if (r & 1) { --r; t1[r] = max(t1[r], value); } } } int query1(int p) { p = col[p]; int res = 0; for (p += n; p > 0; p >>= 1) res = max(res, t1[p]); return res; } void modify2(int l, int r, int value) { l = bbeglin(l); r = bbeglin(r + 1); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) { t2[l] = max(t2[l], value); l++; } if (r & 1) { --r; t2[r] = max(t2[r], value); } } } int query2(int p) { p = lin[p]; int res = 0; for (p += n; p > 0; p >>= 1) res = max(res, t2[p]); return res; } int a[1000000]; int b[1000000]; char c[1000000]; int main() { int q; scanf(" %d %d", &n, &q); for (int i = 0; i < q; i++) { scanf(" %d %d %c", &a[i], &b[i], &c[i]); if (c[i] == 'U') u.push_back(a[i]); else v.push_back(b[i]); } sort(u.begin(), u.end()); sort(v.begin(), v.end()); int count = 0; for (int i = 0; i < u.size(); i++) { if (i == 0 || u[i] != u[i - 1]) { col[u[i]] = count; colrev[count++] = u[i]; } } col[1000000001] = count; colrev[count] = 1000000001; ncol = count; count = 0; for (int i = 0; i < v.size(); i++) { if (i == 0 || v[i] != v[i - 1]) { lin[v[i]] = count; linrev[count++] = v[i]; } } lin[1000000001] = count; linrev[count] = 1000000001; nlin = count; n = max(nlin, ncol); for (int i = 0; i < q; i++) { if (c[i] == 'U') { if (usedcol[a[i]]) { printf("0\n"); continue; } usedcol[a[i]] = 1; int k = query1(a[i]); printf("%d\n", b[i] - k); if (b[i] - k >= 1) { modify2(k + 1, b[i], a[i]); } } else { if (usedlin[b[i]]) { printf("0\n"); continue; } usedlin[b[i]] = 1; int k = query2(b[i]); printf("%d\n", a[i] - k); if (a[i] - k >= 1) modify1(k + 1, a[i], b[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; long long int mod = 1e9 + 7; int l[400200], r[400200], x1[400200], x2[400200], p[400200], du[400200], dl[400200]; bool in(char c) { if (c == 'U' || c == 'L') return true; return false; } int split(int t1, int t2) { if (t1 == 0) return t2; if (t2 == 0) return t1; if (p[t1] > p[t2]) { r[t1] = split(r[t1], t2); return t1; } l[t2] = split(t1, l[t2]); return t2; } bool find(int t, int xx) { if (t == 0) return false; if (x1[t] <= xx && x2[t] >= xx) return true; if (x1[t] > xx) return find(l[t], xx); if (x2[t] < xx) return find(r[t], xx); } vector<int> merge(int t, int xx) { vector<int> ans(3, 0); if (t == 0) return ans; if (x2[t] < xx) { vector<int> p; p = merge(r[t], xx); r[t] = p[0]; ans[0] = t; ans[1] = p[1]; ans[2] = p[2]; return ans; } if (x1[t] > xx) { vector<int> p; p = merge(l[t], xx); l[t] = p[2]; ans[0] = p[0]; ans[1] = p[1]; ans[2] = t; return ans; } ans[0] = l[t]; ans[1] = t; ans[2] = r[t]; l[t] = 0; r[t] = 0; return ans; } void solve(int n, int k) { int i, j, m, q, z, x = 2; vector<int> w; int ans = 0, ll, rr; for (i = 0; i < 400200; i++) { p[i] = rand() ^ (rand() << 16); l[i] = 0; r[i] = 0; du[i] = 0; dl[i] = 0; } q = 1; x1[1] = 1; x2[1] = n; char c; for (i = 0; i < k; i++) { scanf("%d%d", &ll, &rr); while (!in(c = getchar())) ; if (find(q, ll)) { w = merge(q, ll); if (c == 'U') { ans = x2[w[1]] - ll + 1 + du[w[1]]; if (ll > x1[w[1]]) { x1[x] = x1[w[1]]; x2[x] = ll - 1; du[x] = du[w[1]] + x2[w[1]] - ll + 1; dl[x] = dl[w[1]]; dl[w[1]] = 0; x1[w[1]] = ll + 1; if (x1[w[1]] <= x2[w[1]]) w[2] = split(w[1], w[2]); q = split(split(w[0], x), w[2]); x++; } else { x1[w[1]] = ll + 1; dl[w[1]] = 0; if (x1[w[1]] <= x2[w[1]]) w[2] = split(w[1], w[2]); q = split(w[0], w[2]); } } else { ans = ll - x1[w[1]] + 1 + dl[w[1]]; if (ll < x2[w[1]]) { x1[x] = ll + 1; x2[x] = x2[w[1]]; dl[x] = dl[w[1]] + (ll - x1[w[1]] + 1); du[x] = du[w[1]]; x2[w[1]] = ll - 1; du[w[1]] = 0; if (x1[w[1]] <= x2[w[1]]) w[0] = split(w[0], w[1]); q = split(w[0], split(x, w[2])); x++; } else { x2[w[1]] = ll - 1; du[w[1]] = 0; if (x1[w[1]] <= x2[w[1]]) w[2] = split(w[1], w[2]); q = split(w[0], w[2]); } } printf("%d\n", ans); } else { printf("0\n"); } } printf("\n"); } int main() { #pragma comment(linker, "/STACK:1073741824") int n, m, k, i; while (scanf("%d%d", &n, &k) != EOF) { solve(n, k); } 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 = 1011110000; vector<int> ke; int n, m; struct Point { int x, y; char c; Point(int _x = 0, int _y = 0, char _c = 0) : x(_x), y(_y), c(_c) {} } pt[201111]; struct SegT { int n, num[3201111]; void init(int _n) { n = _n; fill(num, num + 4 * n, 0); } void maxi(int l, int r, int x) { maxi(l, r, x, 0, n, 0); } int find(int i) { return find(i, 0, n, 0); } void maxi(int l, int r, int x, int tl, int tr, int id) { int md = (tr + tl) / 2; if (r <= tl || tr <= l) return; push(tl, tr, id); if (l <= tl && tr <= r) { num[id] = max(x, num[id]); return; } maxi(l, r, x, tl, md, id * 2 + 1); maxi(l, r, x, md, tr, id * 2 + 2); } int find(int i, int tl, int tr, int id) { int md = (tr + tl) / 2; push(tl, tr, id); if (tr - tl == 1) return num[id]; if (i < md) return find(i, tl, md, id * 2 + 1); else return find(i, md, tr, id * 2 + 2); } void push(int tl, int tr, int id) { if (tl - tr == 1) return; num[id * 2 + 1] = max(num[id * 2 + 1], num[id]); num[id * 2 + 2] = max(num[id * 2 + 2], num[id]); } } stx, sty; inline int ker(int x) { return lower_bound(ke.begin(), ke.end(), x) - ke.begin(); } void input() { scanf("%d%d", &n, &m); ke.clear(); for (int i = 0; i < m; i++) { cin >> pt[i].x >> pt[i].y >> pt[i].c; ke.push_back(pt[i].x); ke.push_back(pt[i].y); } ke.push_back(0); sort(ke.begin(), ke.end()); ke.resize(unique(ke.begin(), ke.end()) - ke.begin()); } void solve() { bool vst[401111]; memset(vst, 0, sizeof(vst)); stx.init(ke.size()); sty.init(ke.size()); for (int i = 0; i < m; i++) { int x = pt[i].x, y = pt[i].y; if (vst[ker(x)]) { printf("0\n"); continue; } vst[ker(x)] = 1; swap(x, y); if (pt[i].c == 'U') { printf("%d\n", x - sty.find(ker(y))); stx.maxi(ker(sty.find(ker(y))), ker(x) + 1, y); } else { printf("%d\n", y - stx.find(ker(x))); sty.maxi(ker(stx.find(ker(x))), ker(y) + 1, x); } } } int main() { int t = 1; while (t--) { input(); solve(); } }
CPP