Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, q, a, b;
char c;
map<int, int> mpU, mpL;
int main() {
scanf("%d%d", &n, &q);
mpU[0] = 0, mpL[0] = 0;
while (q--) {
scanf("%d%d", &a, &b);
getchar();
scanf("%c", &c);
if (c == 'U') {
auto itL = mpL.lower_bound(b);
auto itU = mpU.lower_bound(a);
if ((itU != mpU.end() && itU->first == a) ||
(itL != mpL.end() && itL->first == b)) {
printf("0\n");
continue;
}
itL--;
if (itU == mpU.end() || itL->first >= (n + 1 - itU->first)) {
mpU[a] = b - (itL->first);
printf("%d\n", b - (itL->first));
} else {
int res = (itU->first) - a + (itU->second);
mpU[a] = res;
printf("%d\n", res);
}
} else {
auto itL = mpL.lower_bound(b);
auto itU = mpU.lower_bound(a);
if ((itU != mpU.end() && itU->first == a) ||
(itL != mpL.end() && itL->first == b)) {
printf("0\n");
continue;
}
itU--;
if (itL == mpL.end() || itU->first >= (n + 1 - itL->first)) {
mpL[b] = a - (itU->first);
printf("%d\n", a - (itU->first));
} else {
int res = (itL->first) - b + (itL->second);
mpL[b] = res;
printf("%d\n", res);
}
}
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T mod_v(T num) {
if (num >= 0)
return num % 1000000007;
else
return (num % 1000000007 + 1000000007) % 1000000007;
}
template <class T>
inline T gcd(T a, T b) {
a = abs(a);
b = abs(b);
while (b) b ^= a ^= b ^= a %= b;
return a;
}
template <class T>
T fast_pow(T n, T p) {
if (p == 0) return 1;
if (p % 2) {
T g = mod_v(mod_v(n) * mod_v(fast_pow(n, p - 1)));
return g;
} else {
T g = fast_pow(n, p / 2);
g = mod_v(mod_v(g) * mod_v(g));
return g;
}
}
template <class T>
inline T modInverse(T n) {
return fast_pow(n, 1000000007 - 2);
}
template <class T>
inline void debug(string S1, T S2, string S3) {
cout << S1 << S2 << S3;
}
bool equalTo(double a, double b) {
if (fabs(a - b) <= 1e-9)
return true;
else
return false;
}
bool notEqual(double a, double b) {
if (fabs(a - b) > 1e-9)
return true;
else
return false;
}
bool lessThan(double a, double b) {
if (a + 1e-9 < b)
return true;
else
return false;
}
bool lessThanEqual(double a, double b) {
if (a < b + 1e-9)
return true;
else
return false;
}
bool greaterThan(double a, double b) {
if (a > b + 1e-9)
return true;
else
return false;
}
bool greaterThanEqual(double a, double b) {
if (a + 1e-9 > b)
return true;
else
return false;
}
template <class T>
inline int in(register T& num) {
register char c = 0;
num = 0;
bool n = false;
while (c < 33) c = getchar();
while (c > 33) {
if (c == '-')
n = true;
else
num = num * 10 + c - '0';
c = getchar();
}
num = n ? -num : num;
return 1;
}
class nod {
public:
int x, y;
char ch;
};
nod kp[200005];
vector<int> ax;
vector<int> ay;
int q, n, RL, DU;
int u, v, s;
class nod_1 {
public:
int tree[4 * 200002];
int lazy[4 * 200002];
void build(int p, int l, int r) {
if (l == r) {
tree[p] = 0;
lazy[p] = 0;
return;
}
int mid = (l + r) >> 1;
int left = p << 1;
int right = left | 1;
build(left, l, mid);
build(right, mid + 1, r);
tree[p] = max(tree[left], tree[right]);
lazy[p] = 0;
}
void update(int p, int l, int r) {
if (u > r || v < l) return;
if (u <= l && r <= v) {
tree[p] = max(s, tree[p]);
lazy[p] = max(s, lazy[p]);
return;
}
int mid = (l + r) >> 1;
int left = p << 1;
int right = left | 1;
if (lazy[p]) {
lazy[left] = max(lazy[left], lazy[p]);
lazy[right] = max(lazy[right], lazy[p]);
tree[left] = max(tree[left], lazy[p]);
tree[right] = max(tree[right], lazy[p]);
lazy[p] = 0;
}
update(left, l, mid);
update(right, mid + 1, r);
tree[p] = max(tree[left], tree[right]);
}
void qu(int p, int l, int r) {
if (u > r || u < l) return;
if (l == r && u == r) {
s = max(s, tree[p]);
return;
}
int mid = (l + r) >> 1;
int left = p << 1;
int right = left | 1;
if (lazy[p]) {
lazy[left] = max(lazy[left], lazy[p]);
lazy[right] = max(lazy[right], lazy[p]);
tree[left] = max(tree[left], lazy[p]);
tree[right] = max(tree[right], lazy[p]);
lazy[p] = 0;
}
qu(left, l, mid);
qu(right, mid + 1, r);
tree[p] = max(tree[left], tree[right]);
}
};
nod_1 segTree1, segTree2;
int main() {
in(n), in(q);
for (int i = 0; i < q; i++) {
scanf("%d%d %c", &kp[i].x, &kp[i].y, &kp[i].ch);
ax.push_back(kp[i].x);
ay.push_back(kp[i].y);
}
ax.push_back(0);
ay.push_back(0);
sort((ax).begin(), (ax).end());
sort((ay).begin(), (ay).end());
vector<int>::iterator it = unique((ax).begin(), (ax).end());
ax.resize(distance(ax.begin(), it));
it = unique((ay).begin(), (ay).end());
ay.resize(distance(ay.begin(), it));
map<int, int> map1;
map<int, int> map2;
for (int i = 1; i < ((int)ax.size()); i++) {
map1[ax[i]] = i;
}
for (int i = 1; i < ((int)ay.size()); i++) {
map2[ay[i]] = i;
}
RL = ax.size() - 1;
DU = ay.size() - 1;
segTree1.build(1, 1, RL);
segTree2.build(1, 1, DU);
map<pair<int, int>, int> maps;
for (int i = 0; i < q; i++) {
if (maps[make_pair(kp[i].x, kp[i].y)]) {
printf("0\n");
continue;
}
maps[make_pair(kp[i].x, kp[i].y)] = 1;
if (kp[i].ch == 'U') {
u = map1[kp[i].x];
s = 0;
segTree1.qu(1, 1, RL);
printf("%d\n", kp[i].y - s);
v = map2[kp[i].y];
u = map2[s];
s = kp[i].x;
segTree2.update(1, 1, DU);
} else {
u = map2[kp[i].y];
s = 0;
segTree2.qu(1, 1, DU);
printf("%d\n", kp[i].x - s);
v = map1[kp[i].x];
u = map1[s];
s = kp[i].y;
segTree1.update(1, 1, RL);
}
}
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> mp1, mp2;
map<int, bool> used;
inline int read() {
int red = 0, f_f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f_f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') red = red * 10 + ch - '0', ch = getchar();
return red * f_f;
}
int main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
int x = read(), y = read(), ans = 0;
char s[5];
scanf("%s", s);
if (used[x])
printf("0\n");
else if (s[0] == 'U') {
map<int, int>::iterator it = mp1.lower_bound(x);
if (it != mp1.end())
ans = (it->second);
else
ans = 0;
mp1[x] = ans;
mp2[y] = x;
printf("%d\n", y - ans);
} else {
map<int, int>::iterator it = mp2.lower_bound(y);
if (it != mp2.end())
ans = (it->second);
else
ans = 0;
mp1[x] = y;
mp2[y] = ans;
printf("%d\n", x - ans);
}
used[x] = 1;
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 |
import java.io.*;
import java.util.*;
/**
* Created by sbabkin on 9/14/2015.
*/
public class SolverE {
public static void main(String[] args) throws IOException {
new SolverE().Run();
}
BufferedReader br;
PrintWriter pw;
StringTokenizer stok;
private String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
stok = new StringTokenizer(br.readLine());
}
return stok.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
class Tree {
Tree left = null, right = null;
int toSet = -1;
public void push() {
if (toSet >= 0 && left != null) {
if (left.toSet == -1 || left.toSet < toSet) {
left.toSet = toSet;
}
if (right.toSet == -1 || right.toSet < toSet) {
right.toSet = toSet;
}
toSet = -1;
}
}
public int getMax(int ind, int curL, int curR) {
push();
if (toSet >= 0) {
return toSet;
}
int mid = (curL + curR) >> 1;
if (ind <= mid) {
return left.getMax(ind, curL, mid);
} else {
return right.getMax(ind, mid + 1, curR);
}
}
public void setMax(int max, int curL, int curR, int l, int r) {
if (l > r) {
return;
}
if (toSet>=max){
return;
}
if (l == curL && r == curR) {
if (toSet == -1 || toSet < max) {
toSet = max;
}
return;
}
int mid = (curL + curR) >> 1;
if (left == null) {
left = new Tree();
right = new Tree();
}
push();
left.setMax(max, curL, mid, l, Math.min(r, mid));
right.setMax(max, mid + 1, curR, Math.max(l, mid + 1), r);
}
}
int n;
int kolq;
// Tree hor = new Tree();
// Tree vert = new Tree();
int[] toSet;
int[] leftInd, rightInd;
int masSize;
public void push(int pos) {
if (toSet[pos] >= 0 && leftInd[pos] >=0) {
if (toSet[leftInd[pos]] == -1 || toSet[leftInd[pos]] < toSet[pos]) {
toSet[leftInd[pos]] = toSet[pos];
}
if (toSet[rightInd[pos]] == -1 || toSet[rightInd[pos]] < toSet[pos]) {
toSet[rightInd[pos]] = toSet[pos];
}
toSet[pos] = -1;
}
}
public int getMax(int pos, int ind, int curL, int curR) {
push(pos);
if (toSet[pos] >= 0) {
return toSet[pos];
}
int mid = (curL + curR) >> 1;
if (ind <= mid) {
return getMax(leftInd[pos], ind, curL, mid);
} else {
return getMax(rightInd[pos], ind, mid + 1, curR);
}
}
public void setMax(int pos, int max, int curL, int curR, int l, int r) {
if (l > r) {
return;
}
if (toSet[pos]>=max){
return;
}
if (l == curL && r == curR) {
if (toSet[pos] == -1 || toSet[pos] < max) {
toSet[pos] = max;
}
return;
}
int mid = (curL + curR) >> 1;
if (leftInd[pos] < 0) {
leftInd[pos]=masSize;
rightInd[pos]=masSize+1;
masSize+=2;
}
push(pos);
setMax(leftInd[pos], max, curL, mid, l, Math.min(r, mid));
setMax(rightInd[pos], max, mid + 1, curR, Math.max(l, mid + 1), r);
}
private void Run() throws IOException {
// br = new BufferedReader(new FileReader("input.txt"));
// pw = new PrintWriter("output.txt");
br=new BufferedReader(new InputStreamReader(System.in));
pw=new PrintWriter(new OutputStreamWriter(System.out));
solve();
pw.flush();
pw.close();
}
private void solve() throws IOException {
n = nextInt();
kolq = nextInt();
// hor.toSet = 0;
// vert.toSet = 0;
toSet =new int[62*kolq];
leftInd=new int[62*kolq];
rightInd=new int[62*kolq];
Arrays.fill(toSet, -1);
Arrays.fill(leftInd, -1);
Arrays.fill(rightInd, -1);
masSize=2;
toSet[0]=0;
toSet[1]=0; //vert - 0 , hor - 1
Set<Integer> setUsed = new HashSet<Integer>();
for (int i = 0; i < kolq; i++) {
int x = nextInt();
int y = nextInt();
String type = nextToken();
if (type.equals("L")) {
if (setUsed.contains(x)){
pw.println(0);
} else {
int start = getMax(0, y, 1, n);
pw.println(x - start);
setMax(1, y, 1, n, start + 1, x);
// setMax(0, x, 1, n, y, y);
setUsed.add(x);
}
} else {
if (setUsed.contains(x)){
pw.println(0);
} else {
int start = getMax(1, x, 1, n);
pw.println(y - start);
setMax(0, x, 1, n, start + 1, y);
// setMax(1, y, 1, n, x, x);
setUsed.add(x);
}
}
}
}
}
| JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q, x, y, ans;
char cc;
cin >> n >> q;
map<int, pair<char, int>> mp;
mp[0] = make_pair('U', n + 1);
mp[n + 1] = make_pair('L', n + 1);
while (q--) {
cin >> x >> y >> cc;
auto it = mp.lower_bound(x);
if (it->first == x) {
cout << 0 << '\n';
continue;
}
if (cc == 'L') it--;
ans = abs(it->first - x);
if (it->second.first == cc) ans += it->second.second;
mp[x] = make_pair(cc, ans);
cout << ans << '\n';
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Set;
/**
* Created by hama_du on 15/07/09.
*/
public class C {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int q = in.nextInt();
int[][] queries = new int[q][3];
for (int i = 0; i < q ; i++) {
queries[i][0] = in.nextInt()-1;
queries[i][1] = in.nextInt()-1;
queries[i][2] = in.nextChar();
}
Set<Integer> doneX = new HashSet<>();
LargeSegmentTree rowSeg = new LargeSegmentTree(n);
LargeSegmentTree colSeg = new LargeSegmentTree(n);
for (int i = 0 ; i < q ; i++) {
if (queries[i][2] == 'L') {
rowSeg.dig(queries[i][1]);
} else {
colSeg.dig(queries[i][0]);
}
}
for (int i = 0 ; i < q ; i++) {
if (doneX.contains(queries[i][0])) {
out.println(0);
continue;
}
doneX.add(queries[i][0]);
if (queries[i][2] == 'L') {
int rightMost = rowSeg.max(queries[i][1]);
colSeg.apply(Math.max(rightMost, 0), queries[i][0] + 1, queries[i][1]);
out.println(queries[i][0] - rightMost);
} else {
int downMost = colSeg.max(queries[i][0]);
rowSeg.apply(Math.max(downMost, 0), queries[i][1] + 1, queries[i][0]);
out.println(queries[i][1] - downMost);
}
}
out.flush();
}
static class LargeSegmentTree {
static final int MAX = 3000000;
int n;
int nodeID;
int[] fr;
int[] to;
int[] value;
int[] leftID;
int[] rightID;
// [0,n)
LargeSegmentTree(int _n) {
n = _n;
fr = new int[MAX];
to = new int[MAX];
value = new int[MAX];
leftID = new int[MAX];
rightID = new int[MAX];
Arrays.fill(value, -1);
Arrays.fill(leftID, -1);
Arrays.fill(rightID, -1);
addNode(0, n);
}
void addNode(int a, int b) {
fr[nodeID] = a;
to[nodeID] = b;
nodeID++;
}
int addLeftNode(int parentID) {
if (leftID[parentID] != -1) {
return leftID[parentID];
}
fr[nodeID] = fr[parentID];
to[nodeID] = (fr[parentID] + to[parentID]) / 2;
leftID[parentID] = nodeID;
return nodeID++;
}
int addRightNode(int parentID) {
if (rightID[parentID] != -1) {
return rightID[parentID];
}
fr[nodeID] = (fr[parentID] + to[parentID]) / 2;
to[nodeID] = to[parentID];
rightID[parentID] = nodeID;
return nodeID++;
}
void dig(int pos) {
int currentID = 0;
while (to[currentID] - fr[currentID] > 1) {
int med = (to[currentID] + fr[currentID]) / 2;
if (fr[currentID] <= pos && pos < med) {
currentID = addLeftNode(currentID);
} else {
currentID = addRightNode(currentID);
}
}
}
int max(int pos) {
int currentID = 0;
int val = -1;
while (true) {
val = Math.max(val, value[currentID]);
if (to[currentID] - fr[currentID] == 1) {
break;
}
int med = (to[currentID] + fr[currentID]) / 2;
if (fr[currentID] <= pos && pos < med) {
currentID = addLeftNode(currentID);
} else {
currentID = addRightNode(currentID);
}
}
return val;
}
void apply(int a, int b, int val) {
apply(0, a, b, val);
}
// [a,b)
void apply(int id, int a, int b, int val) {
if (b <= fr[id] || to[id] <= a) {
return;
}
if (a <= fr[id] && to[id] <= b) {
value[id] = Math.max(value[id], val);
return;
}
if (to[id] - fr[id] == 1) {
return;
}
if (leftID[id] != -1) {
apply(leftID[id], a, b, val);
}
if (rightID[id] != -1) {
apply(rightID[id], a, b, val);
}
}
}
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;
}
private int next() {
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 char nextChar() {
int c = next();
while (isSpaceChar(c))
c = next();
if ('a' <= c && c <= 'z') {
return (char) c;
}
if ('A' <= c && c <= 'Z') {
return (char) c;
}
throw new InputMismatchException();
}
public String nextToken() {
int c = next();
while (isSpaceChar(c))
c = next();
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = next();
} while (!isSpaceChar(c));
return res.toString();
}
public int nextInt() {
int c = next();
while (isSpaceChar(c))
c = next();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = next();
while (isSpaceChar(c))
c = next();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
} | 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>
int const MAX = 400400;
int const SZ = 4 * MAX;
int tree[2][SZ];
void modify(int id, int v, int l, int r, int from, int to, int val) {
if (r <= from || to <= l) return;
if (from <= l && r <= to) {
tree[id][v] = std::max(tree[id][v], val);
return;
}
int m = (l + r) / 2;
modify(id, 2 * v + 1, l, m, from, to, val);
modify(id, 2 * v + 2, m, r, from, to, val);
}
int getMax(int id, int pos) {
int v = 0, l = 0, r = MAX;
int ret = 0;
while (r - l > 1) {
ret = std::max(ret, tree[id][v]);
int m = (l + r) / 2;
if (pos < m) {
v = 2 * v + 1;
r = m;
} else {
v = 2 * v + 2;
l = m;
}
}
return std::max(ret, tree[id][v]);
}
std::unordered_set<int> was[2];
int x[MAX], y[MAX];
char dir[MAX];
int main() {
int n, q;
scanf("%d%d", &n, &q);
std::vector<int> values;
values.push_back(1);
for (int i = 0; i < q; ++i) {
scanf("%d%d %c", x + i, y + i, dir + i);
values.push_back(x[i]);
values.push_back(y[i]);
}
std::sort(values.begin(), values.end());
values.resize(std::unique(values.begin(), values.end()) - values.begin());
for (int i = 0; i < q; ++i) {
if (dir[i] == 'U') {
if (was[0].count(x[i])) {
puts("0");
} else {
int first =
getMax(1, (std::lower_bound(values.begin(), values.end(), x[i]) -
values.begin())) +
1;
printf("%d\n", y[i] - first + 1);
modify(0, 0, 0, MAX,
(std::lower_bound(values.begin(), values.end(), first) -
values.begin()),
(std::lower_bound(values.begin(), values.end(), y[i]) -
values.begin()) +
1,
x[i]);
was[0].insert(x[i]);
}
} else {
if (was[1].count(y[i])) {
puts("0");
} else {
int first =
getMax(0, (std::lower_bound(values.begin(), values.end(), y[i]) -
values.begin())) +
1;
printf("%d\n", x[i] - first + 1);
modify(1, 0, 0, MAX,
(std::lower_bound(values.begin(), values.end(), first) -
values.begin()),
(std::lower_bound(values.begin(), values.end(), x[i]) -
values.begin()) +
1,
y[i]);
was[1].insert(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;
map<int, int> l, u;
int n, q, ans, x, y;
char opt;
void up() {
ans = 0;
map<int, int>::iterator it = u.lower_bound(x);
if (it == u.end())
ans = y;
else
ans = y - (it->second);
printf("%d\n", ans);
l[y] = x;
u[x] = y - ans;
}
inline void left() {
ans = 0;
map<int, int>::iterator it = l.lower_bound(y);
if (it == l.end())
ans = x;
else
ans = x - (it->second);
printf("%d\n", ans);
l[y] = x - ans;
u[x] = y;
}
inline void read() {
scanf("%d %d", &x, &y);
cin >> opt;
}
inline void out() {
cout << "0" << endl;
return;
}
int main() {
while (scanf("%d %d", &n, &q) != EOF) {
l.clear();
u.clear();
while (q--) {
read();
if (opt == 'U')
u.count(x) ? out() : up();
else
l.count(y) ? out() : left();
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class C {
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter out;
public void solve() throws IOException {
int N = nextInt();
int Q = nextInt();
HashSet<Integer> setC = new HashSet<Integer>();
HashSet<Integer> setR = new HashSet<Integer>();
int[][] queries = new int[Q][];
for (int q = 0; q < Q; q++) {
String[] SS = reader.readLine().split(" ");
int C = Integer.parseInt(SS[0]);
int R = Integer.parseInt(SS[1]);
int isL = SS[2].equals("L")? 0: 1;
queries[q] = new int[] {C, R, isL};
}
for (int q = 0; q < Q; q++) {
setC.add(queries[q][0]);
setR.add(queries[q][1]);
}
ArrayList<Integer> listC = new ArrayList<Integer>(setC);
ArrayList<Integer> listR = new ArrayList<Integer>(setR);
listC.add(0);
listR.add(0);
Collections.sort(listC);
Collections.sort(listR);
HashMap<Integer, Integer> mapC = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> mapR = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> mapCRev = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> mapRRev = new HashMap<Integer, Integer>();
for (int c: listC) {
mapC.put(c, mapC.size());
mapCRev.put(mapC.size()-1, c);
}
for (int r: listR) {
mapR.put(r, mapR.size());
mapRRev.put(mapR.size()-1, r);
}
SegmentTree rowTree = new SegmentTree(mapR.size());
SegmentTree colTree = new SegmentTree(mapC.size());
boolean[] ate = new boolean[listC.size()+1];
for (int q = 0; q < Q; q++) {
int C = mapC.get(queries[q][0]);
int R = mapR.get(queries[q][1]);
boolean isL = queries[q][2] == 0;
if (ate[C]) {
out.println(0);
continue;
}
ate[C] = true;
if (isL) {
int block = rowTree.query(R, R);
// out.println(mapCRev.get(C) + ", " + mapCRev.get(block) + ", " + C + ", " + block);
out.println(mapCRev.get(C) - mapCRev.get(block));
colTree.updateRange(block+1, C, R);
} else {
int block = colTree.query(C, C);
out.println(mapRRev.get(R) - mapRRev.get(block));
rowTree.updateRange(block+1, R, C);
}
}
}
public class SegmentTree{
int length;
int[] values;
int[] delta;
public SegmentTree(int length){
this.length = length;
int size = Integer.highestOneBit(length) * 4;
this.values = new int[size];
this.delta = new int[size];
Arrays.fill(delta, -1);
}
public int query(int l, int r){
return queryInternal(0, 0, length-1, l, r);
}
private int queryInternal(int node, int b, int e, int l, int r){
if( l > e || r < b ) return 0;
if( b >= l && e <= r ) return values[node];
propagateDelta(node, b, e);
int mid = (b+e)/2;
return Math.max(queryInternal(node*2+1, b, mid, l, r), queryInternal(node*2+2, mid+1, e, l, r));
}
public void updateRange(int l, int r, int v){
updateRangeInternal(0, 0, length-1, l, r, v);
}
private void updateRangeInternal(int node, int b, int e, int l, int r, int v){
// out.println("inc range: " + b + ", " + e);
if( l > e || r < b ) return;
if( b >= l && e <= r) {
delta[node] = Math.max(delta[node], v);
applyInc(node, b, e, v);
return;
}
int mid = (b+e)/2;
propagateDelta(node, b, e);
updateRangeInternal(node*2+1, b, mid, l, r, v);
updateRangeInternal(node*2+2, mid+1, e, l, r, v);
values[node] = Math.max( values[node*2+1], values[node*2+2] );
}
private void propagateDelta(int node, int b, int e){
if( delta[node] == -1) return;
int mid = (b+e)/2;
delta[node*2+1] = Math.max(delta[node*2+1], delta[node]);
delta[node*2+2] = Math.max(delta[node*2+2], delta[node]);
applyInc(node*2+1, b, mid, delta[node]);
applyInc(node*2+2, mid+1, e, delta[node]);
delta[node] = -1;
}
private void applyInc(int node, int b, int e, int v){
values[node] = Math.max(values[node], v);
}
}
/**
* @param args
*/
public static void main(String[] args) {
new C().run();
}
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
out = new PrintWriter(System.out);
solve();
reader.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.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> mp;
int x[200010], y[200010];
int n, q;
int main() {
ios::sync_with_stdio(false);
cin >> n >> q;
mp[0] = mp[n + 1] = q;
while (q--) {
char c;
cin >> x[q] >> y[q] >> c;
map<int, int>::iterator it = mp.lower_bound(x[q]);
if (it->first == x[q]) {
printf("0\n");
continue;
}
mp[x[q]] = q;
if (c == 'U') {
printf("%d\n", y[q] - y[it->second]);
y[q] = y[it->second];
} else {
it--;
it--;
printf("%d\n", x[q] - x[it->second]);
x[q] = x[it->second];
}
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, m;
int a, b;
char c;
set<pair<int, int> > st;
set<pair<int, int> >::iterator it;
int x[200005], y[200005];
int main() {
cin >> n >> m;
int i;
st.insert(make_pair(0, 0));
st.insert(make_pair(n + 1, m + 1));
for (i = 1; i <= m; i++) {
cin >> y[i] >> x[i];
getchar();
cin >> c;
if (c == 'U') {
it = st.lower_bound(make_pair(x[i], -1));
if (it->first > x[i]) it--;
} else
it = st.upper_bound(make_pair(x[i], -1));
if (it->first == x[i]) {
puts("0");
continue;
}
st.insert(make_pair(x[i], i));
if (c == 'U') {
printf("%d\n", x[i] - x[it->second]);
x[i] = x[it->second];
} else {
printf("%d\n", y[i] - y[it->second]);
y[i] = y[it->second];
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
char c[3];
map<int, int> vis;
map<int, int> vis1;
set<int> st1;
set<int> st2;
int main() {
int n, m, i, j, k, x, y, z, u, v;
scanf("%d%d", &n, &m);
set<int>::iterator it1, it2;
st1.insert(n + 1);
st2.insert(n + 1);
st1.insert(0);
st2.insert(0);
while (m--) {
scanf("%d%d%s", &y, &x, c);
if (vis1[x] != 0) {
printf("0\n");
continue;
}
if (c[0] == 'U') {
vis[0] = 0;
vis[n + 1] = 0;
it1 = st1.upper_bound(y);
it2 = st2.upper_bound(x);
it2--;
if (*it1 > n - *it2 + 1) {
z = vis[x] = *it2;
} else if (*it1 < n - *it2 + 1) {
z = vis[x] = vis[n + 1 - *it1];
} else {
z = 0;
}
printf("%d\n", x - z);
st1.insert(y);
} else {
it1 = st1.upper_bound(y);
it2 = st2.upper_bound(x);
it1--;
if (*it1 < n - *it2 + 1) {
z = vis[x] = vis[*it2];
} else if (*it1 > n - *it2 + 1) {
z = vis[x] = *it1;
} else {
z = 0;
}
printf("%d\n", y - z);
st2.insert(x);
}
vis1[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;
const int Len = 2333333;
char buf[Len], *p1 = buf, *p2 = buf, duf[Len], *q1 = duf;
inline char gc();
inline int rd();
inline void pc(char c);
inline void rt(int x);
inline void flush();
template <class T>
inline T Max(T a, T b) {
return a > b ? a : b;
}
template <class T>
inline T Min(T a, T b) {
return a < b ? a : b;
}
int n, q, u, v, x, y, k, ans;
struct Node {
int l, r, v;
};
struct tree {
int cnt;
Node a[6662333];
void Query(int u, int l, int r) {
ans = Min(ans, a[u].v);
if (l == r) return;
int mid = (l + r) >> 1;
if (x <= mid) {
if (!a[u].l) return;
Query(a[u].l, l, mid);
} else {
if (!a[u].r) return;
Query(a[u].r, mid + 1, r);
}
}
void Modify(int u, int l, int r) {
if (r < x || l > y) return;
if (l >= x && r <= y) return void(a[u].v = Min(a[u].v, k));
int mid = (l + r) >> 1;
if (!a[u].l) a[u].l = ++cnt, a[cnt].v = n + 1;
if (!a[u].r) a[u].r = ++cnt, a[cnt].v = n + 1;
Modify(a[u].l, l, mid), Modify(a[u].r, mid + 1, r);
}
} tr1, tr2;
unordered_map<int, int> vis;
inline char Get() {
char c;
while ((c = gc()) != 'L' && c != 'U')
;
return c;
}
int main() {
n = rd(), q = rd(), tr1.cnt = tr2.cnt = 1, tr1.a[1].v = tr2.a[1].v = n + 1;
while (q--) {
u = rd(), v = rd();
if (vis[u]) {
rt(0), pc('\n');
continue;
}
vis[u] = 1;
if (Get() == 'L') {
x = u, ans = n + 1, tr1.Query(1, 1, n), ans = n + 1 - ans, rt(u - ans),
pc('\n'), x = ans + 1, y = u, k = n + 1 - v, tr2.Modify(1, 1, n);
} else {
x = u, ans = n + 1, tr2.Query(1, 1, n), ans = n + 1 - ans, rt(v - ans),
pc('\n'), x = u, y = n - ans, k = n + 1 - u, tr1.Modify(1, 1, n);
}
}
return flush(), 0;
}
inline char gc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Len, stdin), p1 == p2)
? -1
: *p1++;
}
inline int rd() {
char c;
while (!isdigit(c = gc()) && c != '-')
;
int f = c == '-' ? c = gc(), 1 : 0, x = c ^ 48;
while (isdigit(c = gc())) x = ((x + (x << 2)) << 1) + (c ^ 48);
return f ? -x : x;
}
inline void pc(char c) {
q1 == duf + Len &&fwrite(q1 = duf, 1, Len, stdout), *q1++ = c;
}
inline void rt(int x) {
x < 0 ? pc('-'), x = -x : 0, pc((x >= 10 ? rt(x / 10), x % 10 : x) + 48);
}
inline void flush() { fwrite(duf, 1, q1 - duf, stdout); }
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class E {
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();
TreeSet<Integer> set = new TreeSet<Integer>();
set.add(0);
int[] x = new int[q], y = new int[q];
char[] c = new char[q];
for(int i = 0; i < q; ++i)
{
set.add(x[i] = sc.nextInt());
set.add(y[i] = sc.nextInt());
c[i] = sc.next().charAt(0);
}
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
int idx = 0;
for(int s: set)
map.put(s, ++idx);
SegmentTree up = new SegmentTree(idx), left = new SegmentTree(idx);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < q; ++i)
{
if(c[i] == 'U')
{
idx = map.get(x[i]);
int bound = up.query(idx);
sb.append(y[i] - bound + "\n");
left.update(x[i], map.get(bound), map.get(y[i]));
up.update(y[i], idx, idx);
}
else
{
idx = map.get(y[i]);
int bound = left.query(idx);
sb.append(x[i] - bound + "\n");
up.update(y[i], map.get(bound), map.get(x[i]));
left.update(x[i], idx, idx);
}
}
out.print(sb);
out.flush();
out.close();
}
static class SegmentTree
{
int[] sTree;
int N;
SegmentTree(int n)
{
N = 1; while(N < n) N <<= 1;
sTree = new int[N<<1];
}
int query(int x)
{
int node = 1, b = 1, e = N;
while(b != e)
{
int mid = b + e >> 1;
propagate(node);
if(x <= mid)
{
node = node<<1;
e = mid;
}
else
{
node = node<<1|1;
b = mid + 1;
}
}
return sTree[node];
}
void update(int val, int l, int r) { update(1, 1, N, l, r, val); }
void update(int node, int b, int e, int l, int r, int val)
{
if(r < b || e < l)
return;
if(l <= b && e <= r)
{
sTree[node] = Math.max(sTree[node], val);
return;
}
int mid = b + e >> 1;
propagate(node);
update(node<<1, b, mid, l, r, val);
update(node<<1|1, mid + 1, e, l, r, val);
}
void propagate(int node)
{
sTree[node<<1] = Math.max(sTree[node<<1], sTree[node]);
sTree[node<<1|1] = Math.max(sTree[node<<1|1], sTree[node]);
sTree[node] = 0;
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public Scanner(FileReader r){ br = new BufferedReader(r);}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException { return Double.parseDouble(next()); }
public boolean ready() throws IOException {return br.ready();}
}
} | 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<pair<int, int>, int> > S;
int n, q;
int main() {
cin >> n >> q;
for (int i = 0; i < q; i++) {
pair<pair<int, int>, int> nu;
char dir[2];
cin >> nu.first.first >> nu.first.second >> dir;
if (dir[0] == 'U')
nu.first.second = 0;
else
nu.first.second = 1;
pair<pair<int, int>, int> srcp;
srcp.first.first = nu.first.first;
srcp.first.second = srcp.second = 0;
std::set<pair<pair<int, int>, int> >::iterator it = S.lower_bound(srcp);
if ((*it).first.first == nu.first.first) {
nu.second = 0;
} else if (dir[0] == 'U') {
if (it == S.end()) {
nu.second = n + 1 - nu.first.first;
} else {
if ((*it).first.second == 0) {
nu.second = (*it).second + (*it).first.first - nu.first.first;
} else {
nu.second = (*it).first.first - nu.first.first;
}
}
} else {
if (it != S.begin()) --it;
if ((*it).first.first > nu.first.first) it = S.end();
if (it == S.end()) {
nu.second = nu.first.first;
} else {
if ((*it).first.second == 1) {
nu.second = (*it).second + nu.first.first - (*it).first.first;
} else {
nu.second = nu.first.first - (*it).first.first;
}
}
}
cout << nu.second << endl;
if (nu.second > 0) S.insert(nu);
}
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 const N = 2e5 + 781;
int const INF = 2e9;
int q, cn, TL[N * 4], TU[N * 4], vL[N], vU[N], h[N];
set<pair<int, int>> had;
struct z {
int x, y, t, id, hv;
z(){};
z(int x, int y, int t, int id) : x(x), y(y), t(t), id(id){};
};
vector<z> qs;
vector<pair<int, int>> a;
void initL(int v, int l, int r) {
if (l == r) {
TL[v] = INF;
} else {
initL(v * 2, l, (l + r) / 2);
initL(v * 2 + 1, (l + r) / 2 + 1, r);
TL[v] = INF;
}
}
void initU(int v, int l, int r) {
if (l == r) {
TU[v] = INF;
} else {
initU(v * 2, l, (l + r) / 2);
initU(v * 2 + 1, (l + r) / 2 + 1, r);
TU[v] = INF;
}
}
int getMinL(int v, int tl, int tr, int l, int r) {
if (l > r) {
return INF;
} else {
if (tl == l && tr == r) {
return TL[v];
} else {
int tm = (tl + tr) / 2;
return min(getMinL(v * 2, tl, tm, l, min(tm, r)),
getMinL(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
}
}
int getMinU(int v, int tl, int tr, int l, int r) {
if (l > r) {
return INF;
} else {
if (tl == l && tr == r) {
return TU[v];
} else {
int tm = (tl + tr) / 2;
return min(getMinU(v * 2, tl, tm, l, min(tm, r)),
getMinU(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
}
}
bool updL(int v, int tl, int tr, int p, int val) {
if (tl == tr) {
if (TL[v] != INF) {
return false;
}
TL[v] = val;
return true;
} else {
int tm = (tl + tr) / 2;
bool r;
if (p <= tm) {
r = updL(v * 2, tl, tm, p, val);
} else {
r = updL(v * 2 + 1, tm + 1, tr, p, val);
}
TL[v] = min(TL[v * 2 + 1], TL[v * 2]);
return r;
}
}
bool updU(int v, int tl, int tr, int p, int val) {
if (tl == tr) {
if (TU[v] != INF) {
return false;
}
TU[v] = val;
return true;
} else {
int tm = (tl + tr) / 2;
bool r;
if (p <= tm) {
r = updU(v * 2, tl, tm, p, val);
} else {
r = updU(v * 2 + 1, tm + 1, tr, p, val);
}
TU[v] = min(TU[v * 2 + 1], TU[v * 2]);
return r;
}
}
int proceedU(z cq) {
if (had.find(make_pair(cq.x, cq.y)) != had.end()) {
return 0;
}
had.insert(make_pair(cq.x, cq.y));
int l = cq.hv;
int r = q;
int mi;
while (r - l > 1) {
int m = (r + l) / 2;
mi = getMinL(1, 0, q - 1, cq.hv + 1, m);
if (mi <= cq.x) {
r = m;
} else {
l = m;
}
}
if (r == q) {
mi = INF;
} else {
mi = getMinL(1, 0, q - 1, cq.hv + 1, r);
}
vU[cq.hv] = cq.x;
if (mi > cq.x) {
updU(1, 0, q - 1, cq.hv, 1);
return cq.y;
} else {
int lx = vL[r];
updU(1, 0, q - 1, cq.hv, cq.y - (lx - cq.x) + 1);
return (lx - cq.x);
}
}
int proceedL(z cq) {
if (had.find(make_pair(cq.x, cq.y)) != had.end()) {
return 0;
}
had.insert(make_pair(cq.x, cq.y));
int l = -1;
int r = cq.hv;
int mi;
while (r - l > 1) {
int m = (r + l) / 2;
mi = getMinU(1, 0, q - 1, m, cq.hv - 1);
if (mi <= cq.y) {
l = m;
} else {
r = m;
}
}
if (l == -1) {
mi = INF;
} else {
mi = getMinU(1, 0, q - 1, l, cq.hv - 1);
}
vL[cq.hv] = cq.x;
if (mi > cq.y) {
updL(1, 0, q - 1, cq.hv, 1);
return cq.x;
} else {
int lx = vU[l];
updL(1, 0, q - 1, cq.hv, lx + 1);
return (cq.x - lx);
}
}
int main() {
scanf("%d", &q);
scanf("%d", &q);
for (int i = 0; i < q; ++i) {
int x, y;
char t;
scanf("%d %d", &x, &y);
getchar();
t = getchar();
if (t == 'U') {
qs.push_back(z(x, y, 0, i));
a.push_back(make_pair(x, i));
} else {
qs.push_back(z(x, y, 1, i));
a.push_back(make_pair(x, i));
}
}
sort(a.begin(), a.end());
for (int i = 0; i < a.size(); ++i) {
if (i > 0 && a[i].first != a[i - 1].first) {
++cn;
}
qs[a[i].second].hv = cn;
h[cn] = a[i].first;
}
initL(1, 0, q - 1);
initU(1, 0, q - 1);
for (int i = 0; i < q; ++i) {
int r;
if (qs[i].t == 0) {
r = proceedU(qs[i]);
} else {
r = proceedL(qs[i]);
}
printf("%d\n", 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;
const int N = 400010;
int q;
long long k;
int n = 0;
map<pair<int, int>, int> was;
set<pair<int, int> > u;
set<pair<int, int> > l;
set<pair<int, int> >::iterator it;
char typ[N];
pair<int, int> qer[N];
int en = 0;
map<int, int> id;
int name[N];
int cod[N];
int tre[2][4 * N];
int mx(int u, int tl, int tr, int l, int r, int typ) {
if (l > r) return 1e9;
if (tl == l && tr == r) {
return tre[typ][u];
}
int mid = (tl + tr) >> 1;
int t1 = mx(2 * u, tl, mid, l, min(r, mid), typ);
int t2 = mx(2 * u + 1, mid + 1, tr, max(mid + 1, l), r, typ);
return min(t1, t2);
}
void add(int u, int tl, int tr, int l, int r, int v, int typ) {
if (l > r) return;
if (tl == l && tr == r) {
tre[typ][u] = v;
return;
}
int mid = (tl + tr) >> 1;
add(2 * u, tl, mid, l, min(r, mid), v, typ);
add(2 * u + 1, mid + 1, tr, max(mid + 1, l), r, v, typ);
tre[typ][u] = min(tre[typ][2 * u], tre[typ][2 * u + 1]);
}
int main() {
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 4 * N; ++j) tre[i][j] = 1e9;
scanf("%d %d", &n, &q);
for (int i = 0; i < q; ++i)
scanf("%d %d %c\n", &qer[i].first, &qer[i].second, &typ[i]);
for (int i = 0; i < q; ++i) {
cod[en++] = qer[i].first;
cod[en++] = qer[i].second;
}
cod[en++] = 0;
add(1, 0, en - 1, 0, 0, -1e9, 0);
add(1, 0, en - 1, 0, 0, -1e9, 1);
sort(cod, cod + en);
for (int i = 0; i < en; ++i)
if (id.find(cod[i]) == id.end()) {
id[cod[i]] = id.size();
name[id.size() - 1] = cod[i];
}
en = id.size();
for (int i = 0; i < q; ++i) {
int x = id[qer[i].first], y = id[qer[i].second];
if (was[pair<int, int>(x, y)]) {
printf("0\n");
continue;
}
was[pair<int, int>(x, y)] = 1;
int zn[2] = {x, y};
int ityp = !(typ[i] == 'L');
int l = 0, r = zn[ityp], mid, ans = 1e9;
int res = 0;
while (l <= r) {
int mid = (l + r) >> 1;
int t = mx(1, 0, en - 1, mid, zn[ityp], ityp);
if (t <= zn[ityp ^ 1]) {
l = mid + 1;
ans = mid;
} else {
r = mid - 1;
}
}
if (ans == 1e9) {
printf("0\n");
continue;
}
res = name[zn[ityp]] - name[ans];
ityp ^= 1;
add(1, 0, en - 1, zn[ityp], zn[ityp], ans, ityp);
printf("%d\n", res);
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int const M = 202020;
long long const mod = 1000000007LL;
map<int, int> mpu, mpl, mpu2, mpl2;
int idu, idl, idu2, idl2;
int getUId(int x) {
if (mpu.find(x) == mpu.end()) mpu[x] = idu++;
return mpu[x];
}
int getUId2(int x) {
if (mpu2.find(x) == mpu2.end()) mpu2[x] = idu2++;
return mpu2[x];
}
int l[M], r[M];
set<int> su, sl;
set<int> ssu, ssl;
set<int> mark;
int main() {
int n, q;
scanf("%d%d", &n, &q);
sl.insert(n + 1);
su.insert(n + 2);
ssu.insert(n + 1);
ssl.insert(n + 2);
int x, y;
char ch[5];
int ans;
for (int i = 0; i < (q); ++i) {
scanf(" %d %d %s", &x, &y, ch);
if (mark.find(x) != mark.end()) {
puts("0");
continue;
}
mark.insert(x);
int ty = n + 1 - y;
int tx = n + 1 - x;
if (ch[0] == 'U') {
int nu = *su.lower_bound(ty);
int uid = getUId(nu);
int nl = *sl.lower_bound(ty);
int lid = getUId(nl);
int id = getUId(ty);
if (nl < nu) {
ans = nl - ty;
l[id] = nl;
} else {
ans = l[uid] - ty;
l[id] = l[uid];
}
su.insert(ty);
ssu.insert(tx);
} else if (ch[0] == 'L') {
int nu = *ssu.lower_bound(tx);
int uid = getUId2(nu);
int nl = *ssl.lower_bound(tx);
int lid = getUId2(nl);
int id = getUId2(tx);
if (nu < nl) {
ans = nu - tx;
r[id] = nu;
} else {
ans = r[lid] - tx;
r[id] = r[lid];
}
sl.insert(ty);
ssl.insert(tx);
}
printf("%d\n", ans);
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
int n, q;
int mn[N << 2];
vector<int> vx, vy;
int x[N], y[N], d[N];
int idx(int val) { return lower_bound(vx.begin(), vx.end(), val) - vx.begin(); }
int idy(int val) { return lower_bound(vy.begin(), vy.end(), val) - vy.begin(); }
int mx[2][N << 2], lz[2][N << 2];
void pushdown(int id, int rt) {
if (lz[id][rt]) {
mx[id][rt << 1] = max(mx[id][rt << 1], lz[id][rt]);
lz[id][rt << 1] = max(lz[id][rt << 1], lz[id][rt]);
mx[id][rt << 1 | 1] = max(mx[id][rt << 1 | 1], lz[id][rt]);
lz[id][rt << 1 | 1] = max(lz[id][rt << 1 | 1], lz[id][rt]);
lz[id][rt] = 0;
}
}
void update(int id, int l, int r, int rt, int L, int R, int val) {
if (L <= l && r <= R) {
mx[id][rt] = max(mx[id][rt], val);
lz[id][rt] = max(lz[id][rt], val);
return;
}
int mid = (l + r) >> 1;
pushdown(id, rt);
if (L <= mid) update(id, l, mid, rt << 1, L, R, val);
if (R > mid) update(id, mid + 1, r, rt << 1 | 1, L, R, val);
mx[id][rt] = max(mx[id][rt << 1], mx[id][rt << 1 | 1]);
}
int query(int id, int l, int r, int rt, int pos) {
if (l == r) return mx[id][rt];
int mid = (l + r) >> 1;
pushdown(id, rt);
if (pos <= mid) return query(id, l, mid, rt << 1, pos);
return query(id, mid + 1, r, rt << 1 | 1, pos);
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= q; i++) {
scanf("%d%d", &x[i], &y[i]);
char s[2];
scanf("%s", s);
if (s[0] == 'U') d[i] = 1;
vx.push_back(x[i]);
vy.push_back(y[i]);
}
vx.push_back(0);
vy.push_back(0);
sort(vx.begin(), vx.end());
sort(vy.begin(), vy.end());
vx.erase(unique(vx.begin(), vx.end()), vx.end());
vy.erase(unique(vy.begin(), vy.end()), vy.end());
set<int> st;
for (int i = 1; i <= q; i++) {
if (st.count(x[i])) {
printf("0\n");
continue;
}
st.insert(x[i]);
if (d[i] == 1) {
int pos = query(0, 1, vx.size(), 1, idx(x[i]));
printf("%d\n", y[i] - vy[pos]);
update(1, 1, vy.size(), 1, pos + 1, idy(y[i]), idx(x[i]));
} else {
int pos = query(1, 1, vy.size(), 1, idy(y[i]));
printf("%d\n", x[i] - vx[pos]);
update(0, 1, vx.size(), 1, pos + 1, idx(x[i]), idy(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 | import java.util.*;
import java.io.*;
/*
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
6 2
3 4 U
6 1 L
10 10
5 6 U
4 7 U
8 3 L
8 3 L
1 10 U
9 2 U
10 1 L
10 1 L
8 3 U
8 3 U
6
7
3
0
10
2
1
0
0
0
*/
public class chocolate
{
public static void main(String[] arg)
{
new chocolate();
}
public chocolate()
{
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int q = in.nextInt();
Node horizontal = new Node(0, n-1, n-1);
Node vertical = new Node(0, n-1, n-1);
for(int cc = 0; cc < q; cc++)
{
int x = in.nextInt()-1;
int y = in.nextInt()-1;
char op = in.next().charAt(0);
if(op == 'U')
{
if(query(x, x, x+1, vertical) != -1)
{
out.println("0");
}
else
{
int temp = query(0, y, n-x, horizontal);
int height = y-temp;
update(x, x, height+x, vertical);
out.println(height);
}
}
else
{
if(query(y, y, y+1, horizontal) != -1)
{
out.println("0");
}
else
{
int temp = query(0, x, n-y, vertical);
int height = x-temp;
update(y, y, height+y, horizontal);
out.println(height);
}
}
}
in.close(); out.close();
}
int query(int left, int right, int value, Node head)
{
if(head == null || head.li > right || head.ri < left) return -1;
int op3 = -1;
if(left <= head.li && head.ri <= right)
{
if(head.max < value) return -1;
op3 = head.li;
}
// int op3 = head.li;
int op1 = query(left, right, value, head.rc);
if(op1 != -1) return op1;
int op2 = query(left, right, value, head.lc);
return Math.max(op2, op3);
// int op3 = -1;
// int op1 = query(left, right, value, head.lc);
// int op2 = query(left, right, value, head.rc);
// int op3 = head.li;
// return Math.max(Math.max(op1, op2), op3);
}
void update(int left, int right, int value, Node head)
{
if(head.li > right || head.ri < left) return;
if(left <= head.li && head.ri <= right)
{
head.max = Math.max(head.max, value);
return;
}
if(head.lc == null) head.lc = new Node(head.li, head.li + (head.ri-head.li)/2, head.li + (head.ri-head.li)/2);
update(left, right, value, head.lc);
if(head.rc == null) head.rc = new Node(head.li + (head.ri-head.li)/2 + 1, head.ri, head.ri);
update(left, right, value, head.rc);
head.max = Math.max(head.lc.max, head.rc.max);
}
class Node
{
int li, ri;
int max;
Node lc, rc;
public Node(int a, int b, int c)
{
li = a; ri = b;
max = c;
lc = rc = null;
}
}
}
class FastScanner
{
final BufferedReader reader;
String line;
int index;
public FastScanner(InputStream in)
{
reader = new BufferedReader(new InputStreamReader(in));
}
public FastScanner(File f) throws FileNotFoundException
{
reader = new BufferedReader(new FileReader(f));
}
public String readLine()
{
try
{
String line = reader.readLine();
if(line == null) throw new IOException("End of stream");
return line;
}
catch(IOException exception)
{
throw new RuntimeException(exception);
}
}
public String nextLine()
{
if(line==null) return readLine();
String s = line;
line = null;
return s.substring(index);
}
public String next()
{
if(line == null)
{
line = readLine();
index = 0;
}
while(index >= line.length())
{
line = readLine();
index = 0;
}
while(Character.isWhitespace(line.charAt(index)))
{
index++;
while(index >= line.length())
{
line = readLine();
index = 0;
}
}
int endIndex = index;
while(endIndex < line.length() && !Character.isWhitespace(line.charAt(endIndex)))
endIndex++;
String s = line.substring(index, endIndex);
index = endIndex;
return s;
}
public int nextInt() { return Integer.parseInt(next()); }
public long nextLong() { return Long.parseLong(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public void close() {;}
}
| JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, q;
const int N = (1 << 20) + 2, MOD = 1000000007LL;
void max_self(int &x, int y) { x = max(x, y); }
struct node {
int lazy{}, val{};
node *left{}, *right{};
void extend() {
if (left == nullptr) {
left = new node();
right = new node();
}
}
};
struct segmentTree {
node *root;
segmentTree() : root(new node()) {}
void push_down(node *ni, int ns, int ne) {
if (ns == ne) return;
node *lf = ni->left, *rt = ni->right;
if (lf) {
max_self(lf->lazy, ni->lazy);
max_self(lf->val, ni->lazy);
}
if (rt) {
max_self(rt->lazy, ni->lazy);
max_self(rt->val, ni->lazy);
}
}
void update(int qs, int qe, int val) { update(qs, qe, val, root); }
void update(int qs, int qe, int val, node *ni, int ns = 1, int ne = n) {
if (qs > ne || qe < ns) return;
if (ns >= qs && ne <= qe) {
max_self(ni->val, val);
max_self(ni->lazy, val);
return;
}
ni->extend();
push_down(ni, ns, ne);
int mid = ns + (ne - ns) / 2;
update(qs, qe, val, ni->left, ns, mid);
update(qs, qe, val, ni->right, mid + 1, ne);
ni->val = max(ni->left->val, ni->right->val);
}
int query(int qs, int qe) { return query(qs, qe, root); }
int query(int qs, int qe, node *ni, int ns = 1, int ne = n) {
if (qs > ne || qe < ns) return 0;
if (ns >= qs && ne <= qe) {
return ni->val;
}
ni->extend();
push_down(ni, ns, ne);
int mid = ns + (ne - ns) / 2;
return max(query(qs, qe, ni->left, ns, mid),
query(qs, qe, ni->right, mid + 1, ne));
}
};
segmentTree a, b;
int main() {
scanf("%d%d", &n, &q);
for (int i = 0; i < q; i++) {
int X, Y;
char typ;
scanf("%d%d %c", &X, &Y, &typ);
if (typ == 'U') {
int st = a.query(X, X);
int ans = Y - st;
cout << ans << endl;
a.update(X, X, Y);
b.update(st, Y, X);
} else {
int st = b.query(Y, Y);
int ans = X - st;
cout << ans << endl;
a.update(st, X, Y);
b.update(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 | 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>
{
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;
}
@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>
using namespace std;
int n, q, p, x, y;
map<int, pair<char, int> > m;
int qq() {
char c;
scanf("%d%d %c", &x, &y, &c);
map<int, pair<char, int> >::iterator b = m.lower_bound(x);
if (b->first == x) return 0;
if (c == 'L') --b;
int ans = abs(b->first - x);
if (b->second.first == c) ans += b->second.second;
m[x] = {c, ans};
return ans;
}
int main() {
int i;
cin >> n >> q;
m[0] = {'U', 0};
m[n + 1] = {'L', 0};
for (i = 1; i <= q; i++) {
cout << qq() << 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;
int n, m, ans;
map<int, pair<char, int> > Map;
map<int, pair<char, int> >::iterator it;
char c[20];
inline int abs(int a) { return a > 0 ? a : -a; }
int main() {
int i, x, y;
scanf("%d %d", &n, &m);
Map[0] = {'U', 0};
Map[n + 1] = {'L', 0};
while (m--) {
scanf("%d%d%s", &x, &y, c);
it = Map.lower_bound(x);
if (it->first == x) {
puts("0");
continue;
}
if (c[0] == 'L') it--;
ans = abs((it->first) - x);
if (it->second.first == c[0]) ans += it->second.second;
printf("%d\n", ans);
Map[x] = {c[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 N = 2e5 + 5;
set<pair<int, int> > st;
set<pair<int, int> >::iterator it;
int n, q;
char ch[5];
int x[N], y[N];
int main() {
scanf("%d%d", &n, &q);
st.insert(make_pair(0, 0));
st.insert(make_pair(n + 1, q + 1));
for (int i = 1; i <= q; i++) {
scanf("%d%d%s", &y[i], &x[i], ch);
if (ch[0] == 'U') {
it = st.lower_bound(make_pair(x[i], -1));
if (it->first > x[i]) it--;
} else {
it = st.upper_bound(make_pair(x[i], -1));
}
if (it->first == x[i]) {
puts("0");
continue;
}
st.insert(make_pair(x[i], i));
if (ch[0] == 'U') {
printf("%d\n", x[i] - x[it->second]);
x[i] = x[it->second];
} else {
printf("%d\n", y[i] - y[it->second]);
y[i] = y[it->second];
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 5;
map<int, int> mp;
map<int, int>::iterator it;
int res[MAXN];
char oper[MAXN];
int n, m, x;
int main() {
scanf("%d%d", &n, &m);
memset(res, 0, sizeof res);
mp[0] = 0, mp[n + 1] = m + 1;
oper[0] = 'U', oper[m + 1] = 'L';
res[0] = 0, res[m + 1] = 0;
for (int i = 1, tmp; i <= m; i++) {
scanf("%d %d %c", &x, &tmp, &oper[i]);
it = mp.lower_bound(x);
if (it->first == x) {
puts("0");
continue;
}
if (oper[i] == 'L') it--;
int ans = abs((it->first) - x);
if (it != mp.end() && oper[it->second] == oper[i]) {
ans += res[it->second];
}
printf("%d\n", ans);
res[i] = ans;
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;
struct node {
int l, r;
int num;
} Treex[4000009], Treey[4000009];
map<int, int> vis;
int i;
int tot = 1;
int Tot = 1;
int x, y;
char str[5];
void update_y(int ll, int rr, int c, int l, int r, int t) {
if (ll <= l && r <= rr)
Treey[t].num = max(Treey[t].num, c);
else {
if (ll <= ((l + r) >> 1)) {
if (!Treey[t].l) Treey[t].l = ++tot;
update_y(ll, rr, c, l, ((l + r) >> 1), Treey[t].l);
}
if (rr > ((l + r) >> 1)) {
if (!Treey[t].r) Treey[t].r = ++tot;
update_y(ll, rr, c, ((l + r) >> 1) + 1, r, Treey[t].r);
}
}
}
void update_x(int ll, int rr, int c, int l, int r, int t) {
if (ll <= l && r <= rr)
Treex[t].num = max(Treex[t].num, c);
else {
if (ll <= ((l + r) >> 1)) {
if (!Treex[t].l) Treex[t].l = ++Tot;
update_x(ll, rr, c, l, ((l + r) >> 1), Treex[t].l);
}
if (rr > ((l + r) >> 1)) {
if (!Treex[t].r) Treex[t].r = ++Tot;
update_x(ll, rr, c, ((l + r) >> 1) + 1, r, Treex[t].r);
}
}
}
void query_x(int c, int l, int r, int rt) {
i = max(i, Treex[rt].num);
if (l == r) return;
if (((l + r) >> 1) >= c)
query_x(c, l, ((l + r) >> 1), Treex[rt].l);
else
query_x(c, ((l + r) >> 1) + 1, r, Treex[rt].r);
}
void query_y(int c, int l, int r, int rt) {
i = max(i, Treey[rt].num);
if (l == r) return;
if (((l + r) >> 1) >= c)
query_y(c, l, ((l + r) >> 1), Treey[rt].l);
else
query_y(c, ((l + r) >> 1) + 1, r, Treey[rt].r);
}
int main() {
int n, q;
scanf("%d%d", &n, &q);
update_x(0, n, 0, 0, n, 1);
update_y(0, n, 0, 0, n, 1);
while (q--) {
scanf("%d%d%s", &x, &y, str);
if (vis[x]) {
puts("0");
} else {
vis[x] = 1;
i = 0;
if (str[0] == 'U') {
query_x(x, 0, n, 1);
printf("%d\n", y - i);
update_y(i, y, x, 0, n, 1);
} else {
query_y(y, 0, n, 1);
printf("%d\n", x - i);
update_x(i, x, y, 0, n, 1);
}
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int TOUR = 1 << 30;
struct node {
int maxx, prop;
node *l, *r;
node(int _maxx, int _prop, node *_l, node *_r)
: maxx(_maxx), prop(_prop), l(_l), r(_r) {}
node() {}
};
struct tour {
node *NIL, *root;
tour() {
NIL = new node(0, 0, NIL, NIL);
NIL->l = NIL->r = NIL;
root = new node(0, 0, NIL, NIL);
}
void prop(node *p) {
p->l->maxx = max(p->l->maxx, p->prop);
p->l->prop = max(p->l->prop, p->prop);
p->r->maxx = max(p->r->maxx, p->prop);
p->r->prop = max(p->r->prop, p->prop);
p->prop = 0;
}
void insert(node *p, int lo, int hi, int begin, int end, int val) {
if (lo >= end || hi <= begin) return;
if (lo >= begin && hi <= end) {
p->maxx = max(p->maxx, val);
p->prop = max(p->prop, val);
return;
}
if (p->l == NIL) p->l = new node(0, 0, NIL, NIL);
if (p->r == NIL) p->r = new node(0, 0, NIL, NIL);
prop(p);
insert(p->l, lo, (lo + hi) / 2, begin, end, val);
insert(p->r, (lo + hi) / 2, hi, begin, end, val);
p->maxx = max(p->l->maxx, p->r->maxx);
}
int vrati(node *p, int lo, int hi, int begin, int end) {
if (lo >= end || hi <= begin) return 0;
if (lo >= begin && hi <= end) return p->maxx;
if (p->l == NIL) p->l = new node(0, 0, NIL, NIL);
if (p->r == NIL) p->r = new node(0, 0, NIL, NIL);
prop(p);
return max(vrati(p->l, lo, (lo + hi) / 2, begin, end),
vrati(p->r, (lo + hi) / 2, hi, begin, end));
}
} Ver, Hor;
int main() {
int n, q;
scanf("%d%d", &n, &q);
for (int i = 0; i < q; i++) {
int col, row;
char c;
scanf("%d%d %c", &col, &row, &c);
if (c == 'U') {
int tmp = Hor.vrati(Hor.root, 0, TOUR, col, col + 1);
printf("%d\n", row - tmp);
Ver.insert(Ver.root, 0, TOUR, tmp + 1, row + 1, col);
Hor.insert(Hor.root, 0, TOUR, col, col + 1, row);
} else {
int tmp = Ver.vrati(Ver.root, 0, TOUR, row, row + 1);
printf("%d\n", col - tmp);
Hor.insert(Hor.root, 0, TOUR, tmp + 1, col + 1, row);
Ver.insert(Ver.root, 0, TOUR, row, row + 1, 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 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int N = 300010;
const int nbits = 31;
const int blocks = 170;
int n, q, x[N], y[N], x1[N], variable1[N], tmin[4 * N], tmax[4 * N],
tupd1[4 * N], tupd2[4 * N], ts[N];
char t[N];
pair<int, int> p1[N], p2[N];
bool used[N];
void pushMax(int v) {
if (tupd2[v] != 0) {
tmax[v * 2] = max(tmax[v * 2], tupd2[v]);
tmax[v * 2 + 1] = max(tmax[v * 2 + 1], tupd2[v]);
tupd2[v * 2] = max(tupd2[v * 2], tupd2[v]);
tupd2[v * 2 + 1] = max(tupd2[v * 2 + 1], tupd2[v]);
tupd2[v] = 0;
}
}
void updateMax(int v, int tl, int tr, int l, int r, int val) {
if (l > r) return;
if (l == tl && r == tr) {
tmax[v] = max(tmax[v], val);
tupd2[v] = max(tupd2[v], val);
return;
}
int tm = (tl + tr) / 2;
pushMax(v);
updateMax(v * 2, tl, tm, l, min(r, tm), val);
updateMax(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
tmax[v] = max(tmax[v * 2], tmax[v * 2 + 1]);
}
int getMax(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (l == tl && r == tr) return tmax[v];
int tm = (tl + tr) / 2;
pushMax(v);
return max(getMax(v * 2, tl, tm, l, min(r, tm)),
getMax(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
void pushMin(int v) {
if (tupd1[v] != inf) {
tmin[v * 2] = min(tmin[v * 2], tupd1[v]);
tmin[v * 2 + 1] = min(tmin[v * 2 + 1], tupd1[v]);
tupd1[v * 2] = min(tupd1[v * 2], tupd1[v]);
tupd1[v * 2 + 1] = min(tupd1[v * 2 + 1], tupd1[v]);
tupd1[v] = inf;
}
}
void updateMin(int v, int tl, int tr, int l, int r, int val) {
if (l > r) return;
if (l == tl && r == tr) {
tmin[v] = min(tmin[v], val);
tupd1[v] = min(tupd1[v], val);
return;
}
int tm = (tl + tr) / 2;
pushMin(v);
updateMin(v * 2, tl, tm, l, min(r, tm), val);
updateMin(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
tmin[v] = min(tmin[v * 2], tmin[v * 2 + 1]);
}
int getMin(int v, int tl, int tr, int l, int r) {
if (l > r) return inf;
if (l == tl && r == tr) return tmin[v];
int tm = (tl + tr) / 2;
pushMin(v);
return min(getMin(v * 2, tl, tm, l, min(r, tm)),
getMin(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
int main() {
scanf("%d%d\n", &n, &q);
for (int i = 1; i <= q; i++) scanf("%d%d %c\n", &x[i], &y[i], &t[i]);
for (int i = 1; i <= q; i++) p1[i] = make_pair(x[i], i);
sort(p1 + 1, p1 + q + 1);
int val = 1;
for (int i = 1; i <= q; i++) {
if (i > 1 && p1[i].first != p1[i - 1].first) val++;
x1[p1[i].second] = val;
}
for (int i = 1; i <= q; i++) p2[i] = make_pair(y[i], i);
sort(p2 + 1, p2 + q + 1);
val = 1;
for (int i = 1; i <= q; i++) {
if (i > 1 && p2[i].first != p2[i - 1].first) val++;
variable1[p2[i].second] = val;
}
for (int i = 1; i <= q; i++) ts[variable1[i]] = y[i];
for (int i = 1; i <= 4 * q; i++) {
tmin[i] = inf;
tupd1[i] = inf;
tmax[i] = 0;
tupd2[i] = 0;
}
memset(used, false, sizeof(used));
for (int i = 1; i <= q; i++) {
int res;
if (used[variable1[i]])
res = 0;
else {
used[variable1[i]] = true;
if (t[i] == 'L') {
val = getMin(1, 1, q, variable1[i], variable1[i]);
int val1;
if (val == inf) {
val = q + 1;
val1 = n + 1;
} else
val1 = ts[val];
res = x[i] - (n + 1 - val1);
updateMax(1, 1, q, variable1[i], val - 1, variable1[i]);
} else {
val = getMax(1, 1, q, variable1[i], variable1[i]);
res = y[i] - ts[val];
updateMin(1, 1, q, val + 1, variable1[i], variable1[i]);
}
}
printf("%d\n", res);
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > s;
map<int, int> mp;
int n, q;
int main() {
scanf("%d%d", &n, &q);
s.insert(make_pair(0, 1));
s.insert(make_pair(n + 1, 0));
for (; q--;) {
int x, y;
char c;
scanf("%d%d %c", &x, &y, &c);
if (mp.count(x)) {
printf("0\n");
continue;
}
if (c == 'U') {
pair<int, int> p = *s.lower_bound(make_pair(x, -1));
if (p.second == 1)
mp[x] = p.first - x + mp[p.first];
else
mp[x] = p.first - x;
s.insert(make_pair(x, 1));
} else {
set<pair<int, int> >::iterator it = s.lower_bound(make_pair(x, -1));
it--;
pair<int, int> p = *it;
if (p.second == 0)
mp[x] = x - p.first + mp[p.first];
else
mp[x] = x - p.first;
s.insert(make_pair(x, 0));
}
cout << mp[x] << "\n";
}
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | /**
* @author Juan Sebastian Beltran Rojas
* @mail [email protected]
* @veredict No enviado
* @problemId CF555C
* @problemName Case of Chocolate
* @judge http://codeforces.com/
* @category segment tree
* @level medium
* @date 1/7/2015
**/
import java.io.*;
import java.util.*;
import static java.lang.Integer.*;
import static java.lang.Math.*;
public class CF555C {
public static void main(String args[]) throws Throwable {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st=new StringTokenizer(in.readLine());
int N=parseInt(st.nextToken()), Q=parseInt(st.nextToken());
TreeSet<Integer> set=new TreeSet<Integer>();
set.add(0);
set.add(N);
int[][] arr=new int[Q][];
for (int i=0;i<Q;i++) {
st=new StringTokenizer(in.readLine());
int A=parseInt(st.nextToken()),B=parseInt(st.nextToken()),C=st.nextToken().charAt(0);
arr[i]=new int[]{A,B,C};
set.add(A);
set.add(B);
}
int[] numbers=new int[set.size()];
{
int i=0;
for(int a:set)
numbers[i++]=a;
}
SegmentTreeLazy horizontal=SegmentTreeLazy.init(new long[numbers.length]);
SegmentTreeLazy vertical=SegmentTreeLazy.init(new long[numbers.length]);
for(int i=0;i<Q;i++) {
int A=Arrays.binarySearch(numbers,arr[i][0]),B=Arrays.binarySearch(numbers,arr[i][1]),C=arr[i][2];
if(C=='L') {
long max = vertical.get(B, B+1);
int p=Arrays.binarySearch(numbers, (int)max);
sb.append(arr[i][0]-max).append("\n");
horizontal.set(p, A+1, arr[i][1]);
vertical.set(B, B+1, arr[i][0]);
} else {
long max = horizontal.get(A, A+1);
int p=Arrays.binarySearch(numbers, (int)max);
sb.append(arr[i][1]-max).append("\n");
vertical.set(p, B+1, arr[i][0]);
horizontal.set(A, A+1, arr[i][1]);
}
}
System.out.print(new String(sb));
}
static class SegmentTreeLazy {
private long setValue;
private boolean flag;
private long value;
private int cotaDesde, cotaHasta;
private SegmentTreeLazy izq,der;
public SegmentTreeLazy() {
}
static int pasos=0;
private static long[] vals;
public static SegmentTreeLazy init(long[] vals) {
SegmentTreeLazy.vals=vals;
return init(0,vals.length);
}
private static SegmentTreeLazy init(int desde, int hasta) {
if(hasta<=desde)
return null;
SegmentTreeLazy result=new SegmentTreeLazy();
result.cotaDesde=desde;
result.cotaHasta=hasta;
if(hasta-desde==1)
result.value=vals[desde];
else {
SegmentTreeLazy izq=init(desde,(hasta+desde)/2);
SegmentTreeLazy der=init((hasta+desde)/2, hasta);
result.izq=izq;
result.der=der;
//CHANGE FUNCTION
result.value=Math.max((izq==null?0:izq.value),(der==null?0:der.value));
}
return result;
}
public long set(int desde, int hasta, long newValue) {
if(flag)
propagar(setValue);
if(desde==cotaDesde&&hasta==cotaHasta) {
propagar(newValue);
}
else {
if(izq!=null&&izq.cotaDesde<=desde&&izq.cotaHasta>=hasta) {
//CHANGE FUNCTION
value=Math.max(izq.set(desde, hasta, newValue),(der!=null?der.get(der.cotaDesde, der.cotaHasta):0));
}
else if(der!=null&&der.cotaDesde<=desde&&der.cotaHasta>=hasta) {
//CHANGE FUNCTION
value=Math.max(der.set(desde, hasta, newValue),(izq!=null?izq.get(izq.cotaDesde, izq.cotaHasta):0));
}
else {
//CHANGE FUNCTION
value=Math.max(izq.set(desde, izq.cotaHasta, newValue),der.set(der.cotaDesde, hasta, newValue));
}
}
return value;
}
public void propagar(long propValue) {
flag=false;
//value=setValue;
setValue=0;
//CHANGE FUNCTION
value=Math.max(value, propValue);
if(izq!=null) {
izq.flag=true;
izq.setValue=Math.max(propValue, izq.setValue);
}
if(der!=null) {
der.flag=true;
der.setValue=Math.max(propValue, der.setValue);
}
}
public long get(int desde, int hasta) {
pasos++;
if(flag) {
propagar(setValue);
}
if(desde==cotaDesde&&hasta==cotaHasta)
return value;
if(izq!=null&&izq.cotaDesde<=desde&&izq.cotaHasta>=hasta)
return izq.get(desde, hasta);
else if(der!=null&&der.cotaDesde<=desde&&der.cotaHasta>=hasta)
return der.get(desde, hasta);
else {
//CHANGE FUNCTION
return Math.max(izq.get(desde, izq.cotaHasta),der.get(der.cotaDesde, hasta));
}
}
@Override
public String toString() {
return toString(0);
}
private String toString(int level) {
String str="";
for(int i=0;i<level;i++)
str+="\t";
str+=cotaDesde+" - "+cotaHasta+": -- "+value+" -> "+flag+" - "+setValue+"\n";
if(izq!=null)
str+=izq.toString(level+1);
if(der!=null)
str+=der.toString(level+1);
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 {
int a, x, y;
node(){};
node(int A, int X, int Y) {
a = A;
x = X;
y = Y;
}
bool operator<(const node t) const { return a < t.a; }
};
int i, n, q;
set<node> s;
int main() {
ios::sync_with_stdio(0);
cin >> n >> q;
s.insert(node(0, 0, 0));
for (i = 1; i <= q; i++) {
int x, y;
char c;
cin >> x >> y >> c;
set<node>::iterator it = s.upper_bound(node(x, 0, 0));
it--;
node tmp = *it;
if (x == it->a) {
printf("0\n");
continue;
}
if (c == 'U') {
printf("%d\n", y - it->y);
s.insert(node(x, x, it->y));
} else {
printf("%d\n", x - it->x);
s.erase(it);
s.insert(node(tmp.a, tmp.x, y));
s.insert(node(x, tmp.x, tmp.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 | //package round310;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
public class C {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni(), Q = ni();
int[][] co = new int[Q][];
int[] xs = new int[Q];
for(int i = 0;i < Q;i++){
co[i] = new int[]{ni(), ni(), nc(), -1};
xs[i] = co[i][0];
}
int[] imap = shrinkX(xs);
for(int i = 0;i < Q;i++){
co[i][3] = xs[i];
}
SegmentTreeOverwrite sty = new SegmentTreeOverwrite(Q);
SegmentTreeOverwrite stx = new SegmentTreeOverwrite(Q);
LST lsty = new LST(Q);
LST lstx = new LST(Q);
for(int i = 0;i < Q;i++){
if(co[i][2] == 'U'){
int y = sty.attack(co[i][3]);
out.println(co[i][1]-y);
int ind = Arrays.binarySearch(imap, n+1-y);
if(y == 0){
ind = Q;
}
assert ind >= 0;
int next = lsty.next(co[i][3]);
if(next == -1)next = Q;
lsty.set(co[i][3]);
stx.update(co[i][3], Math.min(ind, next), co[i][0]);
sty.update(co[i][3], co[i][3]+1, co[i][1]);
}else{
int x = stx.attack(co[i][3]);
out.println(co[i][0]-x);
int ind = Arrays.binarySearch(imap, x);
if(x == 0){
ind = -1;
}
int prev = lstx.prev(co[i][3]);
if(prev == -1)prev = -1;
lstx.set(co[i][3]);
sty.update(Math.max(ind+1, prev+1), co[i][3]+1, co[i][1]);
stx.update(co[i][3], co[i][3]+1, co[i][0]);
}
}
}
public static class LST {
public long[][] set;
public int n;
// public int size;
public LST(int n) {
this.n = n;
int d = 1;
for(int m = n;m > 1;m>>>=6, d++);
set = new long[d][];
for(int i = 0, m = n>>>6;i < d;i++, m>>>=6){
set[i] = new long[m+1];
}
// size = 0;
}
// [0,r)
public LST setRange(int r)
{
for(int i = 0;i < set.length;i++, r=r+63>>>6){
for(int j = 0;j < r>>>6;j++){
set[i][j] = -1L;
}
if((r&63) != 0)set[i][r>>>6] |= (1L<<r)-1;
}
return this;
}
// [0,r)
public LST unsetRange(int r)
{
if(r >= 0){
for(int i = 0;i < set.length;i++, r=r+63>>>6){
for(int j = 0;j < r+63>>>6;j++){
set[i][j] = 0;
}
if((r&63) != 0)set[i][r>>>6] &= ~((1L<<r)-1);
}
}
return this;
}
public LST set(int pos)
{
if(pos >= 0 && pos < n){
// if(!get(pos))size++;
for(int i = 0;i < set.length;i++, pos>>>=6){
set[i][pos>>>6] |= 1L<<pos;
}
}
return this;
}
public LST unset(int pos)
{
if(pos >= 0 && pos < n){
// if(get(pos))size--;
for(int i = 0;i < set.length && (i == 0 || set[i-1][pos] == 0L);i++, pos>>>=6){
set[i][pos>>>6] &= ~(1L<<pos);
}
}
return this;
}
public boolean get(int pos)
{
return pos >= 0 && pos < n && set[0][pos>>>6]<<~pos<0;
}
public int prev(int pos)
{
for(int i = 0;i < set.length && pos >= 0;i++, pos>>>=6, pos--){
int pre = prev(set[i][pos>>>6], pos&63);
if(pre != -1){
pos = pos>>>6<<6|pre;
while(i > 0)pos = pos<<6|63-Long.numberOfLeadingZeros(set[--i][pos]);
return pos;
}
}
return -1;
}
public int next(int pos)
{
for(int i = 0;i < set.length && pos>>>6 < set[i].length;i++, pos>>>=6, pos++){
int nex = next(set[i][pos>>>6], pos&63);
if(nex != -1){
pos = pos>>>6<<6|nex;
while(i > 0)pos = pos<<6|Long.numberOfTrailingZeros(set[--i][pos]);
return pos;
}
}
return -1;
}
private static int prev(long set, int n)
{
long h = Long.highestOneBit(set<<~n);
if(h == 0L)return -1;
return Long.numberOfTrailingZeros(h)-(63-n);
}
private static int next(long set, int n)
{
long h = Long.lowestOneBit(set>>>n);
if(h == 0L)return -1;
return Long.numberOfTrailingZeros(h)+n;
}
@Override
public String toString()
{
List<Integer> list = new ArrayList<Integer>();
for(int pos = next(0);pos != -1;pos = next(pos+1)){
list.add(pos);
}
return list.toString();
}
}
public static class SegmentTreeOverwrite {
public int M, H, N;
public int[] cover;
public static int I = Integer.MAX_VALUE;
public SegmentTreeOverwrite(int n)
{
N = n;
M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
H = M>>>1;
cover = new int[M];
}
public void update(int l, int r, int v){ update(l, r, v, l, 0, H, 1); }
private void update(int l, int r, int v, int begin, int cl, int cr, int cur)
{
if(l <= cl && cr <= r){
cover[cur] = v;
}else{
int mid = cl+cr>>>1;
if(cover[cur] != I){ // back-propagate
cover[2*cur] = cover[2*cur+1] = cover[cur];
cover[cur] = I;
}
if(cl < r && l < mid){
update(l, r, v, begin, cl, mid, 2*cur);
}
if(mid < r && l < cr){
update(l, r, v, begin, mid, cr, 2*cur+1);
}
}
}
public int attack(int x) {
int val = 0;
for(int i = H+x;i >= 1;i>>>=1){
if(cover[i] != I){
val = cover[i];
}
}
return val;
}
}
public static int[] shrinkX(int[] a) {
int n = a.length;
long[] b = new long[n];
for (int i = 0; i < n; i++)
b[i] = (long) a[i] << 32 | i;
Arrays.sort(b);
int[] ret = new int[n];
int p = 0;
ret[0] = (int) (b[0] >> 32);
for (int i = 0; i < n; i++) {
if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) {
p++;
ret[p] = (int) (b[i] >> 32);
}
a[(int) b[i]] = p;
}
return Arrays.copyOf(ret, p + 1);
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new C().run(); }
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | //package CF;
import java.io.*;
import java.util.*;
public class E {
static class SegmentTree { // 1-based DS, OOP
int N; //the number of elements in the array as a power of 2 (i.e. after padding)
int sTree[][];
SegmentTree(int in)
{
N = in;
sTree = new int[N<<1][2]; //no. of nodes = 2*N - 1, we add one to cross out index zero
}
void update_range(int i, int j, int val, int dir) // O(log n)
{
update_range(1,1,N,i,j,val,dir);
}
void update_range(int node, int b, int e, int i, int j, int val, int dir)
{
if(i > e || j < b)
return;
if(b >= i && e <= j)
{
sTree[node][dir] = Math.max(val,sTree[node][dir]);
}
else
{
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node<<1,b,mid,i,j,val,dir);
update_range(node<<1|1,mid+1,e,i,j,val,dir);
}
}
void propagate(int node, int b, int mid, int e)
{
sTree[node<<1][0] = Math.max(sTree[node][0],sTree[node<<1][0]);
sTree[node<<1][1] = Math.max(sTree[node][1],sTree[node<<1][1]);
sTree[node<<1|1][0] = Math.max(sTree[node][0],sTree[node<<1|1][0]);
sTree[node<<1|1][1] = Math.max(sTree[node][1],sTree[node<<1|1][1]);
sTree[node][0] = sTree[node][1] = 0;
}
int query(int i, int j, int val)
{
return query(1,1,N,i,j,val);
}
int query(int node, int b, int e, int i, int j, int val) // O(log n)
{
if(i>e || j <b)
return 0;
if(b>= i && e <= j)
return sTree[node][val];
int mid = b + e >> 1;
propagate(node, b, mid, e);
int q1 = query(node<<1,b,mid,i,j,val);
int q2 = query(node<<1|1,mid+1,e,i,j,val);
return Math.max(q1, q2);
}
}
public static void main(String[] args) throws IOException {
Scanner bf = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
bf.nextInt();int q = bf.nextInt();
int [][] queries = new int[q][3];
TreeSet<Integer> rw = new TreeSet<Integer>();
TreeSet<Integer> cl = new TreeSet<Integer>();
HashMap<Integer,Integer> r = new HashMap<Integer,Integer>();
HashMap<Integer,Integer> c = new HashMap<Integer,Integer>();
for (int i = 0; i < q; i++)
{
queries[i] = new int [] {bf.nextInt(),bf.nextInt(),bf.next().charAt(0)=='U'?0:1};
cl.add(queries[i][0]);
rw.add(queries[i][1]);
}
int count = 1;
for(int i : rw){
r.put(i, count++);
}
count = 1;
for(int i : cl){
c.put(i, count++);
}
int N = 1; while(N < count) N <<= 1;
SegmentTree sg = new SegmentTree(N);
int tmp = 0;
for (int i = 0; i < queries.length; i++)
{
int col = c.get(queries[i][0]), row = r.get(queries[i][1]);
if(queries[i][2] == 0){ //up : update left of next cols to be my col
tmp = (sg.query(col, col,0));
Integer in = (rw.ceiling(tmp+1));
if(in != null)
sg.update_range(r.get(in),row, queries[i][0], 1);
sg.update_range(col,col, queries[i][1], 0);
}
else{ //left: update up of col lower than me to be my row
tmp = (sg.query(row, row,1));
Integer in = (cl.ceiling(tmp+1));
if(in != null)
sg.update_range(c.get(in),col, queries[i][1], 0);
sg.update_range(row,row, queries[i][0], 1);
}
out.println(queries[i][1-queries[i][2]] - tmp);
// for (int j = 0; j < sg.lazy.length; j++)
// {
// out.print(Arrays.toString(sg.lazy[j]));
// out.println(Arrays.toString(sg.sTree[j]));
// }
}
out.flush();
out.close();
}
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();}
}
} | 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 P556E {
static long get_val(TreeSet<LPair> map, long query){
return map.ceiling(new LPair(query,0)).h - query + 1;
}
static boolean has_val(TreeSet<LPair> map, long query){
return map.contains(new LPair(query,0));
}
static void print_map(TreeSet<LPair> map)
{
for(LPair LP: map){
System.out.print("["+LP.upper_bound+","+LP.h+"]");
}
System.out.println();
}
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
int q = sc.nextInt();
TreeSet<LPair> column_map = new TreeSet<LPair>();
TreeSet<LPair> row_map = new TreeSet<LPair>();
column_map.add(new LPair(n+1,n));
row_map.add(new LPair(n+1,n));
for (int i=0; i<q; i++){
int c = sc.nextInt();
int r = sc.nextInt();
String S = sc.next();
long h = 0;
if (S.charAt(0)=='U')
{
if(!has_val(row_map, r))
{
h = get_val(column_map, c);
if (h > 0){
long b = r-h;
row_map.add(new LPair(b, b-1+get_val(row_map, b)));
row_map.add(new LPair(r, n-c));
//System.out.print("Row_map: "); print_map(row_map);
}
}
}
else if (S.charAt(0)=='L')
{
if(!has_val(column_map, c))
{
h = get_val(row_map, r);
if (h > 0){
long b = c-h;
column_map.add(new LPair(b, b-1+get_val(column_map, b)));
column_map.add(new LPair(c, n-r));
//System.out.print("column_map: "); print_map(column_map);
}
}
}
System.out.println(h);
}
}
}
class LPair implements Comparable<LPair>{
long h;
long upper_bound;
public LPair(long upper_bound, long h) {
super();
this.upper_bound = upper_bound;
this.h = h;
}
public int compareTo(LPair L){
return (this.upper_bound > L.upper_bound) ? 1 : (this.upper_bound<L.upper_bound) ? -1 : 0;
}
} | JAVA |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct Node {
int s, e;
int u, l;
Node() {}
Node(int s, int e, int u, int l) : s(s), e(e), u(u), l(l) {}
friend bool operator<(const Node A, const Node B) {
return A.s == B.s
? (A.e == B.e ? (A.u == B.u ? A.l < B.l : A.u < B.u) : A.e < B.e)
: A.s < B.s;
}
};
int main() {
int n, q;
scanf("%d%d", &n, &q);
Node inital = Node(1, n, n, n);
set<Node> st;
set<int> isQ;
st.insert(inital);
for (int i = 0; i < q; i++) {
int a, b;
char opt;
cin >> a >> b >> opt;
if (isQ.count(a)) {
puts("0");
continue;
}
isQ.insert(a);
set<Node>::iterator it = st.upper_bound(Node(a, 0x3f3f3f3f, 0, 0));
--it;
Node pos = *it;
int s = pos.s, e = pos.e, u = pos.u, l = pos.l;
st.erase(it);
if (opt == 'U') {
printf("%d\n", u - (a - s));
if (a != s) st.insert(Node(s, a - 1, u, l - (e - a + 1)));
if (a != e) st.insert(Node(a + 1, e, u - (a - s + 1), e - a));
}
if (opt == 'L') {
printf("%d\n", l - (e - a));
if (a != s) st.insert(Node(s, a - 1, a - s, l - (e - a + 1)));
if (a != e) st.insert(Node(a + 1, e, u - (a - s + 1), l));
}
}
return 0;
}
| CPP |
555_C. Case of Chocolate | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an n Γ n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action.
Input
The first line contains integers n (1 β€ n β€ 109) and q (1 β€ q β€ 2Β·105) β the size of the chocolate bar and the number of actions.
Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 β€ xi, yi β€ n, xi + yi = n + 1) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up).
Output
Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.
Examples
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note
Pictures to the sample tests:
<image>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2 | 9 | import java.io.*;
import java.util.*;
public class C{
public static boolean DEBUG = false;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st;
st = getst(br);
int n = nextInt(st);
int q = nextInt(st);
TreeMap<Integer, Range> tree = new TreeMap<Integer, Range>();
tree.put(0, new Range(0, 0, n));
tree.put(n+1, new Range(-1, -1, -1));
for(int i = 0; i < q; i++){
st = getst(br);
int c = nextInt(st);
int r = nextInt(st);
char dir = st.nextToken().charAt(0);
//debug(tree);
int x1 = tree.floorKey(c);
if(x1 == c){
pw.println(0);
continue;
}
int x2 = tree.ceilingKey(c)-1;
Range ra = tree.get(x1);
x1++;
int r1 = c-x1;
int w1 = 0;
int h1 = 0;
int r2 = x2-c;
int w2 = 0;
int h2 = 0;
int ans = 0;
if(dir == 'U'){
h1 = ra.h + (x2-c+1);
h2 = ra.h;
w1 = ra.w;
w2 = 0;
ans = r2 + ra.h + 1;
} else {
h1 = 0;
h2 = ra.h;
w1 = ra.w;
w2 = ra.w + (c-x1+1);
ans = r1 + ra.w + 1;
}
pw.println(ans);
ra.w = w1;
ra.h = h1;
ra.r = r1;
tree.put(c, new Range(w2, h2, r2));
}
br.close();
pw.close();
}
static class Range {
int w;
int h;
int r;
public Range(int mw, int mh, int mr){
w = mw;
h = mh;
r = mr;
}
public String toString(){
return "(r=" + r + ", w=" + w + ", h=" + h + ")";
}
}
public static void debug(Object o){
if(DEBUG){
System.out.println("~" + o);
}
}
public static StringTokenizer getst(BufferedReader br) throws Exception{
return new StringTokenizer(br.readLine(), " ");
}
public static int nextInt(BufferedReader br) throws Exception{
return Integer.parseInt(br.readLine());
}
public static int nextInt(StringTokenizer st) throws Exception{
return Integer.parseInt(st.nextToken());
}
public static long nextLong(BufferedReader br) throws Exception{
return Long.parseLong(br.readLine());
}
public static long nextLong(StringTokenizer st) throws Exception{
return Long.parseLong(st.nextToken());
}
public static double nextDouble(BufferedReader br) throws Exception{
return Double.parseDouble(br.readLine());
}
public static double nextDouble(StringTokenizer st) throws Exception{
return Double.parseDouble(st.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 | import java.io.*;
import static java.lang.Math.*;
import java.util.*;
import java.util.function.*;
import java.lang.*;
public class Main {
final static boolean debug = false;
final static String fileName = "";
final static boolean useFiles = false;
public static void main(String[] args) throws FileNotFoundException {
PrintWriter writer = new PrintWriter(System.out);
new Task(new InputReader(System.in), writer).solve();
writer.close();
}
}
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 double nextDouble() {
return Double.parseDouble(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public byte nextByte() {
return Byte.parseByte(next());
}
}
class Task {
class Cell implements Comparable<Cell>{
Cell(int x, int xAdd, int yAdd){
X = x;
XAdd = xAdd;
YAdd = yAdd;
}
int XAdd, YAdd, X;
@Override
public int compareTo(Cell o){
return X - o.X;
}
}
public void solve() {
int n = in.nextInt();
int q = in.nextInt();
TreeSet<Cell> cells = new TreeSet<>();
cells.add(new Cell(0, 0, 0));
cells.add(new Cell(n + 1, 0, 0));
for (int i = 0; i < q; i++){
int x = in.nextInt();
int y = in.nextInt();
char dir = in.next().charAt(0);
Cell curr = new Cell(x, 0, 0);
if (cells.contains(curr)){
out.println(0);
continue;
}
Cell prev = cells.lower(curr);
Cell next = cells.higher(curr);
int r = next.X - x - 1;
int l = x - prev.X - 1;
int result;
if (dir == 'U'){
result = prev.XAdd + r + 1;
//x
curr.XAdd = prev.XAdd;
curr.YAdd = 0;
//prev
prev.XAdd = prev.XAdd + r + 1;
prev.YAdd = prev.YAdd;
}
else {
result = prev.YAdd + l + 1;
curr.XAdd = prev.XAdd;
curr.YAdd = prev.YAdd + l + 1;
prev.XAdd = 0;
prev.YAdd = prev.YAdd;
}
cells.add(curr);
// for (int c : cells){
// out.println(c + " " + xAdd[c] + " " + yAdd[c]);
// }
out.println(result);
}
}
private InputReader in;
private PrintWriter out;
Task(InputReader in, PrintWriter out) {
this.in = in;
this.out = out;
}
}
| 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 double EPS = -1e8;
const double Pi = acos(-1);
bool inline equ(double a, double b) { return fabs(a - b) < EPS; }
const int MAXN = 200010;
int n, q;
set<tuple<int, int, int> > bst;
int qryl(int x, int y) {
auto it = bst.lower_bound(make_tuple(x, -1, -1));
if (it == bst.end()) return y;
int tx, tl, typ;
tie(tx, tl, typ) = *it;
assert(x <= tx);
if (tx == x) return 0;
if (typ == 1)
return tx - x;
else
return tl + tx - x;
}
int qryu(int x, int y) {
auto it = bst.upper_bound(make_tuple(x, 1100000000, 1100000000));
if (it == bst.begin()) return x;
it--;
int tx, tl, typ;
tie(tx, tl, typ) = *it;
assert(tx <= x);
if (tx == x) return 0;
if (typ == 0)
return x - tx;
else
return tl + x - tx;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
while (q--) {
int x, y;
char typ[2];
cin >> y >> x >> typ;
if (typ[0] == 'L') {
int res = qryl(x, y);
cout << res << '\n';
if (res) bst.insert(make_tuple(x, res, 0));
} else {
int res = qryu(x, y);
cout << res << '\n';
if (res) bst.insert(make_tuple(x, res, 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 |
import java.util.*;
import java.io.*;
public class CaseOfChocolate555C {
static Random rd = new Random();
static int n;
static class Node {
int from, to, up, left;
Node lchild, rchild;
public Node(int a, int b, int c, int d) {
this.from = a;
this.to = b;
this.up = c;
this.left = d;
this.lchild = null;
this.rchild = null;
}
}
static Node find(Node root, int t) {
if (root == null) return null;
if (root.from <= t && root.to >= t) return root;
if (root.from > t) return find(root.lchild, t);
return find(root.rchild, t);
}
static Node insert(Node root, Node node) {
if (root == null) return node;
if (root.from > node.to) {
if (rd.nextInt(2) == 0) {
node.rchild = root;
return node;
}
root.lchild = insert(root.lchild, node);
} else {
if (rd.nextInt(2) == 0) {
node.lchild = root;
return node;
}
root.rchild = insert(root.rchild, node);
}
return root;
}
static void split(Node root, int mid, char ch) {
if (root.from == root.to) {
root.up = n - mid + 2;
root.left = root.from + 1;
} else {
if (root.from == mid) {
root.from++;
if (ch == 'U')
root.left = mid + 1;
} else if (root.to == mid) {
root.to--;
if (ch == 'L')
root.up = n - mid + 2;
} else {
if (root.to - mid > mid - root.from) {
if (ch == 'U') {
Node node = new Node(root.from, mid - 1, root.up, root.left);
root.from = mid + 1;
root.left = mid + 1;
root.lchild = insert(root.lchild, node);
} else {
Node node = new Node(root.from, mid - 1, n - mid + 2, root.left);
root.from = mid + 1;
root.lchild = insert(root.lchild, node);
}
} else {
if (ch == 'U') {
Node node = new Node(mid + 1, root.to, root.up, mid + 1);
root.to = mid - 1;
root.rchild = insert(root.rchild, node);
} else {
Node node = new Node(mid + 1, root.to, root.up, root.left);
root.to = mid - 1;
root.up = n - mid + 2;
root.rchild = insert(root.rchild, node);
}
}
}
}
}
static void go() {
n = in.nextInt();
int q = in.nextInt();
Node tree = new Node(1, n, 1, 1);
for (int i = 0; i < q; i++) {
int a = in.nextInt();
int b = in.nextInt();
char c = in.nextCharArray(1)[0];
Node node = find(tree, a);
if (node == null) {
out.println(0);
} else {
if (c == 'U')
out.println(b - node.up + 1);
else
out.println(a - node.left + 1);
split(node, a, c);
}
}
}
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 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
hs = list(map(int, input().split()))
maxx = 0
ans = []
for h in reversed(hs):
if h > maxx:
ans.append(0)
else:
ans.append(maxx-h+1)
maxx = max(maxx, h)
print(' '.join(map(str, reversed(ans)))) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | # from sys import stdout
from bisect import bisect_left as bs
from math import log as lg
from math import factorial as f
log = lambda *x: print(*x)
def cin(*fn, def_fn=str):
i,r = 0,[]
if fn: def_fn=fn[0]
for c in input().split(' '):
r+=[(fn[i] if len(fn)>i else def_fn) (c)]
i+=1
return r
####################################################
n, = cin(int)
h = cin(int)
r = [0]*n
mxr = h[-1]
for i in range(n-2,-1,-1):
if h[i]>mxr: r[i],mxr = 0, h[i]
else: r[i] = mxr-h[i]+1
log(' '.join(str(c) for c in r))
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
import java.math.BigDecimal;
//import java.math.BigInteger;
public class Main{
public static void main(String[] args)
{ Scanner scan= new Scanner(System.in);
int num,b,temp;
int[] arr=new int[100001];
int[] adder=new int[100001];
num=scan.nextInt();
for(int i=0;i<num;i++)
arr[i]=scan.nextInt();
adder[num-1]=0;
temp=arr[num-1];
for(int i=num-2;i>=0;i--)
{
if(temp>=arr[i])
{ adder[i]=temp-arr[i]+1;
}
else {temp=Math.max(temp, arr[i]);adder[i]=0;}
}
for(int i=0;i<num;i++)
System.out.print(adder[i]+" ");
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int INF = 0x7FFFFFFF;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = 2 * acos(0.0);
const int maxn = 1e5 + 1666;
long long maxval[maxn << 2], mmax, aa[maxn], len;
void PushUp(int rt) { maxval[rt] = max(maxval[rt << 1], maxval[rt << 1 | 1]); }
void Build(int rt, int l, int r) {
if (l == r) {
scanf("%d", &maxval[rt]);
aa[len] = maxval[rt];
len++;
return;
}
int m = (l + r) >> 1;
Build(rt << 1, l, m);
Build(rt << 1 | 1, m + 1, r);
PushUp(rt);
}
void Query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) {
mmax = max(mmax, maxval[rt]);
return;
}
int m = (l + r) >> 1;
if (L <= m) Query(rt << 1, l, m, L, R);
if (R > m) Query(rt << 1 | 1, m + 1, r, L, R);
}
int main() {
long long n;
scanf("%I64d", &n);
len = 1;
Build(1, 1, n);
for (long long i = 1; i <= n; i++) {
mmax = -INF;
Query(1, 1, n, i + 1, n);
if (i != 1) printf(" ");
if (aa[i] > mmax)
printf("0");
else
printf("%I64d", mmax - aa[i] + 1);
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | var numeric = readline(),
floor = readline().split(' '),
result = [0];
for (; floor.length > 1; ) {
var last_floor = +floor[floor.length - 1],
prev_floor = +floor[floor.length - 2];
if (prev_floor > last_floor) {
result.unshift(0);
floor.splice(floor.length - 1, 1);
} else {
if (last_floor === prev_floor) {
result.unshift(1);
floor.splice(floor.length - 1, 1);
} else {
result.unshift( (last_floor - prev_floor) + 1 );
floor.splice(floor.length - 2, 1);
}
}
}
//result.splice(0,1);
print(result.join(' ')); | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | from collections import Counter
n=int(input())
a=[int(x) for x in input().split()]
ra=a[::-1]
mx=[ra[0]]
c=Counter(a)
for i in range(1,n):
mx.append(max(mx[i-1], ra[i]))
for i in range(n):
if mx[n-1-i]==a[i]:
if c[a[i]]>1:
print(1, end=" ")
else:
print(0, end=" ")
else:
print(mx[n-1-i]+1-a[i], end=" ")
if c[a[i]]>1:
c[a[i]]-=1
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=input()
a=map(int,raw_input().split())
m=[0]*n
m[n-1]=a[n-1]
for i in xrange(n-2,-1,-1):
m[i]=max(m[i+1],a[i])
ans=[0]*n
for i in xrange(n-1):
ans[i]=max(0,m[i+1]+1-a[i])
ans[n-1]=0
print ' '.join(map(str,ans)) | PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | /* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int h[] = new int[n];
int a[] = new int[n];
for(int i = 0;i<n;i++){
a[i]=sc.nextInt();
}
h[n-1]=-1;
for(int i = n-2;i>=0;i--){
h[i]=Math.max(a[i+1],h[i+1]);
}
for(int i = 0;i<n;i++){
if(a[i]>h[i]){
System.out.print(0 + " ");
}
else{
System.out.print(h[i]-a[i]+1 + " ");
}
}
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
scanf("%I64d", &n);
vector<long long> a(n), mx(n + 1, 0);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = n - 1; i >= 0; i--) mx[i] = max(mx[i + 1], a[i]);
for (int i = 0; i < n; i++)
if (a[i] <= mx[i + 1])
printf("%I64d ", mx[i + 1] - a[i] + 1);
else
printf("0 ");
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.util.*;
import java.io.*;
/**
*
* @author ringod
*/
public class ACM {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int [] a = new int [n];
int [] b = new int [n];
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
}
int maxRightHouse = 0;
for (int i = n - 1; i >= 0; --i) {
b[i] = 0;
if (maxRightHouse >= a[i]) {
b[i] = maxRightHouse - a[i] + 1;
} else {
maxRightHouse = a[i];
}
}
for (int i = 0; i < n; ++i) {
out.print(b[i] + " ");
}
out.flush();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
class node {
public:
long long int second;
long long int length;
};
void FastIO() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
FastIO();
long long int n, i;
cin >> n;
long long int a[n], ans[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
long long int maxi = a[n - 1];
ans[n - 1] = 0;
for (i = n - 2; i >= 0; i--) {
ans[i] = max(maxi - a[i] + 1, (long long int)0);
maxi = max(maxi, a[i]);
}
for (i = 0; i < n - 1; i++) cout << ans[i] << " ";
cout << ans[i] << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int INF = ~0U >> 1;
const double eps = 1e-6;
const long double PI = acos(0.0) * 2.0;
const int N = 5 + 100000;
long long int a[N], dp[N];
int main() {
int n;
while (scanf("%d", &n) == 1) {
for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
dp[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
dp[i] = max(a[i], dp[i + 1]);
}
for (int i = 0; i < n - 1; i++) {
if (i) printf(" ");
if (a[i] > dp[i + 1])
printf("0");
else
printf("%I64d", abs(dp[i] - a[i]) + 1);
}
puts(" 0");
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Scanner;
import java.util.ArrayList;
public class omar {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
ArrayList<Integer>omar=new ArrayList();
int x=user_input.nextInt();
int k[]=new int[x];
for(int i=0;i<x;i++) {
k[i] = user_input.nextInt();
}
for(int c=x-1;c>0;c--) {
if (k[c] >= k[c - 1]) {
omar.add((k[c] - k[c - 1]) + 1);
k[c - 1] = k[c];
} else {
omar.add(0);
}
}
for(int e=omar.size()-1;e>=0;e--) {
System.out.println(omar.get(e));
}
System.out.println(0);
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(raw_input())
h = [int(x) for x in raw_input().split()]
maxr = [0] * n
add = [0] * n
i = n-2
while i >= 0:
maxr[i] = max(maxr[i+1], h[i+1])
add[i] = max(0, maxr[i] - h[i] + 1)
i -= 1
result = ' '.join([str(item) for item in add])
result = result.strip()
print result
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #
# _,add8ba,
# ,d888888888b,
# d8888888888888b _,ad8ba,_
# d888888888888888) ,d888888888b,
# I8888888888888888 _________ ,8888888888888b
# __________`Y88888888888888P"""""""""""baaa,__ ,888888888888888,
# ,adP"""""""""""9888888888P""^ ^""Y8888888888888888I
# ,a8"^ ,d888P"888P^ ^"Y8888888888P'
# ,a8^ ,d8888' ^Y8888888P'
# a88' ,d8888P' I88P"^
# ,d88' d88888P' "b,
# ,d88' d888888' `b,
# ,d88' d888888I `b,
# d88I ,8888888' ___ `b,
# ,888' d8888888 ,d88888b, ____ `b,
# d888 ,8888888I d88888888b, ,d8888b, `b
#,8888 I8888888I d8888888888I ,88888888b 8,
#I8888 88888888b d88888888888' 8888888888b 8I
#d8886 888888888 Y888888888P' Y8888888888, ,8b
#88888b I88888888b `Y8888888^ `Y888888888I d88,
#Y88888b `888888888b, `""""^ `Y8888888P' d888I
#`888888b 88888888888b, `Y8888P^ d88888
# Y888888b ,8888888888888ba,_ _______ `""^ ,d888888
# I8888888b, ,888888888888888888ba,_ d88888888b ,ad8888888I
# `888888888b, I8888888888888888888888b, ^"Y888P"^ ____.,ad88888888888I
# 88888888888b,`888888888888888888888888b, "" ad888888888888888888888'
# 8888888888888698888888888888888888888888b_,ad88ba,_,d88888888888888888888888
# 88888888888888888888888888888888888888888b,`"""^ d8888888888888888888888888I
# 8888888888888888888888888888888888888888888baaad888888888888888888888888888'
# Y8888888888888888888888888888888888888888888888888888888888888888888888888P
# I888888888888888888888888888888888888888888888P^ ^Y8888888888888888888888'
# `Y88888888888888888P88888888888888888888888888' ^88888888888888888888I
# `Y8888888888888888 `8888888888888888888888888 8888888888888888888P'
# `Y888888888888888 `888888888888888888888888, ,888888888888888888P'
# `Y88888888888888b `88888888888888888888888I I888888888888888888'
# "Y8888888888888b `8888888888888888888888I I88888888888888888'
# "Y88888888888P `888888888888888888888b d8888888888888888'
# ^""""""""^ `Y88888888888888888888, 888888888888888P'
# "8888888888888888888b, Y888888888888P^
# `Y888888888888888888b `Y8888888P"^
# "Y8888888888888888P `""""^
# `"m88888888888P'
# ^""""""""'
n = int(input())
a = list(map(int, input().split()))
m = -1
ans = []
for i in range(n):
if a[n-i-1] > m:
ans.append("0")
else:
ans.append(str(m+1-a[n-i-1]))
m = max(a[n-i-1], m)
print(" ".join(ans[::-1])) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=input()
l=[int(i)for i in raw_input().split()]
r=[l[-1]]
for i in xrange(n-2):
r.append(max(l[-2-i],r[-1]))
print ' '.join(['0' if l[i]>r[-1-i] else str(r[-1-i]-l[i]+1) for i in xrange(n-1)]),'0', | PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec;
vector<int> res;
int n;
cin >> n;
while (n--) {
int a;
cin >> a;
vec.push_back(a);
}
int max = 0;
res.resize(vec.size());
for (int i = vec.size() - 1; i >= 0; i--) {
if (vec[i] > max) {
res[i] = 0;
max = vec[i];
} else {
res[i] = max - vec[i] + 1;
}
}
for (int i = 0; i < res.size(); i++) {
cout << res[i];
if (i != vec.size() - 1) cout << " ";
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
ar = map(int,raw_input().split())
ar.reverse()
ans = [0]
m = ar[0]
for i in ar[1:]:
if i==m:
ans.append(1)
elif i>m:
ans.append(0)
m = i
else:
ans.append(m+1-i)
ans.reverse()
print " ".join(str(i) for i in ans)
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(raw_input())
l = [int(x) for x in raw_input().split()]
last = 0
ret = []
for i in xrange(n):
ret.append(str(max(0, last+1-l[n-1-i])))
last = max(last, l[n-1-i])
print " ".join(ret[::-1])
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.Arrays;
import java.util.Scanner;
public class floors {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int b[]=new int[n];
int c[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
int max=0;
//max=a[n-1];
for(int i=n-1;i>=0;i--)
{
if(max<a[i])
{
b[i]=0;
}
else
{
b[i]=max+1-a[i];
}
max=Math.max(a[i],max);
}
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]+" ");
}
sc.close();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int max =0,index=0;
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
if(arr[i]>=max){
max = arr[i];
index=i;
}
}
int last = 0;
int[] prr=new int[n];
for(int i=n-1;i>=0;i--){
prr[i] = Math.max(0,(last+1)-arr[i]);
last = Math.max(arr[i],last);
}
for(int i=0;i<n;i++){
System.out.println(prr[i]);
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
n = int(input())
houses = list(map(int,input().split()))
lis = [0]*n
mx = 0
for i in range(n-1,-1,-1):
if houses[i] > mx:
mx = houses[i]
else:
lis[i] = mx - houses[i] + 1
print (*lis)
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
n= int(input())
a= [int(i) for i in input().split()]
pref= [0]*n
from collections import defaultdict
d = defaultdict(int)
pref[-1] = a[-1]
d[pref[-1]]=n-1
for i in range(n-2,-1,-1):
pref[i] = max(pref[i+1],a[i])
if d[pref[i]]==0:
d[pref[i]]=i
#print(pref)
#print(d)
out = [0]*n
for i in range(n):
if a[i]==pref[i] and i==d[a[i]]:
continue
elif a[i]==pref[i]:
out[i] = 1
else:
out[i] = pref[i]-a[i]+1
print(*out)
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = list(map(int, input().split())) + [0]
h_max = [0] * (n + 1)
for i in range(n - 1, 0,-1):
h_max[i-1] = max(h_max[i], h[i] + 1)
res = []
for i in range(n):
res.append(str(max(h_max[i] - h[i], 0)))
print(" ".join(res)) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = list(map(int, input().split()))
r = [0]
m = h[-1]
for i in range(n-2,-1,-1):
if h[i] <= m:
r.append(m + 1 - h[i])
else:
r.append(0)
m = h[i]
for i in range(n - 1,-1,-1):
print(r[i], end=' ') | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long arr[n];
for (int a = 0; a < n; a++) {
cin >> arr[a];
}
long long max = arr[n - 1];
arr[n - 1] = 0;
for (int a = n - 2; a >= 0; a--) {
if (max < arr[a]) {
max = arr[a];
arr[a] = 0;
} else {
arr[a] = max + 1 - arr[a];
}
}
for (int a = 0; a < n; a++) {
cout << arr[a] << " ";
}
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | a = int(input())
b = list(map(int, input().split()))
mark = 0
answer = list()
for x in range(a):
if b[a - x - 1] > mark:
answer.append(0)
mark = b[a - x - 1]
else:
answer.append(mark - b[a - x - 1] + 1)
for x in range(a):
print(answer[a - x - 1]) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = input()
h = map(int, raw_input().split())
res = [0] * n
tmp = h[n - 1]
for i in xrange(n - 2, -1, -1):
if h[i] <= tmp:
res[i] = tmp - h[i] + 1
else:
res[i] = 0
tmp = h[i]
ans = ""
for i in res:
ans += str(i) + " "
print ans | PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | from sys import stdin, stdout
input = stdin.readline
n=int(input())
a=list(map(int,input().split()))
b=[0]*n
m=a[n-1]-1
for i in range(n-1,-1,-1):
if(a[i]<=m):
b[i]=m-a[i]+1
else:
m=a[i]
b[i]=0
print(" ".join(map(str,b)),end=" ")
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int s[n];
for (int i = 0; i < (n); i++) cin >> s[i];
int b[n], x;
b[n - 1] = 0;
x = s[n - 1];
for (int i = (n - 2); i >= 0; i--) {
if (x == s[i])
b[i] = 1;
else if (x > s[i])
b[i] = (x - s[i] + 1);
else
b[i] = 0;
x = x > s[i] ? x : s[i];
}
for (int i = 0; i < (n); i++) cout << b[i] << " ";
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
import java.io.*;
public class yahia {
public static void main (String [] Yahia_Mostafa) throws Exception {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
int [] x=new int [n];
TreeMap<Integer, Integer>tMap=new TreeMap<Integer, Integer>(Collections.reverseOrder());
for (int i=0;i<n;++i) {
x[i]=sc.nextInt();
int a = tMap.getOrDefault(x[i], 0);
tMap.put(x[i], a+1);
}
int [ ] z=new int [n];StringBuilder sBuilder=new StringBuilder();
z[n-1]=0;
int max =x[n-1];
for(int i =n-2;i>=0;--i) {
if (x[i]<=max)
z[i]=max+1-x[i];
else
max =x[i];
}
for (int i : z)
sBuilder.append(i+ " ");
System.out.println(sBuilder);
// for (int i =0;i<n;++i)
// sBuilder.append(b) (int i=n-2;i>=0
}
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);
}
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, max = -1;
cin >> n;
long long int a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = n - 1; i >= 0; i--) {
if (max < a[i]) {
max = a[i];
b[n - i] = 0;
} else {
b[n - i] = max + 1 - a[i];
}
}
for (int i = n; i > 0; i--) {
cout << b[i] << " ";
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
*
* @author ramilagger
*/
public class Main {
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static final PrintWriter pw = new PrintWriter(System.out);
public void run() throws IOException {
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
int max = 0;
int top;
for (int i = n - 1; i > -1; i--) {
top = max;
max = Math.max(a[i], max);
a[i] = a[i] > top ? 0 : top - a[i] + 1;
}
for (int i = 0; i < n; i++) {
pw.print(a[i] + " ");
}
pw.println();
pw.close();
}
public static void main(String[] args) throws IOException {
new Main().run();
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.math.BigInteger;
import java.util.Scanner;
public class ez {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] floors = new int[n];
for(int i =0; i < n; i++){
floors[i]=in.nextInt();
}
int[] max = new int[n];
for(int i = n-2; i>=0; i--){
max[i]=Math.max(max[i+1], floors[i+1]+1);
}
for(int i = 0; i < n; i++){
System.out.print((Math.max(0, max[i]-floors[i]))+" ");
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.util.*;
public class test {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int[] anss = new int[n];
int[] dh = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
dh[n-1] = 0;
anss[n-1]=0;
String ans = "";
for (int j = n - 2; j >= 0; j--) {
if (arr[j] <= arr[j+1]) {
dh[j] = arr[j+1] - arr[j] + 1;
arr[j] = arr[j+1];
anss[j] = dh[j];
}
else {
dh[j] = 0;
anss[j] = dh[j];
}
}
ans="";
for(int i=0; i<n; i++){
//System.out.print(anss[i]+" ");
ans=ans+anss[i]+" ";
if((i+1)%100==0){
System.out.print(ans);
ans="";
}
}
System.out.print(ans);
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
s = [int(i) for i in input().split()]
cnt = 0
m = 0
for i in range(n-1,-1,-1):
if s[i]>m:
m = s[i]
s[i]=0
else:
s[i]=m-s[i]+1
print(*s)
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
const int MAXN = (int)10e5 + 5;
using namespace std;
int v[MAXN];
int maxH[MAXN];
int main() {
int N, h;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> v[i];
}
int curMax = 0;
for (int i = N - 1; i >= 0; i--) {
curMax = max(curMax, v[i]);
maxH[i] = curMax;
}
for (int i = 0; i < N - 1; i++) {
if (v[i] > (maxH[i + 1]))
cout << 0 << ' ';
else
cout << maxH[i + 1] - v[i] + 1 << " ";
}
cout << 0 << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 5;
int A[MX], ans[MX];
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
}
ans[n] = 0;
int Max = A[n];
for (int i = n - 1; i >= 1; i--) {
ans[i] = Max + 1 - A[i];
if (ans[i] < 0) ans[i] = 0;
Max = max(Max, A[i]);
}
for (int i = 1; i <= n; i++) {
printf("%d%c", ans[i], i == n ? '\n' : ' ');
}
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int i, n, temp = 0;
scanf("%d", &n);
int a[n], b[n];
for (i = 0; i < n; i++) scanf("%d", &a[i]);
for (i = n - 1; i >= 0; i--) {
if (a[i] > temp) {
temp = a[i];
b[i] = 0;
} else {
b[i] = temp - a[i] + 1;
}
}
for (i = 0; i < n; i++) cout << b[i] << " ";
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
a=list(map(int,input().split()))
b=[0]*n
mx=a[n-1]
for i in range(n-2,-1,-1):
b[i]=max(0,mx-a[i]+1)
if a[i]>mx:mx=a[i]
print(*b)
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | __author__ = 'Devesh Bajpai'
'''
https://codeforces.com/problemset/problem/581/B
Solution:
'''
def solve(n, h_arr):
h_increment = [0] * n
largest = h_arr[n-1]
for i in xrange(n-2, -1, -1):
if h_arr[i] <= largest:
h_increment[i] = largest - h_arr[i] + 1
else:
largest = h_arr[i]
return " ".join(str(h_inc) for h_inc in h_increment)
if __name__ == "__main__":
n = int(raw_input())
h_arr = map(int, raw_input().split(" "))
print solve(n, h_arr)
| PYTHON |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
arr = [int(x) for x in input().split()]
arr1 = [0]*n
m = arr[n-1]
for i in range(n-2,-1,-1):
if arr[i]<m:
arr1[i] = m-arr[i]+1
elif arr[i]==m:
arr1[i] = 1
else:
m = arr[i]
print(*arr1)
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import javax.swing.plaf.synth.SynthUI;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class d {
public static void main(String args[]) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n=sc.nextInt();
int[]a = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int[]ans = new int[n];
int max=Integer.MIN_VALUE,ind=n-1;
for(int i=n-1;i>=0;i--) {
if (max < a[i]) {
ind = i;
max = a[i];
}
ans[i] = max - a[i];
if (ind != i)
ans[i]++;
}
for(int i =0;i<n;i++) {
out.print(ans[i] + " ");
}
out.close();
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static void shuffle(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
public static int gcd(int x, int y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
String nextLine() throws IOException {
return reader.readLine();
}
}
}
class points{
int x, y;
points(int x,int y){
this.x=x;
this.y=y;
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n], r[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
r[n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (arr[i] > arr[r[i + 1]])
r[i] = i;
else
r[i] = r[i + 1];
}
for (int i = 0; i < n; i++) {
if (r[i] == i) {
cout << 0 << " ";
} else
cout << arr[r[i]] - arr[i] + 1 << " ";
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import sys
def solve():
n, = rv()
h, = rl(1)
maxsofar = 0
res = [0] * n
for i in range(n - 1, -1, -1):
if h[i] <= maxsofar:
res[i] = maxsofar - h[i] + 1
else:
maxsofar = h[i]
prt(res)
def prt(l): return print(' '.join(map(str, l)))
def rs(): return map(str, input().split())
def rv(): return map(int, input().split())
def rl(n): return [list(map(int, input().split())) for _ in range(n)]
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
solve() | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | //package codeforces;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class A implements Closeable {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
A() throws IOException {
// reader = new BufferedReader(new FileReader("input.txt"));
// writer = new PrintWriter(new FileWriter("output.txt"));
}
StringTokenizer stringTokenizer;
String next() throws IOException {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
private int MOD = 1000 * 1000 * 1000 + 7;
int sum(int a, int b) {
a += b;
return a >= MOD ? a - MOD : a;
}
int product(int a, int b) {
return (int) (1l * a * b % MOD);
}
int pow(int x, int k) {
int result = 1;
while (k > 0) {
if (k % 2 == 1) {
result = product(result, x);
}
x = product(x, x);
k /= 2;
}
return result;
}
int inv(int x) {
return pow(x, MOD - 2);
}
void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = nextInt();
}
int[] suffMax = new int[n + 1];
for(int i = n - 1; i >= 0; i--) {
suffMax[i] = Math.max(suffMax[i + 1], a[i]);
}
for(int i = 0; i < n; i++) {
writer.printf("%d ", Math.max(0, suffMax[i + 1] + 1 - a[i]));
}
writer.println();
}
public static void main(String[] args) throws IOException {
try (A a = new A()) {
a.solve();
}
}
@Override
public void close() throws IOException {
reader.close();
writer.close();
}
} | JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> street(N);
for (int i = 0; i < N; i++) {
cin >> street[i];
}
int max_floors = -1;
for (int i = N - 1; i >= 0; i--) {
int temp = max(0, max_floors - street[i] + 1);
max_floors = max(max_floors, street[i]);
street[i] = temp;
}
for (int i = 0; i < street.size(); i++) {
cout << street[i] << " ";
}
cout << endl;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
h = list(map(int,input().split()))
a = []
if n==1:
print(0)
quit()
y = h[-1]
h[-1] = 0
for i in reversed(range(n-1)):
if h[i]>y:
y = max(h[i],y)
h[i] = 0
h[-1] = 0
else:
h[i] = (y-h[i]+1)
"""
tmp = h[-1]
for i in reversed(range(n-1)):
if h[i]<=tmp:
a.append(tmp-h[i]+1)
tmp = max(tmp,h[i])
else:a.append(0)"""
print(' '.join(map(str,(h)))) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, i, m, a[100001], b[100001];
int main() {
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
m = a[n - 1];
for (i = n - 2; i >= 0; i--) {
if (m >= a[i]) b[i] = m - a[i] + 1;
if (m < a[i]) m = a[i];
}
for (i = 0; i < n; i++) cout << b[i] << ' ';
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
l=list(map(int,input().split()))
# l.reverse()
v=[]
# v.append(0)
ma=0
for i in range(n-1,-1,-1):
# print(l[i])
if l[i]>ma:
v.append(0)
ma=l[i]
# # continue
else:
v.append(max(0,ma-l[i]+1))
v.reverse()
print(*v) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | readline();
var houses = readline().split(" ");
var max = -1, L = houses.length, result = [];
for (var i = L-1; i>=0; --i) {
var T = houses[i];
result.push(T > max ? 0 : max - T + 1);
max = Math.max(T, max);
}
result.reverse();
print(result.join(" "));
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input())
heights = list(map(int, input().split()))
highest = 0
result = n * [0]
for i in range(len(heights) - 1, -1, -1):
if heights[i] <= highest:
result[i] = highest - heights[i] + 1
else:
highest = heights[i]
print(' '.join(map(str, result)))
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
public class Main {
public static boolean debug = false;
public static void main(String[] args) {
InputStream stream = System.in;
try {
if (debug) {
stream = new FileInputStream("test.in");
}
} catch (Exception e) {
System.err.println("TEST");
}
Scanner scanner = new Scanner(stream);
int N = scanner.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list1 = new ArrayList<Integer>();
for(int i=0;i<N;++i) {
int x = scanner.nextInt();
list.add(x);
list1.add(0);
}
list1.add(0);
list.add(0);
for(int i=N-1;i>=0;--i) {
int x = list1.get(i+1);
x = Math.max(x,list.get(i+1));
list1.set(i, x);
}
for(int i=0;i<N;++i) {
System.out.print(Math.max(0, list1.get(i) -list.get(i) + 1));
System.out.print(" ");
}
}
}
| JAVA |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> v(n, 0);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
vector<long long> ans(n, 0);
ans[n - 1] = v[n - 1];
for (long long i = n - 2; i >= 0; i--) {
ans[i] = max<long long>(v[i], ans[i + 1]);
}
for (long long i = 0; i < n - 1; i++) {
cout << max<long long>(ans[i + 1] + 1 - v[i], 0) << " ";
}
cout << "0" << endl;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n=int(input())
a=list(map(int,input().split()[:n]))
m=0
ans=[0]*(n)
for i in range(n-1,-1,-1):
if (a[i]>m):
ans[i]=0
m=a[i]
else:
ans[i]=m-a[i]+1
print(*ans) | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | n = int(input());
l = list(map(int, input().split()));
ans = n * [0];
ma = 0; i = n - 1;
while(i >= 0):
if(l[i] <= ma):
ans[i] = ma - l[i] + 1;
else:
ans[i] = 0;
ma = l[i];
i -= 1;
for i in range(n):
print(str(ans[i]) + ' ', end=''); | PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> s(n, 0);
s[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) s[i] = max(a[i], s[i + 1]);
for (int i = 0; i < n - 1; i++) cout << max(0, s[i + 1] - a[i] + 1) << " ";
cout << 0;
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | #include <bits/stdc++.h>
using namespace std;
int n, x;
int f[200100], nn[200100];
int mi(int a, int b) { return a < b ? a : b; }
int ma(int a, int b) { return a > b ? a : b; }
int main() {
scanf("%d", &n);
int h = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &f[i]);
}
for (int i = n; i >= 1; i--) {
if (f[i] > h)
nn[i] = 0;
else {
nn[i] = (h + 1) - f[i];
}
h = ma(h, f[i]);
}
for (int i = 1; i <= n; i++) {
printf("%d", nn[i]);
if (i == n)
printf("\n");
else
printf(" ");
}
return 0;
}
| CPP |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 |
input()
H = [int(x) for x in input().split(' ')]
T = []
mx = H[-1]
for i, e in enumerate(H):
j = len(H)-i-1
if j == len(H)-1:
T.append(0)
else:
h = mx-H[j]
if h >= 0:
T.append(h+1)
else:
T.append(0)
mx = max(mx, H[j])
for i, x in enumerate(T):
print(T[len(T)-i-1], end=" ")
print()
| PYTHON3 |
581_B. Luxurious Houses | The capital of Berland has n multifloor buildings. The architect who built up the capital was very creative, so all the houses were built in one row.
Let's enumerate all the houses from left to right, starting with one. A house is considered to be luxurious if the number of floors in it is strictly greater than in all the houses with larger numbers. In other words, a house is luxurious if the number of floors in it is strictly greater than in all the houses, which are located to the right from it. In this task it is assumed that the heights of floors in the houses are the same.
The new architect is interested in n questions, i-th of them is about the following: "how many floors should be added to the i-th house to make it luxurious?" (for all i from 1 to n, inclusive). You need to help him cope with this task.
Note that all these questions are independent from each other β the answer to the question for house i does not affect other answers (i.e., the floors to the houses are not actually added).
Input
The first line of the input contains a single number n (1 β€ n β€ 105) β the number of houses in the capital of Berland.
The second line contains n space-separated positive integers hi (1 β€ hi β€ 109), where hi equals the number of floors in the i-th house.
Output
Print n integers a1, a2, ..., an, where number ai is the number of floors that need to be added to the house number i to make it luxurious. If the house is already luxurious and nothing needs to be added to it, then ai should be equal to zero.
All houses are numbered from left to right, starting from one.
Examples
Input
5
1 2 3 1 2
Output
3 2 0 2 0
Input
4
3 2 1 4
Output
2 3 4 0 | 2 | 8 | __author__ = 'User'
n = int(input())
arr = list(map(int, input().split()))
mx = [0] * (n + 1)
for i in range(n - 1, -1, -1):
#print(arr[i], mx[i + 1])
mx[i] = max(mx[i + 1], arr[i])
for j in range(n):
if mx[j + 1] >= arr[j]:
print(mx[j] - arr[j] + 1, end = " ")
else:
print(0, end = " ") | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.