Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long N = 2e5 + 36;
const long long INF = 1e9 + 7;
const long long X[8] = {1, 1, -1, -1, 2, 2, -2, -2};
const long long Y[8] = {2, -2, 2, -2, 1, -1, 1, -1};
pair<pair<int, int>, int> a[N];
set<pair<int, int> > st;
unordered_map<int, int> zam[2], obr[2];
int T[2], p = 1, tr[2][N * 4], pu[2][N * 4];
void push(int v, int x) {
pu[x][v << 1] = max(pu[x][v << 1], pu[x][v]);
tr[x][v << 1] = max(tr[x][v << 1], pu[x][v << 1]);
pu[x][v << 1 | 1] = max(pu[x][v << 1 | 1], pu[x][v]);
tr[x][v << 1 | 1] = max(tr[x][v << 1 | 1], pu[x][v]);
pu[x][v] = -1;
}
int query(int v, int x, int L, int R, int l, int r) {
if (R < l || L > r) {
return -1;
}
if (l <= L && R <= r) {
return tr[x][v];
}
push(v, x);
return max(query(v << 1, x, L, (L + R) >> 1, l, r),
query(v << 1 | 1, x, ((L + R) >> 1) + 1, R, l, r));
}
void change(int v, int x, int L, int R, int l, int r, int y) {
if (R < l || L > r) {
return;
}
if (l <= L && R <= r) {
pu[x][v] = max(pu[x][v], y);
tr[x][v] = max(tr[x][v], y);
return;
}
push(v, x);
change(v << 1, x, L, (L + R) >> 1, l, r, y);
change(v << 1 | 1, x, ((L + R) >> 1) + 1, R, l, r, y);
return;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<int> h[2];
for (int i(0); i < q; ++i) {
char x;
cin >> a[i].first.first >> a[i].first.second >> x;
h[0].push_back(a[i].first.first);
h[1].push_back(a[i].first.second);
if (x == 'U') {
a[i].second = 1;
} else {
a[i].second = 0;
}
}
sort(h[0].begin(), h[0].end());
sort(h[1].begin(), h[1].end());
for (int i(0); i < 2; ++i) {
for (int j(0); j < int(h[i].size()); ++j) {
if (!zam[i].count(h[i][j])) {
obr[i][T[i]] = h[i][j];
zam[i][h[i][j]] = T[i]++;
}
}
}
for (int i(0); i < q; ++i) {
a[i].first.first = zam[0][a[i].first.first];
a[i].first.second = zam[1][a[i].first.second];
}
while (p < q) {
p <<= 1;
}
for (int i(0); i <= p * 2; ++i) {
pu[0][i] = -1;
tr[0][i] = -1;
pu[1][i] = -1;
tr[1][i] = -1;
}
vector<int> ans;
for (int i(0); i < q; ++i) {
if (st.count(a[i].first)) {
ans.push_back(0);
continue;
}
if (a[i].second == 0) {
int H = query(1, 0, p, p * 2 - 1, p + a[i].first.second,
p + a[i].first.second);
if (H == -1) {
ans.push_back(obr[0][a[i].first.first]);
} else {
ans.push_back(obr[0][a[i].first.first] - obr[0][H]);
}
int l = max(H, 0);
change(1, 1, p, p * 2 - 1, l + p, a[i].first.first + p,
a[i].first.second);
} else {
int H =
query(1, 1, p, p * 2 - 1, p + a[i].first.first, p + a[i].first.first);
if (H == -1) {
ans.push_back(obr[1][a[i].first.second]);
} else {
ans.push_back(obr[1][a[i].first.second] - obr[1][H]);
}
int l = max(H, 0);
change(1, 0, p, p * 2 - 1, l + p, a[i].first.second + p,
a[i].first.first);
}
st.insert(a[i].first);
}
for (auto to : ans) {
cout << to << 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;
class sgt {
private:
vector<int> Max;
vector<int> Lc;
vector<int> Rc;
int Pool = 0;
public:
sgt() {}
sgt(int N) : Lc(N * 50), Rc(N * 50), Max(N * 50, -1) {}
void update(int L, int R, int &X, int A, int B, int V) {
if (L > B || R < A) {
return;
}
if (X == 0) {
X = ++Pool;
}
if (L >= A && R <= B) {
Max[X] = max(Max[X], V);
return;
}
int Mid = L + R >> 1;
update(L, Mid, Lc[X], A, B, V);
update(Mid + 1, R, Rc[X], A, B, V);
}
int query(int L, int R, int X, int P) {
if (X == 0) {
return -1;
}
if (L == R) {
return Max[X];
}
int Mid = L + R >> 1;
if (P <= Mid) {
return max(Max[X], query(L, Mid, Lc[X], P));
} else {
return max(Max[X], query(Mid + 1, R, Rc[X], P));
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
sgt Row(Q);
sgt Col(Q);
int R0 = 0;
int R1 = 0;
map<pair<int, int>, bool> vis;
while (Q-- > 0) {
int X, Y;
char Dir;
cin >> X >> Y >> Dir;
X--;
Y--;
if (vis.find({X, Y}) != vis.end()) {
cout << 0 << '\n';
continue;
}
vis[{X, Y}] = true;
if (Dir == 'U') {
int U = Col.query(0, N - 1, R0, X);
cout << Y - U << '\n';
Row.update(0, N - 1, R1, U + 1, Y, X);
}
if (Dir == 'L') {
int L = Row.query(0, N - 1, R1, Y);
cout << X - L << '\n';
Col.update(0, N - 1, R0, L + 1, X, Y);
}
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.util.TreeSet;
import java.util.TreeMap;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Map;
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
*/
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 {
private class Pieces{
int x, y;
public Pieces(int x, int y) {
this.x = x;
this.y = y;
}
}
private TreeSet<Integer> st;
private TreeMap<Integer, Pieces> heap;
private int n, m;
private StringBuffer res;
public void solve(int testNumber, InputReader in, PrintWriter out) {
n = in.nextInt(); m = in.nextInt();
st = new TreeSet<>();
heap = new TreeMap<>();
res = new StringBuffer();
heap.put(n + 1, new Pieces(0, 0));
while (m-- > 0) {
int x = in.nextInt(), y = in.nextInt();
String s = in.next();
if (st.contains(x)) {
res.append(0).append('\n');
continue;
}
st.add(x);
Map.Entry<Integer, Pieces> p = heap.ceilingEntry(x);
if (s.charAt(0) == 'U') {
res.append(y - p.getValue().x).append("\n");
heap.put(x, new Pieces(p.getValue().x, p.getValue().y));
p.getValue().y = x;
}
else {
res.append(x - p.getValue().y).append('\n');
heap.put(x, new Pieces(y, p.getValue().y));
}
}
out.println(res.toString());
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
} catch (Exception e) {
throw new UnknownError(e.getMessage());
}
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 |
import java.util.*;
import java.io.*;
public class CaseOfChocolate555C_TreeMap {
static class Border {
int upper, left;
public Border(int a, int b) {
this.upper = a;
this.left = b;
}
}
static void go() {
int n = in.nextInt();
int q = in.nextInt();
TreeMap<Integer, Border> map = new TreeMap<>();
map.put(0, new Border(0, 0));
map.put(n + 1, new Border(0, 0));
for (int i = 0; i < q; i++) {
int l = in.nextInt();
int r = in.nextInt();
char dir = in.nextCharArray(1)[0];
if (dir == 'U') {
int heigher = map.ceilingKey(l);
if (l == heigher) {
out.println(0);
continue;
}
Border b = map.get(heigher);
int c = r - b.upper;
out.println(c);
map.put(l, new Border(b.upper, l));
} else {
int lower = map.floorKey(l);
if (l == lower) {
out.println(0);
continue;
}
Border b = map.get(lower);
int c = l - b.left;
out.println(c);
map.put(l, new Border(n + 1 - l, b.left));
}
}
}
static InputReader in;
static PrintWriter out;
public static void main(String[] args) {
in = new InputReader(System.in);
out = new PrintWriter(System.out);
go();
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int[] nextIntArray(int len) {
int[] ret = new int[len];
for (int i = 0; i < len; i++)
ret[i] = nextInt();
return ret;
}
public long[] nextLongArray(int len) {
long[] ret = new long[len];
for (int i = 0; i < len; i++)
ret[i] = nextLong();
return ret;
}
public int nextInt() {
return (int) nextLong();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextLine() {
StringBuilder sb = new StringBuilder(1024);
int c = read();
while (!(c == '\n' || c == '\r' || c == -1)) {
sb.append((char) c);
c = read();
}
return sb.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder sb = new StringBuilder(1024);
do {
sb.append((char) c);
c = read();
} while (!isSpaceChar(c));
return sb.toString();
}
public char[] nextCharArray(int n) {
char[] ca = new char[n];
for (int i = 0; i < n; i++) {
int c = read();
while (isSpaceChar(c))
c = read();
ca[i] = (char) c;
}
return ca;
}
public static boolean isSpaceChar(int c) {
switch (c) {
case -1:
case ' ':
case '\n':
case '\r':
case '\t':
return true;
default:
return false;
}
}
}
} | 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(false);
int n, m;
cin >> n >> m;
map<int, pair<int, int>> mp;
mp[0] = make_pair(n + 1, 0);
while (m--) {
int x, y, ans;
char dir;
cin >> x >> y >> dir;
if (mp.count(x)) {
cout << 0 << '\n';
continue;
}
auto it = prev(mp.lower_bound(x));
int up = it->second.first - (x - it->first);
int left = it->second.second + (x - it->first);
if (dir == 'U') {
ans = up;
mp[x] = make_pair(up, 0);
}
if (dir == 'L') {
ans = left;
mp[x] = make_pair(up, left);
it->second.first = (x - it->first);
}
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;
const int N = 200000 + 10;
const int M = 1000000007;
const double PI = atan(1) * 4;
const int oo = 1000000005;
int n, q, l, r;
struct node {
int seg, lz;
node *left, *right;
node() {
left = right = NULL;
seg = lz = 0;
}
} * root[2];
void fix(node *cur, int s, int e) {
if (s != e) {
if (!cur->left) cur->left = new node();
if (!cur->right) cur->right = new node();
}
int &laz = cur->lz;
if (!laz) return;
if (s != e) {
int &lz1 = cur->left->lz, &lz2 = cur->right->lz;
lz1 = max(lz1, laz);
lz2 = max(lz2, laz);
}
cur->seg = max(cur->seg, laz);
laz = 0;
}
void update(node *cur, int s, int e, int val) {
fix(cur, s, e);
if (s >= l && e <= r) {
cur->lz = val;
fix(cur, s, e);
return;
}
if (s > r || e < l) return;
update(cur->left, s, (s + e) / 2, val);
update(cur->right, (s + e) / 2 + 1, e, val);
}
int get(node *cur, int s, int e) {
fix(cur, s, e);
if (s > r || e < l) return 0;
if (s == e) return cur->seg;
return get(cur->left, s, (s + e) / 2) + get(cur->right, (s + e) / 2 + 1, e);
}
struct query {
int a, b, c;
query(int x, int y, char z) {
a = x;
b = y;
c = z == 'U';
}
};
set<int> st;
vector<query> Q;
int main() {
cin >> n >> q;
root[0] = new node();
root[1] = new node();
char c;
vector<int> num;
num.push_back(0);
for (int a, b, x, i = 0; i < q; ++i) {
scanf("%d%d %c", &a, &b, &c);
Q.push_back(query(a, b, c));
num.push_back(a);
num.push_back(b);
}
sort((num).begin(), (num).end());
num.resize(unique((num).begin(), (num).end()) - num.begin());
for (int x, i = 0; i < q; ++i) {
int a = Q[i].a, b = Q[i].b, c = Q[i].c;
a = lower_bound((num).begin(), (num).end(), a) - num.begin();
b = lower_bound((num).begin(), (num).end(), b) - num.begin();
if (!st.insert(a).second) {
printf("0\n");
continue;
}
if (c) {
l = r = a;
x = get(root[0], 1, num.size() - 1);
l = x;
r = b;
update(root[1], 1, num.size() - 1, a);
printf("%d\n", n - num[a] + 1 - num[x]);
} else {
l = r = b;
x = get(root[1], 1, num.size() - 1);
l = x;
r = a;
update(root[0], 1, num.size() - 1, b);
printf("%d\n", n - num[b] + 1 - num[x]);
}
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
const int inf = (int)(1e9) * 2;
struct segment_tree {
int limit[N * 4], tag[N * 4];
void buildtree(int u, int l, int r) {
tag[u] = -1, limit[u] = inf;
if (l == r) return;
int mid = l + r >> 1;
buildtree((u << 1), l, mid), buildtree(((u << 1) + 1), mid + 1, r);
}
void pushdown(int u, int l, int r) {
if (tag[u] == -1) return;
if (l == r) {
tag[u] = -1;
return;
}
if (tag[(u << 1)] == -1)
tag[(u << 1)] = tag[u];
else
tag[(u << 1)] = min(tag[(u << 1)], tag[u]);
if (tag[((u << 1) + 1)] == -1)
tag[((u << 1) + 1)] = tag[u];
else
tag[((u << 1) + 1)] = min(tag[((u << 1) + 1)], tag[u]);
limit[(u << 1)] = min(tag[(u << 1)], limit[(u << 1)]),
limit[((u << 1) + 1)] =
min(tag[((u << 1) + 1)], limit[((u << 1) + 1)]);
tag[u] = -1;
}
void modify(int u, int l, int r, int f, int t, int val) {
pushdown(u, l, r);
if (l >= f && r <= t) {
if (tag[u] == -1)
tag[u] = val;
else
tag[u] = min(tag[u], val);
limit[u] = min(limit[u], val);
return;
}
int mid = l + r >> 1;
if (f <= mid) modify((u << 1), l, mid, f, t, val);
if (t > mid) modify(((u << 1) + 1), mid + 1, r, f, t, val);
limit[u] = min(limit[(u << 1)], limit[((u << 1) + 1)]);
}
int query(int u, int l, int r, int t) {
pushdown(u, l, r);
if (l == r) return limit[u];
int mid = l + r >> 1;
if (t <= mid)
return query((u << 1), l, mid, t);
else
return query(((u << 1) + 1), mid + 1, r, t);
}
} TU, TL;
int xd[N], dif;
struct question {
char ch[5];
int x, y;
} ques[N];
int uf[N], lf[N];
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= q; i++) {
scanf("%d%d%s", &ques[i].y, &ques[i].x, ques[i].ch);
xd[i] = ques[i].x;
}
sort(xd + 1, xd + 1 + q);
dif = unique(xd + 1, xd + 1 + q) - xd - 1;
TU.buildtree(1, 1, dif), TL.buildtree(1, 1, dif);
memset(uf, 0, sizeof uf);
memset(lf, 0, sizeof lf);
for (int i = 1; i <= q; i++) {
int id = lower_bound(xd + 1, xd + 1 + dif, ques[i].x) - xd;
int x = ques[i].x, y = ques[i].y;
if (ques[i].ch[0] == 'U') {
if (!uf[id])
uf[id] = 1;
else {
printf("0\n");
continue;
}
int ans = min(x, max(TU.query(1, 1, dif, id) - (n - x), 0));
printf("%d\n", ans);
int r = upper_bound(xd + 1, xd + 1 + dif, x) - xd;
int l = lower_bound(xd + 1, xd + 1 + dif, x - ans + 1) - xd;
if (r - 1 >= l) TL.modify(1, 1, dif, l, r - 1, n - y);
} else {
if (!lf[id])
lf[id] = 1;
else {
printf("0\n");
continue;
}
int ans = min(y, max(TL.query(1, 1, dif, id) - (n - y), 0));
printf("%d\n", ans);
int r = upper_bound(xd + 1, xd + 1 + dif, x + ans - 1) - xd;
int l = lower_bound(xd + 1, xd + 1 + dif, x) - xd;
if (r - 1 >= l) TU.modify(1, 1, dif, l, r - 1, n - 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 node {
int r, c;
int last;
char ty;
node() {}
node(int a, int b, int cc, char t) {
r = a;
c = b;
last = cc;
ty = t;
}
bool operator<(const node& b) const { return r < b.r; }
bool operator==(const node& b) const { return r == b.r && c == b.c; }
};
int cmp(node a, node b) { return a.r > b.r; }
set<node> sb;
set<node>::iterator it;
int main() {
int i, j;
int n, m;
int a, b;
char op;
scanf("%d%d", &n, &m);
for (i = 1; i <= m; i++) {
node tp;
scanf("%d %d %c", &a, &b, &op);
if (op == 'U') {
tp.r = a;
it = sb.lower_bound(tp);
if (it->r == a) {
printf("0\n");
} else if (it == sb.end()) {
sb.insert(node(a, b, 1, op));
printf("%d\n", b);
} else {
if (it->ty == 'U') {
sb.insert(node(a, b, it->last, op));
printf("%d\n", b - it->last + 1);
} else {
sb.insert(node(a, b, it->c + 1, op));
printf("%d\n", b - it->c);
}
}
} else {
tp.r = a;
it = sb.lower_bound(tp);
if (it->r == a) {
printf("0\n");
} else if (it == sb.begin()) {
sb.insert(node(a, b, 1, op));
printf("%d\n", a);
} else {
it--;
if (it->ty == 'L') {
sb.insert(node(a, b, it->last, op));
printf("%d\n", a - it->last + 1);
} else {
sb.insert(node(a, b, it->r + 1, op));
printf("%d\n", a - it->r);
}
}
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.io.*;
import java.util.*;
public class C_new {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
static class Node {
int l, r;
Node left, right;
int val;
public Node(int l, int r) {
this.l = l;
this.r = r;
if (r - l > 1) {
int mid = (l + r) >> 1;
left = new Node(l, mid);
right = new Node(mid, r);
}
}
void set(int ql, int qr, int newVal) {
if (ql >= r || l >= qr) {
return;
}
if (ql <= l && r <= qr) {
val = Math.max(val, newVal);
return;
}
left.set(ql, qr, newVal);
right.set(ql, qr, newVal);
}
int get(int pos) {
if (l == pos && pos + 1 == r) {
return val;
}
int ret = (pos < left.r ? left : right).get(pos);
return Math.max(ret, val);
}
}
void solve() throws IOException {
int n = nextInt();
int q = nextInt();
int[] xs = new int[q];
char[] type = new char[q];
for (int i = 0; i < q; i++) {
nextInt();
xs[i] = nextInt();
type[i] = nextToken().charAt(0);
}
int[] tmp = Arrays.copyOf(xs, xs.length + 2);
tmp[xs.length] = 0;
tmp[xs.length + 1] = n + 1;
int[] u = unique(tmp);
for (int i = 0; i < q; i++) {
xs[i] = Arrays.binarySearch(u, xs[i]);
}
int m = u.length;
Node rootL = new Node(0, m);
Node rootU = new Node(0, m);
boolean[] eatL = new boolean[m];
boolean[] eatU = new boolean[m];
outer: for (int i = 0; i < q; i++) {
int pos = xs[i];
if (type[i] == 'U') {
if (eatU[pos]) {
out.println(0);
continue outer;
}
eatU[pos] = true;
int to = rootU.get(pos);
out.println(u[pos] - u[to]);
rootL.set(to + 1, pos + 1, m - 1 - pos);
} else {
if (eatL[pos]) {
out.println(0);
continue outer;
}
eatL[pos] = true;
int to = m - 1 - rootL.get(pos);
// System.err.println(pos + " " + to);
out.println(u[to] - u[pos]);
rootU.set(pos, to, pos);
}
}
}
int[] unique(int[] a) {
Random rng = new Random();
int n = a.length;
for (int i = 0; i < n; i++) {
int j = rng.nextInt(i + 1);
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
Arrays.sort(a);
int sz = 1;
for (int i = 1; i < n; i++) {
if (a[i] != a[sz - 1]) {
a[sz++] = a[i];
}
}
return Arrays.copyOf(a, sz);
}
C_new() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new C_new();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(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;
map<int, int> ren;
char buf[100];
const int MAXTSZ = int(2e5 + 100) * 2 * 4;
struct query {
int x, y;
char dir;
};
int get_name(int x) {
int sz = ren.size();
return ren.insert(make_pair(x, sz)).first->second;
}
set<int> t_up[MAXTSZ], t_left[MAXTSZ];
typedef set<int> t_type[MAXTSZ];
void tree_add(t_type &t, int tv, int tl, int tr, int pos, int val) {
t[tv].insert(val);
if (tl == tr) return;
int tm = (tl + tr) / 2;
if (pos <= tm)
tree_add(t, 2 * tv, tl, tm, pos, val);
else
tree_add(t, 2 * tv + 1, tm + 1, tr, pos, val);
}
int tree_bound(const t_type &t, int tv, int tl, int tr, int l, int r, int val) {
if (l > r) return -1;
if (tl == l && tr == r) {
auto it = t[tv].upper_bound(val);
return it == t[tv].begin() ? -1 : *(--it);
}
int tm = (tl + tr) / 2;
int res_l = tree_bound(t, 2 * tv, tl, tm, l, min(tm, r), val);
int res_r = tree_bound(t, 2 * tv + 1, tm + 1, tr, max(tm + 1, l), r, val);
return max(res_l, res_r);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
vector<query> v;
vector<int> coords;
coords.push_back(0);
for (int i = 0; i < q; ++i) {
query qr;
scanf("%d%d%s", &qr.x, &qr.y, buf);
qr.dir = *buf;
v.push_back(qr);
coords.push_back(qr.x);
coords.push_back(qr.y);
}
sort(coords.begin(), coords.end());
for (int x : coords) get_name(x);
int tv = 1;
int tl = 0;
int tr = (int)ren.size() - 1;
tree_add(t_up, tv, tl, tr, get_name(0), 0);
tree_add(t_left, tv, tl, tr, get_name(0), 0);
set<int> used_left, used_up;
for (const query &qr : v)
if (qr.dir == 'L') {
if (used_left.count(qr.y)) {
puts("0");
continue;
}
used_left.insert(qr.y);
int bound =
tree_bound(t_up, tv, tl, tr, get_name(0), get_name(qr.y), qr.x);
int len = qr.x - bound;
printf("%d\n", len);
tree_add(t_left, tv, tl, tr, get_name(bound), qr.y);
} else {
if (used_up.count(qr.x)) {
puts("0");
continue;
}
used_up.insert(qr.x);
int bound =
tree_bound(t_left, tv, tl, tr, get_name(0), get_name(qr.x), qr.y);
int len = qr.y - bound;
printf("%d\n", len);
tree_add(t_up, tv, tl, tr, get_name(bound), qr.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 sys
from bisect import bisect
def input():
return sys.stdin.readline().strip()
def solve():
n, q = map(int, input().split())
was = set()
Q = [None]*q
all = [0]*(2*q)
for i in range(q):
x, y, t = input().split()
x, y = int(x), int(y)
Q[i] = (x, y, t)
all[2*i] = x
all[2*i+1] = y
all.sort()
sz = 2*q
V = [0]*(2*sz)
H = [0]*(2*sz)
for x, y, t in Q:
if (x,y) in was:
print(0)
else:
was.add((x,y))
if t == 'L':
TA = H
TB = V
else:
x, y = y, x
TA = V
TB = H
v = bisect(all, y) - 1 + sz
r = 0
while v > 0:
r = max(r, TA[v])
v //= 2
c = x - r
print(c)
r = bisect(all, x) - 1 + sz
l = bisect(all, x - c) + sz
while l <= r:
if l % 2 == 1:
TB[l] = max(TB[l], y)
if r % 2 == 0:
TB[r] = max(TB[r], y)
l = (l+1)//2
r = (r-1)//2
solve()
| PYTHON3 |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | 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<Integer> 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();
if (taken.contains(x)) {
System.out.println(0);
continue;
}
taken.add(x);
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;
const int treesize = 1 << 30;
struct tnode {
tnode *l, *r;
int max;
tnode() {
l = NULL;
r = NULL;
max = 0;
}
};
typedef tnode* pnode;
pnode sett(pnode cur, int cl, int cr, int l, int r, int t) {
if (cl > r || cr < l) return cur;
if (cur == NULL) cur = new tnode;
if (cl >= l && cr <= r) {
cur->max = max(cur->max, t);
return cur;
}
int cm = (cl + cr) / 2;
cur->l = sett(cur->l, cl, cm, l, r, t);
cur->r = sett(cur->r, cm + 1, cr, l, r, t);
return cur;
}
int get(pnode cur, int cl, int cr, int x) {
if (cur == NULL) return 0;
int cm = (cl + cr) / 2;
if (x <= cm)
return max(cur->max, get(cur->l, cl, cm, x));
else
return max(cur->max, get(cur->r, cm + 1, cr, x));
}
pnode root[2];
set<int> was[2];
int n, m;
char t[5];
void doall(int t0, int t1, int wh) {
if (was[t0].count(wh)) {
printf("%d\n", 0);
return;
}
was[t0].insert(wh);
int deep = get(root[t0], 0, treesize - 1, wh);
int ans = n + 1 - wh - deep;
printf("%d\n", ans);
root[t1] =
sett(root[t1], 0, treesize - 1, n + 1 - wh - ans + 1, n + 1 - wh, wh);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d%s", &y, &x, t);
if (t[0] == 'U') {
doall(0, 1, y);
} else {
doall(1, 0, 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;
map<int, int> up, lft;
set<int> used;
int main() {
int N, Q;
scanf("%d %d ", &N, &Q);
;
up.insert(make_pair(0, 0));
lft.insert(make_pair(0, 0));
for (int i = (0); i < (Q); i++) {
int c, r;
scanf("%d %d ", &c, &r);
;
string s;
cin >> s;
if (used.insert(r).second == false) {
printf("0\n");
continue;
}
if (s[0] == 'L') {
map<int, int>::iterator it = lft.insert(make_pair(r, 0)).first;
--it;
printf("%d\n", c - it->second);
lft[r] = it->second;
it = up.insert(make_pair(c, 0)).first;
it--;
up[c] = it->second;
it = up.insert(make_pair(c, 0)).first;
--it;
up[it->first] = r;
} else {
map<int, int>::iterator it = up.insert(make_pair(c, 0)).first;
--it;
printf("%d\n", r - it->second);
up[c] = it->second;
it = lft.insert(make_pair(r, 0)).first;
it--;
lft[r] = it->second;
it = lft.insert(make_pair(r, 0)).first;
--it;
lft[it->first] = c;
}
}
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<pair<int, int> > s;
map<int, int> mp;
int n, q, x, y;
char ss[10];
int main() {
scanf("%d%d", &n, &q);
s.insert(make_pair(0, 1));
s.insert(make_pair(n + 1, 0));
mp.clear();
while (q--) {
scanf("%d%d%s", &x, &y, ss);
if (mp.count(x) != 0) {
printf("0\n");
continue;
}
if (ss[0] == 'U') {
pair<int, int> dq = *s.lower_bound(make_pair(x, -1));
if (dq.second == 1)
mp[x] = dq.first - x + mp[dq.first];
else
mp[x] = dq.first - x;
s.insert(make_pair(x, 1));
} else {
pair<int, int> dq = *(--s.lower_bound(make_pair(x, -1)));
if (dq.second == 1)
mp[x] = x - dq.first;
else
mp[x] = x - dq.first + mp[dq.first];
s.insert(make_pair(x, 0));
}
printf("%d\n", mp[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;
set<pair<int, int> > st;
set<pair<int, int> >::iterator it;
int X[200005], Y[200005], n, m;
int main() {
scanf("%d %d", &n, &m);
st.insert(make_pair(0, 0));
st.insert(make_pair(n + 1, m + 1));
for (int i = 1; i <= m; i++) {
char o;
scanf("%d %d %c", &X[i], &Y[i], &o);
if (o == 'U')
it = st.lower_bound(make_pair(X[i], 0));
else
it = st.upper_bound(make_pair(X[i], m + 1)), --it;
if (it->first == X[i]) {
puts("0");
continue;
}
st.insert(make_pair(X[i], i));
if (o == 'U') {
printf("%d\n", Y[i] - Y[it->second]);
Y[i] = Y[it->second];
} else {
printf("%d\n", X[i] - X[it->second]);
X[i] = X[it->second];
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:67108864")
using namespace std;
const int MX = 200005;
const int MOD = 1000000007;
set<int> L, U;
struct node {
node(int nw) : nw(nw) { left = right = NULL; }
int nw;
node *left, *right;
};
struct segtree {
node* root;
int S = 0, E = 1000000000;
segtree() { root = new node(-1); }
void insert(int s, int e, int nw) { return insert(root, S, E, s, e, nw); }
void insert(node* n, int s, int e, int l, int r, int nw) {
if (e < l || r < s)
return;
else if (l <= s && e <= r) {
n->nw = max(nw, n->nw);
} else {
int m = (s + e) / 2;
if (!n->left) n->left = new node(n->nw), n->right = new node(n->nw);
insert(n->left, s, m, l, r, nw);
insert(n->right, m + 1, e, l, r, nw);
}
}
int read(int ad) { return read(root, S, E, ad); }
int read(node* n, int s, int e, int ad) {
if (!n) return -1;
int m = (s + e) / 2;
if (ad <= m)
return max(n->nw, read(n->left, s, m, ad));
else
return max(n->nw, read(n->right, m + 1, e, ad));
}
} T[2];
set<int> chk;
int main() {
int N, Q;
scanf("%d%d", &N, &Q);
T[0].insert(1, N, 0);
T[1].insert(1, N, 0);
for (int i = 1; i <= Q; i++) {
int X, Y;
char ord[10];
scanf("%d%d%s", &X, &Y, ord);
if (chk.find(X) != chk.end())
printf("0\n");
else if (ord[0] == 'U') {
int ans = T[1].read(X);
printf("%d\n", Y - ans);
T[0].insert(ans, Y, X);
} else {
int ans = T[0].read(Y);
printf("%d\n", X - ans);
T[1].insert(ans, X, Y);
}
chk.insert(X);
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long modulo(long long x, long long y) { return ((x % y) + y) % y; }
struct SegmentMaxTree {
SegmentMaxTree(int default_value, int n) : default_value(default_value) {
sz = 1;
while (sz < n) {
sz = sz << 1;
}
items.resize(2 * sz - 1);
fill(items.begin(), items.end(), default_value);
}
void Set(int first, int last, int value) {
return Set(first, last, value, 0, 0, sz - 1);
}
void Set(int first, int last, int value, size_t node, int segment_begin,
int segment_last) {
first = max(first, segment_begin);
last = min(last, segment_last);
if (first > last) {
return;
}
if (first == segment_begin && last == segment_last) {
items[node] = max(value, items[node]);
} else {
auto segment_mid = (segment_begin + segment_last + 1) / 2;
Set(first, last, value, node * 2 + 1, segment_begin, segment_mid - 1);
Set(first, last, value, node * 2 + 2, segment_mid, segment_last);
}
}
int Get(int idx) { return Get(idx, 0, 0, sz - 1); }
int Get(int idx, size_t node, int segment_begin, int segment_last) {
int best_value = items[node];
if (segment_begin == segment_last) {
return best_value;
}
auto segment_mid = (segment_begin + segment_last + 1) / 2;
if (segment_mid <= idx) {
best_value =
max(best_value, Get(idx, 2 * node + 2, segment_mid, segment_last));
} else {
best_value = max(best_value,
Get(idx, 2 * node + 1, segment_begin, segment_mid - 1));
}
return best_value;
}
int default_value;
int sz;
vector<int> items;
};
struct CompressedSegmentMaxTree {
CompressedSegmentMaxTree(int default_value, const set<long long>& items)
: tree(default_value, items.size()) {
for (auto el : items) {
element2index[el] = index2element.size();
index2element.push_back(el);
}
}
void Set(int first, int last, int value) {
tree.Set(element2index.at(first), element2index.at(last), value);
}
int Get(int pos) { return tree.Get(element2index.at(pos)); }
SegmentMaxTree tree;
map<long long, int> element2index;
vector<long long> index2element;
};
struct Record {
int row, col;
char c;
};
void solve() {
int n, q;
cin >> n >> q;
vector<Record> records(q);
set<long long> coordinates;
for (int(i) = 0; (i) < (q); (i)++) {
Record& rec = records[i];
cin >> rec.col >> rec.row >> rec.c;
rec.row--;
rec.col--;
coordinates.insert(rec.col);
coordinates.insert(rec.row);
}
coordinates.insert(-1);
coordinates.insert(0);
coordinates.insert(n);
CompressedSegmentMaxTree max_eaten_by_cols(-1, coordinates);
CompressedSegmentMaxTree max_eaten_by_rows(-1, coordinates);
set<int> seen_left, seen_up;
for (auto& rec : records) {
int row = rec.row;
int col = rec.col;
char c = rec.c;
auto& active_set = (c == 'L') ? max_eaten_by_rows : max_eaten_by_cols;
auto& anti_active_set = (c != 'L') ? max_eaten_by_rows : max_eaten_by_cols;
int point = (c == 'L') ? row : col;
int anti_point = (c != 'L') ? row : col;
int can_eat_upto = active_set.Get(point);
if (seen_left.find(row) != seen_left.end() ||
seen_up.find(col) != seen_up.end()) {
can_eat_upto = anti_point;
} else {
anti_active_set.Set(can_eat_upto, anti_point, point);
}
printf("%d\n", (anti_point - can_eat_upto));
if (c == 'L') {
seen_left.insert(row);
} else {
seen_up.insert(col);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
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>
int n, q;
std::map<int, int> u, l;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) f = (ch == '-') ? -1 : 1, ch = getchar();
while (isdigit(ch)) x = x * 10 + (ch - '0'), ch = getchar();
return x * f;
}
int main() {
int x, y;
char op;
n = read(), q = read();
while (q--) {
x = read(), y = read();
op = getchar();
std::map<int, int>::iterator p;
if (op == 'U') {
int ans = 0;
p = u.lower_bound(x);
if (u.count(x)) {
printf("0\n");
continue;
}
if (p == u.end())
ans = y;
else
ans = y - (p->second);
std::cout << ans << '\n';
l[y] = x;
u[x] = y - ans;
} else {
int ans = 0;
p = l.lower_bound(y);
if (l.count(y)) {
printf("0\n");
continue;
}
if (p == l.end())
ans = x;
else
ans = x - (p->second);
std::cout << ans << '\n';
l[y] = x - ans;
u[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, m, cnt, rt, L[8000100], R[8000100], mx[2][8000100];
map<int, int> t[2];
inline int gi() {
int s = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
return s;
}
void updata(int &x, int l, int r, int xl, int xr, int v, int p) {
if (xl > xr) return;
if (!x) x = ++cnt;
if (xl <= l && r <= xr) {
mx[p][x] = max(mx[p][x], v);
return;
}
int mid = (l + r) >> 1;
if (xr <= mid)
updata(L[x], l, mid, xl, xr, v, p);
else if (xl > mid)
updata(R[x], mid + 1, r, xl, xr, v, p);
else
updata(L[x], l, mid, xl, mid, v, p),
updata(R[x], mid + 1, r, mid + 1, xr, v, p);
}
int query(int x, int l, int r, int v, int p) {
if (l == r) return mx[p][x];
int mid = (l + r) >> 1;
if (v <= mid)
return max(query(L[x], l, mid, v, p), mx[p][x]);
else
return max(query(R[x], mid + 1, r, v, p), mx[p][x]);
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int x = gi(), y = gi();
char ch[4];
scanf("%s", ch);
if (ch[0] == 'L') {
if (t[0][y]) {
puts("0");
continue;
}
int w = query(rt, 1, n, y, 0);
printf("%d\n", x - w);
updata(rt, 1, n, w + 1, x, y, 1), t[0][y] = 1;
} else {
if (t[1][x]) {
puts("0");
continue;
}
int w = query(rt, 1, n, x, 1);
printf("%d\n", y - w);
updata(rt, 1, n, w + 1, y, x, 0), t[1][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 | #include <bits/stdc++.h>
using namespace std;
int n, q, x, y, ans;
char ch[2];
map<int, int> Left, Up;
map<int, int>::iterator it;
int main() {
ios::sync_with_stdio(false);
while (scanf("%d%d", &n, &q) != EOF) {
Left.clear();
Up.clear();
while (q--) {
scanf("%d%d%s", &x, &y, ch);
if (Up.find(x) != Up.end()) {
printf("0\n");
continue;
}
if (ch[0] == 'U') {
it = Up.lower_bound(x);
if (it == Up.end()) {
ans = y;
Up[x] = 0;
} else {
ans = y - it->second;
Up[x] = it->second;
}
Left[y] = x;
} else {
it = Left.lower_bound(y);
if (it == Left.end()) {
ans = x;
Left[y] = 0;
} else {
ans = x - it->second;
Left[y] = it->second;
}
Up[x] = y;
}
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 | 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);
}
SWAG mxrow = new SWAG(Q*2+2);
SWAG mxcol = new SWAG(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);
}
}
}
class SWAG
{
int N;
int A[];
int lazy[];
SWAG(int size)
{
N = size+10;
A = new int[N*4+1000];
lazy = new int[N*4+1000];
}
int q(int index)
{
return q(index, 1, N, 1);
}
void update(int l, int r, int value)
{
update(l, r, value, 1, N, 1);
return;
}
void helloworld(int value, int n)
{
A[n] = Math.max(A[n], value);
lazy[n] = Math.max(lazy[n], value);
}
void clearLazy(int a, int b, int n)
{
if (lazy[n] == 0) return;
if (a < b)
{
helloworld(lazy[n],2*n);
helloworld(lazy[n],2*n+1);
}
lazy[n] = 0;
}
void update(int l, int r, int value, int a, int b, int n)
{
clearLazy(a,b,n);
if (r < a || b < l) return;
if (l <= a && b <= r)
{
helloworld(value,n);
}
else
{
int m = (a + b) / 2;
update(l, r, value, a, m, n*2);
update(l, r, value, m+1, b, n*2+1);
A[n] = Math.max(A[n*2],A[n*2+1]);
}
return;
}
int q(int index, int a, int b, int n)
{
clearLazy(a,b,n);
if (index < a || b < index) return 0;
if (a == b)
{
return A[n];
}
else
{
int m = (a + b) / 2;
if (index <= m)
return q(index,a,m,n*2);
else
return q(index,m+1,b,n*2+1);
}
}
}
}
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 | import java.util.Scanner;
import java.util.TreeSet;
public class Main
{
static private int n;
static private int q;
static private TreeSet<Area> set = new TreeSet<Area>();
static private TreeSet<Integer> used = new TreeSet<Integer>();
private static class Area implements Comparable<Area>, Cloneable
{
public int uBound = -1;
public int lBound = -1;
private int a;
Area(int a)
{
this.a = a;
}
Area setA(int a)
{
Area newArea = new Area(a);
newArea.lBound = lBound;
newArea.uBound = uBound;
return newArea;
}
public Area clone() throws CloneNotSupportedException
{
return (Area) super.clone();
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof Area && a == ((Area) obj).a)
{
return true;
}
return false;
}
@Override
public int compareTo(Area area)
{
int answ;
if (a < area.a)
{
answ = -1;
}
else if (a > area.a)
{
answ = 1;
}
else
{
answ = 0;
}
return answ;
}
// @Override
// public String toString()
// {
// return "[" + first + ", " + second + "]";
// }
}
public static void main(String[] arg)
{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
q = sc.nextInt();
Area initArea = new Area(n);
set.add(initArea);
for (int i = 0; i < q; i++)
{
int x = sc.nextInt() - 1;
int y = sc.nextInt() - 1;
char d = sc.next().charAt(0);
if (!used.contains(y))
{
used.add(y);
Area empty = new Area(y);
Area curr = set.higher(empty);
Area newArea = curr.setA(y);
set.add(newArea);
if (d == 'U')
{
System.out.println(y - curr.uBound);
newArea.lBound = x;
}
else
{
System.out.println(x - curr.lBound);
curr.uBound = y;
}
}
else
{
System.out.println(0);
}
}
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>
const int mod = 1000000007;
const int gmod = 5;
const int inf = 1039074182;
const double eps = 1e-9;
const long long llinf = 1LL * inf * inf;
template <typename T1, typename T2>
inline void chmin(T1 &x, T2 b) {
if (b < x) x = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &x, T2 b) {
if (b > x) x = b;
}
inline void chadd(int &x, int b) {
x += b - mod;
x += (x >> 31 & mod);
}
template <typename T1, typename T2>
inline void chadd(T1 &x, T2 b) {
x += b;
if (x >= mod) x -= mod;
}
template <typename T1, typename T2>
inline void chmul(T1 &x, T2 b) {
x = 1LL * x * b % mod;
}
template <typename T1, typename T2>
inline void chmod(T1 &x, T2 b) {
x %= b, x += b;
if (x >= b) x -= b;
}
template <typename T>
inline T mabs(T x) {
return (x < 0 ? -x : x);
}
using namespace std;
template <typename T>
ostream &operator<<(ostream &cout, vector<T> vec) {
cout << "{";
for (int i = 0; i < (int)vec.size(); i++) {
cout << vec[i];
if (i != (int)vec.size() - 1) cout << ',';
}
cout << "}";
return cout;
}
template <typename T>
void operator*=(vector<T> &vec, int k) {
for (auto &x : vec) x *= k;
}
template <typename T>
void operator-=(vector<T> &a, const vector<T> &b) {
assert(a.size() == b.size());
for (size_t it = 0; it < a.size(); it++) {
a[it] -= b[it];
}
}
template <typename T>
vector<T> operator*(const vector<T> &vec, int k) {
vector<T> res(vec);
res *= k;
return res;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &cout, pair<T1, T2> p) {
cout << "(" << p.first << ',' << p.second << ")";
return cout;
}
template <typename T, typename T2>
ostream &operator<<(ostream &cout, set<T, T2> s) {
vector<T> t;
for (auto x : s) t.push_back(x);
cout << t;
return cout;
}
template <typename T, typename T2>
ostream &operator<<(ostream &cout, multiset<T, T2> s) {
vector<T> t;
for (auto x : s) t.push_back(x);
cout << t;
return cout;
}
template <typename T>
ostream &operator<<(ostream &cout, queue<T> q) {
vector<T> t;
while (q.size()) {
t.push_back(q.front());
q.pop();
}
cout << t;
return cout;
}
template <typename T1, typename T2, typename T3>
ostream &operator<<(ostream &cout, map<T1, T2, T3> m) {
for (auto &x : m) {
cout << "Key: " << x.first << ' ' << "Value: " << x.second << endl;
}
return cout;
}
template <typename T>
T operator*(vector<T> v1, vector<T> v2) {
assert(v1.size() == v2.size());
int n = v1.size();
T res = 0;
for (int i = 0; i < n; i++) {
res += v1[i] * v2[i];
}
return res;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(pair<T1, T2> x, pair<T1, T2> y) {
return make_pair(x.first + y.first, x.second + y.second);
}
template <typename T1, typename T2>
void operator+=(pair<T1, T2> &x, pair<T1, T2> y) {
x.first += y.first;
x.second += y.second;
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> x) {
return make_pair(-x.first, -x.second);
}
template <typename T>
vector<vector<T>> operator~(vector<vector<T>> vec) {
vector<vector<T>> v;
int n = vec.size(), m = vec[0].size();
v.resize(m);
for (int i = 0; i < m; i++) {
v[i].resize(n);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
v[i][j] = vec[j][i];
}
}
return v;
}
void print0x(int x) {
std::vector<int> vec;
while (x) {
vec.push_back(x & 1);
x >>= 1;
}
std::reverse(vec.begin(), vec.end());
for (int i = 0; i < (int)vec.size(); i++) {
std::cout << vec[i];
}
std::cout << ' ';
}
template <typename T>
void print0x(T x, int len) {
std::vector<int> vec;
while (x) {
vec.push_back(x & 1);
x >>= 1;
}
reverse(vec.begin(), vec.end());
for (int i = vec.size(); i < len; i++) {
putchar('0');
}
for (int i = 0; i < vec.size(); i++) {
std::cout << vec[i];
}
std::cout << ' ';
}
using namespace std;
int n;
int q;
tuple<int, int, char> data[200005];
struct SegmentTreeMaxUpdate {
int data[524288 + 5];
int tag[524288 + 5];
int nn;
int size() { return nn; }
void init(int size) {
nn = 1;
while (nn < size) {
nn <<= 1;
}
for (int i = 0; i <= nn * 2; i++) {
data[i] = -1;
tag[i] = -1;
}
}
void pushdown(int x) {
if (x >= nn) return;
data[x * 2] = max(data[x * 2], tag[x]);
data[x * 2 + 1] = max(data[x * 2 + 1], tag[x]);
tag[x * 2] = max(tag[x * 2], tag[x]);
tag[x * 2 + 1] = max(tag[x * 2 + 1], tag[x]);
tag[x] = -1;
}
void update(int ql, int qr, int value) { update(1, 0, nn, ql, qr, value); }
void update(int pos, int value) { update(pos, pos + 1, value); }
void update(int x, int l, int r, int ql, int qr, int value) {
if (l >= qr || r <= ql) return;
pushdown(x);
if (l >= ql && r <= qr) {
data[x] = max(data[x], value);
tag[x] = max(tag[x], value);
return;
}
update(x * 2, l, l + r >> 1, ql, qr, value);
update(x * 2 + 1, l + r >> 1, r, ql, qr, value);
data[x] = max(data[x * 2], data[x * 2 + 1]);
}
inline int query(int ql, int qr) { return query(1, 0, nn, ql, qr); }
inline int query(int pos) { return query(pos, pos + 1); }
int query(int x, int l, int r, int ql, int qr) {
if (l >= qr || r <= ql) return -1;
pushdown(x);
if (l >= ql && r <= qr) return data[x];
return max(query(x * 2, l, (l + r) >> 1, ql, qr),
query(x * 2 + 1, (l + r) >> 1, r, ql, qr));
}
} sgtR, sgtC;
vector<int> lshRow, lshCol;
int main() {
cin >> n >> q;
sgtR.init(q);
sgtC.init(q);
for (int i = 0; i < q; i++) {
int x, y;
char c;
scanf("%d %d %c", &x, &y, &c);
swap(x, y);
x--;
y--;
lshRow.push_back(x);
lshCol.push_back(y);
data[i] = make_tuple(x, y, c);
}
sort(lshRow.begin(), lshRow.end());
sort(lshCol.begin(), lshCol.end());
lshRow.erase(unique(lshRow.begin(), lshRow.end()), lshRow.end());
lshCol.erase(unique(lshCol.begin(), lshCol.end()), lshCol.end());
for (int i = 0; i < q; i++) {
int x, y;
char c;
tie(x, y, c) = data[i];
x = lower_bound(lshRow.begin(), lshRow.end(), x) - lshRow.begin();
y = lower_bound(lshCol.begin(), lshCol.end(), y) - lshCol.begin();
if (c == 'U') {
int row = sgtC.query(y);
printf("%d\n", lshRow[x] - (row == -1 ? -1 : lshRow[row]));
sgtR.update(row, x + 1, y);
sgtC.update(y, x);
} else {
int col = sgtR.query(x);
printf("%d\n", lshCol[y] - (col == -1 ? -1 : lshCol[col]));
sgtC.update(col, y + 1, x);
sgtR.update(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, m;
map<int, int> u, l;
int update_u(int x, int y) {
int ans = y;
auto it = u.lower_bound(x);
if (it->first == x) {
return 0;
}
ans = min(ans, it->second + it->first - x);
auto it2 = l.lower_bound(x);
if (it2->first < it->first) ans = min(ans, it2->first - x);
u[x] = ans;
return ans;
}
int update_l(int x, int y) {
int ans = x;
auto it = l.upper_bound(x);
--it;
if (it->first == x) {
return 0;
}
ans = min(ans, it->second + x - it->first);
auto it2 = u.upper_bound(x);
it2--;
if (it2->first > it->first) ans = min(ans, x - it2->first);
l[x] = ans;
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
u[0] = n + 1;
u[n + 1] = 0;
l[0] = 0;
l[n + 1] = n + 1;
int x, y;
char c;
while (m--) {
cin >> x >> y >> c;
if (c == 'U') {
cout << (update_u(x, y)) << "\n";
;
} else {
cout << (update_l(x, y)) << "\n";
;
}
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int infint = (long long)1e9 + 7;
const long long inf = (long long)1e18;
const int MAXN = (int)2e5 + 3;
int n, q;
set<int> S;
unordered_map<int, char> C;
unordered_map<int, int> ans;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
set<int> S;
for (int i = 0; i < q; i++) {
int x, y;
char c;
cin >> x >> y >> c;
if (ans.count(x)) {
cout << 0 << "\n";
continue;
}
C[x] = c;
if (c == 'L') {
int last = -1;
if (S.size() == 0 || *S.begin() > x)
;
else {
auto it = S.upper_bound(x);
it--;
last = *it;
}
if (last == -1) {
cout << x << "\n";
ans[x] = x;
S.insert(x);
continue;
}
if (C[last] == 'U') {
cout << x - last << "\n";
ans[x] = x - last;
S.insert(x);
continue;
} else {
cout << ans[last] + x - last << "\n";
ans[x] = ans[last] + x - last;
S.insert(x);
continue;
}
} else {
int last = -1;
if (S.size() == 0 || *S.rbegin() < x)
;
else {
auto it = S.upper_bound(x);
last = *it;
}
if (last == -1) {
cout << y << "\n";
ans[x] = y;
S.insert(x);
continue;
}
if (C[last] == 'L') {
cout << last - x << "\n";
ans[x] = last - x;
S.insert(x);
continue;
} else {
cout << ans[last] + last - x << "\n";
ans[x] = ans[last] + last - x;
S.insert(x);
continue;
}
}
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import sys
from bisect import bisect
def input():
return sys.stdin.readline().strip()
def solve():
n, q = map(int, input().split())
was = set()
Q = [None]*q
all = [0]*(2*q)
for i in range(q):
x, y, t = input().split()
x, y = int(x), int(y)
Q[i] = (x, y, t)
all[2*i] = x
all[2*i+1] = y
all.sort()
V = [0]*(4*q)
H = [0]*(4*q)
for x, y, t in Q:
if (x,y) in was:
print(0)
else:
was.add((x,y))
if t == 'L':
TA = H
TB = V
else:
x, y = y, x
TA = V
TB = H
v = bisect(all, y) - 1 + q + q
r = 0
while v > 0:
r = max(r, TA[v])
v //= 2
c = x - r
print(c)
r = bisect(all, x) - 1 + q + q
l = bisect(all, x - c) + q + q
while l <= r:
if l % 2 == 1:
TB[l] = max(TB[l], y)
if r % 2 == 0:
TB[r] = max(TB[r], y)
l = (l+1)//2
r = (r-1)//2
solve()
| PYTHON |
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;
using pii = pair<int, int>;
int n, q;
set<pii> sv[2];
set<int> cor[2];
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n >> q;
int x, y;
char c;
while (q--) {
int res;
cin >> x >> y >> c;
bool b = c == 'U';
if (!b) swap(x, y);
if (cor[b].count(-x)) {
cout << "0\n";
continue;
}
auto it = sv[b].lower_bound({x, 0});
auto it2 = cor[!b].lower_bound(-y);
res = y;
if (it != sv[b].end()) res = it->first - x + it->second;
if ((it != sv[b].end() && it2 != cor[!b].end() &&
it->first > n + 1 + *it2) ||
(it == sv[b].end() && it2 != cor[!b].end()))
res = n + 1 + *it2 - x;
sv[b].insert({x, res});
cor[b].insert(-x);
cout << res << '\n';
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long n;
long long x, y;
int q;
string ss;
map<long long, long long> shang;
map<long long, long long> zuo;
int main() {
scanf("%lld %d", &n, &q);
for (int i = 1; i <= q; i++) {
cin >> x >> y >> ss;
if (ss[0] == 'U') {
map<long long, long long>::iterator it = shang.lower_bound(x);
if (it->first == x) {
cout << "0" << endl;
continue;
}
if (it == shang.end()) {
cout << y << endl;
shang[x] = 0;
zuo[y] = x;
} else {
cout << y - (it->second) << endl;
shang[x] = it->second;
zuo[y] = x;
}
} else {
map<long long, long long>::iterator it = zuo.lower_bound(y);
if (it->first == y) {
cout << "0" << endl;
continue;
}
if (it == zuo.end()) {
cout << x << endl;
zuo[y] = 0;
shang[x] = y;
} else {
cout << x - (it->second) << endl;
zuo[y] = it->second;
shang[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 maxN = 2 * 1000 * 100 + 10;
vector<int> X, Y;
struct Que {
int x, y;
char ch;
};
Que a[maxN];
typedef int Seg[4 * maxN];
Seg seg[2];
int gg(int p, Seg seg, int xl, int xr, int ind = 1) {
if (xr - xl == 1) return seg[ind];
int xm = (xl + xr) >> 1;
seg[ind * 2] = max(seg[ind * 2], seg[ind]);
seg[ind * 2 + 1] = max(seg[ind * 2 + 1], seg[ind]);
if (p < xm)
return gg(p, seg, xl, xm, ind * 2);
else
return gg(p, seg, xm, xr, ind * 2 + 1);
}
void ss(int ql, int qr, int v, Seg seg, int xl, int xr, int ind = 1) {
if (xr <= ql || qr <= xl) return;
if (ql <= xl && xr <= qr) {
seg[ind] = max(seg[ind], v);
return;
}
int xm = (xl + xr) >> 1;
seg[ind * 2] = max(seg[ind * 2], seg[ind]);
seg[ind * 2 + 1] = max(seg[ind * 2 + 1], seg[ind]);
ss(ql, qr, v, seg, xl, xm, ind * 2);
ss(ql, qr, v, seg, xm, xr, ind * 2 + 1);
}
int usedX[maxN], usedY[maxN];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a[i].y >> a[i].x >> a[i].ch;
X.push_back(a[i].x);
Y.push_back(a[i].y);
}
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end()) - X.begin());
sort(Y.begin(), Y.end());
Y.resize(unique(Y.begin(), Y.end()) - Y.begin());
for (int i = 0; i < m; i++) {
int x = lower_bound(X.begin(), X.end(), a[i].x) - X.begin();
int y = lower_bound(Y.begin(), Y.end(), a[i].y) - Y.begin();
if (a[i].ch == 'U') {
if (usedY[y]) {
cout << 0 << '\n';
continue;
}
usedY[y] = true;
int v = gg(y, seg[1], 0, Y.size());
int k = lower_bound(X.begin(), X.end(), v) - X.begin();
cout << a[i].x - v << '\n';
ss(k, x + 1, a[i].y, seg[0], 0, X.size());
} else {
if (usedX[x]) {
cout << 0 << '\n';
continue;
}
usedX[x] = true;
int v = gg(x, seg[0], 0, X.size());
int k = lower_bound(Y.begin(), Y.end(), v) - Y.begin();
cout << a[i].y - v << '\n';
ss(k, y + 1, a[i].x, seg[1], 0, Y.size());
}
}
}
| 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 TREE = 1002000;
vector<int> xx;
vector<int> yy;
int n, m;
int x[TREE], y[TREE];
char o[TREE];
map<int, bool> ux;
map<int, bool> uy;
int tx[TREE * 5], ty[TREE * 5];
map<int, int> mx;
map<int, int> my;
int getx(int v, int l, int r, int pos) {
if (pos > r || pos < l) return 0;
int ans = tx[v];
if (l == r) return ans;
int m = (l + r) / 2;
int f1 = getx(v + v, l, m, pos);
int f2 = getx(v + v + 1, m + 1, r, pos);
f1 = max(f1, f2);
return max(f1, ans);
}
int gety(int v, int l, int r, int pos) {
if (pos > r || pos < l) return 0;
int ans = ty[v];
if (l == r) return ans;
int m = (l + r) / 2;
int f1 = gety(v + v, l, m, pos);
int f2 = gety(v + v + 1, m + 1, r, pos);
f1 = max(f1, f2);
return max(f1, ans);
}
void updatey(int v, int l, int r, int ll, int rr, int val) {
if (max(l, ll) > min(r, rr)) return;
if (ll <= l && r <= rr) {
ty[v] = max(ty[v], val);
return;
}
int m = (l + r) / 2;
updatey(v + v, l, m, ll, rr, val);
updatey(v + v + 1, m + 1, r, ll, rr, val);
}
void updatex(int v, int l, int r, int ll, int rr, int val) {
if (max(l, ll) > min(r, rr)) return;
if (ll <= l && r <= rr) {
tx[v] = max(tx[v], val);
return;
}
int m = (l + r) / 2;
updatex(v + v, l, m, ll, rr, val);
updatex(v + v + 1, m + 1, r, ll, rr, val);
}
void updx(int l, int r, int val) {
l = mx[l], r = mx[r];
updatex(1, 0, TREE, l, r, val);
}
void updy(int l, int r, int val) {
l = my[l], r = my[r];
updatey(1, 0, TREE, l, r, val);
}
int getx(int val) {
val = mx[val];
return getx(1, 0, TREE, val);
}
int gety(int val) {
val = my[val];
return gety(1, 0, TREE, val);
}
int main() {
ios::sync_with_stdio(false);
int q;
cin >> n >> q;
xx.push_back(0);
yy.push_back(0);
for (int i = 0; i < q; ++i) {
cin >> x[i] >> y[i] >> o[i];
xx.push_back(x[i]);
yy.push_back(y[i]);
}
int cnt;
sort(xx.begin(), xx.end());
cnt = 0;
for (int i = 0; i < xx.size(); ++i) {
if (i == 0 || xx[i] != xx[i - 1]) {
mx[xx[i]] = ++cnt;
}
}
sort(yy.begin(), yy.end());
cnt = 0;
for (int i = 0; i < yy.size(); ++i) {
if (i == 0 || yy[i] != yy[i - 1]) {
my[yy[i]] = ++cnt;
}
}
for (int i = 0; i < q; ++i) {
if (o[i] == 'U') {
if (ux[x[i]]) {
cout << 0 << endl;
continue;
}
ux[x[i]] = true;
int val = getx(x[i]);
cout << y[i] - val << endl;
updy(val, y[i], x[i]);
continue;
} else {
if (uy[y[i]]) {
cout << 0 << endl;
continue;
}
uy[y[i]] = true;
int val = gety(y[i]);
cout << x[i] - val << endl;
updx(val, 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;
const int MOD = 1000000007;
const double EXP = 2.7182818284590452;
const double Pi = 3.14159265;
const double EPS = 1e-13;
const int INF = 1000 * 1000 * 1000;
const long long INFL = (long long)INF * (long long)INF;
int gcd(int a, int b) {
if (a < b) swap(a, b);
while (b != 0) {
a %= b;
swap(a, b);
}
return a;
}
long long poww(long long v, long long p) {
if (p == 0) return 1;
if (p & 1) {
return (poww(v, p - 1) * v) % MOD;
} else {
long long t = poww(v, p / 2);
return (t * t) % MOD;
}
}
int n, q;
int row, col, t;
char c;
map<int, pair<char, int> > arr;
map<int, pair<char, int> >::iterator iter;
void accept() {
cin >> n >> q;
arr[0] = std::make_pair('U', 0);
arr[n + 1] = std::make_pair('L', 0);
for (int i = (0); i < (q); ++i) {
scanf("%d%d%c%c", &col, &row, &c, &c);
iter = arr.lower_bound(col);
if (iter->first == col) {
printf("0\n");
continue;
}
if (c == 'L') --iter;
t = ((iter->first - col > 0) ? iter->first - col : -(iter->first - col));
if (iter->second.first == c) t += iter->second.second;
printf("%d\n", t);
arr[col] = std::make_pair(c, t);
}
}
int main(void) {
accept();
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.io.*;
import java.util.*;
public class Solve {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
}
class PairFull<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<PairFull<U, V>> {
public U first;
public V second;
public PairFull(U u, V v) {
this.first = u;
this.second = v;
}
public int hashCode() {
return (first == null ? 0 : first.hashCode() * 31) + (second == null ? 0 : second.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
PairFull<U, V> p = (PairFull<U, V>) o;
return (first == null ? p.first == null : first.equals(p.first)) && (second == null ? p.second == null : second.equals(p.second));
}
public int compareTo(PairFull<U, V> b) {
int cmpU = first.compareTo(b.first);
return cmpU != 0 ? cmpU : second.compareTo(b.second);
}
}
class Pair extends PairFull<Long, Long> {
public Pair(Long u, Long v) {
super(u, v);
}
}
class Task {
public void solve(InputReader in, PrintWriter out) {
long n = in.nextLong(), q = in.nextLong();
TreeMap<Long, Pair> mp = new TreeMap<>();
HashSet<Long> st = new HashSet<>();
mp.put(n, new Pair(0l, n + 1));
for(long i = 0; i < q; i++) {
long x = in.nextLong(), y = in.nextLong();
String s = in.next();
if(st.contains(x)) {
out.println(0);
continue;
}
st.add(x);
Map.Entry<Long, Pair> it = mp.higherEntry(x - 1);
//Long it = mp.lowerKey(x);
Pair b = it.getValue();
if(s.equals("L")) {
long x2 = b.first;
out.println(x - x2);
mp.put(x, new Pair(b.first, x));
} else {
long x2 = b.second;
out.println(x2 - x);
//mp.put(it, new Pair(x, b.second));
//it.setValue(new Pair(x, b.second));
mp.put(it.getKey(), new Pair(x, b.second));
mp.put(x, b);
}
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public double nextFloat() {
return Float.parseFloat(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) {
S = _S;
arr = vector<node>(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>
using namespace std;
int n, q;
map<int, int> u;
map<int, int> l;
int main() {
int x, y;
char op[5];
cin >> n >> q;
while (q--) {
scanf("%d%d%s", &x, &y, op);
map<int, int>::iterator p;
if (op[0] == 'U') {
int ans = 0;
p = u.lower_bound(x);
if (u.count(x)) {
printf("0\n");
continue;
}
if (p == u.end()) {
ans = y;
} else {
ans = y - (p->second);
}
printf("%d\n", ans);
l[y] = x;
u[x] = y - ans;
} else {
int ans = 0;
p = l.lower_bound(y);
if (l.count(y)) {
printf("0\n");
continue;
}
if (p == l.end()) {
ans = x;
} else {
ans = x - (p->second);
}
printf("%d\n", ans);
l[y] = x - ans;
u[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 | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
/**
*
* @author Saju
*
*/
public class Main {
static int[] dx = { 0, 1, 0, -1 };
static int[] dy = { -1, 0, 1, 0 };
static int[] ddx = { 0, 1, 0, -1, -1, 1, 1, -1 };
static int[] ddy = { -1, 0, 1, 0, -1, -1, 1, 1 };
static int[] kx = { 2, 1, -1, -2, -2, -1, 1, 2 };
static int[] ky = { 1, 2, 2, 1, -1, -2, -2, -1 };
static long MOD = Long.MAX_VALUE;
static final int MAX = 50000;
static long INF = Long.MAX_VALUE;
static double PI = 3.1415926535;
private static final double EPSILON = 1e-10;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
/*
*/
int n = in.nextInt();
int k = in.nextInt();
TreeSet<Integer> record = new TreeSet<>();
TreeMap<Integer, Point> points = new TreeMap<>();
points.put(n + 1, new Point(0, 0));
for(int kk = 1; kk <= k; kk++){
int x = in.nextInt();
int y = in.nextInt();
char ch = in.next().charAt(0);
if(record.contains(x)){
System.out.println("0");
continue;
}
record.add(x);
Point tallPoint = points.get(points.higherKey(x));
// System.out.println(points.higherKey(x) + " " + tallPoint);
if(ch == 'U'){
System.out.println(y - tallPoint.y);
points.put(x, new Point(tallPoint.x, tallPoint.y));
tallPoint.x = x;
}
else{
System.out.println(x - tallPoint.x);
points.put(x, new Point(tallPoint.x, y));
}
}
System.exit(0);
}
private static class Point{
int x;
int y;
Point(int x, int y){
this.x = x;
this.y = y;
}
@Override
public String toString() {
return this.x + " " + this.y;
}
}
// nCk
// private static long binomialCoeff(int n, int k){
// long[] arr = new long[k + 1];
// arr[0] = 1;
// for(int i = 1; i <= n; i++){
// for(int j = Math.min(i, k); j > 0; j--){
// arr[j] = (arr[j] + arr[j - 1]) % MOD;
// }
// }
// return arr[k];
// }
// BIT =>
// https://www.hackerearth.com/practice/notes/binary-indexed-tree-or-fenwick-tree/
// static int BIT[] = new int[MAX + 5];
//
// private static int query(int index) {
// int sum = 0;
// for (; index > 0; index -= (index & (-index))) {
// sum += BIT[index];
// }
// return sum;
// }
//
// private static void update(int size, int index, int val) {
// for (; index <= size; index += (index & (-index))) {
// BIT[index] += val;
// }
// }
/*
*
* tutorial :
* https://helloacm.com/algorithms-series-0-1-backpack-dynamic-programming-
* and-backtracking/
*
*/
// here arr and prob zero based index
// static int backPack(int n, int m, int[] arr, double[] prob, double p) {
// double dp[][] = new double[n + 1][m + 1];
// for (int i = 0; i <= n; ++i) {
// dp[i][0] = 1.0;
// }
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= m; j++) {
//
// if (j < arr[i - 1]) { // insufficient capacity
// dp[i][j] = dp[i - 1][j];
// } else {
// dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - arr[i - 1]] * (1.0 -
// prob[i - 1]));
// }
//
// }
// }
// for (int i = m; i >= 0; i--) { // reverse checking the maximum weight
// if ((1.0 - dp[n][i]) <= p) {
// return i;
// }
// }
// return 0;
// }
// here arr and prob one based index
// static int backPack1(int n, int m, int[] arr, double[] prob, double p) {
//
// double dp[] = new double[m + 1];
//
// dp[0] = 1.0;
// for (int i = 1; i <= n; i++) {
// for (int j = m; j >= arr[i]; --j) {
// // dp[j] = dp[j] || dp[j - A[i - 1]];
// dp[j] = Math.max(dp[j], dp[j - arr[i]] * (1.0 - prob[i]));
// }
// }
// for (int i = m; i >= 0; i--) {
// if (1.0 - dp[i] <= p) {
// return i;
// }
// }
// return 0;
// }
/*
*
* tutorial :
* https://www.hackerearth.com/practice/algorithms/searching/ternary-search/
* tutorial/
*
*/
// lightoj -> 1146
// static double ternary(Point a, Point b, Point c, Point d) {
//
// double l = 0;
// double r = 1;
//
// for (int i = 0; i < 200; i++) {
// double mid1 = l + ((r - l) / 3);
// double mid2 = r - ((r - l) / 3);
//
// double dis1 = func(a, b, c, d, mid1);
// double dis2 = func(a, b, c, d, mid2);
//
// if(dis1 > dis2){
// l = mid1;
//
// }
// else{
// r = mid2;
// }
// }
//
// return l;
//
// }
//
// static double func(Point a, Point b, Point c, Point d, double multiple) {
//
// double xDifAB = b.x - a.x;
// double yDifAB = b.y - a.y;
// double xDifCD = d.x - c.x;
// double yDifCD = d.y - c.y;
//
// double x1 = a.x + xDifAB * multiple;
// double y1 = a.y + yDifAB * multiple;
// double x2 = c.x + xDifCD * multiple;
// double y2 = c.y + yDifCD * multiple;
//
// double distance = Math.sqrt(((x1 - x2) * (x1 - x2)) +
// ((y1 - y2) * (y1 - y2)));
//
// return distance;
// }
//
// static class Point{
// double x;
// double y;
//
// Point(double x, double y){
// this.x = x;
// this.y = y;
// }
// }
// static void primeFactorization(int n) {
// int temp = n;
// Map<Integer, Integer> map = new HashMap<Integer, Integer>();
// for (int i = 2; i <= Math.sqrt(n); i++) {
// if (n % i == 0) {
// int count = 0;
// while (n % i == 0) {
// count++;
// n = n / i;
// }
// System.out.println("i: " + i + ", count: " + count);
// map.put(i, count);
// }
//
// }
// if (n != 1) {
// System.out.println(n);
// map.put(n, 1);
// }
//
//// calculateSumOfDivisor(map, temp);
// }
// static int counter[] = new int[MAX];
//
// private static void calculateSumOfDivisor(Map<Integer, Integer> map, int
// n) {
// int sum = 1;
// for(Integer key : map.keySet()){
// int count = map.get(key);
// sum *= ((Math.pow(key, count + 1) - 1) / (key - 1));
// }
// if(sum < MAX){
// if(counter[sum] < n){
//// System.out.println("H");
// counter[sum] = n;
// }
//
// }
//
//// System.out.println(sum);
// }
// static int phi[] = new int[MAX];
// static int phiStepCount[] = new int[MAX];
// static void computeTotient() {
//
// // Create and initialize an array to store
// // phi or totient values
// for (int i = 1; i < MAX; i++) {
// phi[i] = i; // indicates not evaluated yet
// // and initializes for product
// // formula.
// }
//
// // Compute other Phi values
// for (int p = 2; p < MAX; p++) {
//
// // If phi[p] is not computed already,
// // then number p is prime
// if (phi[p] == p) {
//
// // Phi of a prime number p is
// // always equal to p-1.
// phi[p] = p - 1;
//
// // Update phi values of all
// // multiples of p
// for (int i = 2 * p; i < MAX; i += p) {
//
// // Add contribution of p to its
// // multiple i by multiplying with
// // (1 - 1/p)
// phi[i] = (phi[i] / p) * (p - 1);
// }
//
// // for (int i = p; i < MAX; i += p) {
// //
// // phi[i] -= (phi[i] / p);
// // }
//
// }
// phiStepCount[p] = phiStepCount[phi[p]] + 1;
// }
//
// for(int i = 1; i < MAX; i++){
// phiStepCount[i] += phiStepCount[i - 1];
// }
// }
// public static BigInteger floorOfNthRoot(BigInteger x, int n) {
// int sign = x.signum();
// if (n <= 0 || (sign < 0))
// throw new IllegalArgumentException();
// if (sign == 0)
// return BigInteger.ZERO;
// if (n == 1)
// return x;
// BigInteger a;
// BigInteger bigN = BigInteger.valueOf(n);
// BigInteger bigNMinusOne = BigInteger.valueOf(n - 1);
// BigInteger b = BigInteger.ZERO.setBit(1 + x.bitLength() / n);
// do {
// a = b;
// b = a.multiply(bigNMinusOne).add(x.divide(a.pow(n - 1))).divide(bigN);
// } while (b.compareTo(a) == -1);
// return a;
// }
// O(log(max(A, B))).
// static long gcd(long a, long b) {
// if (b == 0)
// return a;
// return gcd(b, a % b);
// }
private static double getDistance(double x1, double y1, double x2, double y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
// private static int getDecimal(String str) {
// int val = 0;
//
// for (int i = str.length() - 1, j = 0; i >= 0; i--, j++) {
// if (str.charAt(i) == '1') {
// val += Math.pow(2, j);
// }
// }
// return val;
// }
// private static int call(int i, int j, int[][] colors) {
//
// if (i >= colors.length) {
// return 0;
// }
//
// if (dp[i][j] != -1) {
// return dp[i][j];
//
// }
//
// int result = Integer.MAX_VALUE;
//
// for (int k = 0; k < 3; k++) {
// if (k == j) {
// continue;
// } else {
// result = Math.min(result, call(i + 1, k, colors) + colors[i][j]);
// }
// }
//
// return dp[i][j] = result;
// }
static int dp[][];
// private static BigInteger fac(int n, int mod) {
//
// if (n <= 0) {
// return BigInteger.ONE;
// }
// return BigInteger.valueOf(n).multiply(fac(n - 1, mod));
// }
// private static long modInverse(long n, long m){
// return bigMod(n, m - 2, m);
// }
private static long bigMod(long n, long k, long m) {
long ans = 1;
while (k > 0) {
if ((k & 1) == 1) {
ans = (ans * n) % m;
}
n = (n * n) % m;
k >>= 1;
}
return ans;
}
// Returns an iterator pointing to the first element
// in the range [first, last] which does not compare less than val.
// private static int lowerBoundNew(long[] arr, long num){
// int start = 0;
// int end = arr.length - 1;
// int index = 0;
// int len = arr.length;
// int mid = 0;
// while(true){
// if(start > end){
// break;
// }
// mid = (start + end) / 2;
// if(arr[mid] > num){
// end = mid - 1;
// }
// else if(arr[mid] < num){
// start = mid + 1;
// }
// else{
// while(mid >= 0 && arr[mid] == num){
// mid--;
// }
// return mid + 1;
// }
// }
// if(arr[mid] < num){
// return mid + 1;
// }
// return mid;
// }
// upper_bound() is a standard library function
// in C++ defined in the header .
// It returns an iterator pointing to
// the first element in the range [first, last)
// that is greater than value, or last if no such element is found
// private static int upperBoundNew(long[] arr, long num){
//
// int start = 0;
// int end = arr.length - 1;
// int index = 0;
// int len = arr.length;
// int mid = 0;
// while(true){
// if(start > end){
// break;
// }
// mid = (start + end) / 2;
// if(arr[mid] > num){
// end = mid - 1;
// }
// else if(arr[mid] < num){
// start = mid + 1;
// }
// else{
// while(mid < len && arr[mid] == num){
// mid++;
// }
// if(mid == len - 1 && arr[mid] == num){
// return mid + 1;
// }
// else{
// return mid;
// }
// }
// }
// if(arr[mid] < num){
// return mid + 1;
// }
// return mid;
// }
// private static int upperBound(long[] arr, long num) {
//
// int start = 0;
// int end = arr.length;
//
// int mid = 0;
// int index = 0;
// while (true) {
//
//// System.out.println(start + " " + end);
// if (start > end) {
// break;
// }
// mid = (start + end) / 2;
// if (arr[mid] > num) {
// end = mid - 1;
//
// } else if (arr[mid] < num) {
// start = mid + 1;
// } else {
// return mid;
// }
//
//// System.out.println("a: " + start + " " + end);
// }
//
// // System.out.println(mid);
// if (arr[mid] < num) {
// index = mid + 1;
// } else {
// index = mid;
// }
// return index;
// }
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
return null;
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
}
}
| 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 = 400010;
struct TREE {
int l, r;
int val, mark;
int mid() { return (l + r) >> 1; }
} tree[2][MAXN << 2];
void pushdown(int k, int op) {
if (tree[op][k].mark) {
tree[op][(k << 1)].val = max(tree[op][(k << 1)].val, tree[op][k].mark);
tree[op][(k << 1 | 1)].val =
max(tree[op][(k << 1 | 1)].val, tree[op][k].mark);
tree[op][(k << 1)].mark = max(tree[op][(k << 1)].mark, tree[op][k].mark);
tree[op][(k << 1 | 1)].mark =
max(tree[op][k].mark, tree[op][(k << 1 | 1)].mark);
tree[op][k].mark = 0;
}
}
void build(int l, int r, int k, int op) {
tree[op][k].l = l;
tree[op][k].r = r;
tree[op][k].val = 0;
tree[op][k].mark = 0;
if (l == r) return;
int mid = tree[op][k].mid();
build(l, mid, (k << 1), op);
build(mid + 1, r, (k << 1 | 1), op);
}
void update(int l, int r, int k, int val, int op) {
if (tree[op][k].l >= l && tree[op][k].r <= r) {
tree[op][k].mark = max(tree[op][k].mark, val);
tree[op][k].val = max(tree[op][k].val, val);
return;
}
pushdown(k, op);
int mid = tree[op][k].mid();
if (r <= mid)
update(l, r, (k << 1), val, op);
else if (l > mid)
update(l, r, (k << 1 | 1), val, op);
else {
update(l, mid, (k << 1), val, op);
update(mid + 1, r, (k << 1 | 1), val, op);
}
}
int query(int pos, int k, int op) {
if (tree[op][k].l == tree[op][k].r) return tree[op][k].val;
pushdown(k, op);
int mid = tree[op][k].mid();
if (pos <= mid)
return query(pos, (k << 1), op);
else
return query(pos, (k << 1 | 1), op);
}
int vec[MAXN], idx;
int HASH(int val) {
int l = 0, r = idx;
while (l <= r) {
int mid = (l + r) >> 1;
if (vec[mid] == val) return mid;
if (vec[mid] < val)
l = mid + 1;
else
r = mid - 1;
}
return l;
}
int n, m;
int x[MAXN / 2], y[MAXN / 2], op[MAXN / 2];
void solve() {
int i;
int cnt = 0;
for (i = 0; i < m; i++) {
char str[2];
scanf("%d%d%s", &x[i], &y[i], str);
vec[++cnt] = x[i];
vec[++cnt] = y[i];
if (str[0] == 'U')
op[i] = 0;
else
op[i] = 1;
}
sort(vec + 1, vec + cnt + 1);
idx = 1;
for (i = 2; i <= cnt; i++)
if (vec[idx] != vec[i]) vec[++idx] = vec[i];
build(1, idx, 1, 0);
build(1, idx, 1, 1);
for (i = 0; i < m; i++) {
int ans = 0;
if (i == 3) i = 3;
if (op[i] == 0) {
int res = query(HASH(x[i]), 1, 0);
ans += y[i] - res;
if (ans) {
int temp = HASH(x[i]);
update(temp, temp, 1, y[i], 0);
int a = HASH(res);
update(a + 1, HASH(y[i]), 1, x[i], 1);
}
} else {
int res = query(HASH(y[i]), 1, 1);
ans += x[i] - res;
if (ans) {
int temp = HASH(y[i]);
update(temp, temp, 1, x[i], 1);
update(HASH(res) + 1, HASH(x[i]), 1, y[i], 0);
}
}
printf("%d\n", ans);
}
}
int main() {
while (scanf("%d%d", &n, &m) == 2) 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;
int n, m, i, j, x, y, s[5], an[200010];
set<pair<int, int> > st1, st2;
set<pair<int, int> >::iterator it, it1;
int main() {
scanf("%d%d", &n, &m);
st1.insert(make_pair(0, 0));
st2.insert(make_pair(0, 0));
st1.insert(make_pair(n + 1, m + 1));
st2.insert(make_pair(n + 1, m + 1));
for (i = 1; i <= m; i++) {
scanf("%d%d%s", &x, &y, s + 1);
if (s[1] == 'U') {
it = st1.lower_bound(make_pair(x, 0));
it1 = st2.lower_bound(make_pair(x, 0));
if ((*it).first == x || (*it1).first == x) {
puts("0");
continue;
}
if ((*it).first < (*it1).first)
an[i] = an[(*it).second];
else
an[i] = (*it1).first;
printf("%d\n", an[i] - x);
st1.insert(make_pair(x, i));
} else {
it = --st2.upper_bound(make_pair(x, m + 1));
it1 = --st1.upper_bound(make_pair(x, m + 1));
if ((*it).first == x || (*it1).first == x) {
puts("0");
continue;
}
if ((*it).first > (*it1).first)
an[i] = an[(*it).second];
else
an[i] = (*it1).first;
printf("%d\n", x - an[i]);
st2.insert(make_pair(x, 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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
/**
* Created by David Zeng on 9/13/2015.
*/
public class Problem555C {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int size = Integer.parseInt(st.nextToken());
int numAct = Integer.parseInt(st.nextToken());
maxSegtree upMov = new maxSegtree();
maxSegtree leftMov = new maxSegtree();
HashMap<Integer, Integer> compress = new HashMap<Integer, Integer>();
int[] reverse = new int[400002];
int[][] commands = new int[numAct][3];
int[] coords = new int[numAct * 2 + 1];
for (int i = 0; i < numAct; i++) {
st = new StringTokenizer(in.readLine());
commands[i][0] = Integer.parseInt(st.nextToken());
commands[i][1] = Integer.parseInt(st.nextToken());
coords[i * 2] = commands[i][0];
coords[i * 2 + 1] = commands[i][1];
char ul = st.nextToken().charAt(0);
commands[i][2] = 0;
if (ul == 'U') {
commands[i][2] = 1;
}
}
Arrays.sort(coords);
int curAdd = 0;
for (int i = 0; i < coords.length; i++) {
if (i == 0 || coords[i] != coords[i - 1]) {
compress.put(coords[i], curAdd);
reverse[curAdd] = coords[i];
curAdd++;
}
}
upMov.build(1, 0, curAdd);
leftMov.build(1, 0, curAdd);
boolean[] xinvalid = new boolean[400002];
for (int i = 0; i < numAct; i++) {
if(xinvalid[compress.get(commands[i][0])]){
System.out.println(0);
continue;
}
if (commands[i][2] == 1) {
int conv = compress.get(commands[i][0]);
int conv2 = compress.get(commands[i][1]);
int stop = upMov.query(1, 0, curAdd, conv, conv);
int actualStop = reverse[stop];
System.out.println(commands[i][1] - actualStop);
leftMov.update(1,0,curAdd,stop,conv2,conv);
} else{
int conv = compress.get(commands[i][1]);
int conv2 = compress.get(commands[i][0]);
int stop = leftMov.query(1, 0, curAdd, conv, conv);
int actualStop = reverse[stop];
System.out.println(commands[i][0] - actualStop);
upMov.update(1,0,curAdd,stop,conv2,conv);
}
xinvalid[compress.get(commands[i][0])] = true;
}
}
public static class maxSegtree {
int[] tree = new int[1100000];
int[] lazyProp = new int[1100000];
public void build(int node, int a, int b) {
if (b < a) {
return;
}
if (a == b) {
tree[node] = 0;
return;
}
int mid = (a + b) / 2;
build(node * 2, a, mid);
build(node * 2 + 1, mid + 1, b);
}
public void update(int node, int a, int b, int left, int right, int change) {
if (lazyProp[node] != 0) {
tree[node] = Math.max(tree[node], lazyProp[node]);
if (a != b) {
lazyProp[node * 2] = Math.max(lazyProp[node * 2], lazyProp[node]);
lazyProp[node * 2 + 1] = Math.max(lazyProp[node * 2 + 1], lazyProp[node]);
}
lazyProp[node] = 0;
}
if (b < a || a > right || b < left) {
return;
}
if (a >= left && b <= right) {
tree[node] = Math.max(tree[node], change);
if (a != b) {
lazyProp[node * 2] = Math.max(lazyProp[node * 2], tree[node]);
lazyProp[node * 2 + 1] = Math.max(lazyProp[node * 2 + 1], tree[node]);
}
return;
}
int mid = (a + b) / 2;
update(node * 2, a, mid, left, right, change);
update(node * 2 + 1, mid + 1, b, left, right, change);
}
public int query(int node, int a, int b, int left, int right) {
if (lazyProp[node] != 0) {
tree[node] = Math.max(tree[node], lazyProp[node]);
if (a != b) {
lazyProp[node * 2] = Math.max(lazyProp[node * 2], lazyProp[node]);
lazyProp[node * 2 + 1] = Math.max(lazyProp[node * 2 + 1], lazyProp[node]);
}
lazyProp[node] = 0;
}
if (b < a || a > right || b < left) {
return Integer.MIN_VALUE;
}
if (a >= left && b <= right) {
return tree[node];
}
int mid = (a + b) / 2;
int c = query(node * 2, a, mid, left, right);
int d = query(node * 2 + 1, mid + 1, b, left, right);
return Math.max(c, d);
}
}
}
| 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 = 1e6 + 69, MOD = 1e9 + 7;
int n, q, ans;
unordered_map<int, bool> mark;
map<int, pair<int, int>> aXis;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
int l, r, type;
char ip;
aXis[0] = {-1, 0}, aXis[n + 1] = {+1, 0};
for (int i = 0; i < q; i++) {
cin >> l >> r >> ip;
type = (ip == 'U' ? +1 : -1);
auto closest = aXis.lower_bound(l);
if (type == -1) closest--;
ans = abs((*closest).first - l);
if (type == (*closest).second.first) ans += (*closest).second.second;
cout << (mark[l] == true ? 0 : ans) << "\n";
if (mark[l] == false) aXis[l] = {type, ans}, mark[l] = 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;
inline void rd(int &x) {
x = 0;
char f = 1, o;
while (o = getchar(), o < 48)
if (o == 45) f = -f;
do x = (x << 3) + (x << 1) + (o ^ 48);
while (o = getchar(), o > 47);
x *= f;
}
char R[200005];
int X[200005], Y[200005], n, m, Q;
int A[200005], Tx[200005], Ty[200005], mark[200005];
int mxx[200005 << 2], mxy[200005 << 2];
void update(int *mx, int a, int b, int v, int l = 1, int r = m, int p = 1) {
if (l > b || r < a) return;
if (l == r || l >= a && r <= b) {
mx[p] = max(mx[p], v);
return;
}
mx[p << 1] = max(mx[p << 1], mx[p]);
mx[p << 1 | 1] = max(mx[p << 1 | 1], mx[p]);
int mid = l + r >> 1;
update(mx, a, b, v, l, mid, p << 1);
update(mx, a, b, v, mid + 1, r, p << 1 | 1);
}
int query(int *mx, int x, int l = 1, int r = m, int p = 1) {
if (l > x || r < x) return 0;
if (l == r) return mx[p];
mx[p << 1] = max(mx[p << 1], mx[p]);
mx[p << 1 | 1] = max(mx[p << 1 | 1], mx[p]);
int mid = l + r >> 1;
return query(mx, x, l, mid, p << 1) + query(mx, x, mid + 1, r, p << 1 | 1);
}
int main() {
rd(n), rd(Q);
for (int i = 0; i < Q; i++) {
rd(X[i]);
rd(Y[i]);
scanf("%c", &R[i]);
A[i] = X[i];
}
sort(A, A + Q);
m = unique(A, A + Q) - A;
for (int i = 0; i < Q; i++) {
int k = lower_bound(A, A + m, X[i]) - A + 1;
Tx[k] = X[i];
Ty[m + 1 - k] = Y[i];
X[i] = k;
Y[i] = m + 1 - k;
}
for (int i = 0; i < Q; i++) {
if (mark[X[i]]) {
puts("0");
continue;
}
mark[X[i]] = 1;
if (R[i] == 'U') {
int k = query(mxx, X[i]);
printf("%d\n", Ty[Y[i]] - Ty[k]);
update(mxy, k + 1, Y[i], X[i]);
} else {
int k = query(mxy, Y[i]);
printf("%d\n", Tx[X[i]] - Tx[k]);
update(mxx, k + 1, 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;
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 + 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 = 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;
vector<int> pts;
template <int N>
struct stree {
int data[N];
int lazy[N];
int BVAL;
function<int(int, int)> op;
void getVals(int& pos, int& lchild, int& rchild, int& lnode, int& rnode,
int& len, int& lb, int& rb) {
len = (pos & -pos);
lchild = (pos - len) / 2;
rchild = lchild + len / 2;
lnode = lchild * 2 + len / 2;
rnode = rchild * 2 + len / 2;
lb = pts[min(lchild, (int)pts.size() - 1)];
rb = pts[min(lchild + len, (int)pts.size() - 1)];
}
void setRange(int l, int r, int val, int pos = N / 2) {
int lchild, rchild, lnode, rnode, len, lb, rb;
getVals(pos, lchild, rchild, lnode, rnode, len, lb, rb);
if (r <= lb || l >= rb) return;
if (l <= lb && rb <= r) {
if (!lazy[pos]) {
lazy[pos] = true;
data[pos] = val;
}
data[pos] = op(data[pos], val);
return;
}
if (lazy[pos]) {
lazy[pos] = false;
if (!lazy[lnode]) {
lazy[lnode] = true;
data[lnode] = data[pos];
}
if (!lazy[rnode]) {
lazy[rnode] = true;
data[rnode] = data[pos];
}
data[lnode] = op(data[lnode], data[pos]);
data[rnode] = op(data[rnode], data[pos]);
}
setRange(l, r, val, lnode);
setRange(l, r, val, rnode);
}
int get(int place, int pos = N / 2) {
int lchild, rchild, lnode, rnode, len, lb, rb;
getVals(pos, lchild, rchild, lnode, rnode, len, lb, rb);
if (place < lb || place >= rb) return BVAL;
int ret = BVAL;
if (lazy[pos]) ret = data[pos];
if (len == 1) return ret;
return op(ret, op(get(place, lnode), get(place, rnode)));
}
};
int imax(int a, int b) { return max(a, b); }
int imin(int a, int b) { return min(a, b); }
stree<(1 << 21)> leftb;
stree<(1 << 21)> upb;
int n, q, p1, p2;
char c;
vector<pair<char, int> > op;
int main() {
cin.sync_with_stdio(false);
cin >> n >> q;
leftb.op = imax;
leftb.BVAL = -1000000010;
upb.op = imin;
upb.BVAL = 1000000010;
pts.push_back(0);
pts.push_back(1);
pts.push_back(n + 1);
for (int i = 0; i < q; i++) {
cin >> p1 >> p2 >> c;
op.push_back(make_pair(c, p1));
pts.push_back(p1);
pts.push_back(p1 + 1);
pts.push_back(p2);
pts.push_back(p2 + 1);
}
sort(pts.begin(), pts.end());
pts.resize(unique(pts.begin(), pts.end()) - pts.begin());
leftb.setRange(1, n + 1, 0);
upb.setRange(1, n + 1, n + 1);
for (int i = 0; i < q; i++) {
p1 = op[i].second;
c = op[i].first;
if (c == 'L') {
int b = leftb.get(p1);
cout << p1 - b << '\n';
upb.setRange(b + 1, p1 + 1, p1);
leftb.setRange(p1, p1 + 1, p1);
}
if (c == 'U') {
int b = upb.get(p1);
cout << b - p1 << '\n';
leftb.setRange(p1, b, p1);
upb.setRange(p1, p1 + 1, p1);
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct node {
node *r, *l;
int v;
node() {
l = NULL;
r = NULL;
v = 0;
}
};
inline void update(node*& cur, int b, int e, int l, int r, int val);
inline int query(node*& cur, int b, int e, int id);
constexpr int MAXN = 1 << 30;
node* root[2];
int n, q;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n >> q;
while (q--) {
int v[2];
char a;
cin >> v[0] >> v[1] >> a;
bool rel = (a == 'L');
bool relax = rel ^ 1;
int ans = query(root[rel], 0, MAXN, v[rel]);
cout << v[relax] - ans << '\n';
update(root[relax], 0, MAXN, ans + 1, v[relax] + 1, v[rel]);
update(root[rel], 0, MAXN, v[rel], v[rel] + 1, v[relax]);
}
}
inline void update(node*& cur, int b, int e, int l, int r, int val) {
if (b >= r || e <= l) return;
if (cur == NULL) cur = new node();
if (l <= b && r >= e) {
cur->v = max(cur->v, val);
} else {
int mid = (b + e) >> 1;
update(cur->l, b, mid, l, r, val);
update(cur->r, mid, e, l, r, val);
}
}
inline int query(node*& cur, int b, int e, int id) {
if (cur == NULL) return 0;
if (b == e - 1)
return cur->v;
else {
int mid = (b + e) >> 1;
if (mid > id)
return max(cur->v, query(cur->l, b, mid, id));
else
return max(cur->v, query(cur->r, mid, e, id));
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | //package acm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
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) {
Tree_update(0, 0, n-1, l, r, v);
}
int get(int x) {
return Tree_get(0, 0, n-1, x);
}
private void Tree_update(int root, int rl, int rr, int l, int r, int v) {
if (l > r) return;
if (l == rl && r == rr) {
arr[root] = Math.max(arr[root], v);
return;
}
int rm = (rl + rr) / 2;
Tree_update(root * 2 + 1, rl, rm, l, Math.min(rm, r), v);
Tree_update(root * 2 + 2, rm+1, rr, Math.max(rm+1,l), r, v);
}
private int Tree_get(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, Tree_get(root * 2 + 1, rl, rm, x));
else return Math.max(res, Tree_get(root * 2 + 2, rm+1, rr, x));
}
}
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 i = 0; i < q; ++i) {
qx[i] = in.nextInt() - 1;
qy[i] = in.nextInt() - 1;
qup[i] = in.next().equals("U");
}
int[] allX = normalize(qx);
int[] allY = normalize(qy);
Tree left = new Tree(allX.length);
Tree up = new Tree(allY.length);
for (int i = 0; i < q; ++i) {
int x = Arrays.binarySearch(allX, qx[i]);
int y = Arrays.binarySearch(allY, qy[i]);
if (qup[i]) {
int stopAt = up.get(x);
if (stopAt == -1) {
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 == -1) {
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 cnt = 1;
for (int i = 1; i < allX.length; ++i) {
if (allX[i] > allX[i-1]) {
allX[cnt++] = allX[i];
}
}
allX = Arrays.copyOf(allX, cnt);
return allX;
}
Random random = new Random(19950315293L + 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 {
BufferedReader reader;
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;
map<int, bool> check;
set<pair<int, pair<char, int> > > s;
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
s.insert(make_pair(n + 1, make_pair('L', n)));
s.insert(make_pair(0, make_pair('U', 1)));
while (q--) {
int x, y, k = 0;
cin >> x >> y;
char type;
cin >> type;
if (check[x]) {
cout << 0 << endl;
continue;
}
check[x] = true;
set<pair<int, pair<char, int> > >::iterator z =
s.upper_bound(make_pair(x, make_pair('.', 0)));
if (type == 'U') {
if ((*z).second.first == 'U') {
k = (*z).second.second;
} else {
k = (*z).first - 1;
}
cout << k - x + 1 << endl;
} else {
z--;
if ((*z).second.first == 'L') {
k = (*z).second.second;
} else {
k = (*z).first + 1;
}
cout << x - k + 1 << endl;
}
s.insert(make_pair(x, make_pair(type, k)));
}
}
| 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.StringTokenizer;
import java.util.TreeSet;
public class Main {
public static void main(String[] args){
new TaskC().solve();
}
}
class TaskC {
public void solve() {
InputStream inputStream = System.in;
//OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
//PrintWriter out = new PrintWriter(outputStream);
//Scanner in=new Scanner(new BufferedInputStream(System.in));
int n=in.nextInt();
int q=in.nextInt();
int[] x=new int[q];
int[] y=new int[q];
String str;
TreeSet<Integer> ts=new TreeSet<Integer>();
TreeSet<Pair> us=new TreeSet<Pair>();
for(int i=0;i<q;i++) {
x[i]=in.nextInt();
y[i]=in.nextInt();
str=in.next();
if(ts.contains(x[i])){
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> {
int col;
int id;
public Pair(int col,int id) {
this.col=col;
this.id=id;
}
@Override
public int compareTo(Pair o) {
int z=Long.compare(col,o.col);
if(z==0){
z=Long.compare(id,o.id);
}
return z;
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
| JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
struct info {
map<long long, long long> same;
set<long long> other;
};
int main() {
std::ios_base::sync_with_stdio(false);
enum direction_type { UP, LEFT };
info a[2];
set<long long> visited;
int N, Q;
cin >> N >> Q;
for (int i = 0; i < Q; i++) {
int x, y, dd;
char d;
cin >> x >> y >> d;
if (visited.find(x) != visited.end()) {
cout << 0 << endl;
continue;
}
visited.insert(x);
int cur;
if (d == 'U') {
dd = 0;
cur = x;
} else {
dd = 1;
cur = y;
}
info& cur_a = a[dd];
info& opp_a = a[!dd];
auto prev_same = cur_a.same.lower_bound(cur);
auto prev_other = cur_a.other.lower_bound(cur);
int res;
if (prev_other == cur_a.other.end()) {
res = N - cur + 1;
cur_a.same.insert(make_pair(cur, N + 1));
opp_a.other.insert(N - cur + 1);
} else {
if (prev_same != cur_a.same.end() && prev_same->first < *prev_other) {
res = prev_same->second - cur;
cur_a.same.insert(make_pair(cur, prev_same->second));
opp_a.other.insert(N - cur + 1);
} else {
res = *prev_other - cur;
cur_a.same.insert(make_pair(cur, *prev_other));
opp_a.other.insert(N - cur + 1);
}
}
cout << res << 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.util.*;
import java.io.*;
import java.math.*;
import java.text.*;
public class Main
{
static FastScanner in = new FastScanner(System.in);
static StringBuilder sb = new StringBuilder();
static DecimalFormat df = new DecimalFormat();
public static void main(String[] args)
{
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.nextToken().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); // to avoid index 0
}
SWAG mxrow = new SWAG(Q*2+2);
SWAG mxcol = new SWAG(Q*2+2);
for (int i = 0; i < Q; i++)
{
// qr/qc = query row/col
int qr = map.get(Y[i]);
int qc = map.get(X[i]);
if (dir[i] == 'U')
{
int r = mxrow.q(qc);
System.out.println(nums[qr] - nums[r]);
mxcol.update(r, qr, qc);
mxrow.update(qc, qc, qr);
}
else
{
int c = mxcol.q(qr);
System.out.println(nums[qc] - nums[c]);
mxrow.update(c, qc, qr);
mxcol.update(qr, qr, qc);
}
}
}
}
class SWAG
{
int N;
int A[];
int lazy[];
SWAG(int size)
{
N = size+10;
A = new int[N*4+1000];
lazy = new int[N*4+1000];
}
int q(int index)
{
return q(index, 1, N, 1);
}
void update(int l, int r, int value)
{
update(l, r, value, 1, N, 1);
return;
}
// update the node n's information
void helloworld(int value, int n)
{
A[n] = Math.max(A[n], value);
lazy[n] = Math.max(lazy[n], value);
}
void clearLazy(int a, int b, int n)
{
if (lazy[n] == 0) return;
if (a < b)
{
// if node n has two children
helloworld(lazy[n],2*n);
helloworld(lazy[n],2*n+1);
}
lazy[n] = 0;
}
void update(int l, int r, int value, int a, int b, int n)
{
clearLazy(a,b,n);
if (r < a || b < l) return;
if (l <= a && b <= r)
{
helloworld(value,n);
}
else
{
int m = (a + b) / 2;
update(l, r, value, a, m, n*2);
update(l, r, value, m+1, b, n*2+1);
A[n] = Math.max(A[n*2],A[n*2+1]);
}
return;
}
int q(int index, int a, int b, int n)
{
clearLazy(a,b,n);
if (index < a || b < index) return 0;
if (a == b)
{
return A[n];
}
else
{
int m = (a + b) / 2;
if (index <= m)
return q(index,a,m,n*2);
else
return q(index,m+1,b,n*2+1);
}
}
}
class FastScanner
{
BufferedReader reader;
StringTokenizer tokenizer;
FastScanner (InputStream inputStream)
{
reader = new BufferedReader(new InputStreamReader(inputStream));
}
String nextToken()
{
while (tokenizer == null || ! tokenizer.hasMoreTokens())
{
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e){}
}
return tokenizer.nextToken();
}
boolean hasNext()
{
if (tokenizer == null || ! tokenizer.hasMoreTokens())
{
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e){ return false; }
}
return true;
}
int nextInt()
{
return Integer.parseInt(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 |
// Problem : C. Case of Chocolate
// Contest : Codeforces - Codeforces Round #310 (Div. 1)
// URL : https://codeforces.com/contest/555/problem/C
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class a implements Runnable{
public static void main(String[] args) {
new Thread(null, new a(), "process", 1<<26).start();
}
public void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//PrintWriter out = new PrintWriter("file.out");
Task solver = new Task();
//int t = scan.nextInt();
int t = 1;
for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
out.close();
}
static class Task {
static final int inf = Integer.MAX_VALUE;
public void solve(int testNumber, FastReader sc, PrintWriter pw) {
//CHECK FOR QUICKSORT TLE
//***********************//
//CHECK FOR INT OVERFLOW
//***********************//
int n = sc.nextInt();
int m = sc.nextInt();
TreeSet<Integer> hs = new TreeSet<Integer>();
int[][] queries = new int[m][3];
for(int i = 0; i < m; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int dir = sc.next().equals("U") ? 1 : 0;
queries[i][0] = x;
queries[i][1] = y;
queries[i][2] = dir;
hs.add(x);
hs.add(y);
}
HashMap<Integer, Integer> compress = new HashMap<>();
int[] compres = new int[hs.size()+1];
int c = 1;
for(int x : hs) {
compress.put(x, c);
compres[c++] = x;
}
segt cols = new segt(hs.size());
segt rows = new segt(hs.size());
for(int i = 0; i < m; i++) {
int x = compress.get(queries[i][0]);
int y = compress.get(queries[i][1]);
int dir = queries[i][2];
if(dir == 1) {
int stop = cols.max(1, 0, hs.size(), x, x);
pw.println(compres[y] - compres[stop]);
rows.update(1, 0, hs.size(), stop, y, x);
cols.update(1, 0, hs.size(), x, x, y);
}
else {
int stop = rows.max(1, 0, hs.size(), y, y);
pw.println(compres[x] - compres[stop]);
cols.update(1, 0, hs.size(), stop, x, y);
rows.update(1, 0, hs.size(), y, y, x);
}
}
}
class segt {
int[] tree;
int[] lazy;
segt(int n) {
tree = new int[4*n];
lazy = new int[4*n];
}
int max(int v, int tl, int tr, int l, int r) {
if(tr - tl < 0 || tr < l || tl > r) {
return Integer.MIN_VALUE;
}
if(tl >= l && tr <= r) {
return tree[v];
}
if(tl == tr) {
return Integer.MIN_VALUE;
}
push(v);
int tm = (tl + tr) / 2;
return Math.max(max(v*2, tl, tm, l, r), max(v*2+1, tm+1, tr, l, r));
}
void update(int v, int tl, int tr, int l, int r, int val) {
if(tr - tl < 0 || tr < l || tl > r) {
return;
}
if(tl >= l && tr <= r) {
lazy[v] = Math.max(val, lazy[v]);
tree[v] = Math.max(val, tree[v]);
return;
}
int tm = (tl + tr) / 2;
update(v*2, tl, tm, l, r, val);
update(v*2+1, tm+1, tr, l, r, val);
}
void push(int v) {
tree[v*2] = Math.max(tree[v*2], lazy[v]);
lazy[v*2] = Math.max(tree[v*2], lazy[v]);
tree[v*2+1] = Math.max(tree[v*2+1], lazy[v]);
lazy[v*2+1] = Math.max(tree[v*2+1], lazy[v]);
lazy[v] = 0;
}
}
}
static long binpow(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
if ((b & 1) == 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
static void sort(int[] x){
shuffle(x);
Arrays.sort(x);
}
static void sort(long[] x){
shuffle(x);
Arrays.sort(x);
}
static class tup implements Comparable<tup>{
int a, b;
tup(int a,int b){
this.a=a;
this.b=b;
}
@Override
public int compareTo(tup o){
return Integer.compare(o.b,b);
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(i + 1);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(i + 1);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct Node {
Node(int s = 0, Node* left = nullptr, Node* right = nullptr)
: val(s), L(left), R(right), lazy(0) {}
Node* L;
Node* R;
int val;
int lazy;
};
struct DynamicSegmentTree {
DynamicSegmentTree(int sz) : n(sz) { root = fetchNode(); }
void upd(int left, int right, int v) {
l = left;
r = right;
val = v;
upd(root, 0, n);
}
int qwr(int left, int right) {
l = left;
r = right;
return qwr(root, 0, n);
}
void push(Node* T) {
if (T->L != nullptr) T->L->lazy = max(T->L->lazy, T->lazy);
if (T->R != nullptr) T->R->lazy = max(T->R->lazy, T->lazy);
T->val = max(T->val, T->lazy);
}
void merge(Node* T) {
if (T->L != nullptr) T->val = max(T->val, T->L->val);
if (T->R != nullptr) T->val = max(T->val, T->R->val);
}
void upd(Node* T, int low, int high) {
if (l <= low && r >= high) {
T->lazy = max(T->lazy, val);
push(T);
return;
}
int mid = ((low + high) >> 1);
if (T->L != nullptr) push(T->L);
if (T->R != nullptr) push(T->R);
if (l <= mid) {
if (T->L == nullptr) T->L = fetchNode();
upd(T->L, low, mid);
}
if (r >= mid + 1) {
if (T->R == nullptr) T->R = fetchNode();
upd(T->R, mid + 1, high);
}
merge(T);
}
int qwr(Node* T, int low, int high) {
if (l <= low && r >= high) {
return T->val;
}
if (T->L == nullptr && T->R == nullptr) {
return T->val;
}
int mid = ((low + high) >> 1);
if (T->L != nullptr) push(T->L);
if (T->R != nullptr) push(T->R);
int resLeft = 0;
int resRight = 0;
if (l <= mid && T->L != nullptr) resLeft = qwr(T->L, low, mid);
if (r >= mid + 1 && T->R != nullptr) resRight = qwr(T->R, mid + 1, high);
return max(T->lazy, max(resLeft, resRight));
}
Node* fetchNode(int s = 0, Node* left = nullptr, Node* right = nullptr) {
tree.push_back(Node(s, left, right));
return &tree.back();
}
int n, l, r, val;
Node* root;
deque<Node> tree;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
DynamicSegmentTree DST_X(1e9 + 9);
DynamicSegmentTree DST_Y(1e9 + 9);
int n, q;
cin >> n >> q;
while (q--) {
int x, y;
char type;
cin >> x >> y >> type;
if (type == 'L') {
int pos = DST_Y.qwr(y, y);
if (pos > x || DST_X.qwr(x, x) == y) {
cout << 0 << "\n";
continue;
}
cout << x - pos << "\n";
DST_X.upd(pos + 1, x, y);
} else {
int pos = DST_X.qwr(x, x);
if (pos > y || DST_Y.qwr(y, y) == x) {
cout << 0 << "\n";
continue;
}
cout << y - pos << "\n";
DST_Y.upd(pos + 1, y, x);
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
struct Segtree {
int N;
vector<T> seg, lazy;
Segtree(int _n) : N(_n) {
seg.resize(4 * N, 0);
lazy.resize(4 * N, 0);
}
T join(T x, T y) { return max(x, y); }
void push(int x, int s, int e) {
if (!lazy[x]) return;
seg[x] = max(seg[x], lazy[x]);
if (s != e) {
lazy[2 * x] = max(lazy[2 * x], lazy[x]);
lazy[2 * x + 1] = max(lazy[2 * x + 1], lazy[x]);
}
lazy[x] = 0;
}
void build(int x, int s, int e) {
if (s == e) {
seg[x] = 0;
return;
}
int mid = (s + e) >> 1;
build(2 * x, s, mid);
build(2 * x + 1, mid + 1, e);
seg[x] = join(seg[2 * x], seg[2 * x + 1]);
}
void update(int x, int s, int e, int l, int r, T val) {
push(x, s, e);
if (s > r || e < l) return;
if (s >= l && e <= r) {
lazy[x] = val;
push(x, s, e);
return;
}
int mid = (s + e) >> 1;
update(2 * x, s, mid, l, r, val);
update(2 * x + 1, mid + 1, e, l, r, val);
seg[x] = join(seg[2 * x], seg[2 * x + 1]);
}
T query(int x, int s, int e, int l, int r) {
push(x, s, e);
if (e < l || s > r) return 0;
if (s >= l && e <= r) {
return seg[x];
}
int mid = (s + e) >> 1;
return join(query(2 * x, s, mid, l, r), query(2 * x + 1, mid + 1, e, l, r));
}
void update(int l, int r, T val) { update(1, 0, N - 1, l, r, val); }
T query(int l, int r) { return query(1, 0, N - 1, l, r); }
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> x(q), y(q), d(q), cc(1, 0);
for (int i = 0; i < q; ++i) {
char c;
cin >> x[i] >> y[i] >> c;
d[i] = (c == 'U');
cc.push_back(x[i]);
cc.push_back(y[i]);
cc.push_back(n + 1 - x[i]);
cc.push_back(n + 1 - y[i]);
}
sort(cc.begin(), cc.end());
cc.erase(unique(cc.begin(), cc.end()), cc.end());
Segtree<int> left(cc.size() + 1), up(cc.size() + 1);
for (int i = 0; i < q; ++i) {
x[i] = lower_bound(cc.begin(), cc.end(), x[i]) - cc.begin();
y[i] = lower_bound(cc.begin(), cc.end(), y[i]) - cc.begin();
if (d[i]) {
int id = up.query(y[i], y[i]);
up.update(y[i], y[i], y[i]);
cout << cc[y[i]] - cc[id] << '\n';
id = lower_bound(cc.begin(), cc.end(), n + 1 - cc[id]) - cc.begin();
y[i] = lower_bound(cc.begin(), cc.end(), n + 1 - cc[y[i]]) - cc.begin();
left.update(y[i], id, x[i]);
} else {
int id = left.query(x[i], x[i]);
left.update(x[i], x[i], x[i]);
cout << cc[x[i]] - cc[id] << '\n';
id = lower_bound(cc.begin(), cc.end(), n + 1 - cc[id]) - cc.begin();
x[i] = lower_bound(cc.begin(), cc.end(), n + 1 - cc[x[i]]) - cc.begin();
up.update(x[i], id, 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 maxn = 12000003;
int mx[maxn], z[maxn], L[maxn], R[maxn], cnt, n, Q;
struct SEG {
int root;
void change(int &p, int l, int r, int seg_l, int seg_r, int k) {
if (!p) p = ++cnt;
if (seg_l <= l && r <= seg_r) {
z[p] = max(z[p], k);
return;
}
int mid = (l + r) >> 1;
if (seg_l <= mid) change(L[p], l, mid, seg_l, seg_r, k);
if (seg_r > mid) change(R[p], mid + 1, r, seg_l, seg_r, k);
}
int query(int p, int l, int r, int pos) {
if (!p) return 0;
if (l == r) return max(z[p], mx[p]);
int mid = (l + r) >> 1;
if (pos <= mid)
return max(z[p], query(L[p], l, mid, pos));
else
return max(z[p], query(R[p], mid + 1, r, pos));
}
} row, col;
int main() {
scanf("%d%d", &n, &Q);
while (Q--) {
int x, y;
char mo[2];
scanf("%d%d%s", &x, &y, mo);
if (*mo == 'U') {
int mx = col.query(col.root, 1, n, x);
printf("%d\n", y - mx);
if (mx < y) row.change(row.root, 1, n, mx + 1, y, x);
col.change(col.root, 1, n, x, x, y);
} else {
int mx = row.query(row.root, 1, n, y);
printf("%d\n", x - mx);
if (mx < x) col.change(col.root, 1, n, mx + 1, x, y);
row.change(row.root, 1, n, y, 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;
int x, y;
char op[5];
map<int, int> u;
map<int, int> l;
int n, q;
int main() {
scanf("%d %d", &n, &q);
while (q--) {
scanf("%d%d%s", &x, &y, op);
map<int, int>::iterator p;
if (op[0] == 'U') {
p = u.lower_bound(x);
int ans = 0;
if (u.count(x)) {
printf("0\n");
continue;
} else {
if (p == u.end())
ans = y;
else
ans = y - (p->second);
}
printf("%d\n", ans);
l[y] = x;
u[x] = y - ans;
} else {
p = l.lower_bound(y);
int ans = 0;
if (l.count(y)) {
printf("0\n");
continue;
} else {
if (p == l.end())
ans = x;
else
ans = x - (p->second);
}
printf("%d\n", ans);
u[x] = y;
l[y] = x - ans;
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
map<int, int> mp;
char way[200005];
int res[200005];
std::ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
way[0] = 'U', way[q + 1] = 'L';
mp[0] = 0;
mp[n + 1] = q + 1;
res[0] = res[q + 1] = 0;
for (int i = 1; i <= q; i++) {
int x, y;
cin >> x >> y >> way[i];
auto it = mp.lower_bound(x);
if (it->first == x) {
cout << 0 << endl;
continue;
}
if (way[i] == 'L') it--;
int ans = abs(it->first - x);
if (way[i] == way[it->second]) ans += res[it->second];
cout << (res[i] = ans) << endl;
mp[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<pair<pair<int, int>, int> > pcs;
int l[1010101];
int u[1010101];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
int i2 = 2;
pcs.insert({{1, n}, 1});
pcs.insert({{n + 1, n + 2}, 1});
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
char s;
cin >> s;
auto it = pcs.lower_bound({{x, n + 3}, 0});
it--;
auto t = *it;
if (t.first.first > x || t.first.second < x) {
cout << "0\n";
} else {
if (s == 'L') {
cout << x - l[t.second] << '\n';
} else {
cout << y - u[t.second] << '\n';
}
pcs.erase(it);
if (x > t.first.first) {
l[i2] = l[t.second];
u[i2] = u[t.second];
if (s == 'L') {
u[i2] = y;
}
pcs.insert({{t.first.first, x - 1}, i2++});
}
if (x < t.first.second) {
l[i2] = l[t.second];
u[i2] = u[t.second];
if (s == 'U') {
l[i2] = x;
}
pcs.insert({{x + 1, t.first.second}, i2++});
}
}
}
}
| 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 qry {
int x, y;
char op;
};
bool hc[2][800010];
int c[2][800010];
int N, st;
int query(int x, int n = 1, int l = 0, int r = N) {
if (l == r)
return c[st][n];
else {
if (hc[st][n]) {
c[st][2 * n] = max(c[st][2 * n], c[st][n]);
c[st][2 * n + 1] = max(c[st][2 * n + 1], c[st][n]);
hc[st][2 * n] = hc[st][2 * n + 1] = 1;
c[st][n] = hc[st][n] = 0;
}
int m = (l + r) / 2;
if (x <= m)
return query(x, 2 * n, l, m);
else
return query(x, 2 * n + 1, m + 1, r);
}
}
void update(int x, int y, int v, int n = 1, int l = 0, int r = N) {
if (x <= l && r <= y) {
hc[st][n] = 1;
c[st][n] = max(c[st][n], v);
} else {
int m = (l + r) / 2;
if (hc[st][n]) {
c[st][2 * n] = max(c[st][n], c[st][2 * n]);
c[st][2 * n + 1] = max(c[st][n], c[st][2 * n + 1]);
hc[st][2 * n] = hc[st][2 * n + 1] = 1;
c[st][n] = hc[st][n] = 0;
}
if (y > m) update(x, y, v, 2 * n + 1, m + 1, r);
if (x <= m) update(x, y, v, 2 * n, l, m);
}
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
vector<qry> qrs(q);
vector<int> UP;
vector<int> LF;
for (int i = 0; i < q; i++) {
scanf("%d%d%c%c", &qrs[i].x, &qrs[i].y, &qrs[i].op, &qrs[i].op);
if (qrs[i].op == 'U')
UP.push_back(qrs[i].x);
else
LF.push_back(qrs[i].y);
}
sort(UP.begin(), UP.end());
sort(LF.begin(), LF.end());
UP.resize(unique(UP.begin(), UP.end()) - UP.begin());
LF.resize(unique(LF.begin(), LF.end()) - LF.begin());
int up = UP.size();
int lf = LF.size();
for (int i = 0; i < q; i++) {
if (qrs[i].op == 'U') {
int id = lower_bound(UP.begin(), UP.end(), qrs[i].x) - UP.begin();
N = up;
st = 0;
int te = query(id);
update(id, id, qrs[i].y);
printf("%d\n", qrs[i].y - te);
int l = upper_bound(LF.begin(), LF.end(), te) - LF.begin();
int r = upper_bound(LF.begin(), LF.end(), qrs[i].y) - LF.begin();
r--;
N = lf;
st = 1;
if (r >= l) update(l, r, qrs[i].x);
} else {
int id = lower_bound(LF.begin(), LF.end(), qrs[i].y) - LF.begin();
N = lf;
st = 1;
int te = query(id);
update(id, id, qrs[i].x);
printf("%d\n", qrs[i].x - te);
int l = upper_bound(UP.begin(), UP.end(), te) - UP.begin();
int r = upper_bound(UP.begin(), UP.end(), qrs[i].x) - UP.begin();
r--;
N = up;
st = 0;
if (r >= l) update(l, r, qrs[i].y);
}
}
}
| 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;
vector<string> split(const string& s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c)) v.emplace_back(x);
return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\n';
err(++it, args...);
}
const int M = 500010;
map<pair<int, int>, int> done;
struct node {
int maxx, prop;
node *left, *right;
node() {
maxx = 0;
prop = 0;
left = right = NULL;
}
void reproduce() {
if (left == NULL) left = new node();
if (right == NULL) right = new node();
}
void propagate(int val, bool flag) {
maxx = max(maxx, val);
if (!flag) {
reproduce();
left->prop = max(left->prop, val);
right->prop = max(right->prop, val);
}
prop = -1;
}
void update(int l, int r, int st, int ed, int val) {
if (l >= st && r <= ed)
prop = max(prop, val);
else if (l > ed || r < st)
return;
else {
reproduce();
int m = (l + r) / 2;
left->update(l, m, st, ed, val);
right->update(m + 1, r, st, ed, val);
}
}
int query(int l, int r, int x, int val) {
if (l == r && r == x)
return max(val, prop);
else if (x >= l && x <= r) {
reproduce();
int m = (l + r) / 2;
return max(left->query(l, m, x, max(val, prop)),
right->query(m + 1, r, x, max(val, prop)));
} else
return 0;
}
};
node xtree, ytree;
int main() {
int i, j, k, x, y, n, q, ans;
char t[10];
scanf("%d%d", &n, &q);
while (q--) {
scanf("%d%d%s", &x, &y, t);
if (done[{x, y}]) {
printf("%d\n", 0);
continue;
}
if (t[0] == 'U') {
k = xtree.query(0, n, x, 0);
ans = y - k;
ytree.update(0, n, k + 1, y, x);
} else {
k = ytree.query(0, n, y, 0);
ans = x - k;
xtree.update(0, n, k + 1, x, y);
}
printf("%d\n", ans);
done[{x, y}] = 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;
set<pair<int, int>> S;
map<pair<int, int>, pair<int, int>> M;
set<int> bio;
int main(void) {
int n, q;
scanf("%d %d", &n, &q);
S.insert({0, n - 1});
M[{0, n - 1}] = {-1, -1};
for (int i = (0); i < (q); ++i) {
int x, y;
char d[11];
scanf("%d %d %s", &y, &x, d);
--x, --y;
if (bio.count(y)) {
printf("0\n");
continue;
}
bio.insert(y);
auto it = S.upper_bound({y, n + 1});
assert(it != S.begin());
--it;
auto p = *it;
auto r = M[*it];
S.erase(it);
M.erase(*it);
if (d[0] == 'U') {
printf("%d\n", x - r.first);
if (y > p.first) {
S.insert({p.first, y - 1});
M[{p.first, y - 1}] = r;
}
if (y < p.second) {
S.insert({y + 1, p.second});
M[{y + 1, p.second}] = {r.first, y};
}
}
if (d[0] == 'L') {
printf("%d\n", y - r.second);
if (y > p.first) {
S.insert({p.first, y - 1});
M[{p.first, y - 1}] = {x, r.second};
}
if (y < p.second) {
S.insert({y + 1, p.second});
M[{y + 1, p.second}] = r;
}
}
}
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;
set<pair<int, pair<int, int> > > s;
set<pair<int, pair<int, int> > >::iterator t;
map<int, bool> c, r;
string S;
int main() {
cin >> n >> q;
s.insert(make_pair(0, make_pair(0, 1)));
s.insert(make_pair(n + 1, make_pair(0, 0)));
for (int x, y, ans, k, a, b; q--;) {
cin >> x >> y >> S;
if (c[y] || r[x]) {
cout << 0 << endl;
continue;
}
t = s.upper_bound(make_pair(x, make_pair(0, 0)));
if (S == "L") t--;
k = (*t).second.second;
a = (*t).second.first;
b = (*t).first;
if (S == "U") {
if (k == 0)
ans = b - x;
else
ans = a + b - x;
} else {
if (k == 0)
ans = x - b + a;
else
ans = x - b;
}
cout << ans << endl;
s.insert(make_pair(x, make_pair(ans, (int)(S == "U"))));
c[y] = true;
r[x] = 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;
using LL = long long;
using pii = pair<LL, LL>;
const LL INF = 9223372036854775807LL;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
map<int, pii> fields;
fields[0] = make_pair(0, n);
fields[n] = make_pair(-1, -1);
map<int, bool> seen;
for (int i = 0; i < int(q); ++i) {
int x, y;
string dir;
cin >> x >> y >> dir;
--x;
if (seen[x]) {
cout << "0\n";
continue;
}
seen[x] = true;
map<int, pii>::iterator it = fields.upper_bound(x);
map<int, pii>::iterator it2 = it;
it2--;
pii bounds = it2->second;
int low = it2->first;
if ("L" == dir) {
cout << x + 1 - bounds.first << '\n';
fields[low] = make_pair(bounds.first, x);
fields[x + 1] = make_pair(bounds.first, bounds.second);
} else {
cout << bounds.second - x << '\n';
fields[low] = make_pair(bounds.first, bounds.second);
fields[x + 1] = make_pair(x + 1, bounds.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.util.*;
import java.io.*;
public class c {
public static void main(String[] args) throws IOException
{
input.init(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = input.nextInt(), m = input.nextInt();
int[] rs = new int[m], cs = new int[m];
char[] types = new char[m];
TreeSet<Integer> set = new TreeSet<Integer>();
set.add(-1);
for(int i = 0; i<m; i++)
{
cs[i] = input.nextInt()-1;
rs[i] = input.nextInt()-1;
set.add(cs[i]);
set.add(rs[i]);
types[i] = input.next().charAt(0);
}
HashMap<Integer, Integer> compress = new HashMap<Integer, Integer>();
int[] rev = new int[set.size()];
int at = 0;
for(int x : set)
{
compress.put(x, at);
rev[at] = x;
at++;
}
n = set.size();
IT rows = new IT(n), cols = new IT(n);
for(int i = 0; i<m; i++)
{
int realRow = rs[i], realCol = cs[i];
int col = compress.get(cs[i]), row = compress.get(rs[i]);
char c = types[i];
int query = (c == 'U') ? cols.get(col, col) : rows.get(row, row);
if(query < 0) query = 0;
out.println((c == 'U') ? realRow - rev[query] : realCol - rev[query]);
if(c == 'U')
{
rows.add(query+1, row, col);
cols.add(col, col, row);
}
else
{
cols.add(query+1, col, row);
rows.add(row, row, col);
}
}
out.close();
}
//Minimum Interval Tree
static class IT
{
static int oo = 987654321;
int[] left,right, val, a, b, prop;
IT(int n)
{
left = new int[4*n];
right = new int[4*n];
val = new int[4*n];
prop = new int[4*n];
a = new int[4*n];
b = new int[4*n];
init(0,0, n-1);
}
int init(int at, int l, int r)
{
a[at] = l;
b[at] = r;
if(l==r)
{
val[at]= prop[at] = -oo;
left[at] = right [at] = -1;
}
else
{
int mid = (l+r)/2;
left[at] = init(2*at+1,l,mid);
right[at] = init(2*at+2,mid+1,r);
val[at] = prop[at] = -oo;
}
return at++;
}
//return the max over [x,y]
int get(int x, int y)
{
return go(x,y, 0);
}
void push(int at)
{
if(prop[at] != -oo)
{
go3(a[left[at]], b[left[at]], prop[at], left[at]);
go3(a[right[at]], b[right[at]], prop[at], right[at]);
prop[at] = -oo;
}
}
int go(int x,int y, int at)
{
if(at==-1 || y<a[at] || x>b[at]) return -oo;
if(x <= a[at] && y>= b[at]) return val[at];
push(at);
return Math.max(go(x, y, left[at]), go(x, y, right[at]));
}
//max v to elements x through y
void add(int x, int y, int v)
{
go3(x, y, v, 0);
}
void go3(int x, int y, int v, int at)
{
if(at==-1) return;
if(y < a[at] || x > b[at]) return;
x = Math.max(x, a[at]);
y = Math.min(y, b[at]);
if(y == b[at] && x == a[at])
{
val[at] = Math.max(val[at], v);
prop[at] = Math.max(prop[at], v);
return;
}
push(at);
go3(x, y, v, left[at]);
go3(x, y, v, right[at]);
val[at] = Math.max(val[left[at]], val[right[at]]);
}
}
public static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
}
}
| JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, q;
map<int, char> direction;
map<int, int> answer;
map<int, int> row;
int main() {
scanf("%d%d", &n, &q);
long long ret = 0;
int x, y;
char dir[3];
set<int> event;
for (int i = 0; i < q; i++) {
scanf("%d%d%s", &y, &x, dir);
if (direction.find(y) != direction.end()) {
printf("0\n");
continue;
}
int ret = -1;
if (dir[0] == 'U') {
auto firstEvent = event.upper_bound(y);
if (firstEvent == event.end()) {
ret = x;
} else {
if (direction[*firstEvent] == 'U') {
ret = answer[*firstEvent] + x - row[*firstEvent];
} else {
ret = x - row[*firstEvent];
}
}
} else {
if ((int)event.size() == 0) {
ret = y;
} else {
auto firstEvent = event.upper_bound(y);
if (firstEvent == event.begin()) {
ret = y;
} else {
--firstEvent;
if (direction[*firstEvent] == 'U') {
ret = y - *firstEvent;
} else {
ret = answer[*firstEvent] + y - *firstEvent;
}
}
}
}
row[y] = x;
event.insert(y);
direction[y] = dir[0];
answer[y] = ret;
printf("%d\n", ret);
}
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:134217728")
using namespace std;
int n, m;
struct R {
int x1, y1, x2, y2;
R() {}
R(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};
bool operator<(const R& a, const R& b) {
return max(a.x1, a.y1) < max(b.x1, b.y1);
}
char buf[2];
int main() {
scanf("%d%d", &n, &m);
set<R> S;
S.insert(R(1, 1, n, n));
while (m-- > 0) {
int x, y;
scanf("%d%d", &x, &y);
y = n - y + 1;
scanf("%s", buf);
set<R>::iterator it = S.upper_bound(R(x, y, x, y));
if (it == S.begin()) {
printf("0\n");
continue;
}
it--;
if (it->y2 < y || it->x2 < x) {
printf("0\n");
continue;
}
R r = *it;
S.erase(r);
if (buf[0] == 'L') {
printf("%d\n", x - r.x1 + 1);
if (r.y1 != y) S.insert(R(r.x1, r.y1, r.x2, y - 1));
if (r.y2 != y) S.insert(R(r.x1, y + 1, r.x2, r.y2));
} else {
printf("%d\n", r.y2 - y + 1);
if (r.x1 != x) S.insert(R(r.x1, r.y1, x - 1, r.y2));
if (r.x2 != x) S.insert(R(x + 1, r.y1, r.x2, r.y2));
}
}
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<pair<int, int> > con;
set<pair<int, int> >::iterator it;
int n, m;
char s[50];
int x[200007], y[200007];
int main() {
while (~scanf("%d%d", &n, &m)) {
con.clear();
x[0] = y[0] = 0;
x[m + 1] = y[m + 1] = 0;
con.insert(make_pair(0, m + 1));
con.insert(make_pair(n + 1, m + 1));
for (int i = 1; i <= m; i++) {
scanf("%d%d", &x[i], &y[i]);
scanf("%s", s);
if (s[0] == 'U')
it = con.lower_bound(make_pair(x[i], -1));
else {
it = con.upper_bound(make_pair(x[i], m + 1));
it--;
}
if (it->first == x[i]) {
puts("0");
continue;
}
con.insert(make_pair(x[i], i));
if (s[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];
}
}
}
}
| 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.StringTokenizer;
import java.util.TreeSet;
public class T310E {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
StringTokenizer st = new StringTokenizer(line);
final int n = Integer.parseInt(st.nextToken());
final int m = Integer.parseInt(st.nextToken());
StringBuilder sb = new StringBuilder();
ArrayList<Bar> columns = new ArrayList<>(), rows = new ArrayList<>();
for (int i = 0; i < m; i++) {
line = br.readLine();
st = new StringTokenizer(line);
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
String dir = st.nextToken();
Bar bar1, bar2;
ArrayList<Bar> tree, target;
if (dir.charAt(0) == 'U') {
bar1 = new Bar(y, x);
bar2 = new Bar(x, 1);
tree = rows;
target = columns;
} else {
bar2 = new Bar(y, 1);
bar1 = new Bar(x, y);
tree = columns;
target = rows;
}
int idx = Collections.binarySearch(tree, bar1);
int idx1 = Collections.binarySearch(target, bar2);
if (idx >= 0 || idx1 >= 0) {
// used
sb.append("0\n");
continue;
}
int eaten = bar1.index;
for (int j = -2 - idx; j >= 0 && ! tree.isEmpty(); j--) {
Bar b = tree.get(j);
if ( b.height <= bar1.height ) {
// found
eaten = bar1.index - b.index;
bar2.height = b.index;
break;
}
}
sb.append(eaten).append("\n");
target.add(-idx1 - 1, bar2);
//bar2.height;
}
System.out.println(sb.toString());
}
private static class Bar implements Comparable<Bar> {
private int height, index;
Bar(int i, int h) {
height = h;
index = i;
}
@Override
public int compareTo(Bar o) {
// reverse sort
int diff = index - o.index;
return diff;
}
}
}
| 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 N = 800010;
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) {
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) {
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;
const int maxn = 2e5 + 5;
map<int, pair<char, int> > m;
int main() {
long long n, q;
scanf("%lld%lld", &n, &q);
m[0] = make_pair('U', 0);
m[n + 1] = make_pair('L', 0);
while (q--) {
long long x, y;
string s;
scanf("%lld%lld", &x, &y);
cin >> s;
auto it = m.lower_bound(x);
if (it->first == x) {
puts("0");
continue;
}
if (s[0] == 'L') it--;
long long ans = abs((it->first) - x);
if (it->second.first == s[0]) ans += it->second.second;
printf("%lld\n", ans);
m[x] = make_pair(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>
using namespace std;
const int maxn = 200010;
int X[maxn];
int N, M;
struct node {
int x, y;
char d[5];
} op[maxn];
bool vis[maxn];
struct IntervalTree {
int up[maxn << 2];
int down[maxn << 2];
int set1[maxn << 2];
int set2[maxn << 2];
void build(int o, int l, int r) {
up[o] = down[o] = 0;
set1[o] = set2[o] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(o << 1, l, mid);
build(o << 1 | 1, mid + 1, r);
}
void update(int o, int l, int r, int q1, int q2, int x, bool f) {
if (q1 <= l && r <= q2) {
if (f && x > up[o])
set1[o] = x, up[o] = x;
else if (!f && x > down[o])
set2[o] = x, down[o] = x;
return;
}
pushdown(o, f);
int mid = (l + r) >> 1;
if (q1 <= mid) update(o << 1, l, mid, q1, q2, x, f);
if (q2 > mid) update(o << 1 | 1, mid + 1, r, q1, q2, x, f);
}
void pushdown(int o, bool f) {
if (f) {
if (set1[o]) {
if (set1[o] > set1[o << 1])
set1[o << 1] = set1[o], up[o << 1] = set1[o];
if (set1[o] > set1[o << 1 | 1])
set1[o << 1 | 1] = set1[o], up[o << 1 | 1] = set1[o];
set1[o] = 0;
}
} else {
if (set2[o]) {
if (set2[o] > set2[o << 1])
set2[o << 1] = set2[o], down[o << 1] = set2[o];
if (set2[o] > set2[o << 1 | 1])
set2[o << 1 | 1] = set2[o], down[o << 1 | 1] = set2[o];
set2[o] = 0;
}
}
}
int query(int o, int l, int r, int pos, bool f) {
if (l == r) return f ? up[o] : down[o];
pushdown(o, f);
int mid = (l + r) >> 1;
if (pos <= mid) return query(o << 1, l, mid, pos, f);
return query(o << 1 | 1, mid + 1, r, pos, f);
}
} tree;
int main() {
scanf("%d%d", &M, &N);
for (int i = 1; i <= N; i++) {
scanf("%d%d%s", &op[i].y, &op[i].x, op[i].d);
X[i] = op[i].x;
}
sort(X + 1, X + 1 + N);
tree.build(1, 1, N);
for (int i = 1; i <= N; i++) {
int tmp = lower_bound(X + 1, X + 1 + N, op[i].x) - X;
if (vis[tmp]) {
printf("0\n");
continue;
}
vis[tmp] = 1;
if (op[i].d[0] == 'U') {
int pos = tree.query(1, 1, N, tmp, 1);
int tmp1 = lower_bound(X + 1, X + 1 + N, pos) - X;
if (pos == 0) tmp1 = 0;
if (tmp1 + 1 <= tmp) tree.update(1, 1, N, tmp1 + 1, tmp, op[i].y, 0);
printf("%d\n", op[i].x - pos);
} else {
int pos = tree.query(1, 1, N, tmp, 0);
int tmp1 = lower_bound(X + 1, X + 1 + N, 1 + M - pos) - X;
if (pos == 0) tmp1 = N + 1;
if (tmp1 - 1 >= tmp) tree.update(1, 1, N, tmp, tmp1 - 1, op[i].x, 1);
printf("%d\n", op[i].y - pos);
}
}
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 Comp {
long long left, right, leftBorder, upBorder;
bool isLeftVertical, isRightVertical;
};
class Solution {
public:
void solve(std::istream& in, std::ostream& out) {
long long n;
int q;
in >> n >> q;
map<int, Comp> components;
set<int> eaten;
components[1] = {1, n, 1, 1, true, false};
while (q--) {
char c;
int x, y;
in >> x >> y >> c;
if (!eaten.insert(x).second) {
out << "0\n";
continue;
}
auto it = components.upper_bound(x);
--it;
Comp cur = it->second;
components.erase(it);
int res = 0;
if (c == 'U') {
if (cur.isRightVertical) {
res = y - cur.upBorder + 1;
} else {
res = cur.right - x + 1;
}
if (x > cur.left) {
components[cur.left] = {cur.left, x - 1,
cur.leftBorder, cur.upBorder,
cur.isLeftVertical, true};
}
if (x < cur.right) {
components[x + 1] = {x + 1, cur.right, x + 1,
cur.upBorder, true, cur.isRightVertical};
}
} else {
if (cur.isLeftVertical) {
res = x - cur.left + 1;
} else {
res = x - cur.leftBorder + 1;
}
if (x > cur.left) {
components[cur.left] = {cur.left, x - 1,
cur.leftBorder, y + 1,
cur.isLeftVertical, false};
}
if (x < cur.right) {
components[x + 1] = {x + 1, cur.right, cur.leftBorder,
cur.upBorder, false, cur.isRightVertical};
}
}
out << res << "\n";
}
}
};
void solve(std::istream& in, std::ostream& out) {
out << std::setprecision(12);
Solution solution;
solution.solve(in, out);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
istream& in = cin;
ostream& out = cout;
solve(in, out);
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 IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const double DINF = numeric_limits<double>::infinity();
const double EPS = 1e-8;
const int DX[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int DY[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int N = 1 << 18;
class SegTreeL {
int a[N * 2 - 1];
public:
SegTreeL() { memset(a, -1, sizeof(a)); }
const int operator[](int x) const {
x += N - 1;
int r = a[x];
while (x != 0) {
x = (x - 1) >> 1;
r = max(r, a[x]);
}
return r;
}
void pinkiePie(int l, int r, int x, int c = 0, int cl = 0, int cr = N - 1) {
if (l > cr || r < cl) return;
if (l <= cl && cr <= r) {
a[c] = max(a[c], x);
return;
}
int c1 = (c << 1) + 1;
int c2 = c1 + 1;
a[c1] = max(a[c1], a[c]);
a[c2] = max(a[c2], a[c]);
a[c] = -1;
pinkiePie(l, r, x, c1, cl, (cl + cr) >> 1);
pinkiePie(l, r, x, c2, ((cl + cr) >> 1) + 1, cr);
}
} stL;
class SegTreeU {
int a[N * 2 - 1], nn;
public:
void init(int n) {
nn = n;
for (int i = 0; i < N * 2 - 1; ++i) a[i] = n;
}
const int operator[](int x) const {
x += N - 1;
int r = a[x];
while (x != 0) {
x = (x - 1) >> 1;
r = min(r, a[x]);
}
return r;
}
void pinkiePie(int l, int r, int x, int c = 0, int cl = 0, int cr = N - 1) {
if (l > cr || r < cl) return;
if (l <= cl && cr <= r) {
a[c] = min(a[c], x);
return;
}
int c1 = (c << 1) + 1;
int c2 = c1 + 1;
a[c1] = min(a[c1], a[c]);
a[c2] = min(a[c2], a[c]);
a[c] = nn;
pinkiePie(l, r, x, c1, cl, (cl + cr) >> 1);
pinkiePie(l, r, x, c2, ((cl + cr) >> 1) + 1, cr);
}
} stU;
int n, q;
int x[N];
char c[N];
int xs[N];
bool used[N];
int main() {
ios::sync_with_stdio(false);
cin >> n >> q;
stU.init(n);
for (int i = 0; i < q; ++i) {
int fluttershy;
cin >> x[i] >> fluttershy >> c[i];
--x[i];
xs[i] = x[i];
}
sort(xs, xs + q);
for (int i = 0; i < q; ++i) {
int xsp = lower_bound(xs, xs + q, x[i]) - xs;
if (used[xsp]) {
cout << "0\n";
continue;
}
used[xsp] = true;
if (c[i] == 'U') {
int b = stU[xsp];
cout << b - x[i] << "\n";
stL.pinkiePie(lower_bound(xs, xs + q, x[i]) - xs,
lower_bound(xs, xs + q, b) - xs, x[i]);
} else {
int b = stL[xsp];
cout << x[i] - b << "\n";
stU.pinkiePie(lower_bound(xs, xs + q, b) - xs,
lower_bound(xs, xs + q, x[i]) - xs, 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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main
{
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(), q = sc.nextInt();
int row_idx = 1, col_idx = 1;
TreeMap<Integer, Integer> row_map = new TreeMap<>(), col_map = new TreeMap<>();
TreeMap<Integer, Integer> row_map2 = new TreeMap<>(), col_map2 = new TreeMap<>();
int [] x = new int[q], y = new int[q], x2 = new int[q], y2 = new int[q];
char [] op = new char[q];
row_map.put(0, 0);
col_map.put(0, 0);
row_map2.put(0, 0);
col_map2.put(0, 0);
for (int i = 0; i < q; i++) {
y[i] = sc.nextInt() - 1;
x[i] = sc.nextInt() - 1;
x2[i] = x[i];
y2[i] = y[i];
op[i] = sc.next().charAt(0);
}
Arrays.sort(x2); Arrays.sort(y2);
for (int i = 0; i < q; i++) {
if (!row_map.containsKey(x2[i])){
row_map.put(x2[i], row_idx++);
row_map2.put(row_idx - 1, x2[i]);
}
if (!col_map.containsKey(y2[i])){
col_map.put(y2[i], col_idx++);
col_map2.put(col_idx - 1, y2[i]);
}
}
ST row_st = new ST(row_map.size()), col_st = new ST(col_map.size());
for (int i = 0; i < q; i++) {
int col = y[i], row = x[i];
if (op[i] == 'L') {
int compressed_furthest_col = row_st.query(1, 0, row_idx - 1, row_map.get(x[i]), row_map.get((x[i])));
int actual = compressed_furthest_col == -1 ? -1 : col_map2.get(compressed_furthest_col);
out.println(col - actual);
col_st.update(1, 0, col_idx - 1, Math.max(0, compressed_furthest_col), col_map.get(col), row_map.get(row));
row_st.update(1, 0, row_idx - 1, row_map.get(row), row_map.get(row), col_map.get(col));
}
else {
int compressed_furthest_row = col_st.query(1, 0, col_idx - 1, col_map.get(y[i]), col_map.get(y[i]));
int actual = compressed_furthest_row == -1 ? -1 : row_map2.get(compressed_furthest_row);
out.println(row - actual);
row_st.update(1, 0, row_idx - 1, Math.max(compressed_furthest_row, 0), row_map.get(row), col_map.get(col));
col_st.update(1, 0, col_idx - 1, col_map.get(col), col_map.get(col), row_map.get(row));
}
}
out.flush();
out.close();
}
static class ST {
int n;
int [] T, lazy;
ST (int sz) {
n = sz;
int pow = 0;
while (1 << pow < n) pow++;
T = new int[((1 << (pow + 1))) << 1];
lazy = new int[(1 << (pow + 1)) << 1];
Arrays.fill(T, -1);
Arrays.fill(lazy, -1);
}
void update (int node, int s, int e, int l, int r, int idx) {
// System.out.println("idx = " + idx);
propagate (node);
// System.out.println(node + " " + s + " " + e + " " + l + " " + r + " " + idx);
if (s >= l && e <= r) {
T[node] = Math.max(T[node], idx);
lazy[node] = Math.max(lazy[node], idx);
}
else if (s > r || e < l) return;
else {
int mid = (s + e) >> 1;
update (node << 1, s, mid, l, r, idx);
update (node << 1 | 1, mid + 1, e, l, r, idx);
T[node] = Math.max(T[node << 1], T[node << 1 | 1]);
}
}
int query (int node, int s, int e, int l, int r) {
propagate (node);
if (s >= l && e <= r) return T[node];
else if (s > r || e < l) return -1;
else {
int mid = (s + e) >> 1;
return Math.max(query(node << 1, s, mid, l, r), query(node << 1 | 1, mid + 1, e, l, r));
}
}
void propagate (int node) {
if (lazy[node] != -1) {
lazy[node << 1] = Math.max(lazy[node << 1], lazy[node]);
T[node << 1] = Math.max(T[node << 1], lazy[node << 1]);
lazy[node << 1 | 1] = Math.max(lazy[node << 1 | 1], lazy[node]);
T[node << 1 | 1] = Math.max(lazy[node << 1 | 1], T[node << 1 | 1]);
lazy[node] = -1;
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
public boolean nextEmpty() throws IOException
{
String s = nextLine();
st = new StringTokenizer(s);
return s.isEmpty();
}
}
} | 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;
using pii = pair<int, int>;
int n, q;
set<pii> sv[2];
set<int> X, Y;
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n >> q;
int x, y;
char c;
while (q--) {
int res;
cin >> x >> y >> c;
if (c == 'U') {
if (X.count(-x)) {
cout << "0\n";
continue;
}
auto it = sv[0].lower_bound({x, 0});
auto it2 = Y.lower_bound(-y);
res = y;
if (it != sv[0].end()) res = it->first - x + it->second;
if ((it != sv[0].end() && it2 != Y.end() && it->first > n + 1 + *it2) ||
(it == sv[0].end() && it2 != Y.end()))
res = n + 1 + *it2 - x;
sv[0].insert({x, res});
X.insert(-x);
} else {
if (Y.count(-y)) {
cout << "0\n";
continue;
}
auto it = sv[1].lower_bound({y, 0});
auto it2 = X.lower_bound(-x);
res = x;
if (it != sv[1].end()) res = it->first - y + it->second;
if ((it != sv[1].end() && it2 != X.end() && it->first > n + 1 + *it2) ||
(it == sv[1].end() && it2 != X.end()))
res = n + 1 + *it2 - y;
sv[1].insert({y, res});
Y.insert(-y);
}
cout << res << '\n';
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
vector<int> x, y;
map<int, int> mx, my;
int pass[maxn];
int seg[2][4 * maxn];
int lazy[2][4 * maxn];
void refresh(int t, int idx, int st, int ed) {
int l = 2 * idx, r = l + 1;
if (lazy[t][idx] == 0) return;
seg[t][idx] = max(seg[t][idx], lazy[t][idx]);
if (st != ed) {
lazy[t][l] = max(lazy[t][l], lazy[t][idx]);
lazy[t][r] = max(lazy[t][r], lazy[t][idx]);
}
lazy[t][idx] = 0;
}
void build(int t, int idx, int st, int ed) {
if (st == ed) {
seg[t][idx] = 0;
lazy[t][idx] = 0;
return;
}
int l = 2 * idx, r = l + 1, md = (st + ed) / 2;
build(t, l, st, md);
build(t, r, md + 1, ed);
lazy[t][idx] = 0;
seg[t][idx] = seg[t][l] + seg[t][r];
}
void update(int t, int idx, int st, int ed, int ini, int fim, int val) {
refresh(t, idx, st, ed);
if (fim < st || ed < ini) return;
if (ini <= st && ed <= fim) {
lazy[t][idx] = val;
refresh(t, idx, st, ed);
return;
}
int l = 2 * idx, r = l + 1, md = (st + ed) / 2;
update(t, l, st, md, ini, fim, val);
update(t, r, md + 1, ed, ini, fim, val);
}
int query(int t, int idx, int st, int ed, int pos) {
refresh(t, idx, st, ed);
if (pos < st || ed < pos) return 0;
if (st == ed && pos == ed) return seg[t][idx];
int l = 2 * idx, r = l + 1, md = (st + ed) / 2;
if (md >= pos) return query(t, l, st, md, pos);
if (md + 1 <= pos) return query(t, r, md + 1, ed, pos);
}
struct event {
char a;
int b, c;
event() { a = b = c = 0; }
event(char x, int y, int z) {
a = x;
b = y;
c = z;
}
} e[maxn];
int main() {
int n, q;
scanf("%d %d", &n, &q);
for (int i = 0; i < q; i++) {
char a;
int b, c;
scanf("%d %d %c", &b, &c, &a);
e[i] = event(a, b, c);
x.push_back(b);
y.push_back(c);
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
vector<int>::iterator it;
it = unique(x.begin(), x.end());
x.resize(distance(x.begin(), it));
it = unique(y.begin(), y.end());
y.resize(distance(y.begin(), it));
for (int i = 0; i < x.size(); i++) {
mx[x[i]] = i + 1;
}
for (int i = 0; i < y.size(); i++) {
my[y[i]] = i + 1;
}
build(0, 1, 1, x.size());
build(1, 1, 1, y.size());
for (int i = 0; i < q; i++) {
if (pass[mx[e[i].b]] == 0) {
pass[mx[e[i].b]] = 1;
char let = e[i].a;
int res;
if (let == 'U') {
int qry = query(0, 1, 1, x.size(), mx[e[i].b]);
res = e[i].c - qry;
if (my[qry] + 1 <= my[e[i].c])
update(1, 1, 1, y.size(), my[qry] + 1, my[e[i].c], e[i].b);
} else {
int qry = query(1, 1, 1, y.size(), my[e[i].c]);
res = e[i].b - qry;
if (mx[qry] + 1 <= mx[e[i].b])
update(0, 1, 1, x.size(), mx[qry] + 1, mx[e[i].b], e[i].c);
}
printf("%d\n", res);
} else
printf("0\n");
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, nrq, ny, nx, aix[8 * 200005], aiy[8 * 200005], ixx[200005], iyy[200005];
set<int> yu, xl, copies;
set<int>::iterator it;
map<int, int> xx, yy;
pair<pair<int, int>, char> q[200005];
void build_ai(int ai[], int node, int l, int r) {
ai[node] = 2000000000;
if (l == r) return;
int mid = (l + r) >> 1;
build_ai(ai, 2 * node, l, mid);
build_ai(ai, 2 * node + 1, mid + 1, r);
}
void update(int ai[], int node, int l, int r, int &pos, int &val) {
if (l == r) {
ai[node] = val;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
update(ai, 2 * node, l, mid, pos, val);
else
update(ai, 2 * node + 1, mid + 1, r, pos, val);
ai[node] = (((ai[2 * node]) < (ai[2 * node + 1])) ? (ai[2 * node])
: (ai[2 * node + 1]));
}
void queryMin(int ai[], int node, int l, int r, int lq, int rq, int &ans) {
if (node == 1) {
ans = 2000000000;
}
if (l >= lq && r <= rq) {
ans = (((ans) < (ai[node])) ? (ans) : (ai[node]));
return;
}
int mid = (l + r) >> 1;
if (mid >= lq) queryMin(ai, 2 * node, l, mid, lq, rq, ans);
if (mid < rq) queryMin(ai, 2 * node + 1, mid + 1, r, lq, rq, ans);
}
int main() {
int i;
scanf("%d %d", &n, &nrq);
for (i = 1; i <= nrq; i++) {
scanf("%d %d %c", &q[i].first.second, &q[i].first.first, &q[i].second);
yu.insert(q[i].first.second);
xl.insert(q[i].first.first);
}
for (it = yu.begin(), ny = 1; it != yu.end(); it++, ny++) {
yy[*it] = ny;
iyy[ny] = *it;
}
ny--;
for (it = xl.begin(), nx = 1; it != xl.end(); it++, nx++) {
xx[*it] = nx;
ixx[nx] = *it;
}
nx--;
build_ai(aix, 1, 1, nx);
build_ai(aiy, 1, 1, ny);
for (i = 1; i <= nrq; i++) {
int x = q[i].first.first, y = q[i].first.second;
if (copies.find(x) != copies.end()) {
printf("0\n");
continue;
}
copies.insert(x);
if (q[i].second == 'U') {
int x0, ans;
queryMin(aix, 1, 1, nx, 1, xx[x], ans);
if (ans <= y) {
int l = 1, r = xx[x], mid;
while (l < r) {
mid = (l + r) >> 1;
queryMin(aix, 1, 1, nx, mid, xx[x], ans);
if (ans <= y)
l = mid + 1;
else
r = mid - 1;
}
while (l > xx[x]) l--;
queryMin(aix, 1, 1, nx, l, xx[x], ans);
while (ans > y) queryMin(aix, 1, 1, nx, --l, xx[x], ans);
x0 = l;
} else
x0 = 0;
x0 = ixx[x0];
printf("%d\n", x - x0);
update(aiy, 1, 1, ny, yy[y], x0);
} else {
int y0, ans;
queryMin(aiy, 1, 1, ny, 1, yy[y], ans);
if (ans <= x) {
int l = 1, r = yy[y], mid;
while (l < r) {
mid = (l + r) >> 1;
queryMin(aiy, 1, 1, ny, mid, yy[y], ans);
if (ans <= x)
l = mid + 1;
else
r = mid - 1;
}
while (l > yy[y]) l--;
queryMin(aiy, 1, 1, ny, l, yy[y], ans);
while (ans > x) queryMin(aiy, 1, 1, ny, --l, yy[y], ans);
y0 = l;
} else
y0 = 0;
y0 = iyy[y0];
printf("%d\n", y - y0);
update(aix, 1, 1, nx, xx[x], y0);
}
}
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;
void read(bool out = 0) {}
int seg[2][8 * 500009] = {};
int from, to;
void insert(int x, int y, int value, int type = 0, int second = from,
int e = to, int p = 1) {
if (x <= second && y >= e) {
seg[type][p] = max(seg[type][p], value);
return;
}
if (x > e || second > y) return;
int mid = second + e >> 1;
insert(x, y, value, type, second, mid, p << 1);
insert(x, y, value, type, mid + 1, e, p << 1 | 1);
}
int get(int x, int y, int type = 0, int second = from, int e = to, int p = 1) {
int value = seg[type][p];
if (x > e || second > y)
return 0;
else if (x <= second && y >= e) {
return value;
}
int mid = second + e >> 1;
value = max(value, get(x, y, type, second, mid, p << 1));
value = max(value, get(x, y, type, mid + 1, e, p << 1 | 1));
return value;
}
pair<int, int> x[2 * 500009], y[2 * 500009];
int pos[2][2 * 500009], place[2][2 * 500009];
bool type[2 * 500009];
char tmp;
int n;
int m;
int find_ind(int value, bool dir, bool type) {
if (dir) {
int lo = 0, hi = m - 1;
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
int cur = type ? y[mid].first : x[mid].first;
if (cur <= value)
lo = mid;
else
hi = mid - 1;
}
return lo;
} else {
int lo = 0, hi = m - 1;
while (lo < hi) {
int mid = (lo + hi - 1) >> 1;
int cur = type ? y[mid].first : x[mid].first;
if (cur >= value)
hi = mid;
else
lo = mid + 1;
}
return hi;
}
}
int ans[2 * 500009];
int main() {
read();
scanf("%d%d", &n, &m);
from = 0, to = m - 1;
map<int, int> vis;
for (int i = 0; i < m; i++) {
int tmpx, tmpy;
scanf("%d %d %c\n", &tmpx, &tmpy, &tmp);
x[i].first = tmpx;
y[i].first = tmpy;
if (vis[tmpx]) ans[i] = -1;
vis[tmpx] = 1;
type[i] = tmp == 'U';
pos[0][i] = x[i].first, pos[1][i] = y[i].first;
x[i].second = y[i].second = i;
}
sort(x, x + m);
sort(y, y + m);
for (int i = 0; i < m; i++) {
place[0][x[i].second] = i;
place[1][y[i].second] = i;
}
for (int i = 0; i < m; i++) {
bool tp = type[i];
int u = pos[!tp][i];
int v = pos[tp][i];
int uind = place[!tp][i];
int vind = place[tp][i];
int st = get(uind, uind, !tp);
if (ans[i] != -1)
ans[i] = v - st;
else
ans[i] = 0;
int indst = find_ind(st, 0, tp);
int lst = find_ind(v, 1, tp);
insert(indst, lst, u, tp);
printf("%d\n", ans[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 INF = 0x7f7f7f7f;
void setup() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(15);
}
struct seg_tree {
int S;
vector<int> arr;
seg_tree(int _S) {
S = _S;
arr = vector<int>(2 * S, INF);
}
void upd(int i, int v) {
i += S + 1;
arr[i] = v;
while (i > 1) {
i /= 2;
arr[i] = min(arr[2 * i], arr[2 * i + 1]);
}
}
int rmq(int i, int j) {
int res = INF;
for (i += S + 1, j += S + 1; i <= j; i /= 2, j /= 2) {
if ((i & 1) == 1) res = min(res, arr[i]), i++;
if ((j & 1) == 0) res = min(res, arr[j]), 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.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(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 | import java.util.TreeSet;
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
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskE solver = new TaskE();
solver.solve(1, in, out);
out.close();
}
}
class TaskE {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int q = in.nextInt();
TreeSet<EatenChocolate> magicSet = new TreeSet<>();
magicSet.add(new EatenChocolate(0, 0, 0));
magicSet.add(new EatenChocolate(n + 1, 0, 0));
for (int i = 0; i < q; i++) {
int x = in.nextInt();
int y = in.nextInt();
char o = in.next().toCharArray()[0];
if (o == 'U') {
EatenChocolate lim = magicSet.higher(new EatenChocolate(x - 1, 0, 0));
if (lim.obstacle == x) {
out.println(0);
continue;
}
int ans = lim.obstacle - x + lim.ch;
magicSet.add(new EatenChocolate(x, ans, 0));
out.println(ans);
} else {
EatenChocolate lim = magicSet.lower(new EatenChocolate(x + 1, 0, 0));
if (lim.obstacle == x) {
out.println(0);
continue;
}
int ans = x - lim.obstacle + lim.cv;
magicSet.add(new EatenChocolate(x, 0, ans));
out.println(ans);
}
}
}
class EatenChocolate implements Comparable<EatenChocolate> {
int obstacle;
int ch;
int cv;
public EatenChocolate(int r, int ch, int cv) {
this.obstacle = r;
this.ch = ch;
this.cv = cv;
}
public int compareTo(EatenChocolate o) {
return this.obstacle - o.obstacle;
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
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 |
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static BufferedReader in;
static StringTokenizer stk;
static boolean[] isPrime = new boolean[5000];
public static void main(String[] args) throws Exception {
in = new BufferedReader(new InputStreamReader(System.in));
// in = new BufferedReader(new FileReader("input.txt"));
stk = new StringTokenizer(in.readLine());
// Start of User Code
proc();
// End of User Code
in.close();
}
static TreeMap<Long, Piece> k = new TreeMap<Long, Piece>();
static long ans = 0L;
static class Piece {
long a, b;
long maxC, maxR;
Piece(long a, long b, long maxC, long maxR) {
this.a = a;
this.b = b;
this.maxC = maxC;
this.maxR = maxR;
}
@Override
public String toString() {
return String.format("%d %d %d %d", a, b, maxC, maxR);
}
LinkedList<Piece> eat(long c, long r, char dir) {
ans = 0;
LinkedList<Piece> res = new LinkedList<Piece>();
if (c < a || c > b) return res;
if (dir == 'U') ans = r - maxR + 1;
else ans = c - maxC + 1;
if (a == b) return res;
if (c == a) {
if (dir == 'U') {
res.add(new Piece(c + 1, b, c + 1, maxR));
} else {
res.add(new Piece(c + 1, b, maxC, maxR));
}
} else if (c == b) {
if (dir == 'U') {
res.add(new Piece(a, c - 1, maxC, maxR));
} else {
res.add(new Piece(a, c - 1, maxC, r + 1));
}
} else {
if (dir == 'U') {
res.add(new Piece(a, c - 1, maxC, maxR));
res.add(new Piece(c + 1, b, c + 1, maxR));
} else {
res.add(new Piece(a, c - 1, maxC, r + 1));
res.add(new Piece(c + 1, b, maxC, maxR));
}
}
return res;
}
}
static void proc() throws Exception {
long n = nl();
k.put(1L, new Piece(1, n, 1, 1));
int q = ni();
StringBuilder sb = new StringBuilder(2000000);
//System.out.println(k);
for(int i =0; i<q; i++){
long c = nl();
long r = nl();
//System.out.println(c + ", " + r);
char dir = ns().charAt(0);
Long t = k.floorKey(c);
if(t != null){
Piece p = k.remove(t);
if(p.b < c){
k.put(t, p);
}
LinkedList<Piece> x = p.eat(c, r, dir);
sb.append(ans + "\n");
//System.out.println(ans);
for(Piece z: x){
//if(k.containsKey(z.a)) System.out.println("ERROR");
k.put(z.a, z);
}
}else{
sb.append(0 + "\n");
//System.out.println(0);
}
//System.out.println(k);
}
System.out.print(sb.toString());
}
static long modPow(long n, long pow, long mod) {
return BigInteger.valueOf(n).modPow(BigInteger.valueOf(pow), BigInteger.valueOf(mod)).longValue();
}
static long modInv(long n, long mod, boolean isPrimeModuli) {
if (isPrimeModuli) {
return modPow(n, mod - 2, mod);
}
return BigInteger.valueOf(n).modInverse(BigInteger.valueOf(mod)).longValue();
}
// calc factorials
static long[] fact;
static void calcFactorials(int n) {
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = fact[i - 1] * i;
}
}
static void calcFactorialsModM(int n, long M) {
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = fact[i - 1] * i;
fact[i] %= M;
}
}
static long ncr(int n, int r) {
return fact[n] / (fact[n - r] * fact[r]);
}
static long ncrModM(int n, int r, long MOD, boolean isPrimeModuli) {
return (((fact[n] * modInv(fact[n - r], MOD, isPrimeModuli)) % MOD) * modInv(fact[r], MOD, isPrimeModuli)) % MOD;
}
static long toL(String s) {
return Long.parseLong(s);
}
static long toL(BigInteger n) {
return n.longValue();
}
static int toI(String s) {
return Integer.parseInt(s);
}
static double toD(String s) {
return Double.parseDouble(s);
}
static void printf(String format, Object... args) {
System.out.printf(format, args);
}
static int ni() throws Exception {
while (!stk.hasMoreTokens()) {
stk = new StringTokenizer(in.readLine());
}
return Integer.parseInt(stk.nextToken());
}
static long nl() throws Exception {
while (!stk.hasMoreTokens()) {
stk = new StringTokenizer(in.readLine());
}
return Long.parseLong(stk.nextToken());
}
static double nd() throws Exception {
while (!stk.hasMoreTokens()) {
stk = new StringTokenizer(in.readLine());
}
return Double.parseDouble(stk.nextToken());
}
static String ns() throws Exception {
while (!stk.hasMoreTokens()) {
stk = new StringTokenizer(in.readLine());
}
return stk.nextToken();
}
static int min(int a, int b) {
return a < b ? a : b;
}
static int max(int a, int b) {
return a > b ? a : b;
}
static long min(long a, long b) {
return a < b ? a : b;
}
static long max(long a, long b) {
return a > b ? a : b;
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
} | 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 N = 300010;
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);
}
}
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;
template <class T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
map<long long, long long> mk;
long long a, uy, b, c, n, m, T[2][100007 * 15], po;
char type[100007 * 15];
pair<long long, long long> d[100007 * 15];
vector<long long> v;
set<long long> s;
void nh() {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 0; i < int(v.size()); i++) mk[v[i]] = i + 1;
po = int(v.size());
}
void find(int x, int l, int r, int v, int w) {
uy = max(uy, T[w][v]);
if (l == r) return;
if (x <= (l + r) / 2)
find(x, l, (l + r) / 2, v * 2, w);
else
find(x, (l + r) / 2 + 1, r, v * 2 + 1, w);
}
void upd(int x, int y, int l, int r, int v, int w, long long z) {
if (x > r || y < l) return;
if (x <= l && r <= y) {
T[w][v] = max(T[w][v], z);
return;
}
upd(x, y, l, (l + r) / 2, v * 2, w, z);
upd(x, y, (l + r) / 2 + 1, r, v * 2 + 1, w, z);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> d[i].first >> d[i].second >> type[i];
v.push_back(d[i].first), v.push_back(d[i].second);
}
nh();
for (int i = 1; i <= m; i++) {
uy = 0;
if (s.count(d[i].second)) {
cout << "0" << endl;
continue;
}
if (type[i] == 'U') {
find(mk[d[i].first], 1, po + 1, 1, 0);
cout << d[i].second - uy << endl;
upd(mk[uy] + 1, mk[d[i].second], 1, po + 1, 1, 1, d[i].first);
} else {
find(mk[d[i].second], 1, po + 1, 1, 1);
cout << d[i].first - uy << endl;
upd(mk[uy] + 1, mk[d[i].first], 1, po + 1, 1, 0, d[i].second);
}
s.insert(d[i].second);
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
struct T {
int node[N << 2], lazy[N << 2];
void up(int pos, int value) {
node[pos] = max(node[pos], value);
lazy[pos] = max(lazy[pos], value);
}
void push_down(int pos) {
up(pos << 1, lazy[pos]);
up(pos << 1 | 1, lazy[pos]);
}
void push_up(int pos) { node[pos] = max(node[pos << 1], node[pos << 1 | 1]); }
void update(int pos, int l, int r, int ll, int rr, int value) {
if (rr < l || ll > r) return;
if (ll <= l && rr >= r) {
up(pos, value);
return;
}
push_down(pos);
update(pos << 1, l, ((l + r) >> 1), ll, rr, value);
update(pos << 1 | 1, ((l + r) >> 1) + 1, r, ll, rr, value);
push_up(pos);
}
int query(int pos, int l, int r, int id) {
if (id > r || id < l) return 0;
if (l == r) return node[pos];
push_down(pos);
int ret = max(query(pos << 1, l, ((l + r) >> 1), id),
query(pos << 1 | 1, ((l + r) >> 1) + 1, r, id));
push_up(pos);
return ret;
}
} t[2];
struct Node {
int x, y, z;
Node() {}
Node(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {}
};
int main() {
int n, m;
char str[10];
while (cin >> n >> m) {
memset(t, 0, sizeof(t));
vector<Node> q(m);
vector<int> xc, yc;
xc.push_back(0), yc.push_back(0);
for (int i = 0; i < (m); ++i) {
scanf("%d%d%s", &q[i].y, &q[i].x, str);
q[i].z = (str[0] == 'L');
xc.push_back(q[i].x), yc.push_back(q[i].y);
}
sort(xc.begin(), xc.end()), sort(yc.begin(), yc.end());
xc.erase(unique(xc.begin(), xc.end()), xc.end());
yc.erase(unique(yc.begin(), yc.end()), yc.end());
n = xc.size();
set<int> s;
for (int i = 0; i < (m); ++i) {
if (s.count(q[i].x)) {
puts("0");
continue;
}
s.insert(q[i].x);
if (q[i].z) {
int id =
distance(yc.begin(), lower_bound(yc.begin(), yc.end(), q[i].y));
int y = t[0].query(1, 0, yc.size(), id);
printf("%d\n", yc[id] - yc[y]);
id = n - id, y = n - y;
t[1].update(1, 0, xc.size(), id, y, id);
} else {
int id =
distance(xc.begin(), lower_bound(xc.begin(), xc.end(), q[i].x));
int x = t[1].query(1, 0, xc.size(), id);
printf("%d\n", xc[id] - xc[x]);
id = n - id, x = n - x;
t[0].update(1, 0, yc.size(), id, x, id);
}
}
}
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<pair<int, int>, pair<int, int> > mp;
map<pair<int, int>, int> done;
map<int, int> up, leftMap;
int main() {
int N, Q;
int r, c;
char dir;
scanf("%d%d", &N, &Q);
up[0] = leftMap[0] = 1;
mp[pair<int, int>(1, N)] = pair<int, int>(0, 0);
map<pair<int, int>, pair<int, int> >::iterator it;
while (Q--) {
scanf("%d %d %c", &c, &r, &dir);
if (done[pair<int, int>(c, r)]) {
printf("0\n");
continue;
}
done[pair<int, int>(c, r)] = 1;
it = mp.upper_bound(pair<int, int>(c, N + 1));
if (!(c >= it->first.first && c <= it->first.second)) it--;
pair<int, int> cur = it->second;
if (dir == 'L') {
printf("%d\n", c - cur.second);
pair<int, int> key = it->first;
mp.erase(it);
if (key.first == key.second) continue;
if (c > key.first) {
mp[pair<int, int>(key.first, c - 1)] = pair<int, int>(r, cur.second);
}
if (key.second > c) {
mp[pair<int, int>(c + 1, key.second)] =
pair<int, int>(cur.first, cur.second);
}
} else {
printf("%d\n", r - cur.first);
pair<int, int> key = it->first;
mp.erase(it);
if (key.first == key.second) continue;
if (c > key.first) {
mp[pair<int, int>(key.first, c - 1)] =
pair<int, int>(cur.first, cur.second);
}
if (key.second > c) {
mp[pair<int, int>(c + 1, key.second)] = pair<int, int>(cur.first, c);
}
}
}
}
| 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 = 2 * 2 * 200010;
int tree[2][4 * MAXN];
int lazy[2][4 * MAXN];
int coords[MAXN];
int X[MAXN], Y[MAXN];
char OP[MAXN];
void go(int idx, int l, int r, int tree[], int lazy[]) {
if (lazy[idx] != 0) {
tree[idx] = max(tree[idx], lazy[idx]);
if (l != r) {
lazy[2 * idx] = max(lazy[2 * idx], lazy[idx]);
lazy[2 * idx + 1] = max(lazy[2 * idx + 1], lazy[idx]);
}
lazy[idx] = 0;
}
}
void update(int px, int py, int val, int idx, int l, int r, int tree[],
int lazy[]) {
go(idx, l, r, tree, lazy);
if (py < l || px > r) return;
if (l >= px && r <= py) {
tree[idx] = max(tree[idx], val);
if (l != r) {
lazy[2 * idx] = max(lazy[2 * idx], val);
lazy[2 * idx + 1] = max(lazy[2 * idx + 1], val);
}
return;
}
int m = (l + r) / 2;
update(px, py, val, 2 * idx, l, m, tree, lazy);
update(px, py, val, 2 * idx + 1, m + 1, r, tree, lazy);
tree[idx] = max(tree[2 * idx], tree[2 * idx + 1]);
}
int query(int px, int py, int idx, int l, int r, int tree[], int lazy[],
int tgt) {
if (py < l || px > r) {
return 0;
}
go(idx, l, r, tree, lazy);
if (l >= px && r <= py) {
return tree[idx];
}
int m = (l + r) / 2;
int p1 = query(px, py, 2 * idx, l, m, tree, lazy, tgt);
int p2 = query(px, py, 2 * idx + 1, m + 1, r, tree, lazy, tgt);
return max(p1, p2);
}
int main() {
ios::sync_with_stdio(false);
int n, q, sz = 0;
cin >> n >> q;
coords[sz++] = 1;
coords[sz++] = 0;
for (int i = 0; i < (q); ++i) {
cin >> X[i] >> Y[i] >> OP[i];
coords[sz++] = X[i];
coords[sz++] = X[i] + 1;
coords[sz++] = Y[i];
coords[sz++] = Y[i] + 1;
}
sort(coords, coords + sz);
sz = unique(coords, coords + sz) - coords;
set<pair<pair<int, int>, char> > migue;
for (int i = 0; i < (q); ++i) {
int ox = X[i];
int oy = Y[i];
char op = OP[i];
if (migue.count({{ox, oy}, op})) {
cout << "0" << endl;
continue;
}
migue.insert({{ox, oy}, op});
int x = lower_bound(coords, coords + sz, ox) - coords;
int y = lower_bound(coords, coords + sz, oy) - coords;
if (op == 'L') {
int val = query(y, y, 1, 0, sz - 1, tree[1], lazy[1], x);
if (val == x) {
cout << 0 << endl;
continue;
}
val = val + 1;
int dif = coords[x] - coords[val] + 1;
cout << dif << endl;
update(val, x, y, 1, 0, sz - 1, tree[0], lazy[0]);
} else {
int val = query(x, x, 1, 0, sz - 1, tree[0], lazy[0], y);
if (val == y) {
cout << 0 << endl;
continue;
}
val = val + 1;
int dif = coords[y] - coords[val] + 1;
cout << dif << endl;
update(val, y, x, 1, 0, sz - 1, tree[1], lazy[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, x, y;
int inf = int(2e9);
char c;
map<pair<int, int>, pair<int, int> > M;
map<pair<int, int>, pair<int, int> >::iterator it;
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> q;
M[make_pair(0, n - 1)] = make_pair(n, n);
for (int i = 0; i < q; ++i) {
cin >> y >> x >> c;
--x;
--y;
M[make_pair(x, inf)] = make_pair(0, 0);
it = M.find(make_pair(x, inf));
if (it == M.begin())
cout << "0" << endl;
else {
--it;
int le = it->first.first;
int ri = it->first.second;
int dd = it->second.first;
int hh = it->second.second;
if (x >= le && x <= ri) {
M.erase(make_pair(le, ri));
if (c == 'L') {
cout << (dd - (x - le)) << endl;
if (x < ri)
M[make_pair(x + 1, ri)] = make_pair(dd - (x + 1 - le), ri - x);
if (x > le)
M[make_pair(le, x - 1)] = make_pair(dd, hh - (ri - x + 1));
} else {
cout << (hh - (ri - x)) << endl;
if (x < ri)
M[make_pair(x + 1, ri)] = make_pair((dd - (x + 1 - le)), hh);
if (x > le)
M[make_pair(le, x - 1)] = make_pair((x - le), (hh - (ri - x + 1)));
}
} else
cout << "0" << endl;
}
M.erase(make_pair(x, inf));
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, m, ans;
struct node {
int l, r, x, y;
};
bool operator<(node p, node q) {
return (p.l == q.l) ? (p.r < q.r) : (p.l < q.l);
}
set<node> S;
int main() {
scanf("%d%d", &n, &m);
S.insert((node){0, 0, 1, 1});
S.insert((node){1, n, 1, 1});
for (int i = 1; i <= m; ++i) {
char op;
int x, y;
scanf("%d %d %c", &x, &y, &op);
node cur = *(--S.lower_bound((node){x, n + 1, 1, 1}));
if (cur.l <= x && x <= cur.r) {
S.erase(cur);
if (op == 'L') {
ans = x - cur.x + 1;
S.insert((node){cur.l, x - 1, cur.x, y + 1});
S.insert((node){x + 1, cur.r, cur.x, cur.y});
} else {
ans = y - cur.y + 1;
S.insert((node){cur.l, x - 1, cur.x, cur.y});
S.insert((node){x + 1, cur.r, x + 1, cur.y});
}
} else
ans = 0;
printf("%d\n", ans);
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | 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<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]);
Query q = new Query(queries[i][1], queries[i][0], i, queries[i][2] == 0, tosolveall.size());
tosolveall.add(q);
if (queries[i][2] == 1) {
tosolveleft.add(q);
} else {
tosolveup.add(q);
}
}
}
int[] solution = new int[tosolveall.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);
}
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 Block {
int l, r, h, w;
Block() : l(), r(), h(), w() {}
Block(int L, int R, int H, int W) : l(L), r(R), h(H), w(W) {}
bool operator<(const Block &p) const { return r <= p.r; }
};
set<Block> skup;
int n, q;
int row, col;
char c[2];
int main() {
scanf("%d%d", &n, &q);
skup.insert(*new Block(1, n, n, n));
while (q--) {
int ret = 0;
scanf("%d%d%s", &col, &row, c);
auto it = skup.upper_bound(*new Block(col, col, 1, 1));
if (it == skup.end()) {
puts("0");
continue;
}
Block xt = *it;
int h = xt.h;
int w = xt.w;
int l = xt.l;
int r = xt.r;
if (col > r || col < l) {
puts("0");
continue;
}
if (c[0] == 'U') {
skup.erase(it);
int aux = r - w + 1;
Block fi = *new Block(l, col - 1, h, col - aux);
Block se = *new Block(col + 1, r, h - (col - l) - 1, w - (col - aux) - 1);
if (fi.l <= fi.r && fi.w > 0) skup.insert(fi);
if (se.l <= se.r && se.h > 0 && se.w > 0) skup.insert(se);
printf("%d\n", h - (col - l));
} else {
skup.erase(it);
int aux = n - l + 1 - h + 1;
Block fi = *new Block(l, col - 1, h - (row - aux) - 1, w - (r - col) - 1);
Block se = *new Block(col + 1, r, row - aux, w);
if (fi.l <= fi.r && fi.w > 0 && fi.h > 0) skup.insert(fi);
if (se.l <= se.r && se.w > 0 && se.h > 0) skup.insert(se);
printf("%d\n", w - (r - col));
}
}
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.SortedMap;
import java.util.TreeMap;
public class P556E {
private final long n;
private final SortedMap<Long, Long> map = new TreeMap<>();
public P556E(long n) {
this.n = n;
map.put(n+1L, n+1L);
map.put(0L, -(n+1L));
}
public long query(long x, long y, boolean left) {
if (map.containsKey(x)) {
return 0;
}
if (left) {
final long x0 = map.headMap(x).lastKey();
final long v0 = map.get(x0);
final long v = (v0 > 0) ? (v0 + (x - x0)) : (x - x0);
// System.err.println("< " + x + ": " + x0 + " " + v0 + ": " + v);
map.put(x, v);
return v;
} else {
final long x0 = map.tailMap(x+1).firstKey();
final long v0 = map.get(x0);
final long v = (v0 > 0) ? (x0 - x) : (-v0 + (x0 - x));
// System.err.println("^ " + x + ": " + x0 + " " + v0 + ": " + v);
map.put(x, -v);
return v;
}
}
public static void main(String[] args) throws IOException {
final BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String[] ss = in.readLine().split(" ", 2);
final long n = Long.parseLong(ss[0]);
final P556E p = new P556E(n);
final int q = Integer.parseInt(ss[1]);
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < q; i++) {
ss = in.readLine().split(" ", 3);
final long v = p.query(Integer.parseInt(ss[0]),
Integer.parseInt(ss[1]),
ss[2].charAt(0) == 'L');
sb.append(v).append('\n');
}
System.out.print(sb.toString());
}
}
| 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 Segment_Tree {
struct tree {
int l, r, tag, v;
} t[200005 << 3];
void Down_date(int k) {
if (t[k].tag == 0) return;
if (t[k].l != t[k].r) {
t[(k << 1)].tag = max(t[(k << 1)].tag, t[k].tag);
t[(k << 1 | 1)].tag = max(t[(k << 1 | 1)].tag, t[k].tag);
t[(k << 1)].v = max(t[(k << 1)].v, t[k].tag);
t[(k << 1 | 1)].v = max(t[(k << 1 | 1)].v, t[k].tag);
}
t[k].tag = 0;
}
void Up_date(int k) {
if (t[k].l == t[k].r) return;
Down_date((k << 1));
Down_date((k << 1 | 1));
t[k].v = max(t[(k << 1)].v, t[(k << 1 | 1)].v);
}
void Build(int k, int l, int r) {
t[k].l = l, t[k].r = r, t[k].tag = 0;
if (l == r) {
t[k].v = 0;
return;
}
int mid = (l + r) >> 1;
Build((k << 1), l, mid);
Build((k << 1 | 1), mid + 1, r);
Up_date(k);
}
void Change(int k, int a, int b, int c) {
int l = t[k].l, r = t[k].r;
if (a > r || b < l) return;
Down_date(k);
if (a <= l && b >= r) {
t[k].tag = max(t[k].tag, c);
t[k].v = max(t[k].v, c);
return;
}
Change((k << 1), a, b, c);
Change((k << 1 | 1), a, b, c);
Up_date(k);
}
int Query(int k, int a, int b) {
int l = t[k].l, r = t[k].r;
if (a > r || b < l) return 0;
Down_date(k);
if (a <= l && b >= r) return t[k].v;
return max(Query((k << 1), a, b), Query((k << 1 | 1), a, b));
}
} A, B;
int n, m, a[200005 << 1], cnt;
map<int, int> serch;
struct typ {
int x, y, f;
} note[200005];
int main() {
scanf("%d%d", &n, &m);
cnt = 0;
a[++cnt] = 1;
serch.clear();
for (int i = 1; i <= m; i++) {
char ch[15];
scanf("%d%d%s", ¬e[i].y, ¬e[i].x, ch);
if (ch[0] == 'U')
note[i].f = 1;
else
note[i].f = 0;
a[++cnt] = note[i].x;
a[++cnt] = note[i].y;
}
sort(a + 1, a + 1 + cnt);
n = unique(a + 1, a + cnt + 1) - a - 1;
for (int i = 1; i <= n; i++) serch[a[i]] = i;
A.Build(1, 1, n);
B.Build(1, 1, n);
for (int i = 1; i <= m; i++) {
int x = serch[note[i].x];
int y = serch[note[i].y];
if (note[i].f == 1) {
int t = A.Query(1, y, y);
printf("%d\n", a[x] - a[t]);
A.Change(1, y, y, x);
if (t + 1 <= x) B.Change(1, t + 1, x, y);
} else {
int t = B.Query(1, x, x);
printf("%d\n", a[y] - a[t]);
B.Change(1, x, x, y);
if (t + 1 <= y) A.Change(1, t + 1, y, x);
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import sys
from bisect import bisect
def input():
return sys.stdin.readline().strip()
def solve():
n, q = map(int, input().split())
was = set()
Q = [None]*q
all = [0]*(2*q)
for i in range(q):
x, y, t = input().split()
x, y = int(x), int(y)
Q[i] = (x, y, t)
all[2*i] = x
all[2*i+1] = y
all.sort()
i = 0
p = -1
F = dict()
for j in range(2*q):
v = all[j]
if v != p:
all[i] = v
F[v] = i
i += 1
p = v
sz = i
all = all[:sz]
V = [0]*(2*sz)
H = [0]*(2*sz)
for x, y, t in Q:
if (x,y) in was:
print(0)
else:
was.add((x,y))
if t == 'L':
TA = H
TB = V
else:
x, y = y, x
TA = V
TB = H
v = F[y] + sz
r = 0
while v > 0:
r = max(r, TA[v])
v //= 2
c = x - r
print(c)
r = F[x] + sz
l = bisect(all, x - c) + sz
while l <= r:
if l % 2 == 1:
TB[l] = max(TB[l], y)
if r % 2 == 0:
TB[r] = max(TB[r], y)
l = (l+1)//2
r = (r-1)//2
solve()
| PYTHON3 |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.io.*;
import java.util.*;
public class CF {
FastScanner in;
PrintWriter out;
class Segment implements Comparable<Segment> {
int from, to, value;
public Segment(int from, int to, int value) {
super();
this.from = from;
this.to = to;
this.value = value;
}
@Override
public int compareTo(Segment o) {
if (from == o.from) {
return Integer.compare(to, o.to);
}
return Integer.compare(from, o.from);
}
@Override
public String toString() {
return "Segment [from=" + from + ", to=" + to + ", value=" + value
+ "]";
}
}
void addSegment(TreeSet<Segment> segments, Segment cur) {
while (true) {
Segment tmp = segments.lower(new Segment(cur.to, Integer.MAX_VALUE,
0));
if (tmp == null) {
segments.add(cur);
break;
}
if (tmp.value >= cur.value) {
if (tmp.to < cur.to) {
segments.add(new Segment(tmp.to + 1, cur.to, cur.value));
}
return;
}
if (tmp.to < cur.from) {
segments.add(cur);
break;
}
segments.remove(tmp);
if (tmp.to > cur.to) {
segments.add(new Segment(cur.to + 1, tmp.to, tmp.value));
}
if (tmp.from < cur.from) {
segments.add(new Segment(tmp.from, cur.from - 1, tmp.value));
}
}
}
class Pair {
int x, y;
public Pair(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
private CF getOuterType() {
return CF.this;
}
}
void solve() {
int n = in.nextInt();
int q = in.nextInt();
TreeSet<Segment> ySegm = new TreeSet<>();
ySegm.add(new Segment(1, n, 0));
TreeSet<Segment> xSegm = new TreeSet<>();
xSegm.add(new Segment(1, n, 0));
HashSet<Pair> allPairs = new HashSet<>();
for (int i = 0; i < q; i++) {
int x = in.nextInt();
int y = in.nextInt();
String dir = in.next();
boolean goesUp = dir.charAt(0) == 'U';
Pair now = new Pair(x, y);
if (allPairs.contains(now)) {
out.println(0);
continue;
}
allPairs.add(now);
if (goesUp) {
Segment seg = ySegm.lower(new Segment(x, Integer.MAX_VALUE, 0));
int curLen = y - seg.value;
out.println(curLen);
if (curLen != 0) {
addSegment(xSegm, new Segment(y - curLen + 1, y, x));
}
} else {
Segment seg = xSegm.lower(new Segment(y, Integer.MAX_VALUE, 0));
int curLen = x - seg.value;
out.println(curLen);
if (curLen != 0) {
addSegment(ySegm, new Segment(x - curLen + 1, x, y));
}
}
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
void run() {
try {
in = new FastScanner(new File("test.in"));
out = new PrintWriter(new File("test.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static void main(String[] args) {
new CF().runIO();
}
} | 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> > up, ho;
set<pair<int, int> >::iterator it;
int main(void) {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
char op;
cin >> y >> x >> op;
if (op == 'U') {
it = up.lower_bound(make_pair(y, -1));
if (it == up.end()) {
up.insert(make_pair(y, 0));
ho.insert(make_pair(x, y));
cout << x << endl;
continue;
}
int yy = it->first, pos = it->second;
if (yy == y) {
cout << 0 << endl;
continue;
}
up.insert(make_pair(y, pos));
ho.insert(make_pair(x, y));
cout << x - pos << endl;
} else {
it = ho.lower_bound(make_pair(x, -1));
if (it == ho.end()) {
ho.insert(make_pair(x, 0));
up.insert(make_pair(y, x));
cout << y << endl;
continue;
}
int xx = it->first, pos = it->second;
if (xx == x) {
cout << 0 << endl;
continue;
}
ho.insert(make_pair(x, pos));
up.insert(make_pair(y, x));
cout << y - pos << 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.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
static final int INF = (int) 1e9;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int m = sc.nextInt();
TreeSet<Integer> values = new TreeSet<>();
Query queries[] = new Query[m];
for (int i = 0; i < m; i++) {
queries[i] = new Query(sc.nextInt(), sc.nextInt(), sc.nextChar());
values.add(queries[i].x);
values.add(queries[i].y);
}
HashMap<Integer, Integer> map = new HashMap<>();
int[] realValue = new int[values.size() + 2];
for (int x : values) {
map.put(x, map.size() + 1);
realValue[map.size()] = x;
}
SegmentTree cols = new SegmentTree(map.size());
SegmentTree rows = new SegmentTree(map.size());
for (Query q : queries) {
int x = q.x;
int y = q.y;
if (q.direction) {
int stopRow = cols.query(map.get(y));
out.println(x - realValue[stopRow]);
x = map.get(x);
y = map.get(y);
cols.update(y, y, x);
rows.update(stopRow + 1, x, y);
} else {
int stopCol = rows.query(map.get(x));
out.println(y - realValue[stopCol]);
x = map.get(x);
y = map.get(y);
rows.update(x, x, y);
cols.update(stopCol, y, x);
}
}
out.close();
out.flush();
}
static class Query {
int x, y;
boolean direction;
public Query(int x, int y, char d) {
this.y = x;
this.x = y;
this.direction = d == 'U';
}
}
static class SegmentTree {
int N;
int[] st, lazy;
public SegmentTree(int n) {
N = 1;
while (N < n)
N <<= 1;
st = new int[N << 1];
lazy = new int[N << 1];
}
int query(int node, int b, int e, int i, int j) {
if (i > e || b > j)
return 0;
if (b >= i && e <= j)
return st[node];
int mid = (b + e) >> 1;
propagate(node);
int q1 = query(node << 1, b, mid, i, j);
int q2 = query(node << 1 | 1, mid + 1, e, i, j);
return Math.max(q1, q2);
}
int query(int point) {
return query(1, 1, N, point, point);
}
void propagate(int node) {
lazy[node << 1] = Math.max(lazy[node], lazy[node << 1]);
lazy[node << 1 | 1] = Math.max(lazy[node], lazy[node << 1 | 1]);
st[node << 1] = Math.max(lazy[node], st[node << 1]);
st[node << 1 | 1] = Math.max(lazy[node], st[node << 1 | 1]);
lazy[node] = 0;
}
void update(int i, int j, int val) {
update(1, 1, N, i, j, val);
}
void update(int node, int b, int e, int i, int j, int val) {
if (i > e || b > j)
return;
if (b >= i && e <= j) {
st[node] = Math.max(val, st[node]);
lazy[node] = Math.max(lazy[node], val);
return;
}
int mid = (b + e) >> 1;
update(node << 1, b, mid, i, j, val);
update(node << 1 | 1, mid + 1, e, i, j, val);
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] ans = new double[n];
for (int i = 0; i < n; i++)
ans[i] = nextDouble();
return ans;
}
public short nextShort() throws IOException {
return Short.parseShort(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 Maxn = 2e5 + 7;
int n, q;
map<int, pair<char, int> > mp;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> q;
mp[0] = make_pair('U', 0), mp[n + 1] = make_pair('L', 0);
for (int i = 0, x, y; i < q; i++) {
char c;
cin >> x >> y >> c;
auto f = mp.lower_bound(x);
if (f->first == x) {
cout << 0 << endl;
continue;
}
if (c == 'L') f--;
int k = 0;
if (c == f->second.first) k = f->second.second;
mp[x] = make_pair(c, k + abs(f->first - x));
cout << k + abs(f->first - x) << endl;
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
const int MOD = 1000000007;
const int TN = 400109;
struct tree {
int mx[TN * 4 + 1];
void upd(int v, int l, int r, int from, int to, int nv) {
if (from > to || from > r || l > to) return;
if (l == from && r == to) {
mx[v] = max(mx[v], nv);
return;
}
int mid = (l + r) / 2;
upd(2 * v, l, mid, from, min(mid, to), nv);
upd(2 * v + 1, mid + 1, r, max(mid + 1, from), to, nv);
}
int getmax(int v, int l, int r, int pos) {
if (l == r) return mx[v];
int v2;
int mid = (l + r) / 2;
if (pos <= mid)
v2 = getmax(2 * v, l, mid, pos);
else
v2 = getmax(2 * v + 1, mid + 1, r, pos);
return max(mx[v], v2);
}
};
const int N = 200009;
int x[N + 1], y[N + 1];
char c[N + 1];
tree T1, T2;
int main() {
int n, q;
cin >> n >> q;
set<int> xx, yy;
for (int i = (1); i <= (q); ++i) {
scanf("%d %d %c", &y[i], &x[i], &c[i]);
for (int dx = (0); dx <= (1); ++dx)
xx.insert(x[i] + dx), yy.insert(y[i] + dx);
}
xx.insert(0);
xx.insert(1);
yy.insert(0);
yy.insert(1);
map<int, int> mem_x;
int sz_x = 0;
for (set<int>::iterator it = xx.begin(); it != xx.end(); ++it)
mem_x[*it] = ++sz_x;
map<int, int> mem_y;
int sz_y = 0;
for (set<int>::iterator it = yy.begin(); it != yy.end(); ++it)
mem_y[*it] = ++sz_y;
set<pair<int, int> > eat;
for (int i = (1); i <= (q); ++i) {
int ans;
if (eat.count(make_pair(x[i], y[i])))
ans = 0;
else if (c[i] == 'L') {
int mx = T2.getmax(1, 1, sz_x, mem_x[x[i]]);
ans = y[i] - mx;
T1.upd(1, 1, sz_y, mem_y[mx + 1], mem_y[y[i]], x[i]);
} else if (c[i] == 'U') {
int mx = T1.getmax(1, 1, sz_y, mem_y[y[i]]);
ans = x[i] - mx;
T2.upd(1, 1, sz_x, mem_x[mx + 1], mem_x[x[i]], y[i]);
}
eat.insert(make_pair(x[i], y[i]));
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;
map<int, pair<int, int> > u, l;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
u[0] = {0, 0};
l[0] = {0, 0};
int n, q, x, y, ans, buf;
pair<int, int> p, p1;
char c;
cin >> n >> q;
while (q--) {
cin >> x >> y >> c;
ans = 0;
if (c == 'U') {
if (u[x].second != 0) {
cout << 0 << '\n';
continue;
}
auto it = u.upper_bound(x);
p = it->second;
if (p.first == 0) {
it = l.upper_bound(y);
it--;
ans = y - it->first;
u[x] = {y - ans + 1, y};
} else {
auto it1 = l.upper_bound(y);
it1--;
if (p.first <= it1->first && p.second >= it1->first)
ans = y - p.first + 1;
else
ans = y - it1->first;
u[x] = {y - ans + 1, y};
}
} else {
if (l[y].second != 0) {
cout << 0 << '\n';
continue;
}
auto it = l.upper_bound(y);
p = it->second;
if (p.first == 0) {
it = u.upper_bound(x);
it--;
ans = x - it->first;
l[y] = {x - ans + 1, x};
} else {
auto it1 = u.upper_bound(x);
it1--;
if (p.first <= it1->first && p.second >= it1->first)
ans = x - p.first + 1;
else
ans = x - it1->first;
l[y] = {x - ans + 1, x};
}
}
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 | 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 leftBound, rightBound, value, pendingValue;
public ShrinkedSegmentTree(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 ShrinkedSegmentTree(l, (r + l) / 2);
R = new ShrinkedSegmentTree((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());
}
}
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 | 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;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
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;
const int inf = 0x3fffffff;
struct Data {
int r, cnt;
bool w;
};
struct Node {
Data dat;
int pri;
Node *ls, *rs;
} * rt;
void zig(Node *&p) {
Node *tmp = p->rs;
p->rs = tmp->ls;
tmp->ls = p;
p = tmp;
}
void zag(Node *&p) {
Node *tmp = p->ls;
p->ls = tmp->rs;
tmp->rs = p;
p = tmp;
}
void insert(Node *&p, int r, int cnt, bool w) {
if (!p)
p = new Node{(Data){r, cnt, w}, rand(), 0, 0};
else if (r < p->dat.r) {
insert(p->ls, r, cnt, w);
if (p->ls->pri > p->pri) zag(p);
} else {
insert(p->rs, r, cnt, w);
if (p->rs->pri > p->pri) zig(p);
}
}
bool exist(Node *p, int num) {
if (!p) return false;
if (p->dat.r == num) return true;
if (num < p->dat.r) return exist(p->ls, num);
return exist(p->rs, num);
}
int getPre(Node *p, int num) {
if (!p) return -inf;
if (p->dat.r < num) return max(p->dat.r, getPre(p->rs, num));
return getPre(p->ls, num);
}
int getNext(Node *p, int num) {
if (!p) return inf;
if (p->dat.r > num) return min(p->dat.r, getNext(p->ls, num));
return getNext(p->rs, num);
}
Data getData(Node *p, int num) {
if (p->dat.r == num) return p->dat;
return num < p->dat.r ? getData(p->ls, num) : getData(p->rs, num);
}
int main() {
int n, q;
cin >> n >> q;
while (q--) {
int x, y;
char c;
cin >> x >> y >> c;
if (exist(rt, y)) {
cout << 0 << '\n';
continue;
}
if (c == 'U') {
int pre = getPre(rt, y), ans;
if (pre < 0)
ans = y;
else {
Data d = getData(rt, pre);
if (!d.w)
ans = d.cnt + y - d.r;
else
ans = y - pre;
}
cout << ans << '\n';
insert(rt, y, ans, false);
} else {
int nxt = getNext(rt, y), ans;
if (nxt >= inf)
ans = x;
else {
Data d = getData(rt, nxt);
if (!d.w)
ans = x - (n + 1 - d.r);
else
ans = d.cnt + x - (n + 1 - d.r);
}
cout << ans << '\n';
insert(rt, y, ans, true);
}
}
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.