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
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct Node { int l, r, m; Node(int ip = 0, int is = 0, int im = 0) { l = ip, r = is, m = im; } }; Node tr[1200005]; long long int a[300005]; int arr[300005]; void combine(int v, int l, int r) { int v1 = 2 * v; int v2 = v1 + 1; int m = (l + r) / 2; int s1 = m - l + 1; int s2 = r - m; if (a[m] == 0 || a[m + 1] == 0 || (a[m] < 0 && a[m + 1] > 0)) { tr[v] = Node(tr[v1].l, tr[v2].r, max(tr[v1].m, tr[v2].m)); } else { tr[v].m = max(max(tr[v1].m, tr[v2].m), tr[v1].r + tr[v2].l); tr[v].l = tr[v1].l + ((tr[v1].m == s1) ? tr[v2].l : 0); tr[v].r = tr[v2].r + ((tr[v2].m == s2) ? tr[v1].r : 0); } } void buildTree(int v, int l, int r) { if (l == r) { if (a[l] == 0) { tr[v] = Node(0, 0, 0); } else { tr[v] = Node(1, 1, 1); } return; } int m = (l + r) / 2; buildTree(2 * v, l, m); buildTree(2 * v + 1, m + 1, r); combine(v, l, r); } void update(int v, int l, int r, int ind) { if (l == r) { if (a[l] == 0) { tr[v] = Node(0, 0, 0); } else { tr[v] = Node(1, 1, 1); } return; } int m = (l + r) / 2; if (ind <= m) update(2 * v, l, m, ind); else update(2 * v + 1, m + 1, r, ind); combine(v, l, r); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 1; i < n; i++) { a[i] = arr[i] - arr[i - 1]; } if (n > 1) buildTree(1, 1, n - 1); int m, l, r, d; cin >> m; while (m--) { cin >> l >> r >> d; l--, r--; if (l != 0) { a[l] += d; update(1, 1, n - 1, l); } if (r < n - 1) { a[r + 1] -= d; update(1, 1, n - 1, r + 1); } cout << tr[1].m + 1 << endl; } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 3e5; int A[N], n; struct Node { int L, R, ans, l, r, pl, pr; long long num1, num2; Node() { ans = -1; } Node(int a) { L = R = l = r = pl = pr = a; ans = 1; num1 = A[a]; num2 = A[a]; } } T[4 * N]; long long flag[4 * N]; long long num1, num2; struct BIT { long long bit[N + 2]; void update(int pos, long long val) { while (pos <= N) { bit[pos] += val; pos += (pos & -pos); } return; } long long query(int pos) { long long sum = 0; while (pos) { sum += bit[pos]; pos -= (pos & -pos); } return sum; } } b1, b2; Node operator+(Node a, Node b) { if (a.ans == -1) return b; if (b.ans == -1) return a; Node c; c.L = a.L; c.R = b.R; c.num1 = a.num1; c.num2 = b.num2; num1 = a.num2; num2 = b.num1; c.ans = max(a.ans, b.ans); c.l = a.l; c.r = b.r; c.pl = a.pl; c.pr = b.pr; if (num1 != num2) { if (num1 > num2) { if (a.l == a.R) c.l = b.l; if (b.l == b.R) { c.pr = min(c.pr, a.pr); } if (a.pl == a.R) c.pl = b.l; c.ans = max(c.ans, b.l - a.pr + 1); } else { if (b.r == b.L) c.r = a.r; if (b.pr == b.L) c.pr = a.r; if (a.r == a.L) { c.pl = max(c.pl, b.pl); } c.ans = max(c.ans, b.pl - a.r + 1); } c.ans = max(c.ans, b.l - a.r + 1); } return c; } void push(int node, int a, int b) { if (flag[node] == 0) return; T[node].num1 += flag[node]; T[node].num2 += flag[node]; if (a != b) { flag[node * 2 + 1] += flag[node]; flag[node * 2 + 2] += flag[node]; } flag[node] = 0; } void update(int node, int a, int b, int lo, int hi, int val) { push(node, a, b); if (hi < a || b < lo) return; if (lo <= a && b <= hi) { flag[node] += val; push(node, a, b); return; } int mi = (a + b) >> 1; update(node * 2 + 1, a, mi, lo, hi, val); update(node * 2 + 2, mi + 1, b, lo, hi, val); T[node] = T[(node << 1) + 1] + T[(node << 1) + 2]; } void build(int node, int a, int b) { if (a == b) { T[node] = Node(a); return; } int mi = (a + b) >> 1; build((node << 1) + 1, a, mi); build((node << 1) + 2, mi + 1, b); T[node] = T[(node << 1) + 1] + T[(node << 1) + 2]; } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } build(0, 0, n - 1); int m; cin >> m; int x, y, val; for (int i = 0; i < m; i++) { cin >> x >> y >> val; update(0, 0, n - 1, x - 1, y - 1, val); printf("%d\n", T[0].ans); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 500000; long long t[N * 4], tr[N * 4], tl[N * 4]; long long a[N], d[N]; inline long long zn(long long x) { if (x < 0) return -1; if (x == 0) return 0; return 1; } void add(int v, int l, int r, int i, long long delt) { if (l == r) { d[i] += delt; if (d[i] != 0) { tr[v] = 1; tl[v] = 1; t[v] = 1; } else { tr[v] = 0; tl[v] = 0; t[v] = 0; } return; } int ir = (v << 1) ^ 1; int il = (v << 1); int c = (l + r) >> 1; if (i <= c) add(il, l, c, i, delt); else add(ir, c + 1, r, i, delt); t[v] = max(t[il], t[ir]); tl[v] = tl[il]; tr[v] = tr[ir]; if (zn(d[c]) >= zn(d[c + 1])) { t[v] = max(t[v], tr[il] + tl[ir]); if (tl[il] == c - l + 1) { tl[v] = max(tl[il] + tl[ir], tl[v]); } if (tr[ir] == r - c) { tr[v] = max(tr[ir] + tr[il], tr[v]); } } t[v] = max(max(tl[v], tr[v]), t[v]); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); } if (n == 1) { int q; scanf("%d", &q); while (q--) { int l, r; long long delt; scanf("%d%d%I64d", &l, &r, &delt); printf("1\n"); } return 0; } n--; for (int i = 1; i <= n; i++) { add(1, 1, n, i, a[i + 1] - a[i]); } int q; scanf("%d", &q); while (q--) { int l, r; long long delt; scanf("%d%d%I64d", &l, &r, &delt); l--; if (l > 0) add(1, 1, n, l, delt); add(1, 1, n, r, -delt); printf("%I64d\n", t[1] + 1LL); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; long long inp[300009]; long long arr[300009]; struct node { int si, ei; int lf, md, rg; node(int sii, int eii, int lff = 0, int mdd = 0, int rgg = 0) { si = sii; ei = eii; lf = lff, rg = rgg, md = mdd; } node() { si = ei = lf = md = rg = 0; } }; struct seg_tree { node nd[4 * 300009]; void init(int in, int s, int e) { node &n = nd[in]; if (s == e) { arr[s] = inp[s] - inp[s + 1]; n = node(s, e); upd_nd(n, 0); return; } int mid = (s + e) / 2; init(2 * in, s, mid); init(2 * in + 1, mid + 1, e); n = merge(nd[2 * in], nd[2 * in + 1]); } node merge(node a, node b) { int l = a.ei; int r = b.si; int lf = a.lf; int rg = b.rg; int md = max(a.md, b.md); if (arr[l] == 0 || arr[r] == 0 || (arr[l] > 0 && arr[r] < 0)) { } else { if (a.md == a.ei - a.si + 1) { lf = max(lf, a.md + b.lf); } if (b.md == b.ei - b.si + 1) { rg = max(rg, b.md + a.rg); } md = max(md, a.rg + b.lf); } return node(a.si, b.ei, lf, md, rg); } void upd_nd(node &n, int v) { arr[n.si] += v; long long x = arr[n.si]; n.lf = n.md = n.rg = (x != 0); } node query_r(int in, int s, int e) { node &n = nd[in]; if (n.si == s && n.ei == e) return n; int mid = (n.si + n.ei) / 2; if (e <= mid) return query_r(2 * in, s, e); else if (s > mid) return query_r(2 * in + 1, s, e); else return merge(query_r(2 * in, s, mid), query_r(2 * in + 1, mid + 1, e)); } void update_p(int in, int i, int v) { node &n = nd[in]; if (n.si == n.ei) { upd_nd(n, v); return; } int mid = (n.si + n.ei) / 2; if (i <= mid) update_p(2 * in, i, v); else update_p(2 * in + 1, i, v); n = merge(nd[2 * in], nd[2 * in + 1]); } }; seg_tree st; int main() { int n; scanf("%d", &n); for (int i = 1; i < n + 1; i++) scanf("%lld", &inp[i]); int m; scanf("%d", &m); if (n == 1) { for (int i = 0; i < m; i++) { printf("%d", 1); printf("\n"); } return 0; } st.init(1, 1, n - 1); for (int i = 0; i < m; i++) { int l, r, d; scanf("%d %d", &l, &r); scanf("%d", &d); if (l != 1) { st.update_p(1, l - 1, -d); } if (r != n) { st.update_p(1, r, d); } int ans = st.query_r(1, 1, n - 1).md + 1; printf("%d", ans); printf("\n"); cout.flush(); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; void sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio; struct FAIL { FAIL() { cout << "CHANGE!!!" << "\n"; } }; inline int sign(long long x) { return x > 0 ? 1 : x == 0 ? 0 : -1; } void recalc(multiset<int> &best, set<int> &s, int pos, vector<long long> &a, int add) { pos += 5; if (sign(a[pos]) == sign(a[pos] + add)) { a[pos] += add; return; } auto it0 = s.lower_bound(pos); auto start = prev(it0, 2); auto end = next(it0, 4); for (auto it = start, it2 = next(it), it3 = next(it2); it3 != end; ++it, ++it2, ++it3) { if (a[*it] > 0 && a[*it2] < 0) { best.erase(best.find(*it3 - *it)); } } for (auto it = start, it2 = next(it); it2 != end; ++it, ++it2) { if (a[*it] != 0) { best.erase(best.find(*it2 - *it)); } } a[pos] += add; for (int i = (pos); i < (pos + 2); ++i) { if (i == 0 || sign(a[i]) * sign(a[i - 1]) != 1) { s.insert(i); } else { s.erase(i); } } for (auto it = start, it2 = next(it), it3 = next(it2); it3 != end; ++it, ++it2, ++it3) { if (a[*it] > 0 && a[*it2] < 0) { best.insert(*it3 - *it); } } for (auto it = start, it2 = next(it); it2 != end; ++it, ++it2) { if (a[*it] != 0) { best.insert(*it2 - *it); } } } int main() { int n; cin >> n; vector<long long> a(n); for (int __i = 0; __i < (n); ++__i) cin >> a[__i]; ; for (int i = (0); i < (n - 1); ++i) { a[i] = a[i + 1] - a[i]; } a.pop_back(); for (int i = (0); i < (5); ++i) { a.insert(a.begin(), 0); } for (int i = (0); i < (5); ++i) { a.push_back(0); } int m; cin >> m; set<int> start; for (int i = (0); i < (n - 1 + 2 * 5); ++i) { if (i == 0 || sign(a[i]) * sign(a[i - 1]) != 1) { start.insert(i); } } multiset<int> best; for (auto it = start.begin(), it2 = next(it), it3 = next(it2); it3 != start.end(); ++it, ++it2, ++it3) { if (a[*it] > 0 && a[*it2] < 0) { best.insert(*it3 - *it); } } for (auto it = start.begin(), it2 = next(it); it2 != start.end(); ++it, ++it2) { if (a[*it] != 0) { best.insert(*it2 - *it); } } vector<int> res(m); for (int j = (0); j < (m); ++j) { int x, y, d; cin >> x >> y >> d; x -= 2; --y; if (0 <= x && x < n - 1) { recalc(best, start, x, a, d); } if (0 <= y && y < n - 1) { recalc(best, start, y, a, -d); } res[j] = best.size() ? *best.rbegin() + 1 : 1; } for (auto elem : res) { cout << elem << "\n"; } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, a[300010], l, r, addv; long long del[300010]; int lx[300010 << 2], rx[300010 << 2], ans[300010 << 2]; bool con(long long x, long long y) { if (!x || !y) return false; if (x > 0) return true; if (x < 0 && y < 0) return true; return false; } void push_up(int id, int l, int r) { int idL = id << 1, idR = id << 1 ^ 1; int mid = l + r >> 1; int mx = max(ans[idL], ans[idR]); lx[id] = lx[idL]; rx[id] = rx[idR]; if (con(del[mid], del[mid + 1])) { mx = max(mx, rx[idL] + lx[idR]); if (lx[idL] == mid - l + 1) lx[id] += lx[idR]; if (rx[idR] == r - mid) rx[id] += rx[idL]; } ans[id] = mx; } void add(int l, int r, int idx, int d, int id) { if (l >= r) { del[idx] += d; if (!del[idx]) rx[id] = lx[id] = ans[id] = 0; else rx[id] = lx[id] = ans[id] = 1; return; } int mid = l + r >> 1; if (mid >= idx) add(l, mid, idx, d, id << 1); else add(mid + 1, r, idx, d, id << 1 ^ 1); push_up(id, l, r); return; } int main() { int ksh = 100000000; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) add(1, n - 1, i, a[i] - a[i - 1], 1); scanf("%d", &m); while (m--) { scanf("%d%d%d", &l, &r, &addv); if (l > 1) add(1, n - 1, l - 1, addv, 1); if (r < n) add(1, n - 1, r, -addv, 1); printf("%d\n", ans[1] + 1); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; long long a[300010]; struct Node { Node *lch, *rch; long long add, lazy, lv, rv; int rhill, lhill, rdec, ldec, maxhill; Node() { lch = rch = nullptr; lv = rv = maxhill = rhill = lhill = rdec = ldec = add = lazy = 0; } inline void up(int l, int r) { int mid = (l + r) >> 1; lv = lch->lv; rv = rch->rv; maxhill = max(lch->maxhill, rch->maxhill); if (lch->rv < rch->lv) maxhill = max(maxhill, lch->rdec + rch->lhill); if (lch->rv > rch->lv) maxhill = max(maxhill, lch->rhill + rch->ldec); if (lch->rv != rch->lv) maxhill = max(maxhill, lch->rdec + rch->ldec); rhill = rch->rhill; if (rhill == r - mid && lch->rv < rch->lv) rhill += lch->rdec; if (rch->ldec == r - mid && lch->rv > rch->lv) rhill = max(rhill, lch->rhill + rch->ldec); if (rch->ldec == r - mid && lch->rv != rch->lv) rhill = max(rhill, lch->rdec + rch->ldec); lhill = lch->lhill; if (lhill == mid - l + 1 && lch->rv > rch->lv) lhill += rch->ldec; if (lch->rdec == mid - l + 1 && lch->rv < rch->lv) lhill = max(lhill, lch->rdec + rch->lhill); if (lch->rdec == mid - l + 1 && lch->rv != rch->lv) lhill = max(lhill, lch->rdec + rch->ldec); rdec = rch->rdec; if (rdec == r - mid && lch->rv < rch->lv) rdec += lch->rdec; ldec = lch->ldec; if (ldec == mid - l + 1 && lch->rv > rch->lv) ldec += rch->ldec; } inline void down() { if (lazy) { lch->add += lazy; lch->lazy += lazy; lch->lv += lazy; lch->rv += lazy; rch->add += lazy; rch->lazy += lazy; rch->lv += lazy; rch->rv += lazy; lazy = 0; } } }* root = nullptr; void init(int l, int r, Node*& node) { node = new Node(); if (l == r) { node->rhill = node->lhill = node->rdec = node->ldec = node->maxhill = 1; node->lv = node->rv = a[l]; } else { int mid = (l + r) >> 1; init(l, mid, node->lch); init(mid + 1, r, node->rch); node->up(l, r); } } void modify(int l, int r, int ql, int qr, int add, Node* node) { if (l == ql && r == qr) { node->add += add; node->lazy += add; node->lv += add; node->rv += add; } else { int mid = (l + r) >> 1; node->down(); if (qr <= mid) modify(l, mid, ql, qr, add, node->lch); else if (ql > mid) modify(mid + 1, r, ql, qr, add, node->rch); else modify(l, mid, ql, mid, add, node->lch), modify(mid + 1, r, mid + 1, qr, add, node->rch); node->up(l, r); } } int main() { int n, m, l, r, d; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", a + i); scanf("%d", &m); init(1, n, root); for (int i = 0; i < m; i++) { scanf("%d%d%d", &l, &r, &d); modify(1, n, l, r, d, root); printf("%d\n", root->maxhill); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; long long a[300005]; int sign(long long x) { return x > 0 ? 1 : -1; } struct Node { int l, r, lx, mx, rx; void up(Node L, Node R) { lx = L.lx; rx = R.rx; mx = max(L.mx, R.mx); if (a[L.r] && a[R.l] && sign(a[L.r]) >= sign(a[R.l])) { mx = max(mx, L.rx + R.lx); if (L.mx == L.r - L.l + 1) lx = L.mx + R.lx; if (R.mx == R.r - R.l + 1) rx = R.mx + L.rx; } mx = max({mx, lx, rx}); } } seg[300005 * 4]; void update(int qx, int c, int l, int r, int i) { seg[i] = {l, r}; if (l == r) { a[qx] += c; if (a[qx] != 0) seg[i] = {l, r, 1, 1, 1}; else seg[i] = {l, r, 0, 0, 0}; return; } int mid = (l + r) / 2; if (qx <= mid) update(qx, c, l, mid, i * 2); else update(qx, c, mid + 1, r, i * 2 + 1); seg[i].up(seg[i * 2], seg[i * 2 + 1]); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]); for (int i = 1; i < n; ++i) a[i] = a[i + 1] - a[i]; for (int i = 1; i < n; ++i) update(i, 0, 1, n - 1, 1); int Q; scanf("%d", &Q); while (Q--) { int l, r, x; scanf("%d%d%d", &l, &r, &x); if (l > 1) update(l - 1, x, 1, n - 1, 1); if (r < n) update(r, -x, 1, n - 1, 1); printf("%d\n", seg[1].mx + 1); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int arr[maxn]; long long a[maxn]; int n, m; int max_suf[4 * maxn], max_pre[4 * maxn], max_wid[4 * maxn]; int sign(long long x) { return x < 0 ? -1 : 1; } int S(int i, int j) { return j - i + 1; } int p; int upd_wid(int o, int L, int R) { int M = L + (R - L) / 2; if (L == R) return max_wid[o] = a[L] ? 1 : 0; else { int w1, w2, w3; if (p <= M) { w1 = upd_wid(2 * o, L, M); w2 = max_wid[2 * o + 1]; } else { w1 = max_wid[2 * o]; w2 = upd_wid(2 * o + 1, M + 1, R); } w3 = a[M] && a[M + 1] && sign(a[M + 1]) <= sign(a[M]) ? S(max_suf[2 * o], M) + S(M + 1, max_pre[2 * o + 1]) : 0; int ans = max(w1, w2); ans = max(ans, w3); return max_wid[o] = ans; } } int upd_suf(int o, int L, int R) { int M = L + (R - L) / 2; if (L == R) return max_suf[o] = L; else { int s1, s2; if (p <= M) { s1 = upd_suf(2 * o, L, M); s2 = max_suf[2 * o + 1]; } else { s2 = upd_suf(2 * o + 1, M + 1, R); s1 = max_suf[2 * o]; } int ans = s2; if (s2 == M + 1 && a[M] && a[M + 1] && sign(a[M + 1]) <= sign(a[M])) ans = s1; return max_suf[o] = ans; } } int upd_pre(int o, int L, int R) { int M = L + (R - L) / 2; if (L == R) return max_pre[o] = L; else { int p1, p2; if (p <= M) { p1 = upd_pre(2 * o, L, M); p2 = max_pre[2 * o + 1]; } else { p2 = upd_pre(2 * o + 1, M + 1, R); p1 = max_pre[2 * o]; } int ans = p1; if (p1 == M && a[M] && a[M + 1] && sign(a[M + 1]) <= sign(a[M])) ans = p2; return max_pre[o] = ans; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); for (int i = 1; i < n; i++) { a[i] = arr[i + 1] - arr[i]; p = i; upd_pre(1, 1, n - 1); upd_suf(1, 1, n - 1); upd_wid(1, 1, n - 1); } scanf("%d", &m); for (int i = 1; i <= m; i++) { int l, r, v; scanf("%d%d%d", &l, &r, &v); int wid; if (n != 1) { p = l - 1; a[l - 1] += v; upd_pre(1, 1, n - 1); upd_suf(1, 1, n - 1); upd_wid(1, 1, n - 1); p = r; a[r] -= v; upd_pre(1, 1, n - 1); upd_suf(1, 1, n - 1); upd_wid(1, 1, n - 1); wid = max_wid[1] + 1; } else wid = 1; printf("%d\n", wid); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> inline void umax(int& m, int u) { u > m && (m = u); } inline int max(int a, int b) { return a > b ? a : b; } struct seg { long long l, r, tag; int ans, len, l0, l1, l2, r0, r1, r2; void add(long long), init(int); } s[1048576]; seg operator+(const seg& a, const seg& b) { seg c; c.len = a.len + b.len, c.tag = 0; c.l = a.l, c.l0 = a.l0, c.l1 = a.l1, c.l2 = a.l2; c.r = b.r, c.r0 = b.r0, c.r1 = b.r1, c.r2 = b.r2; c.ans = max(a.ans, b.ans); if (a.r < b.l) { umax(c.ans, a.r0 + b.l1); if (b.r0 == b.len) c.r0 = b.r0 + a.r0; if (b.r1 == b.len) c.r1 = b.r1 + a.r0; if (a.l2 == a.len) c.l2 = a.l2 + b.l0; if (a.l2 == a.len) c.l1 = a.l2 + b.l1, c.l2 = a.l2 + b.l2; } if (a.r > b.l) { umax(c.ans, a.r1 + b.l0); if (a.l0 == a.len) c.l0 = a.l0 + b.l0; if (a.l1 == a.len) c.l1 = a.l1 + b.l0; if (b.r2 == b.len) c.r2 = b.r2 + a.r0; if (b.r2 == b.len) c.r1 = b.r2 + a.r1, c.r2 = b.r2 + a.r2; } return c; } void seg::add(long long x) { tag += x, l += x, r += x; } void seg::init(int x) { l = r = x, l0 = l1 = l2 = r0 = r1 = r2 = len = ans = 1; } int n; void pushdown(int now) { s[now << 1].add(s[now].tag); s[now << 1 | 1].add(s[now].tag); s[now].tag = 0; } void add(int L, int R, int c, int now = 1, int l = 1, int r = n) { if (L <= l && r <= R) return s[now].add(c); if (s[now].tag) pushdown(now); int m = (l + r) >> 1; if (L <= m) add(L, R, c, now << 1, l, m); if (m < R) add(L, R, c, now << 1 | 1, m + 1, r); s[now] = s[now << 1] + s[now << 1 | 1]; } int a[300001]; void init(int now = 1, int l = 1, int r = n) { if (l == r) return s[now].init(a[l]); int m = (l + r) >> 1; init(now << 1, l, m); init(now << 1 | 1, m + 1, r); s[now] = s[now << 1] + s[now << 1 | 1]; } int main() { int m, l, r, i; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", a + i); init(); scanf("%d", &m); while (m--) scanf("%d%d%d", &l, &r, &i), add(l, r, i), printf("%d\n", s[1].ans); return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> #pragma comment(linker, "/stack:20000000") using namespace std; template <class T> int sign(T x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } struct Node { int lval, mval, rval; }; Node tr[2340400]; long long a[1010101]; inline void recalc(int cur, int l, int r) { int m = (l + r) / 2; int dcur = cur + cur; tr[cur].mval = max(tr[dcur].mval, tr[dcur + 1].mval); if (!a[m] || !a[m + 1] || sign(a[m]) < sign(a[m + 1])) { tr[cur].lval = tr[dcur].lval; tr[cur].rval = tr[dcur + 1].rval; } else { tr[cur].mval = max(tr[cur].mval, tr[dcur].rval + tr[dcur + 1].lval); if (tr[dcur].mval == m - l + 1) { tr[cur].lval = tr[dcur].lval + tr[dcur + 1].lval; } else { tr[cur].lval = tr[dcur].lval; } if (tr[dcur + 1].mval == r - m) { tr[cur].rval = tr[dcur + 1].rval + tr[dcur].rval; } else { tr[cur].rval = tr[dcur + 1].rval; } } } void build(int cur, int l, int r) { if (l == r) { int x = !!a[l]; tr[cur] = {x, x, x}; } else { int m = (l + r) / 2; int dcur = cur + cur; build(dcur, l, m); build(dcur + 1, m + 1, r); recalc(cur, l, r); } } void update(int cur, int l, int r, int pos, int val) { if (l == r) { a[pos] += val; int x = !!a[l]; tr[cur] = {x, x, x}; } else { int m = (l + r) / 2; int dcur = cur + cur; if (pos <= m) { update(dcur, l, m, pos, val); } else { update(dcur + 1, m + 1, r, pos, val); } recalc(cur, l, r); } } int arr[1010101]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (int i = 0; i < n - 1; ++i) { a[i] = arr[i + 1] - arr[i]; } if (n > 1) { build(1, 0, n - 2); } int m; scanf("%d", &m); while (m--) { int l, r, d; scanf("%d%d%d", &l, &r, &d); if (n == 1) { puts("1"); continue; } if (l != 1) { update(1, 0, n - 2, l - 2, d); } if (r != n) { update(1, 0, n - 2, r - 1, -d); } printf("%d\n", tr[1].mval + 1); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 2; int n, q, l, r, ar[N], x; struct st { long long pr, sf, prr, sff, mx, must, sl, pl, l; st() { l = pr = sf = prr = sff = mx = must = sl = pl = 0; } }; st t[N * 4]; inline st combine(st a, st b) { st c; c.pr = a.pr; c.sf = b.sf; if (a.l == 1) c.prr = b.pr; else c.prr = a.prr; if (b.l == 1) c.sff = a.sf; else c.sff = b.sff; c.l = a.l + b.l; c.mx = max(a.mx, b.mx); c.pl = a.pl; c.sl = b.sl; int t1 = 0, t2 = 0; if (a.sf < a.sff) t1 = 1; if (a.sf > a.sff) t1 = 2; if (b.pr > b.prr) t2 = 1; if (b.pr < b.prr) t2 = 2; if (a.sf < b.pr) { if (t1 != 1) { c.mx = max(c.mx, a.sl + b.pl); if (a.sl == a.l) c.pl += b.pl; if (b.pl == b.l) c.sl += a.sl; } else { c.mx = max(c.mx, b.pl + 1); if (b.pl == b.l) c.sl++; } } if (a.sf > b.pr) { if (t2 != 2) { c.mx = max(c.mx, a.sl + b.pl); if (a.sl == a.l) c.pl += b.pl; if (b.pl == b.l) c.sl += a.sl; } else { c.mx = max(c.mx, a.sl + 1); if (a.sl == a.l) c.pl++; } } return c; } void build(int v = 1, int tl = 1, int tr = n) { if (tl == tr) { t[v].pl = t[v].sl = t[v].l = t[v].mx = 1; t[v].sf = t[v].sff = t[v].pr = t[v].prr = ar[tl]; } else { int tm = (tl + tr) >> 1; build(v + v, tl, tm); build(v + v + 1, tm + 1, tr); t[v] = combine(t[v + v], t[v + v + 1]); } } inline void push(int v, int tl, int tr) { t[v].pr += t[v].must; t[v].prr += t[v].must; t[v].sf += t[v].must; t[v].sff += t[v].must; if (tl != tr) { t[v + v].must += t[v].must; t[v + v + 1].must += t[v].must; } t[v].must = 0; } inline void update(int l, int r, int x, int v = 1, int tl = 1, int tr = n) { push(v, tl, tr); if (l > r || l > tr || tl > r) return; if (l == tl && r == tr) { t[v].must = x; push(v, tl, tr); return; } int tm = (tl + tr) >> 1; update(l, min(tm, r), x, v + v, tl, tm); update(max(l, tm + 1), r, x, v + v + 1, tm + 1, tr); t[v] = combine(t[v + v], t[v + v + 1]); } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", ar + i); build(); cin >> q; while (q--) { scanf("%d%d%d", &l, &r, &x); update(l, r, x); printf("%I64d\n", t[1].mx); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct Node { int pref, suf, pref_neg, suf_pos, ans, cant; Node() {} void init(long long v) { if (v < 0) { pref = 0; suf = 0; pref_neg = 1; suf_pos = 0; ans = 1; cant = 1; } else if (v > 0) { pref = 0; suf = 0; pref_neg = 0; suf_pos = 1; ans = 1; cant = 1; } else { pref = 0; suf = 0; pref_neg = 0; suf_pos = 0; ans = 0; cant = 1; } } void ver() { cout << "cant = " << cant << endl << "pref = " << pref << endl << "suf = " << suf << endl << "pref_neg = " << pref_neg << endl << "suf_pos = " << suf_pos << endl << "ans = " << ans << endl; } void merge(const Node &a, const Node &b) { cant = a.cant + b.cant; suf = pref = 0; if (a.suf_pos == a.cant && b.pref_neg == b.cant) pref = suf = cant; suf = max(suf, b.suf); if (b.suf == b.cant) suf += a.suf_pos; pref = max(pref, a.pref); if (a.pref == a.cant) pref += b.pref_neg; suf_pos = b.suf_pos; if (b.suf_pos == b.cant) suf_pos += a.suf_pos; pref_neg = a.pref_neg; if (a.pref_neg == a.cant) pref_neg += b.pref_neg; if (b.pref_neg == b.cant) { suf = max(suf, a.suf + b.pref_neg); suf = max(suf, a.suf_pos + b.pref_neg); } if (a.suf_pos == a.cant) { pref = max(pref, a.suf_pos + b.pref); pref = max(pref, a.suf_pos + b.pref_neg); } ans = max(a.ans, b.ans); ans = max(ans, a.suf_pos + b.pref_neg); ans = max(ans, a.suf + b.pref_neg); ans = max(ans, a.suf_pos + b.pref); ans = max(ans, pref); ans = max(ans, pref_neg); ans = max(ans, suf); ans = max(ans, suf_pos); } }; Node T[1050000]; int I, J, V; long long d[300000]; void init(int id, int L, int R) { if (L == R) T[id].init(d[L]); else { int a = id << 1, M = (L + R) >> 1; init(a, L, M); init(a | 1, M + 1, R); T[id].merge(T[a], T[a | 1]); } } void update(int id, int L, int R) { if (L == R) { d[L] += V; T[id].init(d[L]); } else { int a = id << 1, M = (L + R) >> 1; if (J <= M) update(a, L, M); else if (I > M) update(a | 1, M + 1, R); else { update(a, L, M); update(a | 1, M + 1, R); } T[id].merge(T[a], T[a | 1]); } } Node query(int id, int L, int R) { if (I <= L && R <= J) return T[id]; int a = id << 1, M = (L + R) >> 1; if (J <= M) return query(a, L, M); else if (I > M) return query(a | 1, M + 1, R); else { Node ans; ans.merge(query(a, L, M), query(a | 1, M + 1, R)); return ans; } } int a[300001]; bool valid(int pos) { return (pos <= 9 || (pos > 11 && pos < 14)); } void verT() { cout << "i: "; for (int i = 1; i <= 25; ++i) if (valid(i)) printf("%3d", i); cout << endl; cout << "ans: "; for (int i = 1; i <= 25; ++i) if (valid(i)) printf("%3d", T[i].ans); cout << endl; cout << "pref :"; for (int i = 1; i <= 25; ++i) if (valid(i)) printf("%3d", T[i].pref); cout << endl; cout << "pref_neg:"; for (int i = 1; i <= 25; ++i) if (valid(i)) printf("%3d", T[i].pref_neg); cout << endl; cout << "suf :"; for (int i = 1; i <= 25; ++i) if (valid(i)) printf("%3d", T[i].suf); cout << endl; cout << "suf_pos :"; for (int i = 1; i <= 25; ++i) if (valid(i)) printf("%3d", T[i].suf_pos); cout << endl; cout << "cant :"; for (int i = 1; i <= 25; ++i) if (valid(i)) printf("%3d", T[i].cant); cout << endl; } int main(int argc, char const *argv[]) { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 1; i < n; ++i) d[i - 1] = a[i] - a[i - 1]; if (n > 1) init(1, 0, n - 2); I = 0; J = n - 2; int m, x, y; scanf("%d", &m); while (m--) { scanf("%d %d %d", &x, &y, &V); x--; y--; if (x > 0) { J = I = x - 1; update(1, 0, n - 2); } V = -V; if (y < n - 1) { J = I = y; update(1, 0, n - 2); } I = 0; J = n - 2; if (n > 1) printf("%d\n", query(1, 0, n - 2).ans + 1); else printf("1\n"); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; typedef pair<long long, long long> ll; typedef vector<long long> vl; typedef vector<ll> vll; typedef vector<vl> vvl; template <typename T> ostream &operator<<(ostream &o, vector<T> v) { if (v.size() > 0) o << v[0]; for (unsigned i = 1; i < v.size(); i++) o << " " << v[i]; return o << "\n"; } template <typename U, typename V> ostream &operator<<(ostream &o, pair<U, V> p) { return o << "(" << p.first << ", " << p.second << ") "; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (unsigned i = 0; i < v.size(); i++) in >> v[i]; return in; } template <typename T> istream &operator>>(istream &in, pair<T, T> &p) { in >> p.first; in >> p.second; return in; } vl a(300100), b(300100); struct item { long long pref; long long suf; long long best; item() : pref(0), suf(0), best(0) {} item(long long _pref, long long _suf, long long _best) : pref(_pref), suf(_suf), best(_best) {} }; item t[300100 * 4]; long long sign(long long n) { if (n < 0) return -1; else if (n > 0) return 1; else return 0; } item merge(item x, item y, long long tl, long long tm, long long tr) { item res(0, 0, 0); res.best = max(x.best, y.best); if (sign(b[tm]) <= sign(b[tm + 1])) { res.best = max(res.best, x.suf + y.pref); } res.pref = x.pref; if (x.pref == tm - tl + 1 and sign(b[tm]) <= sign(b[tm + 1])) { res.pref += y.pref; } res.suf = y.suf; if (y.suf == tr - tm and sign(b[tm]) <= sign(b[tm + 1])) { res.suf += x.suf; } return res; } void build(long long node, long long tl, long long tr) { if (tl == tr) { int o = b[tl] != 0; t[node] = item(o, o, o); return; } long long mid = (tl + tr) / 2; build(2 * node + 1, tl, mid); build(2 * node + 2, mid + 1, tr); t[node] = merge(t[2 * node + 1], t[2 * node + 2], tl, mid, tr); return; } void update(long long node, long long tl, long long tr, long long idx, long long val) { if (tl == tr) { b[tl] += val; long long o = b[tl] != 0; t[node] = item(o, o, o); return; } long long mid = (tl + tr) / 2; if (idx <= mid) update(2 * node + 1, tl, mid, idx, val); else update(2 * node + 2, mid + 1, tr, idx, val); t[node] = merge(t[2 * node + 1], t[2 * node + 2], tl, mid, tr); } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(20); long long n; cin >> n; a.resize(n); cin >> a; b.resize(n - 1); for (long long i = (0); i < (long long)n; i++) b[i] = a[i] - a[i + 1]; if (n != 1) build(0, 0, n - 2); long long q; cin >> q; while (q--) { long long l, r, x; cin >> l >> r >> x; if (n == 1) { cout << "1" << "\n"; continue; } l--; r--; if (l) update(0, 0, n - 2, l - 1, -x); if (r < n - 1) update(0, 0, n - 2, r, x); cout << t[0].best + 1 << "\n"; } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct Tree { int lx, mx, rx; } tree[3000050]; long long a[300050]; int F(long long x) { return x > 0 ? 1 : -1; } void Update(int root, int nowl, int nowr, int askl, int askr) { int mid = (nowl + nowr) / 2; if (nowr < askl || askr < nowl) return; if (askl <= nowl && nowr <= askr) { if (a[mid]) tree[root].lx = tree[root].rx = tree[root].mx = 1; else tree[root].lx = tree[root].rx = tree[root].mx = 0; return; } Update(2 * root, nowl, mid, askl, askr); Update(2 * root + 1, mid + 1, nowr, askl, askr); tree[root].mx = max(tree[2 * root].mx, tree[2 * root + 1].mx); tree[root].lx = tree[2 * root].lx; tree[root].rx = tree[2 * root + 1].rx; if (a[mid] && a[mid + 1] && F(a[mid]) >= F(a[mid + 1])) { tree[root].mx = max(tree[root].mx, tree[2 * root].rx + tree[2 * root + 1].lx); if (tree[2 * root].lx == mid - nowl + 1) tree[root].lx = mid - nowl + 1 + tree[2 * root + 1].lx; if (tree[2 * root + 1].rx == nowr - mid) tree[root].rx = nowr - mid + tree[2 * root].rx; } tree[root].mx = max(tree[root].mx, max(tree[root].lx, tree[root].rx)); } int main() { int n, m, i, x, y, z; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (i = 1; i < n; i++) a[i] = a[i + 1] - a[i], Update(1, 1, n - 1, i, i); scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d%d%d", &x, &y, &z); if (x > 1) a[x - 1] += z, Update(1, 1, n - 1, x - 1, x - 1); if (y < n) a[y] -= z, Update(1, 1, n - 1, y, y); printf("%d\n", tree[1].mx + 1); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int MaxN = 300005; int N, A[MaxN]; struct ST { long long C[MaxN]; inline int lowbit(int x) { return x & (-x); } inline void add(int x, long long d) { for (; x <= N; x += lowbit(x)) C[x] += d; } inline void init() { memset(C, 0, sizeof(C)); } inline void update(int L, int R, long long d) { if (R < L) return; add(L, d); add(R + 1, -d); } inline long long query(int x) { if (x < 1 || x > N) return 0; long long ret = 0; for (; x; x -= lowbit(x)) ret += C[x]; return ret; } } LX, RX, H; struct BIT { long long bit[MaxN << 2], col[MaxN << 2]; inline void pushUP(int po) { bit[po] = max(bit[(po << 1)], bit[((po << 1) | 1)]); } inline void pushDonw(int po) { if (col[po]) { col[(po << 1)] += col[po]; col[((po << 1) | 1)] += col[po]; bit[(po << 1)] += col[po]; bit[((po << 1) | 1)] += col[po]; col[po] = 0; } } inline void update(int ul, int ur, long long ud, int L, int R, int po) { if (ul <= L && ur >= R) { col[po] += ud; bit[po] += ud; return; } pushDonw(po); int M = (L + R) >> 1; if (ul <= M) update(ul, ur, ud, L, M, (po << 1)); if (ur > M) update(ul, ur, ud, M + 1, R, ((po << 1) | 1)); pushUP(po); } inline void update(int L, int R, int d) { if (R < L) return; update(L, R, d, 1, N, 1); } inline int getans() { return bit[1] - 1; } } ans; inline int findL(int x, int y) { if (y < x) return x - 1; int L = x, R = y, M; while (R > L) { M = (L + R + 1) >> 1; if (M + 1 - LX.query(M) <= x) L = M; else R = M - 1; } return L; } inline int findR(int x, int y) { if (y < x) return y + 1; int L = x, R = y, M; while (R > L) { M = (L + R) >> 1; if (M - 1 + RX.query(M) >= y) R = M; else L = M + 1; } return L; } int main() { scanf("%d", &N); for (int i = 1; i <= N; ++i) { scanf("%d", A + i); H.update(i, i, A[i]); } A[0] = A[N + 1] = 0; long long t = 0; for (int i = 1; i <= N; ++i) { if (A[i] > A[i - 1]) ++t; else t = 1; LX.update(i, i, t); ans.update(i, i, t); } t = 0; for (int i = N; i >= 1; --i) { if (A[i] > A[i + 1]) ++t; else t = 1; RX.update(i, i, t); ans.update(i, i, t); } int M, a, b, c; long long b1, b2, b3, b4; long long ta, tb; scanf("%d", &M); while (M--) { scanf("%d %d %d", &a, &b, &c); b1 = a - 1 ? H.query(a - 1) : 0; b2 = H.query(a); b3 = H.query(b); b4 = b + 1 <= N ? H.query(b + 1) : 0; H.update(a, b, c); ta = LX.query(a - 1); tb = LX.query(b); if (b2 <= b1 && b2 + c > b1) { t = findL(a, b); LX.update(a, t, ta); ans.update(a, t, ta); if (t == b && b4 > b3 + c) { t = findL(b + 1, N); LX.update(b + 1, t, ta); ans.update(b + 1, t, ta); } } if (b4 > b3 && b4 <= b3 + c) { t = findL(b + 1, N); LX.update(b + 1, t, -tb); ans.update(b + 1, t, -tb); } ta = RX.query(a); tb = b + 1 <= N ? RX.query(b + 1) : 0; if (b3 <= b4 && b3 + c > b4) { t = findR(a, b); RX.update(t, b, tb); ans.update(t, b, tb); if (t == a && b1 > b2 + c) { t = findR(1, a - 1); RX.update(t, a - 1, tb); ans.update(t, a - 1, tb); } } if (b1 > b2 && b1 <= b2 + c) { t = findR(1, a - 1); RX.update(t, a - 1, -ta); ans.update(t, a - 1, -ta); } printf("%d\n", ans.getans()); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class AlyonaTowers { int N = Integer.highestOneBit((int) 3e5) << 2; int n; long[] b; Node[] node = new Node[N]; { for (int i = 0; i < N; i++) node[i] = new Node(0, 0, 0); } void solve() { n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); b = new long[n - 1]; for (int i = 0; i < n - 1; i++) b[i] = a[i + 1] - a[i]; if (n > 1) build(0, 0, n - 1); int T = in.nextInt(); while (T-- > 0) { int l = in.nextInt() - 1, r = in.nextInt(), d = in.nextInt(); if (n == 1) { out.println(1); } else { if (l - 1 >= 0) update(l - 1, d); if (r - 1 < n - 1) update(r - 1, -d); out.println(node[0].m + 1); } } } void build(int i, int l, int r) { if (l == r - 1) { int x = b[l] == 0 ? 0 : 1; node[i].l = node[i].m = node[i].r = x; } else { build(2 * i + 1, l, (l + r) / 2); build(2 * i + 2, (l + r) / 2, r); merge(i, l, r); } } void merge(int i, int l, int r) { int m = (l + r) / 2; node[i].m = Math.max(node[2 * i + 1].m, node[2 * i + 2].m); if (b[m - 1] == 0 || b[m] == 0 || sign(b[m - 1]) < sign(b[m])) { node[i].l = node[2 * i + 1].l; node[i].r = node[2 * i + 2].r; } else { node[i].m = Math.max(node[i].m, node[2 * i + 1].r + node[2 * i + 2].l); if (node[2 * i + 1].m == m - l) { node[i].l = node[2 * i + 1].l + node[2 * i + 2].l; } else { node[i].l = node[2 * i + 1].l; } if (node[2 * i + 2].m == r - m) { node[i].r = node[2 * i + 1].r + node[2 * i + 2].r; } else { node[i].r = node[2 * i + 2].r; } } } void update(int i, int d) { update(0, 0, n - 1, i, d); } void update(int k, int l, int r, int i, int d) { if (l == r - 1) { b[l] += d; int x = b[l] == 0 ? 0 : 1; node[k].l = node[k].m = node[k].r = x; } else { int m = (l + r) / 2; if (i < m) update(2 * k + 1, l, (l + r) / 2, i, d); else update(2 * k + 2, (l + r) / 2, r, i, d); merge(k, l, r); } } int sign(long x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } static class Node { int l, m, r; Node(int l, int m, int r) { this.l = l; this.m = m; this.r = r; } } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new AlyonaTowers().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
JAVA
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int a[maxn]; struct SegmentTree { struct Node { int Left, Both, Right; } st[6 * maxn]; int L[6 * maxn], R[6 * maxn]; long long V[maxn]; void Build(int k, int x, int y) { L[k] = x; R[k] = y; if (L[k] == R[k]) return; Build(k << 1, L[k], ((L[k] + R[k]) >> 1)); Build(k << 1 | 1, ((L[k] + R[k]) >> 1) + 1, R[k]); } int Sign(long long val) { if (val < 0) return -1; else if (val == 0) return 0; else return 1; } void Recalc(int k) { st[k].Both = max(st[k << 1].Both, st[k << 1 | 1].Both); st[k].Left = st[k << 1].Left; st[k].Right = st[k << 1 | 1].Right; if (Sign(V[((L[k] + R[k]) >> 1)]) >= Sign(V[((L[k] + R[k]) >> 1) + 1])) { st[k].Both = max(st[k].Both, st[k << 1].Right + st[k << 1 | 1].Left); if (st[k << 1 | 1].Right == R[k] - (((L[k] + R[k]) >> 1) + 1) + 1) st[k].Right = max(st[k].Right, st[k << 1 | 1].Right + st[k << 1].Right); if (st[k << 1].Left == ((L[k] + R[k]) >> 1) - L[k] + 1) st[k].Left = max(st[k].Left, st[k << 1].Left + st[k << 1 | 1].Left); } st[k].Both = max(st[k].Both, max(st[k].Left, st[k].Right)); } void Update(int k, int i, int val) { if (L[k] == R[k] && L[k] == i) { V[L[k]] += (long long)val; if (V[L[k]] != 0) st[k].Left = st[k].Right = st[k].Both = 1; else st[k].Left = st[k].Right = st[k].Both = 0; return; } if (i <= ((L[k] + R[k]) >> 1)) Update(k << 1, i, val); else Update(k << 1 | 1, i, val); Recalc(k); } } Tree; int n, q; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); if (n == 1) { scanf("%d", &q); while (q--) puts("1"); return 0; } Tree.Build(1, 1, n - 1); for (int i = 1; i <= n - 1; ++i) Tree.Update(1, i, a[i + 1] - a[i]); scanf("%d", &q); while (q--) { int l, r, v; scanf("%d%d%d", &l, &r, &v); if (l - 1 != 0) Tree.Update(1, l - 1, v); if (r < n) Tree.Update(1, r, -v); printf("%d\n", Tree.st[1].Both + 1); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct treenode { int max, L, R; }; treenode tree[2000000]; long long g[500000]; int i, m, n, x, y, z; inline void inserttree(int l, int r, int x, int node) { if (l == r) { if (g[l] < 0) tree[node] = (treenode){1, 1, 1}; if (g[l] > 0) tree[node] = (treenode){1, 1, 1}; if (g[l] == 0) tree[node] = (treenode){0, 0, 0}; } else { int mid = (l + r) >> 1; if (x <= mid) inserttree(l, mid, x, node << 1); else inserttree(mid + 1, r, x, node << 1 | 1); tree[node].L = tree[node << 1].L; if ((tree[node << 1].L == mid - l + 1) && ((g[mid] < 0) || (g[mid + 1] > 0)) && (g[mid] != 0) && (g[mid + 1] != 0)) tree[node].L = tree[node].L + tree[node << 1 | 1].L; tree[node].R = tree[node << 1 | 1].R; if ((tree[node << 1 | 1].R == r - mid) && ((g[mid] < 0) || (g[mid + 1] > 0)) && (g[mid] != 0) && (g[mid + 1] != 0)) tree[node].R = tree[node].R + tree[node << 1].R; tree[node].max = max(tree[node << 1].max, tree[node << 1 | 1].max); if (((g[mid] < 0) || (g[mid + 1] > 0)) && (g[mid] != 0) && (g[mid + 1] != 0)) tree[node].max = max(tree[node].max, tree[node << 1].R + tree[node << 1 | 1].L); } return; } inline int fastscanf() { int t = 0; char c = getchar(); while (!((c > 47) && (c < 58))) c = getchar(); while ((c > 47) && (c < 58)) t = t * 10 + c - 48, c = getchar(); return t; } inline void fastprintf(int x) { if (!x) putchar('0'); else { char c[12]; for (c[0] = 0; x > 0; x = x / 10) c[0]++, c[c[0]] = x % 10; for (; c[0] > 0; c[0]--) putchar(c[c[0]] + 48); } puts(""); return; } int main() { n = fastscanf(); for (i = 1; i <= n; i++) g[i] = fastscanf(); for (i = 1; i <= n; i++) g[i] = g[i] - g[i + 1]; n--; if (!n) { m = fastscanf(); for (i = 1; i <= m; i++) fastprintf(1); return 0; } for (i = 1; i <= n; i++) inserttree(1, n, i, 1); m = fastscanf(); for (i = 1; i <= m; i++) { x = fastscanf(), y = fastscanf(), z = fastscanf(); g[x - 1] = g[x - 1] - z, g[y] = g[y] + z; if (x > 0) inserttree(1, n, x - 1, 1); inserttree(1, n, y, 1); fastprintf(tree[1].max + 1); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> const int Mod = (int)1e9 + 7; const int MX = 1073741822; const long long MXLL = 4611686018427387903; const int Sz = 1110111; using namespace std; inline void Read_rap() { ios_base ::sync_with_stdio(0); cin.tie(0); } struct node { int l, m, r; } t[Sz]; int n; long long a[Sz]; inline int sign(long long x) { if (x < 0) return -1; if (x == 0) return 0; if (x > 0) return 1; } inline void recalc(int v, int tl, int tr) { int tmid = (tl + tr) >> 1; t[v].l = t[2 * v].l; t[v].r = t[2 * v + 1].r; t[v].m = max(t[2 * v].m, t[2 * v + 1].m); if (a[tmid] && a[tmid + 1] && sign(a[tmid]) >= sign(a[tmid + 1])) { t[v].m = max(t[v].m, t[2 * v].r + t[2 * v + 1].l); if (t[2 * v].l == tmid - tl + 1) t[v].l += t[2 * v + 1].l; if (t[2 * v + 1].r == tr - tmid) t[v].r += t[2 * v].r; } } void build(int v, int tl, int tr) { if (tl == tr) { bool b = (a[tl] != 0); t[v] = {b, b, b}; return; } int tmid = (tl + tr) >> 1; build(2 * v, tl, tmid); build(2 * v + 1, tmid + 1, tr); recalc(v, tl, tr); } void upd(int v, int tl, int tr, int id, int x) { if (tl == tr) { a[id] += x; bool b = (a[id] != 0); t[v] = {b, b, b}; return; } int tmid = (tl + tr) >> 1; if (id <= tmid) upd(2 * v, tl, tmid, id, x); else upd(2 * v + 1, tmid + 1, tr, id, x); recalc(v, tl, tr); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); n--; for (int i = 1; i <= n; i++) a[i] = (a[i + 1] - a[i]); a[n + 1] = -1; if (n) build(1, 1, n); int m; scanf("%d", &m); while (m--) { if (!n) { printf("1\n"); continue; } int l, r, d; scanf("%d %d %d", &l, &r, &d); l--; if (l) upd(1, 1, n, l, d); if (r <= n) upd(1, 1, n, r, -d); printf("%d\n", max(t[1].m, max(t[1].l, t[1].r)) + 1); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, a[300010], l, r, addv; long long del[300010]; int lx[300010 << 2], rx[300010 << 2], ans[300010 << 2]; bool con(long long x, long long y) { if (!x || !y) return false; if (x > 0) return true; if (x < 0 && y < 0) return true; return false; } void push_up(int id, int l, int r) { int idL = id << 1, idR = id << 1 ^ 1; int mid = l + r >> 1; int mx = max(ans[idL], ans[idR]); lx[id] = lx[idL]; rx[id] = rx[idR]; if (con(del[mid], del[mid + 1])) { mx = max(mx, rx[idL] + lx[idR]); if (lx[idL] == mid - l + 1) lx[id] += lx[idR]; if (rx[idR] == r - mid) rx[id] += rx[idL]; } ans[id] = mx; } void add(int l, int r, int idx, int d, int id) { if (l >= r) { del[idx] += d; if (!del[idx]) rx[id] = lx[id] = ans[id] = 0; else rx[id] = lx[id] = ans[id] = 1; return; } int mid = l + r >> 1; if (mid >= idx) add(l, mid, idx, d, id << 1); else add(mid + 1, r, idx, d, id << 1 ^ 1); push_up(id, l, r); return; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) add(1, n - 1, i, a[i] - a[i - 1], 1); scanf("%d", &m); while (m--) { scanf("%d%d%d", &l, &r, &addv); if (l > 1) add(1, n - 1, l - 1, addv, 1); if (r < n) add(1, n - 1, r, -addv, 1); printf("%d\n", ans[1] + 1); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; char In[1 << 25], *ss = In, *tt = In; inline long long read() { register long long x = 0, f = 1; register char ch = (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 25, stdin), ss == tt) ? EOF : *ss++); while (ch < '0' || ch > '9') f = (ch == '-') ? -1 : f, ch = (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 25, stdin), ss == tt) ? EOF : *ss++); while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 25, stdin), ss == tt) ? EOF : *ss++); return x * f; } long long n, a[300005], m, x, y, d; struct node { long long len, l_val, r_val, l_end, r_end, l_down, r_up, ans, l, r, tag; } f[300005 << 2]; node operator+(node a, node b) { node c; c.l = a.l; c.r = b.r; c.tag = 0; c.len = a.len + b.len; c.l_val = a.l_val; c.r_val = b.r_val; c.l_down = a.l_down; c.r_up = b.r_up; c.ans = max(a.ans, b.ans); c.l_end = a.l_end; c.r_end = b.r_end; if (a.r_val > b.l_val) { if (a.l_down == a.len) c.l_down = max(c.l_down, b.l_down + a.l_down); if (a.l_end == a.len) c.l_end = max(c.l_end, a.l_end + b.l_down); if (b.l_down == b.len) c.r_end = max(c.r_end, b.l_down + a.r_end); c.ans = max(c.ans, a.r_end + b.l_down); } if (a.r_val < b.l_val) { if (b.r_up == b.len) c.r_up = max(c.r_up, b.r_up + a.r_up); if (b.r_end == b.len) c.r_end = max(c.r_end, a.r_up + b.r_end); if (a.r_up == a.len) c.l_end = max(c.r_end, b.l_end + a.r_up); c.ans = max(c.ans, a.r_up + b.l_end); } return c; } void build(long long x, long long l, long long r) { f[x].l = l; f[x].r = r; if (l == r) { f[x].l_val = f[x].r_val = a[l]; f[x].ans = f[x].l_end = f[x].r_end = f[x].l_down = f[x].r_up = f[x].len = 1; return; } long long mid = l + r >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); f[x] = f[x << 1] + f[x << 1 | 1]; } void push(long long x) { if (!f[x].tag) return; long long p = f[x].tag; f[x << 1].tag += p; f[x << 1 | 1].tag += p; f[x << 1].l_val += p; f[x << 1 | 1].r_val += p; f[x << 1].r_val += p; f[x << 1 | 1].l_val += p; f[x].tag = 0; } void ch(long long x, long long l, long long r, long long k) { if (l > f[x].r || r < f[x].l) return; if (l <= f[x].l && f[x].r <= r) { f[x].tag += k; f[x].l_val += k; f[x].r_val += k; return; } push(x); ch(x << 1, l, r, k); ch(x << 1 | 1, l, r, k); f[x] = f[x << 1] + f[x << 1 | 1]; } signed main() { n = read(); for (long long i = 1; i <= n; i++) a[i] = read(); build(1, 1, n); m = read(); while (m--) { x = read(); y = read(); d = read(); ch(1, x, y, d); printf("%lld\n", f[1].ans); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; void sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio; struct FAIL { FAIL() { cout << "CHANGE!!!" << "\n"; } }; inline int sign(long long x) { return x > 0 ? 1 : x == 0 ? 0 : -1; } void recalc(multiset<int> &best, set<int> &s, int pos, vector<long long> &a, int add) { pos += 5; if (sign(a[pos]) == sign(a[pos] + add)) { a[pos] += add; return; } auto it0 = s.lower_bound(pos); auto start = prev(it0, 4); auto end = next(it0, 4); for (auto it = start, it2 = next(it), it3 = next(it2); it3 != end; ++it, ++it2, ++it3) { if (a[*it] > 0 && a[*it2] < 0) { best.erase(best.find(*it3 - *it)); } } for (auto it = start, it2 = next(it); it2 != end; ++it, ++it2) { if (a[*it] != 0) { best.erase(best.find(*it2 - *it)); } } a[pos] += add; for (int i = (pos); i < (pos + 2); ++i) { if (i == 0 || sign(a[i]) * sign(a[i - 1]) != 1) { s.insert(i); } else { s.erase(i); } } for (auto it = start, it2 = next(it), it3 = next(it2); it3 != end; ++it, ++it2, ++it3) { if (a[*it] > 0 && a[*it2] < 0) { best.insert(*it3 - *it); } } for (auto it = start, it2 = next(it); it2 != end; ++it, ++it2) { if (a[*it] != 0) { best.insert(*it2 - *it); } } } int main() { int n; cin >> n; vector<long long> a(n); for (int __i = 0; __i < (n); ++__i) cin >> a[__i]; ; for (int i = (0); i < (n - 1); ++i) { a[i] = a[i + 1] - a[i]; } a.pop_back(); for (int i = (0); i < (5); ++i) { a.insert(a.begin(), 0); } for (int i = (0); i < (5); ++i) { a.push_back(0); } int m; cin >> m; set<int> start; for (int i = (0); i < (n - 1 + 2 * 5); ++i) { if (i == 0 || sign(a[i]) * sign(a[i - 1]) != 1) { start.insert(i); } } multiset<int> best; for (auto it = start.begin(), it2 = next(it), it3 = next(it2); it3 != start.end(); ++it, ++it2, ++it3) { if (a[*it] > 0 && a[*it2] < 0) { best.insert(*it3 - *it); } } for (auto it = start.begin(), it2 = next(it); it2 != start.end(); ++it, ++it2) { if (a[*it] != 0) { best.insert(*it2 - *it); } } vector<int> res(m); for (int j = (0); j < (m); ++j) { int x, y, d; cin >> x >> y >> d; x -= 2; --y; if (0 <= x && x < n - 1) { recalc(best, start, x, a, d); } if (0 <= y && y < n - 1) { recalc(best, start, y, a, -d); } res[j] = best.size() ? *best.rbegin() + 1 : 1; } for (auto elem : res) { cout << elem << "\n"; } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; inline long long getnum() { char c = getchar(); long long num, sign = 1; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') sign = -1; for (num = 0; c >= '0' && c <= '9';) { c -= '0'; num = num * 10 + c; c = getchar(); } return num * sign; } struct node { long long l, r; int a, li, ri, ld, rd, lid, rid; } Tree[1200054]; long long Lazy[1200054]; int A[300004]; void create(int l, int r, int ind) { node &P = Tree[ind]; if (l == r) { P.r = A[l]; P.l = A[l]; P.a = 1; P.li = 1; P.ri = 1; P.ld = 1; P.rd = 1; P.lid = 1; P.rid = 1; return; } int x = (l + r) / 2; create(l, x, 2 * ind); create(x + 1, r, 2 * ind + 1); node &L = Tree[2 * ind]; node &R = Tree[2 * ind + 1]; P.l = L.l; P.r = R.r; P.li = L.li; if (L.li == x - l + 1 && L.r < R.l) P.li = x - l + 1 + R.li; P.ld = L.ld; if (L.ld == x - l + 1 && L.r > R.l) P.ld = x - l + 1 + R.ld; P.ri = R.ri; if (R.ri == r - x && L.r > R.l) P.ri = r - x + L.ri; P.rd = R.rd; if (R.rd == r - x && L.r < R.l) P.rd = r - x + L.rd; P.lid = L.lid; if (L.lid == x - l + 1 && L.r > R.l) P.lid = x - l + 1 + R.ld; if (L.li == x - l + 1 && L.r < R.l) P.lid = x - l + 1 + R.lid; P.rid = R.rid; if (R.rid == r - x && L.r < R.l) P.rid = r - x + L.rd; if (R.ri == r - x && L.r > R.l) P.rid = r - x + L.rid; P.a = max(L.a, R.a); if (L.r > R.l) P.a = max(P.a, L.rid + R.ld); if (L.r < R.l) P.a = max(P.a, L.rd + R.lid); } void update(int l, int r, int ind, int lx, int rx, long long val) { node &P = Tree[ind]; if (l == lx && r == rx) { P.r += val; P.l += val; Lazy[ind] += val; return; } if (Lazy[ind]) { Tree[2 * ind].r += Lazy[ind]; Tree[2 * ind].l += Lazy[ind]; Lazy[2 * ind] += Lazy[ind]; Tree[2 * ind + 1].r += Lazy[ind]; Tree[2 * ind + 1].l += Lazy[ind]; Lazy[2 * ind + 1] += Lazy[ind]; Lazy[ind] = 0; } int x = (l + r) / 2; if (rx <= x) update(l, x, 2 * ind, lx, rx, val); else if (lx > x) update(x + 1, r, 2 * ind + 1, lx, rx, val); else update(l, x, 2 * ind, lx, x, val), update(x + 1, r, 2 * ind + 1, x + 1, rx, val); node &L = Tree[2 * ind]; node &R = Tree[2 * ind + 1]; P.l = L.l; P.r = R.r; P.li = L.li; if (L.li == x - l + 1 && L.r < R.l) P.li = x - l + 1 + R.li; P.ld = L.ld; if (L.ld == x - l + 1 && L.r > R.l) P.ld = x - l + 1 + R.ld; P.ri = R.ri; if (R.ri == r - x && L.r > R.l) P.ri = r - x + L.ri; P.rd = R.rd; if (R.rd == r - x && L.r < R.l) P.rd = r - x + L.rd; P.lid = L.lid; if (L.lid == x - l + 1 && L.r > R.l) P.lid = x - l + 1 + R.ld; if (L.li == x - l + 1 && L.r < R.l) P.lid = x - l + 1 + R.lid; P.rid = R.rid; if (R.rid == r - x && L.r < R.l) P.rid = r - x + L.rd; if (R.ri == r - x && L.r > R.l) P.rid = r - x + L.rid; P.a = max(L.a, R.a); if (L.r > R.l) P.a = max(P.a, L.rid + R.ld); if (L.r < R.l) P.a = max(P.a, L.rd + R.lid); } int main() { int n = getnum(); for (int i = 1; i <= n; i++) A[i] = getnum(); create(1, n, 1); int m = getnum(); for (; m--;) { int l = getnum(), r = getnum(), v = getnum(); update(1, n, 1, l, r, v); printf("%d\n", Tree[1].a); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct node { int l, m, r; }; node t[3000005]; int arr[300005], n, m; long long a[300005]; int sign(long long x) { if (x > 0) return 1; if (x < 0) return -1; return 0; } void cal(int x, int l, int r) { int m = (l + r) / 2; t[x].m = max(t[x * 2].m, t[x * 2 + 1].m); t[x].l = t[x * 2].l; t[x].r = t[x * 2 + 1].r; if (!!a[m] && !!a[m + 1] && sign(a[m]) >= sign(a[m + 1])) { t[x].m = max(t[x].m, t[x * 2].r + t[x * 2 + 1].l); if (t[2 * x].m == m - l + 1) t[x].l = t[2 * x].l + t[2 * x + 1].l; if (t[2 * x + 1].m == r - m) t[x].r = t[2 * x].r + t[2 * x + 1].r; } } void build(int x, int l, int r) { if (l == r) { int tmp = !!a[l]; t[x] = {tmp, tmp, tmp}; return; } int m = (l + r) / 2; build(x * 2, l, m); build(x * 2 + 1, m + 1, r); cal(x, l, r); } void update(int x, int l, int r, int pos, int d) { if (l == r) { a[pos] += d; int tmp = !!a[pos]; t[x] = {tmp, tmp, tmp}; return; } int m = (l + r) / 2; if (pos <= m) update(x * 2, l, m, pos, d); else update(x * 2 + 1, m + 1, r, pos, d); cal(x, l, r); } int main() { cin >> n; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); for (int i = 0; i + 1 < n; i++) a[i] = arr[i + 1] - arr[i]; if (n > 1) build(1, 0, n - 2); cin >> m; while (m--) { int l, r, d; scanf("%d%d%d", &l, &r, &d); if (n == 1) { printf("%d\n", 1); continue; } if (l > 1) update(1, 0, n - 2, l - 2, d); if (r < n) update(1, 0, n - 2, r - 1, -d); printf("%d\n", t[1].m + 1); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct node { int lb, rb, l_max, r_max, s_max; }; int n, m, l, r, k, offset = 1 << 19, a[300004]; long long d[600004]; node trn[1100000]; int sgn(long long x) { if (x > 0) return 1; if (x < 0) return -1; return 0; } void update(int pos) { if (pos < 0) return; if (d[pos + 1] == 0) { trn[pos + offset].l_max = 0; trn[pos + offset].r_max = 0; trn[pos + offset].s_max = 0; } else { trn[pos + offset].l_max = 1; trn[pos + offset].r_max = 1; trn[pos + offset].s_max = 1; } pos = (pos + offset) >> 1; while (pos > 0) { int z = 2 * pos; trn[pos].s_max = max(trn[z].s_max, trn[z + 1].s_max); trn[pos].l_max = trn[z].l_max; trn[pos].r_max = trn[z + 1].r_max; if (sgn(d[trn[z].rb]) >= sgn(d[trn[z + 1].lb])) { int len = trn[z].r_max + trn[z + 1].l_max; trn[pos].s_max = max(len, trn[pos].s_max); if (trn[z].l_max == trn[z].rb - trn[z].lb + 1) trn[pos].l_max += trn[z + 1].l_max; if (trn[z + 1].r_max == trn[z + 1].rb - trn[z + 1].lb + 1) trn[pos].r_max += trn[z].r_max; } pos = pos >> 1; } } int readInt() { bool mnus = false; int result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getchar(); } if (ch == '-') mnus = true; else result = ch - '0'; while (true) { ch = getchar(); if (ch < '0' || ch > '9') break; result = result * 10 + (ch - '0'); } if (mnus) return -result; else return result; } int main() { std::ios::sync_with_stdio(false); n = readInt(); for (int i = 1; i <= n; ++i) a[i] = readInt(); for (int i = 1; i < n; ++i) d[i] = a[i + 1] - a[i]; for (int i = 0; i < offset; ++i) { trn[i + offset].lb = i + 1; trn[i + offset].rb = i + 1; if (d[i + 1] == 0) { trn[i + offset].l_max = 0; trn[i + offset].r_max = 0; trn[i + offset].s_max = 0; } else { trn[i + offset].l_max = 1; trn[i + offset].r_max = 1; trn[i + offset].s_max = 1; } } for (int i = offset - 1; i >= 1; --i) { int t = 2 * i; trn[i].lb = trn[t].lb; trn[i].rb = trn[t + 1].rb; trn[i].s_max = max(trn[t].s_max, trn[t + 1].s_max); trn[i].l_max = trn[t].l_max; trn[i].r_max = trn[t + 1].r_max; if (sgn(d[trn[t].rb]) >= sgn(d[trn[t + 1].lb])) { int len = trn[t].r_max + trn[t + 1].l_max; trn[i].s_max = max(len, trn[i].s_max); if (trn[t].l_max == trn[t].rb - trn[t].lb + 1) trn[i].l_max += trn[t + 1].l_max; if (trn[t + 1].r_max == trn[t + 1].rb - trn[t + 1].lb + 1) trn[i].r_max += trn[t].r_max; } } m = readInt(); for (int i = 0; i < m; ++i) { l = readInt(); r = readInt(); k = readInt(); d[l - 1] += k; d[r] -= k; d[0] = 0; d[n] = 0; update(l - 2); update(r - 1); cout << trn[1].s_max + 1 << endl; } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int N = 300010; const int INF = 0x3f3f3f3f; void MOD(long long &a) { if (a >= mod) a -= mod; } void MOD(long long &a, long long c) { if (a >= c) a -= c; } void ADD(long long &a, long long b) { a += b; MOD(a); } void ADD(long long &a, long long b, long long c) { a += b; MOD(a, c); } long long qpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b /= 2; } return ans; } long long qpow(long long a, long long b, long long c) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % c; a = a * a % c; b /= 2; } return ans; } int n, m, a[N]; long long b[N]; int lefneg[N << 2], rigpos[N << 2], lefmax[N << 2], rigmax[N << 2], ans[N << 2]; void update(int l, int r, int rt) { int m = l + r >> 1; lefneg[rt] = lefneg[rt << 1] == (m - l + 1) ? lefneg[rt << 1] + lefneg[rt << 1 | 1] : lefneg[rt << 1]; rigpos[rt] = rigpos[rt << 1 | 1] == (r - m) ? rigpos[rt << 1 | 1] + rigpos[rt << 1] : rigpos[rt << 1 | 1]; ans[rt] = max(max(ans[rt << 1], ans[rt << 1 | 1]), max(lefmax[rt << 1 | 1] + rigpos[rt << 1], rigmax[rt << 1] + lefneg[rt << 1 | 1])); if (lefmax[rt << 1] == (m - l + 1) && rigpos[rt << 1] == (m - l + 1)) lefmax[rt] = lefmax[rt << 1] + max(lefmax[rt << 1 | 1], lefneg[rt << 1 | 1]); else if (lefmax[rt << 1] == (m - l + 1)) lefmax[rt] = lefmax[rt << 1] + lefneg[rt << 1 | 1]; else lefmax[rt] = lefmax[rt << 1]; if (rigmax[rt << 1 | 1] == (r - m) && lefneg[rt << 1 | 1] == (r - m)) rigmax[rt] = rigmax[rt << 1 | 1] + max(rigmax[rt << 1], rigpos[rt << 1]); else if (rigmax[rt << 1 | 1] == (r - m)) rigmax[rt] = rigmax[rt << 1 | 1] + rigpos[rt << 1]; else rigmax[rt] = rigmax[rt << 1 | 1]; ans[rt] = max(max(lefmax[rt], rigmax[rt]), ans[rt]); } void build(int l, int r, int rt) { if (l == r) { if (b[l] > 0) rigpos[rt] = lefmax[rt] = rigmax[rt] = ans[rt] = 1; else if (b[l] < 0) lefneg[rt] = lefmax[rt] = rigmax[rt] = ans[rt] = 1; return; } int m = l + r >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); update(l, r, rt); } void change(int pos, int val, int l, int r, int rt) { if (l == r && pos == l) { b[l] += val; lefneg[rt] = rigpos[rt] = lefmax[rt] = rigmax[rt] = ans[rt] = 0; if (b[l] > 0) rigpos[rt] = lefmax[rt] = rigmax[rt] = ans[rt] = 1; else if (b[l] < 0) lefneg[rt] = lefmax[rt] = rigmax[rt] = ans[rt] = 1; return; } int m = l + r >> 1; if (pos <= m) change(pos, val, l, m, rt << 1); else change(pos, val, m + 1, r, rt << 1 | 1); update(l, r, rt); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); n--; for (int i = 1; i <= n; i++) b[i] = a[i + 1] - a[i]; scanf("%d", &m); if (n == 0) { while (m--) printf("1\n"); return 0; } build(1, n, 1); while (m--) { int l, r, val; scanf("%d%d%d", &l, &r, &val); if (l - 1 > 0) change(l - 1, val, 1, n, 1); if (r <= n) change(r, -val, 1, n, 1); printf("%d\n", ans[1] + 1); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct treeLen { int n; vector<int> abest; vector<int> al; vector<int> ar; treeLen() { n = 0; } treeLen(vector<int> vec) { n = 1; while (n < vec.size()) { n *= 2; } abest.resize(2 * n); al.resize(2 * n); ar.resize(2 * n); for (int i = n; i < 2 * n; i++) { if (i < vec.size() + n) { abest[i] = vec[i - n] % 2; al[i] = vec[i - n] % 2; ar[i] = vec[i - n] % 2; } else { abest[i] = 0; al[i] = 0; ar[i] = 0; } } int L = 1; int R = 2; for (int i = n - 1; i > 0; i--) { refresh(i, L, R); if (n / R == i) { R *= 2; } } } void refresh(int v, int L, int R) { int siz = R - L + 1; if (siz == 1) { return; } abest[v] = max(ar[2 * v] + al[2 * v + 1], max(abest[2 * v], abest[2 * v + 1])); if (al[2 * v] == siz / 2) { al[v] = abest[v]; } else { al[v] = al[2 * v]; } if (ar[2 * v + 1] == siz / 2) { ar[v] = abest[v]; } else { ar[v] = ar[2 * v + 1]; } } void set(int x, int val) { set(1, 0, n - 1, x, val); } void set(int v, int L, int R, int x, int val) { if (L == R) { abest[v] = val; al[v] = val; ar[v] = val; } else { int mid = (L + R) / 2; if (x <= mid) { set(2 * v, L, mid, x, val); } else { set(2 * v + 1, mid + 1, R, x, val); } refresh(v, L, R); } } int get() { return abest[1]; } void print() { int step2 = 2; for (int i = 1; i < 2 * n; i++) { if (i == step2) { step2 *= 2; cout << "\n"; } cout << "(" << abest[i] << ", " << al[i] << ", " << ar[i] << ") "; } } }; treeLen tl; vector<int> a; vector<long long> da; vector<int> b; int n; void refresh(int i) { if (i == 0 || i == n - 1) { return; } if (da[i - 1] != 0 && da[i] != 0 && !(da[i - 1] < 0 && da[i] > 0)) { tl.set(i, 1); } else { tl.set(i, 0); } } int main() { scanf("%d", &n); a.resize(n); da.resize(n - 1); b.resize(n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int k0 = 0; for (int i = 0; i < n - 1; i++) { da[i] = a[i + 1] - a[i]; if (da[i] == 0) { k0++; } } for (int i = 1; i < n - 1; i++) { if (da[i - 1] != 0 && da[i] != 0 && !(da[i - 1] < 0 && da[i] > 0)) { b[i] = 1; } else { b[i] = 0; } } b[0] = 0; b[n - 1] = 0; tl = treeLen(b); int m; scanf("%d", &m); for (int trash = 0; trash < m; trash++) { int l, r, val; scanf("%d %d %d", &l, &r, &val); l--; r--; if (l != 0) { if (da[l - 1] == 0) { k0--; } else if (da[l - 1] + val == 0) { k0++; } da[l - 1] += val; refresh(l - 1); refresh(l); } if (r != n - 1) { if (da[r] == 0) { k0--; } else if (da[r] - val == 0) { k0++; } da[r] -= val; refresh(r); refresh(r + 1); } int out = tl.get(); if (k0 == n - 1) { out++; } else { out += 2; } cout << out << "\n"; } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
import java.io.*; import java.util.*; public class CF740E { static Random rand = new Random(); static class Skip<K,V> { Comparator<K> comp; static class Node<K,V> { K k; V v; Node<K,V>[] next; Node(int r, K k, V v) { next = new Node[r]; // unchecked this.k = k; this.v = v; } } Node<K,V> header; Node<K,V>[] update; int l, m; Skip(int n, Comparator<K> comp) { m = 1; while (1 << m + m < n) // n <= power(1/p, m) for p = 1/4 m++; header = new Node<>(m, null, null); update = new Node[m]; // unchecked l = 1; this.comp = comp; } private Node<K,V> search(K k) { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) while (x.next[i] != null && comp.compare(x.next[i].k, k) < 0) x = x.next[i]; return x; } private Node<K,V> search_(K k) { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) { while (x.next[i] != null && comp.compare(x.next[i].k, k) < 0) x = x.next[i]; update[i] = x; } return x; } K first() { Node<K,V> x = header; x = x.next[0]; return x == null ? null : x.k; } K last() { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) while (x.next[i] != null) x = x.next[i]; x = x.next[0]; return x == null ? null : x.k; } K lower(K k) { Node<K,V> x = search(k); return x.k; } K floor(K k) { Node<K,V> x = search(k), y = x.next[0]; if (y != null && comp.compare(y.k, k) == 0) return y.k; return x.k; } K higher(K k) { Node<K,V> x = search(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) x = x.next[0]; return x == null ? null : x.k; } V getOrDefault(K k, V v) { Node<K,V> x = search(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) return x.v; return v; } void put(K k, V v) { Node<K,V> x = search_(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) { x.v = v; return; } int r = 1; while (r < m && rand.nextInt(4) == 0) // p = 1/4 r++; if (r > l) { for (int i = l; i < r; i++) update[i] = header; l = r; } x = new Node<>(r, k, v); for (int i = 0; i < r; i++) { x.next[i] = update[i].next[i]; update[i].next[i] = x; } } V remove(K k) { Node<K,V> x = search_(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) { for (int i = 0; i < l; i++) { if (update[i].next[i] != x) break; update[i].next[i] = x.next[i]; } while (l > 1 && header.next[l - 1] == null) l--; V v = x.v; // free(x); return v; } return null; } }; static long[] aa; static class H { int i, j, k; // [i, j) up [j, k) down H(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } } //static TreeSet<H> xx = new TreeSet<>((p, q) -> p.i - q.i); static Skip<H,H> xx; static TreeMap<Integer, Integer> ww = new TreeMap<>(); //static Skip<Integer,Integer> ww = new Skip<>((p, q) -> p - q); static void add(int i, int j, int k) { if (i <= j && j <= k && i < k) { H h = new H(i, j, k); //xx.add(h); xx.put(h, h); int key = k - i; int cnt = ww.getOrDefault(key, 0); ww.put(key, cnt + 1); } } static void remove(H h) { xx.remove(h); int key = h.k - h.i; int cnt = ww.getOrDefault(key, 0); if (cnt == 1) ww.remove(key); else ww.put(key, cnt - 1); } static void init() { int n = aa.length; for (int i = 0, j, k; i < n; ) { j = i; while (j < n && aa[j] > 0) j++; k = j; while (k < n && aa[k] < 0) k++; if (i < k) { add(i, j, k); i = k; } else i++; } } static void join(int i) { H h = xx.floor(new H(i, i, i)); H l = xx.lower(h); H r = xx.higher(h); boolean lh = l != null && l.k == h.i && (l.j == l.k || h.i == h.j); boolean hr = r != null && h.k == r.i && (h.j == h.k || r.i == r.j); if (lh && hr) { remove(l); remove(h); remove(r); add(l.i, h.i == h.j ? l.j : r.j, r.k); } else if (lh) { remove(l); remove(h); add(l.i, h.i == h.j ? l.j : h.j, h.k); } else if (hr) { remove(h); remove(r); add(h.i, r.i == r.j ? h.j : r.j, r.k); } } static void update(int i, int d) { long a = aa[i]; long b = aa[i] += d; // d != 0 if (a == 0) { add(i, b > 0 ? i + 1 : i, i + 1); join(i); } else if (b == 0) { H h = xx.floor(new H(i, i, i)); remove(h); if (a > 0) { add(h.i, i, i); add(i + 1, h.j, h.k); } else { add(h.i, h.j, i); add(i + 1, i + 1, h.k); } } else if (a > 0 && b < 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, i, i + 1); add(i + 1, h.j, h.k); join(i); } else if (a < 0 && b > 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, h.j, i); add(i, i + 1, h.k); join(i); } } static int query() { return ww.isEmpty() ? 1 : ww.lastKey() + 1; //Integer l = ww.last(); //return l == null ? 1 : l + 1; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); aa = new long[n - 1]; int a_ = Integer.parseInt(st.nextToken()); for (int i = 0; i < n - 1; i++) { int a = Integer.parseInt(st.nextToken()); aa[i] = a - a_; a_ = a; } xx = new Skip<>(n, (p, q) -> p.i - q.i); init(); int m = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(System.out); while (m-- > 0) { st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()) - 1; int r = Integer.parseInt(st.nextToken()) - 1; int d = Integer.parseInt(st.nextToken()); if (l > 0) update(l - 1, d); if (r < n - 1) update(r, -d); pw.println(query()); } pw.close(); } }
JAVA
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; class Range { public: int l_inc, l_dec, r_inc, r_dec; long long l, r; int len_hill; int l_hill, r_hill; int len; Range(){}; Range(int x) : l_inc(1), l_dec(1), r_inc(1), r_dec(1), l(x), r(x), len_hill(1), l_hill(1), r_hill(1), len(1) {} }; Range tree[1300000]; long long lazy[1300000]; Range mrg(Range& a, Range& b) { if (a.len == -1) { return b; } if (b.len == -1) { return a; } Range temp; temp.l = a.l; temp.r = b.r; temp.l_inc = a.l_inc; if (a.r < b.l && a.len == a.l_inc) temp.l_inc = a.len + b.l_inc; temp.l_dec = a.l_dec; if (a.r > b.l && a.len == a.l_dec) temp.l_dec = a.len + b.l_dec; temp.r_inc = b.r_inc; if (a.r < b.l && b.len == b.r_inc) temp.r_inc = b.len + a.r_inc; temp.r_dec = b.r_dec; if (a.r > b.l && b.len == b.r_dec) temp.r_dec = b.len + a.r_dec; temp.len = a.len + b.len; temp.l_hill = max(a.l_hill, max(temp.l_dec, temp.l_inc)); if (a.len == a.l_inc && a.r < b.l) temp.l_hill = max(temp.l_hill, a.len + b.l_hill); if (a.len == a.l_hill && a.r > b.l) temp.l_hill = max(temp.l_hill, a.len + b.l_dec); if (a.len == a.l_inc && a.r > b.l) temp.l_hill = max(temp.l_hill, a.r_inc + b.l_dec); temp.r_hill = max(b.r_hill, max(temp.r_dec, temp.r_inc)); if (b.len == b.r_dec && a.r > b.l) temp.r_hill = max(temp.r_hill, b.len + a.r_hill); if (b.len == b.r_hill && a.r < b.l) temp.r_hill = max(temp.r_hill, b.len + a.r_inc); if (b.len == b.r_dec && a.r < b.l) temp.r_hill = max(temp.r_hill, a.r_inc + b.l_dec); temp.len_hill = max(a.len_hill, b.len_hill); if (a.r != b.l) temp.len_hill = max(temp.len_hill, a.r_inc + b.l_dec); if (a.r > b.l) temp.len_hill = max(temp.len_hill, a.r_hill + b.l_dec); if (a.r < b.l) temp.len_hill = max(temp.len_hill, a.r_inc + b.l_hill); temp.len_hill = max(temp.len_hill, temp.l_hill); temp.len_hill = max(temp.len_hill, temp.r_hill); temp.len_hill = max(temp.len_hill, max(max(temp.r_dec, temp.l_dec), max(temp.r_inc, temp.l_inc))); return temp; } Range query(int i, int l, int r, int a, int b) { if (lazy[i]) { tree[i].l += lazy[i]; tree[i].r += lazy[i]; if (l != r) { lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } lazy[i] = 0; } if (l >= a && r <= b) { return tree[i]; } if (l > b || r < a) { Range temp(1); temp.len = -1; return temp; } int mid = (l + r) / 2; Range left = query(i * 2 + 1, l, mid, a, b); Range right = query(i * 2 + 2, mid + 1, r, a, b); return mrg(left, right); } void update(int i, int l, int r, int a, int b, int val) { if (lazy[i]) { tree[i].l += lazy[i]; tree[i].r += lazy[i]; if (l != r) { lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } lazy[i] = 0; } if (l >= a && r <= b) { tree[i].l += val; tree[i].r += val; if (l != r) { lazy[i * 2 + 1] += val; lazy[i * 2 + 2] += val; } return; } if (l > b || r < a) { return; } int mid = (l + r) / 2; update(i * 2 + 1, l, mid, a, b, val); update(i * 2 + 2, mid + 1, r, a, b, val); tree[i] = mrg(tree[i * 2 + 1], tree[i * 2 + 2]); } void build(int i, int l, int r, int* arr) { if (l == r) { tree[i] = Range(arr[l]); return; } int mid = (l + r) / 2; build(i * 2 + 1, l, mid, arr); build(i * 2 + 2, mid + 1, r, arr); tree[i] = mrg(tree[i * 2 + 1], tree[i * 2 + 2]); } int main() { int n; scanf("%d", &n); int* arr = new int[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } build(0, 0, n - 1, arr); int m; scanf("%d", &m); for (int i = 0; i < m; i++) { int l, r, d; scanf("%d%d%d", &l, &r, &d); l--; r--; update(0, 0, n - 1, l, r, d); Range temp = query(0, 0, n - 1, 0, n - 1); int ans = max(temp.len_hill, temp.l_hill); ans = max(ans, temp.r_hill); ans = max(ans, temp.l_dec); ans = max(ans, temp.r_dec); ans = max(ans, temp.l_inc); ans = max(ans, temp.r_inc); printf("%d\n", ans); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
import java.io.*; import java.util.*; public class CF740E { static Random rand = new Random(); static class Skip<K,E> { static class Entry<K,E> { K k; E e; Entry(K k, E e) { this.k = k; this.e = e; } } static class Item<K,E> { Item<K,E> north, south, west, east; Entry<K,E> ent; }; Item<K,E> root = new Item<>(); Comparator<K> comp; Skip(Comparator<K> comp) { this.comp = comp; } private Item<K,E> search(K k) { Item<K,E> x = root; while (x.south != null) { x = x.south; while (x.east != null && comp.compare(x.east.ent.k, k) <= 0) x = x.east; } return x; } K first() { Item<K,E> x = root; while (x.south != null) x = x.south; x = x.east; return x == null || x.ent == null ? null : x.ent.k; } K floor(K k) { Item<K,E> x = search(k); return x.ent == null ? null : x.ent.k; } K lower(K k) { Item<K,E> x = search(k); if (x.ent == null || comp.compare(x.ent.k, k) < 0) return x.ent == null ? null : x.ent.k; x = x.west; return x == null || x.ent == null ? null : x.ent.k; } K higher(K k) { Item<K,E> x = search(k); x = x.east; return x == null || x.ent == null ? null : x.ent.k; } E get(K k) { Item<K,E> x = search(k); Entry<K,E> ent = x.ent; return ent == null || comp.compare(ent.k, k) != 0 ? null : ent.e; } private void raise(Item<K,E> x) { if (x == root) { root = new Item<>(); root.south = x; x.north = root; } } private Item<K,E> insertAfterAbove(Item<K,E> west, Item<K,E> south, Entry<K,E> ent) { Item<K,E> x = new Item<>(); if (west != null) { if (west.east != null) { x.east = west.east; west.east.west = x; } x.west = west; west.east = x; } if (south != null) { if (south.north != null) { x.north = south.north; south.north.south = x; } x.south = south; south.north = x; } x.ent = ent; return x; } void put(K k, E e) { Item<K,E> x = search(k); Entry<K,E> ent = x.ent; if (ent != null && comp.compare(ent.k, k) == 0) { ent.e = e; return; } ent = new Entry<>(k, e); raise(x); Item<K,E> y = insertAfterAbove(x, null, ent); while (rand.nextInt(2) == 0) { while (x.north == null) x = x.west; x = x.north; raise(x); y = insertAfterAbove(x, y, ent); } } E remove(K k) { Item<K,E> x = search(k), y; Entry<K,E> ent = x.ent; if (ent == null || comp.compare(ent.k, k) != 0) return null; while (x != null) { y = x.north; if (x.west != null) x.west.east = x.east; if (x.east != null) x.east.west = x.west; //free(x); x = y; } E e = ent.e; //free(ent); return e; } }; static long[] aa; static class H { int i, j, k; // [i, j) up [j, k) down H(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } } //static TreeSet<H> xx = new TreeSet<>((p, q) -> p.i - q.i); static Skip<H,H> xx = new Skip<>((p, q) -> p.i - q.i); static TreeMap<Integer, Integer> ww = new TreeMap<>(); static void add(int i, int j, int k) { if (i <= j && j <= k && i < k) { H h = new H(i, j, k); //xx.add(h); xx.put(h, h); int key = k - i; int cnt = ww.getOrDefault(key, 0); ww.put(key, cnt + 1); } } static void remove(H h) { xx.remove(h); int key = h.k - h.i; int cnt = ww.getOrDefault(key, 0); if (cnt == 1) ww.remove(key); else ww.put(key, cnt - 1); } static void init() { int n = aa.length; for (int i = 0, j, k; i < n; ) { j = i; while (j < n && aa[j] > 0) j++; k = j; while (k < n && aa[k] < 0) k++; if (i < k) { add(i, j, k); i = k; } else i++; } } static void join(int i) { H h = xx.floor(new H(i, i, i)); H l = xx.lower(h); H r = xx.higher(h); boolean lh = l != null && l.k == h.i && (l.j == l.k || h.i == h.j); boolean hr = r != null && h.k == r.i && (h.j == h.k || r.i == r.j); if (lh && hr) { remove(l); remove(h); remove(r); add(l.i, h.i == h.j ? l.j : r.j, r.k); } else if (lh) { remove(l); remove(h); add(l.i, h.i == h.j ? l.j : h.j, h.k); } else if (hr) { remove(h); remove(r); add(h.i, r.i == r.j ? h.j : r.j, r.k); } } static void update(int i, int d) { long a = aa[i]; long b = aa[i] += d; // d != 0 if (a == 0) { add(i, b > 0 ? i + 1 : i, i + 1); join(i); } else if (b == 0) { H h = xx.floor(new H(i, i, i)); remove(h); if (a > 0) { add(h.i, i, i); add(i + 1, h.j, h.k); } else { add(h.i, h.j, i); add(i + 1, i + 1, h.k); } } else if (a > 0 && b < 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, i, i + 1); add(i + 1, h.j, h.k); join(i); } else if (a < 0 && b > 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, h.j, i); add(i, i + 1, h.k); join(i); } } static int query() { return ww.isEmpty() ? 1 : ww.lastKey() + 1; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); aa = new long[n - 1]; int a_ = Integer.parseInt(st.nextToken()); for (int i = 0; i < n - 1; i++) { int a = Integer.parseInt(st.nextToken()); aa[i] = a - a_; a_ = a; } init(); int m = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(System.out); while (m-- > 0) { st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()) - 1; int r = Integer.parseInt(st.nextToken()) - 1; int d = Integer.parseInt(st.nextToken()); if (l > 0) update(l - 1, d); if (r < n - 1) update(r, -d); pw.println(query()); } pw.close(); } }
JAVA
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
import java.io.*; import java.util.*; public class CF740E { static Random rand = new Random(); static class Skip<K,V> { Comparator<K> comp; static class Node<K,V> { K k; V v; Node<K,V>[] next; Node(int r, K k, V v) { next = new Node[r]; // unchecked this.k = k; this.v = v; } } Node<K,V> header; Node<K,V>[] update; int l, m; Skip(int n, Comparator<K> comp) { m = 1; while (1 << m + m < n) // n <= power(1/p, m) for p = 1/4 m++; header = new Node<>(m, null, null); update = new Node[m]; // unchecked l = 1; this.comp = comp; } private Node<K,V> search(K k) { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) while (x.next[i] != null && comp.compare(x.next[i].k, k) < 0) x = x.next[i]; return x; } private Node<K,V> search_(K k) { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) { while (x.next[i] != null && comp.compare(x.next[i].k, k) < 0) x = x.next[i]; update[i] = x; } return x; } K first() { Node<K,V> x = header; x = x.next[0]; return x == null ? null : x.k; } K last() { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) while (x.next[i] != null) x = x.next[i]; return x.k; } K lower(K k) { Node<K,V> x = search(k); return x.k; } K floor(K k) { Node<K,V> x = search(k), y = x.next[0]; if (y != null && comp.compare(y.k, k) == 0) return y.k; return x.k; } K higher(K k) { Node<K,V> x = search(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) x = x.next[0]; return x == null ? null : x.k; } V getOrDefault(K k, V v) { Node<K,V> x = search(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) return x.v; return v; } void put(K k, V v) { Node<K,V> x = search_(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) { x.v = v; return; } int r = 1; while (r < m && rand.nextInt(4) == 0) // p = 1/4 r++; if (r > l) { for (int i = l; i < r; i++) update[i] = header; l = r; } x = new Node<>(r, k, v); for (int i = 0; i < r; i++) { x.next[i] = update[i].next[i]; update[i].next[i] = x; } } V remove(K k) { Node<K,V> x = search_(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) { for (int i = 0; i < l; i++) { if (update[i].next[i] != x) break; update[i].next[i] = x.next[i]; } while (l > 1 && header.next[l - 1] == null) l--; V v = x.v; // free(x); return v; } return null; } }; static long[] aa; static class H { int i, j, k; // [i, j) up [j, k) down H(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } } //static TreeSet<H> xx = new TreeSet<>((p, q) -> p.i - q.i); static Skip<H,H> xx; //static TreeMap<Integer, Integer> ww = new TreeMap<>(); static Skip<Integer,Integer> ww; static void add(int i, int j, int k) { if (i <= j && j <= k && i < k) { H h = new H(i, j, k); //xx.add(h); xx.put(h, h); int key = k - i; int cnt = ww.getOrDefault(key, 0); ww.put(key, cnt + 1); } } static void remove(H h) { xx.remove(h); int key = h.k - h.i; int cnt = ww.getOrDefault(key, 0); if (cnt == 1) ww.remove(key); else ww.put(key, cnt - 1); } static void init() { int n = aa.length; for (int i = 0, j, k; i < n; ) { j = i; while (j < n && aa[j] > 0) j++; k = j; while (k < n && aa[k] < 0) k++; if (i < k) { add(i, j, k); i = k; } else i++; } } static void join(int i) { H h = xx.floor(new H(i, i, i)); H l = xx.lower(h); H r = xx.higher(h); boolean lh = l != null && l.k == h.i && (l.j == l.k || h.i == h.j); boolean hr = r != null && h.k == r.i && (h.j == h.k || r.i == r.j); if (lh && hr) { remove(l); remove(h); remove(r); add(l.i, h.i == h.j ? l.j : r.j, r.k); } else if (lh) { remove(l); remove(h); add(l.i, h.i == h.j ? l.j : h.j, h.k); } else if (hr) { remove(h); remove(r); add(h.i, r.i == r.j ? h.j : r.j, r.k); } } static void update(int i, int d) { long a = aa[i]; long b = aa[i] += d; // d != 0 if (a == 0) { add(i, b > 0 ? i + 1 : i, i + 1); join(i); } else if (b == 0) { H h = xx.floor(new H(i, i, i)); remove(h); if (a > 0) { add(h.i, i, i); add(i + 1, h.j, h.k); } else { add(h.i, h.j, i); add(i + 1, i + 1, h.k); } } else if (a > 0 && b < 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, i, i + 1); add(i + 1, h.j, h.k); join(i); } else if (a < 0 && b > 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, h.j, i); add(i, i + 1, h.k); join(i); } } static int query() { //return ww.isEmpty() ? 1 : ww.lastKey() + 1; Integer l = ww.last(); return l == null ? 1 : l + 1; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); aa = new long[n - 1]; int a_ = Integer.parseInt(st.nextToken()); for (int i = 0; i < n - 1; i++) { int a = Integer.parseInt(st.nextToken()); aa[i] = a - a_; a_ = a; } xx = new Skip<>(n, (p, q) -> p.i - q.i); ww = new Skip<>(n, (p, q) -> p - q); init(); int m = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(System.out); while (m-- > 0) { st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()) - 1; int r = Integer.parseInt(st.nextToken()) - 1; int d = Integer.parseInt(st.nextToken()); if (l > 0) update(l - 1, d); if (r < n - 1) update(r, -d); pw.println(query()); } pw.close(); } }
JAVA
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct data { int ldown, rdown; long long l, r; int lhiint, rhiint; int best, siz; }; data merge(data a, data b) { data ret; ret.best = max(a.best, b.best); ret.l = a.l, ret.r = b.r; ret.siz = a.siz + b.siz; ret.ldown = a.ldown, ret.rdown = b.rdown; ret.lhiint = a.lhiint, ret.rhiint = b.rhiint; if (a.r > b.l) ret.best = max(ret.best, a.rhiint + b.ldown); if (a.r < b.l) ret.best = max(ret.best, a.rdown + b.lhiint); if (ret.ldown == a.siz && a.r > b.l) ret.ldown += b.ldown; if (ret.rdown == b.siz && a.r < b.l) ret.rdown += a.rdown; ret.best = max(ret.best, ret.ldown); ret.best = max(ret.best, ret.rdown); if (ret.lhiint == a.siz && a.r > b.l) ret.lhiint += b.ldown; if (ret.rhiint == b.siz && a.r < b.l) ret.rhiint += a.rdown; if (a.rdown == a.siz && a.r < b.l) ret.lhiint = max(ret.lhiint, a.siz + b.lhiint); if (b.ldown == b.siz && a.r > b.l) ret.rhiint = max(ret.rhiint, b.siz + a.rhiint); ret.best = max(ret.best, ret.lhiint); ret.best = max(ret.best, ret.rhiint); return ret; } int n; long long p[300007 * 4], a[300007]; data t[300007 * 4]; void print(int node) { cout << "------ " << node << " -------\n"; cout << t[node].l << ' ' << t[node].r << endl; cout << t[node].best << endl; cout << t[node].ldown << ' ' << t[node].rdown << endl; cout << t[node].lhiint << ' ' << t[node].rhiint << endl; } void build(int b, int e, int node) { if (b == e) { t[node].ldown = 1; t[node].rdown = 1; t[node].l = a[b]; t[node].r = a[b]; t[node].lhiint = 1; t[node].rhiint = 1; t[node].best = 1; t[node].siz = 1; return; } int mid = (b + e) / 2; int l = 2 * node, h = l + 1; build(b, mid, l); build(mid + 1, e, h); t[node] = merge(t[l], t[h]); } void propagate(int node) { int l = 2 * node, h = l + 1; t[l].l += p[node], t[l].r += p[node]; t[h].l += p[node]; t[h].r += p[node]; p[l] += p[node]; p[h] += p[node]; p[node] = 0; } void update(int b, int e, int node, int x, int y, long long val) { if (y < b || e < x) return; if (b >= x && e <= y) { t[node].l += val; t[node].r += val; p[node] += val; return; } propagate(node); int mid = (b + e) / 2, l = 2 * node, h = l + 1; update(b, mid, l, x, y, val); update(mid + 1, e, h, x, y, val); t[node] = merge(t[l], t[h]); } int main() { int m, x, y, c; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); scanf("%d", &m); build(1, n, 1); while (m--) { scanf("%d %d %d", &x, &y, &c); update(1, n, 1, x, y, c); printf("%d\n", t[1].best); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; int n; int arr[1001000]; long long a[1000100]; struct node { int lhill, maxval, rhill; } tree[6 * 500000]; int sig(long long x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } void calc(int cur, int l, int r) { int m = (l + r) / 2; int dc = cur * 2; tree[cur].maxval = max(tree[dc].maxval, tree[dc + 1].maxval); if (!a[m] || !a[m + 1] || sig(a[m]) < sig(a[m + 1])) { tree[cur].lhill = tree[dc].lhill; tree[cur].rhill = tree[dc + 1].rhill; } else { tree[cur].maxval = max(tree[cur].maxval, tree[dc].rhill + tree[dc + 1].lhill); if (tree[dc].maxval == m - l + 1) { tree[cur].lhill = tree[dc].lhill + tree[dc + 1].lhill; } else { tree[cur].lhill = tree[dc].lhill; } if (tree[dc + 1].maxval == r - m) { tree[cur].rhill = tree[dc + 1].rhill + tree[dc].rhill; } else { tree[cur].rhill = tree[dc + 1].rhill; } } } void build(int cur, int l, int r) { if (l == r) { int x = !!a[l]; tree[cur] = {x, x, x}; } else { int m = (l + r) / 2; int dc = cur * 2; build(dc, l, m); build(dc + 1, m + 1, r); calc(cur, l, r); } } void update(int cur, int l, int r, int pos, int val) { if (l == r) { a[pos] += val; int x = !!a[l]; tree[cur] = {x, x, x}; } else { int m = (l + r) / 2; int dc = cur * 2; if (pos <= m) { update(dc, l, m, pos, val); } else { update(dc + 1, m + 1, r, pos, val); } calc(cur, l, r); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", arr + i); } for (int i = 0; i < n - 1; i++) { a[i] = arr[i + 1] - arr[i]; } if (n > 1) build(1, 0, n - 2); int m; scanf("%d", &m); while (m--) { int l, r, d; scanf("%d %d %d", &l, &r, &d); if (n == 1) { puts("1"); continue; } if (l != 1) update(1, 0, n - 2, l - 2, d); if (r != n) update(1, 0, n - 2, r - 1, -d); printf("%d\n", tree[1].maxval + 1); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 300005; int n, m, l, r; long long a[maxn], d[maxn], v; struct s { int l, r, mm, lm, rm; inline void get(int t) { mm = lm = rm = t; } } seg[maxn << 2]; inline int sign(long long v) { return !v ? 0 : v > 0 ? 1 : -1; } void pushup(int x) { int l = seg[x].l, r = seg[x].r, m = (l + r) >> 1; seg[x].mm = max(seg[x << 1].mm, seg[x << 1 | 1].mm); seg[x].lm = seg[x << 1].lm; seg[x].rm = seg[x << 1 | 1].rm; if (sign(d[m]) < sign(d[m + 1])) return; if (seg[x].lm == m - l + 1) seg[x].lm += seg[x << 1 | 1].lm; if (seg[x].rm == r - m) seg[x].rm += seg[x << 1].rm; seg[x].mm = max(seg[x].mm, seg[x << 1].rm + seg[x << 1 | 1].lm); } void build(int x, int l, int r) { seg[x].l = l, seg[x].r = r; if (l == r) { seg[x].get(!!d[l]); return; } int m = (l + r) >> 1; build(x << 1, l, m); build(x << 1 | 1, m + 1, r); pushup(x); } void update(int x, int p, long long v) { int l = seg[x].l, r = seg[x].r; if (l == r) { d[l] += v; seg[x].get(!!d[l]); return; } int m = (l + r) >> 1; if (m >= p) update(x << 1, p, v); else update(x << 1 | 1, p, v); pushup(x); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); for (int i = 1; i < n; ++i) d[i] = a[i + 1] - a[i]; --n; if (n) build(1, 1, n); scanf("%d", &m); while (m--) { scanf("%d%d%lld", &l, &r, &v); if (!n) { puts("1"); continue; } if (l != 1) update(1, l - 1, v); if (r != n + 1) update(1, r, -v); printf("%d\n", seg[1].mm + 1); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> const int Mod = (int)1e9 + 7; const int MX = 1073741822; const long long MXLL = 4611686018427387903; const int Sz = 1110111; using namespace std; inline void Read_rap() { ios_base ::sync_with_stdio(0); cin.tie(0); } struct node { int l, m, r; } t[Sz]; int n; long long a[Sz]; inline int sign(long long x) { if (x < 0) return -1; if (x == 0) return 0; if (x > 0) return 1; } inline void recalc(int v, int tl, int tr) { int tmid = (tl + tr) >> 1; t[v].l = t[2 * v].l; t[v].r = t[2 * v + 1].r; t[v].m = max(t[2 * v].m, t[2 * v + 1].m); if (a[tmid] && a[tmid + 1] && sign(a[tmid]) >= sign(a[tmid + 1])) { t[v].m = max(t[v].m, t[2 * v].r + t[2 * v + 1].l); if (t[2 * v].l == tmid - tl + 1) t[v].l += t[2 * v + 1].l; if (t[2 * v + 1].r == tr - tmid) t[v].r += t[2 * v].r; } } void build(int v, int tl, int tr) { if (tl == tr) { bool b = (a[tl] != 0); t[v] = {b, b, b}; return; } int tmid = (tl + tr) >> 1; build(2 * v, tl, tmid); build(2 * v + 1, tmid + 1, tr); recalc(v, tl, tr); } void upd(int v, int tl, int tr, int id, int x) { if (tl == tr) { a[id] += x; bool b = (a[id] != 0); t[v] = {b, b, b}; return; } int tmid = (tl + tr) >> 1; if (id <= tmid) upd(2 * v, tl, tmid, id, x); else upd(2 * v + 1, tmid + 1, tr, id, x); recalc(v, tl, tr); } int main() { Read_rap(); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; n--; for (int i = 1; i <= n; i++) a[i] = (a[i + 1] - a[i]); a[n + 1] = -1; if (n) build(1, 1, n); int m; cin >> m; while (m--) { if (!n) { cout << 1 << '\n'; continue; } int l, r, d; cin >> l >> r >> d; l--; if (l) upd(1, 1, n, l, d); if (r <= n) upd(1, 1, n, r, -d); cout << max(t[1].m, max(t[1].l, t[1].r)) + 1 << '\n'; } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
import java.io.*; import java.util.*; public class CF740E { static Random rand = new Random(); static class Skip<K,V> { Comparator<K> comp; static class Node<K,V> { K k; V v; Node<K,V>[] next; Node(int r, K k, V v) { next = new Node[r]; // unchecked this.k = k; this.v = v; } } Node<K,V> header; Node<K,V>[] update; int l, m; Skip(int n, Comparator<K> comp) { m = 1; while (1 << m + m < n) // n <= power(1/p, m) for p = 1/4 m++; header = new Node<>(m, null, null); update = new Node[m]; // unchecked l = 1; this.comp = comp; } private Node<K,V> search(K k) { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) while (x.next[i] != null && comp.compare(x.next[i].k, k) < 0) x = x.next[i]; return x; } private Node<K,V> search_(K k) { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) { while (x.next[i] != null && comp.compare(x.next[i].k, k) < 0) x = x.next[i]; update[i] = x; } return x; } K first() { Node<K,V> x = header; x = x.next[0]; return x == null ? null : x.k; } K last() { Node<K,V> x = header; for (int i = l - 1; i >= 0; i--) while (x.next[i] != null) x = x.next[i]; return x.k; } K lower(K k) { Node<K,V> x = search(k); return x.k; } K floor(K k) { Node<K,V> x = search(k), y = x.next[0]; if (y != null && comp.compare(y.k, k) == 0) return y.k; return x.k; } K higher(K k) { Node<K,V> x = search(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) x = x.next[0]; return x == null ? null : x.k; } V getOrDefault(K k, V v) { Node<K,V> x = search(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) return x.v; return v; } void put(K k, V v) { Node<K,V> x = search_(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) { x.v = v; return; } int r = 1; while (r < m && rand.nextInt(4) == 0) // p = 1/4 r++; if (r > l) { for (int i = l; i < r; i++) update[i] = header; l = r; } x = new Node<>(r, k, v); for (int i = 0; i < r; i++) { x.next[i] = update[i].next[i]; update[i].next[i] = x; } } V remove(K k) { Node<K,V> x = search_(k); x = x.next[0]; if (x != null && comp.compare(x.k, k) == 0) { for (int i = 0; i < l; i++) { if (update[i].next[i] != x) break; update[i].next[i] = x.next[i]; } while (l > 1 && header.next[l - 1] == null) l--; V v = x.v; // free(x); return v; } return null; } }; static long[] aa; static class H { int i, j, k; // [i, j) up [j, k) down H(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } } //static TreeSet<H> xx = new TreeSet<>((p, q) -> p.i - q.i); static Skip<H,H> xx; //static TreeMap<Integer, Integer> ww = new TreeMap<>(); static Skip<Integer,Integer> ww; static void add(int i, int j, int k) { if (i <= j && j <= k && i < k) { H h = new H(i, j, k); //xx.add(h); xx.put(h, h); int key = k - i; int cnt = ww.getOrDefault(key, 0); ww.put(key, cnt + 1); } } static void remove(H h) { xx.remove(h); int key = h.k - h.i; int cnt = ww.getOrDefault(key, 0); if (cnt == 1) ww.remove(key); else ww.put(key, cnt - 1); } static void init() { int n = aa.length; for (int i = 0, j, k; i < n; ) { j = i; while (j < n && aa[j] > 0) j++; k = j; while (k < n && aa[k] < 0) k++; if (i < k) { add(i, j, k); i = k; } else i++; } } static void join(int i) { H h = xx.floor(new H(i, i, i)); H l = xx.lower(h); H r = xx.higher(h); boolean lh = l != null && l.k == h.i && (l.j == l.k || h.i == h.j); boolean hr = r != null && h.k == r.i && (h.j == h.k || r.i == r.j); if (lh && hr) { remove(l); remove(h); remove(r); add(l.i, h.i == h.j ? l.j : r.j, r.k); } else if (lh) { remove(l); remove(h); add(l.i, h.i == h.j ? l.j : h.j, h.k); } else if (hr) { remove(h); remove(r); add(h.i, r.i == r.j ? h.j : r.j, r.k); } } static void update(int i, int d) { long a = aa[i]; long b = aa[i] += d; // d != 0 if (a == 0) { add(i, b > 0 ? i + 1 : i, i + 1); join(i); } else if (b == 0) { H h = xx.floor(new H(i, i, i)); remove(h); if (a > 0) { add(h.i, i, i); add(i + 1, h.j, h.k); } else { add(h.i, h.j, i); add(i + 1, i + 1, h.k); } } else if (a > 0 && b < 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, i, i + 1); add(i + 1, h.j, h.k); join(i); } else if (a < 0 && b > 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, h.j, i); add(i, i + 1, h.k); join(i); } } static int query() { //return ww.isEmpty() ? 1 : ww.lastKey() + 1; Integer l = ww.last(); return l == null ? 1 : l + 1; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); aa = new long[n - 1]; int a_ = Integer.parseInt(st.nextToken()); for (int i = 0; i < n - 1; i++) { int a = Integer.parseInt(st.nextToken()); aa[i] = a - a_; a_ = a; } xx = new Skip<>(n, (p, q) -> p.i - q.i); ww = new Skip<>((int) Math.sqrt(n), (p, q) -> p - q); init(); int m = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(System.out); while (m-- > 0) { st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()) - 1; int r = Integer.parseInt(st.nextToken()) - 1; int d = Integer.parseInt(st.nextToken()); if (l > 0) update(l - 1, d); if (r < n - 1) update(r, -d); pw.println(query()); } pw.close(); } }
JAVA
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int MAX = (int)3e5 + 10; struct Node { long long vl, vr; int len; int seg_l, seg_r; int value_l, value_r, value; bool cd_l, cd_r; long long D; bool emp; }; int n; int a[MAX]; Node T[2 * MAX]; int h; void init() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; } Node combine(const Node& t1, const Node& t2) { if (t1.emp) return t2; if (t2.emp) return t1; Node t; t.emp = false; t.D = 0; t.vl = t1.vl; t.vr = t2.vr; t.len = t1.len + t2.len; t.seg_l = t1.seg_l; t.seg_r = t2.seg_r; if (t1.seg_l == t1.len && t2.vl < t1.vr) t.seg_l += t2.seg_l; if (t2.seg_r == t2.len && t1.vr < t2.vl) t.seg_r += t1.seg_r; t.value_l = t1.value_l; t.cd_l = t1.cd_l; if (t1.value_l == t1.len) { if (!t1.cd_l) { if (t1.vr > t2.vl) { t.cd_l = true; t.value_l += t2.seg_l; } else if (t1.vr < t2.vl) { if (t2.value_l >= t2.seg_l) { t.value_l += t2.value_l; t.cd_l = t2.cd_l; } else { t.value_l += t2.seg_l; t.cd_l = true; } } } else { if (t1.vr > t2.vl) t.value_l += t2.seg_l; } } t.value_r = t2.value_r; t.cd_r = t2.cd_r; if (t2.value_r == t2.len) { if (!t2.cd_r) { if (t2.vl > t1.vr) { t.cd_r = true; t.value_r += t1.seg_r; } else if (t2.vl < t1.vr) { if (t1.value_r >= t1.seg_r) { t.value_r += t1.value_r; t.cd_r = t1.cd_r; } else { t.value_r += t1.seg_r; t.cd_r = true; } } } else { if (t1.vr < t2.vl) t.value_r += t1.seg_r; } } t.value = max(t1.value, t2.value); t.value = max(t.value, max(t.value_l, t.value_r)); t.value = max(t.value, max(t.seg_l, t.seg_r)); if (t1.vr != t2.vl) t.value = max(t.value, t1.seg_r + t2.seg_l); if (t1.vr > t2.vl) t.value = max(t.value, t1.value_r + t2.seg_l); if (t1.vr < t2.vl) t.value = max(t.value, t1.seg_r + t2.value_l); return t; } void apply(Node& t, long long value) { t.vl += value; t.vr += value; if (t.len > 1) t.D += value; } void push(int p) { for (int s = h; s > 0; s--) { int i = p >> s; if (T[i].D != 0) { apply(T[i << 1], T[i].D); apply(T[i << 1 | 1], T[i].D); T[i].D = 0; } } } void build(int p) { for (p >>= 1; p >= 1; p >>= 1) if (T[p].D == 0) T[p] = combine(T[p << 1], T[p << 1 | 1]); } void modify(int l, int r, int value) { l += n - 1; r += n; int l0 = l, r0 = r; push(l0); push(r0 - 1); for (; l < r; l >>= 1, r >>= 1) { if (l & 1) apply(T[l++], value); if (r & 1) apply(T[--r], value); } build(l0); build(r0 - 1); } Node query(int l, int r) { l += n - 1; r += n; push(l); push(r - 1); Node resl, resr; resl.emp = resr.emp = true; for (; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = combine(resl, T[l++]); if (r & 1) resr = combine(T[--r], resr); } return combine(resl, resr); } void construct_segment_tree() { for (int i = 1; i <= n; i++) { int id = i + n - 1; T[id].vl = T[id].vr = a[i]; T[id].len = 1; T[id].seg_l = T[id].seg_r = 1; T[id].value_l = T[id].value_r = T[id].value = 1; T[id].cd_l = T[id].cd_r = false; T[id].D = 0; T[id].emp = false; } for (int i = n - 1; i >= 1; i--) T[i] = combine(T[i << 1], T[i << 1 | 1]); h = sizeof(int) * 8 - __builtin_clz(n); } void process() { construct_segment_tree(); int m, l, r, d; cin >> m; while (m--) { cin >> l >> r >> d; modify(l, r, d); cout << query(1, n).value << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); init(); process(); }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 300001; struct segment { long long tree[4 * N], lazy[4 * N]; int n; void resize(int _n) { n = _n; } void shift(int l, int r, int node) { if (!lazy[node]) return; tree[node] += (long long)(r - l + 1) * lazy[node]; if (l != r) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; } lazy[node] = 0; } void update(int l, int r, int node, int left, int right, long long val) { if (l > r) return; shift(l, r, node); if (l > right || r < left) return; if (l >= left && r <= right) { tree[node] += (long long)(r - l + 1) * val; if (l != r) { lazy[node << 1] += val; lazy[node << 1 | 1] += val; } return; } int mid = (l + r) >> 1; update(l, mid, node << 1, left, right, val); update(mid + 1, r, node << 1 | 1, left, right, val); tree[node] = tree[node << 1] + tree[node << 1 | 1]; } long long query(int l, int r, int node, int left, int right) { if (l > r) return 0; shift(l, r, node); if (l > right || r < left) return 0; if (l >= left && r <= right) return tree[node]; int mid = (l + r) >> 1; return query(l, mid, node << 1, left, right) + query(mid + 1, r, node << 1 | 1, left, right); } }; int n, q; segment tree; vector<int> a; set<pair<int, int> > s; multiset<int> sizes; int sign(int idx) { if (idx == 0) return 0; long long x = tree.query(0, n - 1, 1, idx - 1, idx - 1); long long y = tree.query(0, n - 1, 1, idx, idx); if (x < y) return 1; if (x > y) return -1; return 0; } bool bad(int idx) { if (idx == 0) return 1; if (a[idx] == 0 || a[idx - 1] == 0) return 1; if (a[idx - 1] == -1 && a[idx] == 1) return 1; return 0; } void erase(pair<int, int> x) { sizes.erase(sizes.find(x.second - x.first + 1)); s.erase(make_pair(x.first, x.second)); } void insert(pair<int, int> x) { sizes.insert(x.second - x.first + 1); s.insert(make_pair(x.first, x.second)); } void merge(int x) { auto g = *(--s.upper_bound(make_pair(x - 1, 1e9))); auto h = *(--s.upper_bound(make_pair(x, 1e9))); if (g == h) return; erase(g); erase(h); insert(make_pair(g.first, h.second)); } void split(int x) { auto g = *(--s.upper_bound(make_pair(x, 1e9))); if (g.first == x) return; erase(g); insert(make_pair(g.first, x - 1)); insert(make_pair(x, g.second)); } void upd(int idx) { if (idx == 0 || idx == n) return; a[idx] = sign(idx); if (bad(idx)) split(idx); else merge(idx); if (idx + 1 < n) { if (bad(idx + 1)) split(idx + 1); else merge(idx + 1); } } int main() { scanf("%d", &n); tree.resize(n + 1); for (int i = 0, x; i < n && scanf("%d", &x); i++) tree.update(0, n - 1, 1, i, i, x); for (int i = 0; i < n; i++) a.push_back(sign(i)); for (int i = 0; i < n; i++) insert(make_pair(i, i)); for (int i = 1; i < n; i++) if (!bad(i)) merge(i); scanf("%d", &q); while (q--) { int l, r, d; scanf("%d %d %d", &l, &r, &d); l--; r--; if (n == 1) { puts("1"); continue; } tree.update(0, n - 1, 1, l, r, d); upd(l); upd(r + 1); printf("%d\n", *sizes.rbegin() + 1); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int MX = 300005; struct BIT { long long ft[MX]; void init() { memset(ft, 0, sizeof(ft)); } void update(int i, long long k) { while (i < MX) { ft[i] += k; i += i & -i; } } void update(int a, int b, long long k) { update(a, k); update(b + 1, -k); } long long query(int i) { long long sum = 0; while (i) { sum += ft[i]; i -= i & -i; } return sum; } }; struct ST { int st[4 * MX], pre[4 * MX], suf[4 * MX], a[MX]; void init(int i, int j, int pos) { st[pos] = pre[pos] = suf[pos] = 0; if (i < j) { int m = (i + j) / 2; init(i, m, pos * 2); init(m + 1, j, pos * 2 + 1); } else { a[i] = 0; } } void update(int i, int j, int pos, int x, int k) { if (x < i || j < x) return; if (i == j) { a[i] = k; st[pos] = pre[pos] = suf[pos] = abs(k); return; } int m = (i + j) / 2; update(i, m, pos * 2, x, k); update(m + 1, j, pos * 2 + 1, x, k); if (a[m] && a[m + 1] && a[m] >= a[m + 1]) { st[pos] = max({st[pos * 2], st[pos * 2 + 1], suf[pos * 2] + pre[pos * 2 + 1]}); pre[pos] = pre[pos * 2] + pre[pos * 2 + 1] * (pre[pos * 2] == (m - i + 1)); suf[pos] = suf[pos * 2 + 1] + suf[pos * 2] * (suf[pos * 2 + 1] == (j - m)); } else { st[pos] = max(st[pos * 2], st[pos * 2 + 1]); pre[pos] = pre[pos * 2]; suf[pos] = suf[pos * 2 + 1]; } } }; int n, a[MX], q, l, r, d; long long e; BIT ft; ST st; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ft.init(); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; ft.update(i, a[i] - a[i - 1]); } cin >> q; if (n == 1) { while (q--) { cin >> l >> r >> d; cout << 1 << '\n'; } return 0; } st.init(1, n - 1, 1); for (int i = 1; i < n; i++) { e = a[i + 1] - a[i]; if (e) st.update(1, n - 1, 1, i, e / abs(e)); else st.update(1, n - 1, 1, i, 0); } while (q--) { cin >> l >> r >> d; ft.update(l, r, d); if (l > 1) { e = ft.query(l) - ft.query(l - 1); if (e) st.update(1, n - 1, 1, l - 1, e / abs(e)); else st.update(1, n - 1, 1, l - 1, 0); } if (r < n) { e = ft.query(r + 1) - ft.query(r); if (e) st.update(1, n - 1, 1, r, e / abs(e)); else st.update(1, n - 1, 1, r, 0); } cout << st.st[1] + 1 << '\n'; } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; long long n, m, ans, a[2000010], a0[2000010]; struct Node { long long MaxL, MaxR, Max; }; Node tree[2000010]; long long Sign(long long x) { if (x > 0) { return 1; } if (x == 0) { return 0; } return -1; } void Merge(long long idx, long long l, long long r) { if (l == r) { return; } long long mid = (l + r) / 2; tree[idx].Max = max(tree[idx * 2 + 1].Max, tree[idx * 2 + 2].Max); if (!a[mid] or !a[mid + 1] or Sign(a[mid]) < Sign(a[mid + 1])) { tree[idx].MaxL = tree[idx * 2 + 1].MaxL; tree[idx].MaxR = tree[idx * 2 + 2].MaxR; } else { tree[idx].Max = max(tree[idx].Max, tree[idx * 2 + 1].MaxR + tree[idx * 2 + 2].MaxL); if (tree[idx * 2 + 1].MaxL == mid - l + 1) { tree[idx].MaxL = tree[idx * 2 + 1].MaxL + tree[idx * 2 + 2].MaxL; } else { tree[idx].MaxL = tree[idx * 2 + 1].MaxL; } if (tree[idx * 2 + 2].MaxR == r - mid) { tree[idx].MaxR = tree[idx * 2 + 2].MaxR + tree[idx * 2 + 1].MaxR; } else { tree[idx].MaxR = tree[idx * 2 + 2].MaxR; } } } void Init(long long idx, long long l, long long r) { if (l == r) { if (a[l]) { tree[idx] = {1, 1, 1}; } else { tree[idx] = {0, 0, 0}; } return; } long long mid = (l + r) / 2; Init(idx * 2 + 1, l, mid); Init(idx * 2 + 2, mid + 1, r); Merge(idx, l, r); } void Update(long long idx, long long x, long long val, long long l, long long r) { if (l == r) { a[l] += val; if (a[l]) { tree[idx] = {1, 1, 1}; } else { tree[idx] = {0, 0, 0}; } return; } long long mid = (l + r) / 2; if (x <= mid) { Update(idx * 2 + 1, x, val, l, mid); } else { Update(idx * 2 + 2, x, val, mid + 1, r); } Merge(idx, l, r); } signed main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n; for (long long i = 0; i < n; ++i) { cin >> a0[i]; } for (long long i = 0; i < n - 1; ++i) { a[i] = a0[i + 1] - a0[i]; } if (n > 1) { Init(0, 0, n - 2); } cin >> m; for (long long i = 0; i < m; ++i) { long long l, r, x; cin >> l >> r >> x; if (n == 1) { cout << "1\n"; continue; } if (l >= 2) { Update(0, l - 2, x, 0, n - 2); } if (r < n) { Update(0, r - 1, -x, 0, n - 2); } cout << tree[0].Max + 1 << '\n'; } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
import java.io.*; import java.util.*; public class CF740E { static long[] aa; static class H implements Comparable<H> { int i, j, k; // [i, j) up [j, k) down H(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } @Override public int compareTo(H h) { return k - i != h.k - h.i ? (k - i) - (h.k - h.i) : i - h.i; } } static TreeSet<H> xx = new TreeSet<>((p, q) -> p.i - q.i); static TreeMap<Integer, Integer> ww = new TreeMap<>(); static void add(int i, int j, int k) { if (i <= j && j <= k && i < k) { H h = new H(i, j, k); xx.add(h); int key = k - i; int cnt = ww.getOrDefault(key, 0); ww.put(key, cnt + 1); } } static void remove(H h) { xx.remove(h); int key = h.k - h.i; int cnt = ww.getOrDefault(key, 0); if (cnt == 1) ww.remove(key); else ww.put(key, cnt - 1); } static void init() { int n = aa.length; for (int i = 0, j, k; i < n; ) { j = i; while (j < n && aa[j] > 0) j++; k = j; while (k < n && aa[k] < 0) k++; if (i < k) { add(i, j, k); i = k; } else i++; } } static void join(int i) { H h = xx.floor(new H(i, i, i)); H l = xx.lower(h); H r = xx.higher(h); boolean lh = l != null && l.k == h.i && (l.j == l.k || h.i == h.j); boolean hr = r != null && h.k == r.i && (h.j == h.k || r.i == r.j); if (lh && hr) { remove(l); remove(h); remove(r); add(l.i, h.i == h.j ? l.j : r.j, r.k); } else if (lh) { remove(l); remove(h); add(l.i, h.i == h.j ? l.j : h.j, h.k); } else if (hr) { remove(h); remove(r); add(h.i, r.i == r.j ? h.j : r.j, r.k); } } static void update(int i, int d) { long a = aa[i]; long b = aa[i] += d; // d != 0 if (a == 0) { add(i, b > 0 ? i + 1 : i, i + 1); join(i); } else if (b == 0) { H h = xx.floor(new H(i, i, i)); remove(h); if (a > 0) { add(h.i, i, i); add(i + 1, h.j, h.k); } else { add(h.i, h.j, i); add(i + 1, i + 1, h.k); } } else if (a > 0 && b < 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, i, i + 1); add(i + 1, h.j, h.k); join(i); } else if (a < 0 && b > 0) { H h = xx.floor(new H(i, i, i)); remove(h); add(h.i, h.j, i); add(i, i + 1, h.k); join(i); } } static int query() { return ww.isEmpty() ? 1 : ww.lastKey() + 1; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); aa = new long[n - 1]; int a_ = Integer.parseInt(st.nextToken()); for (int i = 0; i < n - 1; i++) { int a = Integer.parseInt(st.nextToken()); aa[i] = a - a_; a_ = a; } init(); int m = Integer.parseInt(br.readLine()); PrintWriter pw = new PrintWriter(System.out); while (m-- > 0) { st = new StringTokenizer(br.readLine()); int l = Integer.parseInt(st.nextToken()) - 1; int r = Integer.parseInt(st.nextToken()) - 1; int d = Integer.parseInt(st.nextToken()); if (l > 0) update(l - 1, d); if (r < n - 1) update(r, -d); pw.println(query()); } pw.close(); } }
JAVA
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int maxn = 300005; int n, m, l, r; long long v, a[maxn], d[maxn]; struct s { int l, r, lm, mm, rm; void get(int t) { lm = rm = mm = t; } } seg[maxn << 2]; inline int sign(long long x) { return !x ? 0 : x > 0 ? 1 : -1; } void pushup(int x) { int l = seg[x].l, r = seg[x].r, m = (l + r) >> 1; seg[x].mm = max(seg[x << 1].mm, seg[x << 1 | 1].mm); seg[x].lm = seg[x << 1].lm; seg[x].rm = seg[x << 1 | 1].rm; if (!d[m] || !d[m + 1] || sign(d[m]) < sign(d[m + 1])) return; if (seg[x].lm == m - l + 1) seg[x].lm += seg[x << 1 | 1].lm; if (seg[x].rm == r - m) seg[x].rm += seg[x << 1].rm; seg[x].mm = max(seg[x].mm, seg[x << 1].rm + seg[x << 1 | 1].lm); } void build(int x, int l, int r) { seg[x].l = l, seg[x].r = r; if (l == r) { seg[x].get(!!d[l]); return; } int m = (l + r) >> 1; build(x << 1, l, m); build(x << 1 | 1, m + 1, r); pushup(x); } void update(int x, int p, long long v) { int l = seg[x].l, r = seg[x].r; if (l == r) { d[l] += v; seg[x].get(!!d[l]); return; } int m = (l + r) >> 1; if (m >= p) update(x << 1, p, v); else update(x << 1 | 1, p, v); pushup(x); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); for (int i = 1; i < n; ++i) d[i] = a[i + 1] - a[i]; --n; if (n) build(1, 1, n); scanf("%d", &m); while (m--) { scanf("%d%d%lld", &l, &r, &v); if (!n) { puts("1"); continue; } if (l != 1) update(1, l - 1, v); if (r != n + 1) update(1, r, -v); printf("%d\n", seg[1].mm + 1); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> const int MaxN = 300000; using namespace std; long long arr[MaxN + 5], a[MaxN + 5], n; struct Segment { int l, r, lx, mx, rx; int Sign(long long x) { return x > 0 ? 1 : -1; } void Update(Segment A, Segment B) { lx = A.lx, rx = B.rx; mx = max(A.mx, B.mx); if (a[A.r] && a[B.l] && Sign(a[A.r]) >= Sign(a[B.l])) { mx = max(mx, A.rx + B.lx); if (A.mx == A.r - A.l + 1) lx = A.mx + B.lx; if (B.mx == B.r - B.l + 1) rx = B.mx + A.rx; } mx = max(mx, max(lx, rx)); l = A.l, r = B.r; } } seg[MaxN * 4 + 5]; void Update(int l, int r, int t, int x, int w) { if (l == r) { a[x] += w; if (a[x] != 0) seg[t] = (Segment){l, r, 1, 1, 1}; else seg[t] = (Segment){l, r, 0, 0, 0}; return; } int mid = (l + r) >> 1; if (x <= mid) Update(l, mid, t * 2, x, w); else Update(mid + 1, r, t * 2 + 1, x, w); seg[t].Update(seg[t * 2], seg[t * 2 + 1]); } int main() { while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); for (int i = 1; i <= n; i++) a[i] = arr[i + 1] - arr[i]; for (int i = 1; i < n; i++) Update(1, n - 1, 1, i, 0); int m; scanf("%d", &m); for (int i = 1; i <= m; i++) { int l, r, w; scanf("%d%d%d", &l, &r, &w); if (l - 1 >= 1) Update(1, n - 1, 1, l - 1, w); if (r <= n - 1) Update(1, n - 1, 1, r, -w); printf("%d\n", seg[1].mx + 1); } } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 300300; struct Node { int l, r, l_line, r_line, maxn; long long lv, rv; } node[MAXN << 2]; int a[MAXN]; void push_up(int rt) { int lson = rt << 1, rson = rt << 1 | 1; node[rt].maxn = node[lson].maxn; node[rt].maxn = max(node[rt].maxn, node[rson].maxn); if (!(node[lson].rv <= 0 && node[rson].lv >= 0)) node[rt].maxn = max(node[lson].r_line + node[rson].l_line, node[rt].maxn); node[rt].lv = node[lson].lv; node[rt].rv = node[rson].rv; node[rt].l_line = node[lson].l_line; if (node[lson].l_line == node[lson].r - node[lson].l + 1 && !(node[lson].rv <= 0 && node[rson].lv >= 0)) node[rt].l_line += node[rson].l_line; node[rt].r_line = node[rson].r_line; if (node[rson].r_line == node[rson].r - node[rson].l + 1 && !(node[lson].rv <= 0 && node[rson].lv >= 0)) node[rt].r_line += node[lson].r_line; } void build(int l, int r, int rt) { node[rt].l = l; node[rt].r = r; if (l == r) { node[rt].lv = a[r] - a[r - 1]; node[rt].rv = a[r] - a[r - 1]; if (a[r] - a[r - 1]) node[rt].r_line = node[rt].l_line = node[rt].maxn = 1; else node[rt].r_line = node[rt].l_line = node[rt].maxn = 0; return; } int mid = (l + r) >> 1; build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1); push_up(rt); } void update(int rt, int idx, int d) { if (node[rt].l == node[rt].r) { node[rt].lv += d; node[rt].rv += d; if (node[rt].lv) node[rt].r_line = node[rt].l_line = node[rt].maxn = 1; else node[rt].r_line = node[rt].l_line = node[rt].maxn = 0; return; } int mid = (node[rt].l + node[rt].r) >> 1; if (idx <= mid) update(rt << 1, idx, d); else update(rt << 1 | 1, idx, d); push_up(rt); } int main() { int n, m, l, r, d; cin >> n; for (int i = 0; i < n; ++i) scanf("%d", &a[i]); if (n > 1) build(1, n - 1, 1); scanf("%d", &m); while (m--) { scanf("%d %d %d", &l, &r, &d); if (n == 1) { cout << 1 << endl; continue; } if (l != 1) update(1, l - 1, d); if (r != n) update(1, r, -d); printf("%d\n", node[1].maxn + 1); } }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.*; /* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'====== `=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pass System Test! */ public class E { private static class Task { void solve(FastScanner in, PrintWriter out) throws Exception { int N = in.nextInt(); long[] a = in.nextLongArray(N); TreeMap<Integer, Integer> left = new TreeMap<>(); int from = 0; for (int i = 0; i < N; i++) { if (i == N - 1 || a[i] >= a[i + 1]) { left.put(from, i); from = i + 1; } } TreeMap<Integer, Integer> right = new TreeMap<>(); from = N - 1; for (int i = N - 2; i >= -1; i--) { if (i < 0 || a[i] <= a[i + 1]) { right.put(i + 1, from); from = i; } } RMQ rmq = new RMQ(N); for (Map.Entry<Integer, Integer> entry : left.entrySet()) { from = entry.getKey(); int to = entry.getValue(); if (!right.containsKey(to)) continue; int to2 = right.get(to); int width = to2 - from + 1; rmq.update(to, width); } long[] d = new long[N - 1]; for (int i = 0; i < N - 1; i++) { d[i] = a[i + 1] - a[i]; } int M = in.nextInt(); TreeSet<Integer> tmp = new TreeSet<>(); for (int i = 0; i < M; i++) { int l = in.nextInt() - 1; int r = in.nextInt() - 1; long v = in.nextInt(); if (l > 0) { long prev = d[l - 1]; d[l - 1] += v; if (prev <= 0 && d[l - 1] > 0) { // left merge from = left.floorKey(l - 1); int to = left.get(l); left.remove(l); rmq.update(l, 0); left.put(from, to); tmp.add(from); } if (prev < 0 && d[l - 1] >= 0) { // right cut Map.Entry<Integer, Integer> entry = right.floorEntry(l - 1); from = entry.getKey(); int to = entry.getValue(); right.put(from, l - 1); right.put(l, to); rmq.update(from, 0); tmp.add(left.floorKey(from)); tmp.add(left.floorKey(l)); } } if (r < N - 1) { long prev = d[r]; d[r] -= v; if (prev > 0 && d[r] <= 0) { // left cut Map.Entry<Integer, Integer> entry = left.floorEntry(r); from = entry.getKey(); int to = entry.getValue(); left.put(from, r); left.put(r + 1, to); rmq.update(to, 0); tmp.add(from); tmp.add(r + 1); } if (prev >= 0 && d[r] < 0) { // right merge from = right.floorKey(r); int to = right.get(r + 1); right.remove(r + 1); right.put(from, to); rmq.update(from, 0); tmp.add(left.floorKey(from)); } } for (int f : tmp) { int k = left.get(f); Integer to = right.get(k); if (to==null) continue; rmq.update(k, to - f + 1); } tmp.clear(); out.println(rmq.query(0, N + 1)); } } class RMQ { private int N; private long[] seg; RMQ(int M) { N = Integer.highestOneBit(M) * 2; seg = new long[N * 2]; } public void update(int k, long value) { seg[k += N - 1] = value; while (k > 0) { k = (k - 1) / 2; seg[k] = Math.max(seg[k * 2 + 1], seg[k * 2 + 2]); } } //[a, b) long query(int a, int b) { return query(a, b, 0, 0, N); } long query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0; if (a <= l && r <= b) return seg[k]; long x = query(a, b, k * 2 + 1, l, (l + r) / 2); long y = query(a, b, k * 2 + 2, (l + r) / 2, r); return Math.max(x, y); } } } /** * ??????????????? */ public static void main(String[] args) throws Exception { OutputStream outputStream = System.out; FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } private static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int bufferLength = 0; private boolean hasNextByte() { if (ptr < bufferLength) { return true; } else { ptr = 0; try { bufferLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (bufferLength <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) { array[i] = nextDouble(); } return array; } double[][] nextDoubleMap(int n, int m) { double[][] map = new double[n][]; for (int i = 0; i < n; i++) { map[i] = nextDoubleArray(m); } return map; } public int nextInt() { return (int) nextLong(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } public String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } public char[][] nextCharMap(int n) { char[][] array = new char[n][]; for (int i = 0; i < n; i++) array[i] = next().toCharArray(); return array; } public int[][] nextIntMap(int n, int m) { int[][] map = new int[n][]; for (int i = 0; i < n; i++) { map[i] = nextIntArray(m); } return map; } } }
JAVA
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 1 << 19; struct Node { int l, r; long long val; int lPos, lNeg, rPos, rNeg, inAns, lAns, rAns; Node() : l(-1), r(-1), val(), lPos(), lNeg(), rPos(), rNeg(), inAns(), lAns(), rAns() {} Node(int _l, int _r) : l(_l), r(_r), val(0), lPos(0), lNeg(0), rPos(0), rNeg(0), inAns(0), lAns(0), rAns(0) {} void update() { if (val > 0) { lPos = rPos = 1; lNeg = rNeg = 0; inAns = lAns = rAns = 1; } else if (val < 0) { lPos = rPos = 0; lNeg = rNeg = 1; inAns = lAns = rAns = 1; } else { lPos = rPos = 0; lNeg = rNeg = 0; inAns = lAns = rAns = 0; } } }; Node tree[2 * N + 7]; Node merge(Node L, Node R) { if (L.l == -1) return R; if (R.l == -1) return L; Node A = Node(L.l, R.r); int lenL = L.r - L.l, lenR = R.r - R.l; A.lPos = L.lPos + (L.lPos == lenL ? R.lPos : 0); A.lNeg = L.lNeg + (L.lNeg == lenL ? R.lNeg : 0); A.rPos = R.rPos + (R.rPos == lenR ? L.rPos : 0); A.rNeg = R.rNeg + (R.rNeg == lenR ? L.rNeg : 0); A.inAns = max(L.inAns, R.inAns); A.inAns = max(A.inAns, L.rAns + R.lNeg); A.inAns = max(A.inAns, R.lAns + L.rPos); A.lAns = L.lAns + (L.lAns == lenL ? R.lNeg : 0); if (L.lPos == lenL) A.lAns = max(A.lAns, lenL + R.lAns); A.rAns = R.rAns + (R.rAns == lenR ? L.rPos : 0); if (R.lNeg == lenR) A.rAns = max(A.rAns, lenR + L.rAns); A.inAns = max(A.inAns, max(A.lAns, A.rAns)); return A; } void buildTree() { for (int i = 0; i < N; i++) tree[N + i] = Node(i, i + 1); for (int i = N - 1; i > 0; i--) tree[i] = merge(tree[2 * i], tree[2 * i + 1]); return; } long long a[N]; int n; void changeTree(int v) { tree[v + N].val = a[v]; v += N; tree[v].update(); while (v > 1) { v >>= 1; tree[v] = merge(tree[2 * v], tree[2 * v + 1]); } return; } int main() { buildTree(); scanf("%d", &n); n--; long long prev; scanf("%lld", &prev); for (int i = 0; i < n; i++) { long long x; scanf("%lld", &x); a[i] = x - prev; prev = x; changeTree(i); } int q; scanf("%d", &q); while (q--) { int l, r; long long x; scanf("%d%d%I64d", &l, &r, &x); l -= 2; r--; if (l >= 0) { a[l] += x; changeTree(l); } if (r < n) { a[r] -= x; changeTree(r); } printf("%d\n", 1 + tree[1].inAns); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; struct Node { int pref, suf, pref_neg, suf_pos, ans, cant; Node() {} void init(long long v) { if (v < 0) { pref = 0; suf = 0; pref_neg = 1; suf_pos = 0; ans = 1; cant = 1; } else if (v > 0) { pref = 0; suf = 0; pref_neg = 0; suf_pos = 1; ans = 1; cant = 1; } else { pref = 0; suf = 0; pref_neg = 0; suf_pos = 0; ans = 0; cant = 1; } } void merge(const Node &a, const Node &b) { cant = a.cant + b.cant; suf = pref = 0; if (a.suf_pos == a.cant && b.pref_neg == b.cant) pref = suf = cant; suf = max(suf, b.suf); if (b.suf == b.cant) suf += a.suf_pos; pref = max(pref, a.pref); if (a.pref == a.cant) pref += b.pref_neg; suf_pos = b.suf_pos; if (b.suf_pos == b.cant) suf_pos += a.suf_pos; pref_neg = a.pref_neg; if (a.pref_neg == a.cant) pref_neg += b.pref_neg; if (b.pref_neg == b.cant) { suf = max(suf, a.suf + b.pref_neg); suf = max(suf, a.suf_pos + b.pref_neg); } if (a.suf_pos == a.cant) { pref = max(pref, a.suf_pos + b.pref); pref = max(pref, a.suf_pos + b.pref_neg); } ans = max(a.ans, b.ans); ans = max(ans, a.suf_pos + b.pref_neg); ans = max(ans, a.suf + b.pref_neg); ans = max(ans, a.suf_pos + b.pref); ans = max(ans, pref); ans = max(ans, pref_neg); ans = max(ans, suf); ans = max(ans, suf_pos); } }; Node T[1050000]; int I, J, V; long long d[300000]; void init(int id, int L, int R) { if (L == R) T[id].init(d[L]); else { int a = id << 1, M = (L + R) >> 1; init(a, L, M); init(a | 1, M + 1, R); T[id].merge(T[a], T[a | 1]); } } void update(int id, int L, int R) { if (L == R) { d[L] += V; T[id].init(d[L]); } else { int a = id << 1, M = (L + R) >> 1; if (J <= M) update(a, L, M); else if (I > M) update(a | 1, M + 1, R); else { update(a, L, M); update(a | 1, M + 1, R); } T[id].merge(T[a], T[a | 1]); } } Node query(int id, int L, int R) { if (I <= L && R <= J) return T[id]; int a = id << 1, M = (L + R) >> 1; if (J <= M) return query(a, L, M); else if (I > M) return query(a | 1, M + 1, R); else { Node ans; ans.merge(query(a, L, M), query(a | 1, M + 1, R)); return ans; } } int a[300001]; int main(int argc, char const *argv[]) { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 1; i < n; ++i) d[i - 1] = a[i] - a[i - 1]; if (n > 1) init(1, 0, n - 2); I = 0; J = n - 2; int m, x, y; scanf("%d", &m); while (m--) { scanf("%d %d %d", &x, &y, &V); x--; y--; if (x > 0) { J = I = x - 1; update(1, 0, n - 2); } V = -V; if (y < n - 1) { J = I = y; update(1, 0, n - 2); } I = 0; J = n - 2; if (n > 1) printf("%d\n", query(1, 0, n - 2).ans + 1); else printf("1\n"); } return 0; }
CPP
740_E. Alyona and towers
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ— 1 Γ— 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them. Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≀ k ≀ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar. After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it. Input The first line contain single integer n (1 ≀ n ≀ 3Β·105) β€” the number of towers. The second line contain n integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the number of cubes in each tower. The third line contain single integer m (1 ≀ m ≀ 3Β·105) β€” the number of additions. The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 ≀ l ≀ r ≀ n, 1 ≀ di ≀ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri. Output Print m lines. In i-th line print the maximum width of the hills after the i-th addition. Example Input 5 5 5 5 5 5 3 1 3 2 2 2 1 4 4 1 Output 2 4 5 Note The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4. After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
2
11
#include <bits/stdc++.h> using namespace std; inline int readInt() { static int n, ch; n = 0, ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) n = n * 10 + ch - '0', ch = getchar(); return n; } const int MAX_N = 300000 + 3; int n, m, a[MAX_N]; namespace segment_tree { const int MAX_NODE = (1 << 19) * 2; inline void relax(int &a, const int &b) { if (b > a) a = b; } struct node { long long lVal, rVal; int l, r; int maxHill, maxLeftHill, maxRightHill; bool descend, ascend; int maxLeftDescend, maxRightAscend; } datas[MAX_NODE]; inline node merge(const node &lhs, const node &rhs) { node res; res.lVal = lhs.lVal, res.rVal = rhs.rVal, res.l = lhs.l, res.r = rhs.r; res.maxHill = max(lhs.maxHill, rhs.maxHill); if (lhs.rVal < rhs.lVal) relax(res.maxHill, lhs.maxRightAscend + rhs.maxLeftHill); else if (lhs.rVal > rhs.lVal) relax(res.maxHill, lhs.maxRightHill + rhs.maxLeftDescend); res.maxLeftHill = lhs.maxLeftHill; if (lhs.r - lhs.l == lhs.maxLeftHill) { if (lhs.ascend) { if (lhs.rVal < rhs.lVal) relax(res.maxLeftHill, lhs.maxLeftHill + rhs.maxLeftHill); else if (lhs.rVal > rhs.lVal) relax(res.maxLeftHill, lhs.maxLeftHill + rhs.maxLeftDescend); } else { if (lhs.rVal > rhs.lVal) relax(res.maxLeftHill, lhs.maxLeftHill + rhs.maxLeftDescend); } } res.maxRightHill = rhs.maxRightHill; if (rhs.r - rhs.l == rhs.maxRightHill) { if (rhs.descend) { if (rhs.lVal < lhs.rVal) relax(res.maxRightHill, rhs.maxRightHill + lhs.maxRightHill); else if (rhs.lVal > lhs.rVal) relax(res.maxRightHill, lhs.maxRightAscend + rhs.maxRightHill); } else { if (rhs.lVal > lhs.rVal) relax(res.maxRightHill, lhs.maxRightAscend + rhs.maxRightHill); } } res.descend = false; if (lhs.descend && rhs.descend && lhs.rVal > rhs.lVal) res.descend = true; res.ascend = false; if (lhs.ascend && rhs.ascend && lhs.rVal < rhs.lVal) res.ascend = true; res.maxLeftDescend = lhs.maxLeftDescend; if (lhs.descend && lhs.rVal > rhs.lVal) relax(res.maxLeftDescend, lhs.maxLeftDescend + rhs.maxLeftDescend); res.maxRightAscend = rhs.maxRightAscend; if (rhs.ascend && rhs.lVal > lhs.rVal) relax(res.maxRightAscend, lhs.maxRightAscend + rhs.maxRightAscend); return res; } long long addV[MAX_NODE]; inline void build(int o, int l, int r) { if (r - l == 1) { datas[o].lVal = datas[o].rVal = a[l]; datas[o].l = l, datas[o].r = r; datas[o].maxHill = datas[o].maxLeftHill = datas[o].maxRightHill = 1; datas[o].ascend = datas[o].descend = true; datas[o].maxLeftDescend = datas[o].maxRightAscend = 1; } else { build((((o) << 1) + 1), l, (((l) + (r)) >> 1)), build((((o) << 1) + 2), (((l) + (r)) >> 1), r); datas[o] = merge(datas[(((o) << 1) + 1)], datas[(((o) << 1) + 2)]); } } inline void giveTag(int o, long long v) { addV[o] += v; datas[o].lVal += v, datas[o].rVal += v; } inline void pushDown(int o) { if (addV[o]) { giveTag((((o) << 1) + 1), addV[o]); giveTag((((o) << 1) + 2), addV[o]); addV[o] = 0; } } inline void modify(int o, int l, int r, int a, int b, int d) { if (l >= a && r <= b) giveTag(o, d); else { pushDown(o); if ((((l) + (r)) >> 1) > a) modify((((o) << 1) + 1), l, (((l) + (r)) >> 1), a, b, d); if ((((l) + (r)) >> 1) < b) modify((((o) << 1) + 2), (((l) + (r)) >> 1), r, a, b, d); datas[o] = merge(datas[(((o) << 1) + 1)], datas[(((o) << 1) + 2)]); } } inline void print(int o, int l, int r) { if (r - l == 1) printf("%d\n", datas[o].lVal); else { pushDown(o); print((((o) << 1) + 1), l, (((l) + (r)) >> 1)), print((((o) << 1) + 2), (((l) + (r)) >> 1), r); datas[o] = merge(datas[(((o) << 1) + 1)], datas[(((o) << 1) + 2)]); } } } // namespace segment_tree using namespace segment_tree; int main() { n = readInt(); for (int i = 0; i < n; ++i) a[i] = readInt(); build(0, 0, n); m = readInt(); for (int i = 0, l, r, v; i < m; ++i) { l = readInt() - 1, r = readInt(), v = readInt(); modify(0, 0, n, l, r, v); printf("%d\n", datas[0].maxHill); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Jialin Ouyang ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; QuickScanner in = new QuickScanner(inputStream); QuickWriter out = new QuickWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { static int OFFSET = 1000000000; public void solve(int testNumber, QuickScanner in, QuickWriter out) { int n = in.nextInt(); out.println("YES"); for (int i = 0; i < n; ++i) { int x = (in.nextInt() + OFFSET) & 1; int y = (in.nextInt() + OFFSET) & 1; in.nextInt(); in.nextInt(); out.println(((x << 1) | y) + 1); } } } static class QuickWriter { private final PrintWriter writer; public QuickWriter(OutputStream outputStream) { this.writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public QuickWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; ++i) { if (i > 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.print('\n'); } public void close() { writer.close(); } } static class QuickScanner { private static final int BUFFER_SIZE = 1024; private InputStream stream; private byte[] buffer; private int currentPosition; private int numberOfChars; public QuickScanner(InputStream stream) { this.stream = stream; this.buffer = new byte[BUFFER_SIZE]; this.currentPosition = 0; this.numberOfChars = 0; } public int nextInt() { int c = nextNonSpaceChar(); boolean positive = true; if (c == '-') { positive = false; c = nextChar(); } int res = 0; do { if (c < '0' || '9' < c) throw new RuntimeException(); res = res * 10 + c - '0'; c = nextChar(); } while (!isSpaceChar(c)); return positive ? res : -res; } public int nextNonSpaceChar() { int res = nextChar(); for (; isSpaceChar(res) || res < 0; res = nextChar()) ; return res; } public int nextChar() { if (numberOfChars == -1) { throw new RuntimeException(); } if (currentPosition >= numberOfChars) { currentPosition = 0; try { numberOfChars = stream.read(buffer); } catch (Exception e) { throw new RuntimeException(e); } if (numberOfChars <= 0) { return -1; } } return buffer[currentPosition++]; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\t' || isEndOfLineChar(c); } public boolean isEndOfLineChar(int c) { return c == '\n' || c == '\r' || c < 0; } } }
JAVA
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); printf("YES\n"); for (int i = 0; i < n; i++) { int arr[4]; for (int j = 0; j < 4; j++) { scanf("%d", &arr[j]); } int x = arr[0] % 2; if (x < 0) x = 1; int y = arr[1] % 2; if (y < 0) y = 1; printf("%d\n", (2 * x) + (y) + 1); } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int n, i, x, y, a, b; int main() { cout << "YES\n"; cin >> n; for (i = 1; i <= n; i++) { cin >> x >> y >> a >> b; if (x % 2 == 0 and y % 2 == 0) cout << "1\n"; else if (x % 2 == 0 and (y % 2 == 1 or y % 2 == -1)) cout << "2\n"; else if ((x % 2 == 1 or x % 2 == -1) and y % 2 == 0) cout << "3\n"; else if ((x % 2 == 1 or x % 2 == -1) and (y % 2 == 1 or y % 2 == -1)) cout << "4\n"; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; cout << "YES\n"; int a, b, c; while (n--) { cin >> a >> b >> c >> c; cout << (2 * (a & 1) + (b & 1) + 1) << "\n"; } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; long long n; void solve() { cin >> n; long long x, y, a, b; cout << "YES\n"; for (long long i = 1; i <= (n); ++i) { cin >> x >> y >> a >> b; cout << 2 * (x & 1) + (y & 1) + 1; cout << '\n'; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); solve(); return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
'''plan noticed that if both upperle ''' from sys import stdin, stdout # n = int(stdin.readline().rstrip()) # n = int(input()) all_lines = stdin.read().split('\n') stdout.write('YES\n') for line in all_lines[1:-1]: x1, y1, x2, y2 = (int(x) % 2 for x in line.split()) num = 2 * x2 + y2 + 1 # stdout.write(str(x2) + ' ' + str(y2) + '\n') print(num) #stdout.flush() #exit() # for i in range(n): # coordinates.append([int(x) % 2 for x in input().split()]) # for i in range(n): # coordinates.append([int(x) % 2 for x in stdin.readline().rstrip().split()]) # stdout.write('YES\n') # for coordinate in coordinates: # x1, y1, x2, y2 = coordinate # stdout.write(str(2 * x2 + y2 + 1) + '\n')
PYTHON3
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int i; int x1, y1, x2, y2; int x; printf("YES\n"); for (i = 1; i <= n; i++) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 % 2 == 0) { if (y1 % 2 == 0) x = 1; else x = 2; } else { if (y1 % 2 == 0) x = 3; else x = 4; } printf("%d\n", x); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { if (!b) return a; return gcd(b, a % b); } int main() { int n; printf("YES\n"); scanf("%d", &n); for (int i = 0; i < n; i++) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); printf("%d\n", ((4 + 2 * (x1 % 2) + (y1 % 2)) % 4) + 1); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n, x1, x2, y1, y2; scanf("%d", &n); puts("YES"); for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); printf("%d\n", (((x1 & 1) << 1) + (y1 & 1) + 1)); } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; cout << "YES" << endl; for (int x1, x2, y1, y2, i = 0; i < n; i++) { cin >> x1 >> y1 >> x2 >> y2; int x = min(x1, x2); int y = min(y1, y2); cout << 1 + abs(x) % 2 + 2 * (abs(y) % 2) << endl; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 0.0000000001; const int INF = 0x3f3f3f3f; const int N = 1000000 + 100; struct node { int x1, y1, x2, y2; } a[N]; int main() { int n; while (scanf("%d", &n) != EOF) { cout << "YES" << endl; for (int i = 1; i <= n; i++) { cin >> a[i].x1 >> a[i].y1 >> a[i].x2 >> a[i].y2; printf("%d\n", 1 + 2 * (abs(a[i].x1) % 2) + abs(a[i].y1) % 2); } } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; long long arr[1000009]; long long brr[1000009]; long long idx[1000009]; long long st[1000009]; int main() { long long n; cin >> n; cout << "YES\n"; while (n--) { int x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; cout << 2 * (abs(x1) % 2) + (abs(y1) % 2) + 1 << endl; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n, i, a, b, x, y, ans = 0; scanf("%d", &n); printf("YES\n"); for (int i = 1; i <= n; i++) { scanf("%d %d %d %d", &a, &b, &x, &y); x = min(a, x); y = min(y, b); ans = abs(x) % 2; ans = ans * 2 + (abs(y) % 2); printf("%d\n", ans + 1); } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x1, y1, x2, y2; int x; cout << "YES" << endl; for (int i = 0; i < n; i++) { cin >> x1 >> y1 >> x2 >> y2; if (x1 % 2 == 0) { if (y1 % 2 == 0) x = 1; else x = 2; } else { if (y1 % 2 == 0) x = 3; else x = 4; } cout << x << endl; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
import java.io.*; import java.math.BigInteger; import java.util.*; public class Template implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); void init() throws FileNotFoundException { try { in = new BufferedReader(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { try { tok = new StringTokenizer(in.readLine(), " :"); } catch (Exception e) { return null; } } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } int[] readIntArray(int size) throws IOException { int[] res = new int[size]; for (int i = 0; i < size; i++) { res[i] = readInt(); } return res; } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } <T> List<T>[] createGraphList(int size) { List<T>[] list = new List[size]; for (int i = 0; i < size; i++) { list[i] = new ArrayList<>(); } return list; } public static void main(String[] args) { new Template().run(); // new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start(); } long timeBegin, timeEnd; void time() { timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); } long memoryTotal, memoryFree; void memory() { memoryFree = Runtime.getRuntime().freeMemory(); System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10) + " KB"); } public void run() { try { timeBegin = System.currentTimeMillis(); memoryTotal = Runtime.getRuntime().freeMemory(); init(); solve(); out.close(); if (System.getProperty("ONLINE_JUDGE") == null) { time(); memory(); } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void solve() throws IOException { int n = readInt(); out.println("YES"); int DIFF = (int) 1e9 + 100; while (n-- > 0) { int x = readInt() + DIFF; int y = readInt() + DIFF; x = Math.min(x, readInt() + DIFF) % 2; y = Math.min(y, readInt() + DIFF) % 2; out.println(2 * x + y + 1); } } }
JAVA
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; typedef struct Rect { int x1, y1, x2, y2; } Rect; class TaskD { private: inline bool overlap(const Rect& A, const Rect& B) { return (A.x1 <= B.x2 && A.x2 >= B.x1 && A.y1 < B.y2 && A.y2 > B.y1) || (A.x1 < B.x2 && A.x2 > B.x1 && A.y1 <= B.y2 && A.y2 >= B.y1); } bool dfs(int u, const vector<vector<int>>& g, vector<int>& color) { int col = 1; for (int i = 0; i < (int)g[u].size(); ++i) { int v = g[u][i]; if (color[v] == col) { ++col; } } if (col == 5) { return false; } color[u] = col; for (int i = 0; i < (int)g[u].size(); ++i) { int v = g[u][i]; if (color[v] == 0) { if (!dfs(v, g, color)) { return false; } } } return true; } public: void solve(istream& in, ostream& out) { int n; in >> n; vector<Rect> rects(n); for (int i = 0; i < n; ++i) { in >> rects[i].x1 >> rects[i].y1 >> rects[i].x2 >> rects[i].y2; } out << "YES\n"; for (int i = 0; i < n; ++i) { if ((rects[i].x1 % 2 + 2) % 2 == 0 && (rects[i].y1 % 2 + 2) % 2 == 0) { out << "1\n"; } else if ((rects[i].x1 % 2 + 2) % 2 == 1 && (rects[i].y1 % 2 + 2) % 2 == 0) { out << "2\n"; } else if ((rects[i].x1 % 2 + 2) % 2 == 1 && (rects[i].y1 % 2 + 2) % 2 == 1) { out << "3\n"; } else if ((rects[i].x1 % 2 + 2) % 2 == 0 && (rects[i].y1 % 2 + 2) % 2 == 1) { out << "4\n"; } } } }; int main() { std::ios::sync_with_stdio(false); TaskD solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int i, n; cin >> n; vector<int> A(n); for (i = 0; i < n; ++i) { int x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; int X = abs(x1), Y = abs(y1); A[i] = 2 * (X % 2) + Y % 2 + 1; } cout << "YES" << endl; for (i = 0; i < n; ++i) cout << A[i] << endl; return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void RI() {} template <typename... T> void RI(int& head, T&... tail) { head = read(); RI(tail...); } int n; int main() { RI(n); puts("YES"); while (n--) { int x1, y1, x2, y2; RI(x1, y1, x2, y2); printf("%d\n", 2 * (x1 & 1) + (y1 & 1) + 1); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; puts("YES"); for (int i = 1; i <= n; i++) { int a, b, c, d; cin >> a >> b >> c >> d; c = abs(c); d = abs(d); printf("%d\n", 1 + d % 2 + 2 * (c % 2)); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { long long n, x1, y1, x2, y2; cout << "YES" << endl; cin >> n; for (int i = 0; i < n; i++) { cin >> x1 >> y1 >> x2 >> y2; if ((x1 % 2 + 2) % 2 == 0 && (y1 % 2 + 2) % 2 == 0) { cout << "1" << endl; } else if ((x1 % 2 + 2) % 2 == 0 && (y1 % 2 + 2) % 2 == 1) { cout << "4" << endl; } else if ((x1 % 2 + 2) % 2 == 1 && (y1 % 2 + 2) % 2 == 1) { cout << "3" << endl; } else { cout << "2" << endl; } } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; const long long maxN = 1e6 + 5; const long long inf = 1e10; const long long mod = 1e9 + 7; long long n; long long x[maxN], y[maxN], a, b; int main() { ios_base::sync_with_stdio(0); cin >> n; for (long long i = 1; i <= n; i++) { cin >> x[i] >> y[i] >> a >> b; } cout << "YES" << endl; for (long long i = 1; i <= n; i++) { if (x[i] & 1 && y[i] & 1) cout << 1 << endl; else if (x[i] & 1 && !(y[i] & 1)) cout << 2 << endl; else if (!(x[i] & 1) && y[i] & 1) cout << 3 << endl; else cout << 4 << endl; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
n = int(input()) print("YES") for i in range(n): x1, y1, x2, y2 = input().split() x1 = int(x1) y1 = int(y1) print((x1 % 2) * 2 + (y1 % 2) + 1)
PYTHON3
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int n, x, y, k; int main() { cin >> n; cout << "YES\n"; for (int i = 1; i <= n; ++i) { cin >> x >> y; x += 1e9; y += 1e9; if (x % 2 == 0 && y % 2 == 0) cout << 1 << "\n"; else if (x % 2 == 0 && y % 2 == 1) cout << 2 << "\n"; if (x % 2 == 1 && y % 2 == 0) cout << 3 << "\n"; if (x % 2 == 1 && y % 2 == 1) cout << 4 << "\n"; cin >> x >> y; } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; void RI() {} template <typename... T> void RI(int& head, T&... tail) { scanf("%d", &head); RI(tail...); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; RI(n); cout << "YES" << endl; while (n--) { int x1, y1, x2, y2; RI(x1, y1, x2, y2); cout << (((x1 & 1) << 1) + (y1 & 1) + 1) << endl; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> int n, col[500005]; int main() { scanf("%d", &n); for (int a, b, c, d, i = 1; i <= n; i++) { scanf("%d %d %d %d", &a, &b, &c, &d); if (a < 0) a = -a; if (b < 0) b = -b; if (c < 0) c = -c; if (d < 0) d = -d; int p = (a & 1); int q = (b & 1); p = p * 2 + q; col[i] = p; } puts("YES"); for (int i = 1; i <= n; i++) { printf("%d\n", col[i] + 1); } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 1; void solve() { puts("YES"); int n; scanf("%d", &n); int x1, y1, x2, y2; for (int i = 1; i <= n; i++) { scanf("%d", &x1); scanf("%d", &y1); scanf("%d", &x2); scanf("%d", &y2); printf("%d\n", (2 * (abs(x1) % 2) + (abs(y1) % 2)) % 4 + 1); } } int main() { int t = 1; while (t--) { solve(); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, x1, x2, y1, y2; scanf("%lld", &n); printf("YES\n"); while (n--) { scanf("%lld%lld", &x1, &y1); scanf("%lld%lld", &x2, &y2); if (x1 < 0) x1 = -(x1); if (y1 < 0) y1 = -(y1); if (x1 % 2 == 0 && y1 % 2 == 0) printf("1\n"); else if (x1 % 2 == 0 && y1 % 2 == 1) printf("2\n"); else if (x1 % 2 == 1 && y1 % 2 == 0) printf("3\n"); else printf("4\n"); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { printf("YES\n"); int n, a, b, c, d; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d%d%d", &a, &b, &c, &d); if (!(a % 2) && !(b % 2)) printf("3\n"); else if (!(a % 2) && (b % 2)) printf("4\n"); else if ((a % 2) && !(b % 2)) printf("2\n"); else printf("1\n"); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeSet; public class C { static ArrayList<Integer> [] adj; public static void main(String[] args)throws Throwable { MyScanner sc=new MyScanner(); PrintWriter pw=new PrintWriter(System.out); int n=sc.nextInt(); pw.println("YES"); while(n-->0){ int x=(sc.nextInt()%2+2)%2; int y=(sc.nextInt()%2+2)%2; sc.nextInt();sc.nextInt(); pw.println(2*x+y+1); } pw.flush(); pw.close(); } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() {br = new BufferedReader(new InputStreamReader(System.in));} String next() {while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch (IOException e) {e.printStackTrace();}} return st.nextToken();} int nextInt() {return Integer.parseInt(next());} long nextLong() {return Long.parseLong(next());} double nextDouble() {return Double.parseDouble(next());} String nextLine(){String str = ""; try {str = br.readLine();} catch (IOException e) {e.printStackTrace();} return str;} } }
JAVA
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 500005; int ans[MAXN]; int main() { ios_base::sync_with_stdio(false); long long n, x1, y1, x2, y2; cin >> n; for (int i = 0; i < n; i++) { cin >> x1 >> y1 >> x2 >> y2; x1 += (long long)(1e9); y1 += (long long)(1e9); ans[i] = ((x1 & 1) << 1) + (y1 & 1); } cout << "YES" << endl; for (int i = 0; i < n; i++) cout << ans[i] + 1 << "\n"; return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n, x1, y1, x2, y2; scanf("%d", &n); puts("YES"); for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); x1 %= 2, y1 %= 2; if (x1 && y1) puts("1"); if (x1 && !y1) puts("2"); if (!x1 && y1) puts("3"); if (!x1 && !y1) puts("4"); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; cout << "YES" << "\n"; for (int i = 1; i <= n; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; cout << (12 + 2 * (x1 % 2) + (y1 % 2)) % 4 + 1 << "\n"; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int n, x, y; int main() { cin >> n; cout << "YES\n"; for (int i = 1; i <= n; i++) { cin >> x >> y >> x >> y; x += (int)(1e9 + 7); y += (int)(1e9 + 7); cout << 1 + x % 2 + 2 * (y % 2) << endl; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int x[N], y[N]; int main() { int n, a, b; while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; ++i) scanf("%d %d %d %d", &x[i], &y[i], &a, &b); puts("YES"); for (int i = 1; i <= n; ++i) { if (x[i] & 1 && y[i] & 1) puts("1"); else if (x[i] & 1 && !(y[i] & 1)) puts("2"); else if (!(x[i] & 1) && y[i] & 1) puts("3"); else puts("4"); } } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; long long binpow(long long a, long long m) { long long product = 1; long long mult = a; while (m > 0) { if (m % 2 == 1) { product = (product % 1000000007 * mult % 1000000007) % 1000000007; } m = m / 2; mult = (mult % 1000000007 * mult % 1000000007) % 1000000007; } product = product % 1000000007; return product; } int main() { int n; cin >> n; cout << "YES" << endl; for (int i = 0; i < n; i++) { int x[4]; for (int j = 0; j < 4; j++) { cin >> x[j]; } if (x[0] % 2 == 0 && x[1] % 2 == 0) cout << 1 << endl; else if (abs(x[0] % 2) == 1 && x[1] % 2 == 0) cout << 2 << endl; else if (x[0] % 2 == 0 && abs(x[1] % 2) == 1) cout << 3 << endl; else cout << 4 << endl; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
import java.io.*; import java.math.*; import java.util.*; public class Main { public static void solve(FastIO io) { int N = io.nextInt(); io.println("YES"); for (int i = 0; i < N; ++i) { int X = io.nextInt(); int Y = io.nextInt(); io.nextIntArray(2); io.println(1 + (((X & 1) << 1) | (Y & 1))); } } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public 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 int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int 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 int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.flush(); } }
JAVA
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
'''plan noticed that if both upperle ''' from sys import stdin, stdout # n = int(stdin.readline().rstrip()) # n = int(input()) all_lines = stdin.read().split('\n') stdout.write('YES\n') for line in all_lines[1:-1]: x1, y1, x2, y2 = (int(x) % 2 for x in line.split()) num = 2 * x2 + y2 + 1 # stdout.write(str(x2) + ' ' + str(y2) + '\n') stdout.write(str(num) + '\n') #stdout.flush() #exit() # for i in range(n): # coordinates.append([int(x) % 2 for x in input().split()]) # for i in range(n): # coordinates.append([int(x) % 2 for x in stdin.readline().rstrip().split()]) # stdout.write('YES\n') # for coordinate in coordinates: # x1, y1, x2, y2 = coordinate # stdout.write(str(2 * x2 + y2 + 1) + '\n')
PYTHON3
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); ProblemDTimofeyAndRectangles solver = new ProblemDTimofeyAndRectangles(); solver.solve(1, in, out); out.close(); } static class ProblemDTimofeyAndRectangles { public void solve(int testNumber, ScanReader in, PrintWriter out) { int n = in.scanInt(); out.println("YES"); for (int i = 0; i < n; i++) { long x1 = in.scanLong(); long y1 = in.scanLong(); long x2 = in.scanLong(); long y2 = in.scanLong(); out.println(((2 * (x1 % 2)) + (y1 % 2) + 12) % 4 + 1); } } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (index >= total) { index = 0; try { total = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (total <= 0) return -1; } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } public long scanLong() { long integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } } }
JAVA
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int i, x, y, j, n, k, m, t, f, l, r; cin >> n; cout << "YES" << endl; for (l = (1); l <= (n); l++) { cin >> x >> y >> i >> j; if ((x & 1) && (y & 1)) cout << 1 << endl; else if ((x & 1) && !(y & 1)) cout << 2 << endl; else if (!(x & 1) && !(y & 1)) cout << 3 << endl; else cout << 4 << endl; } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int ans[500010] = {}; int n; int a, b, c, d; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d%d%d", &a, &b, &c, &d); a = abs(a); b = abs(b); if (a % 2 == 0 && b % 2 == 0) { ans[i] = 1; } if (a % 2 == 0 && b % 2 == 1) { ans[i] = 2; } if (a % 2 == 1 && b % 2 == 0) { ans[i] = 3; } if (a % 2 == 1 && b % 2 == 1) { ans[i] = 4; } } cout << "YES" << endl; for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; namespace INPUT { const int L = 1 << 15; char _buf[L], *S, *T, c; char _gc() { if (S == T) { T = (S = _buf) + fread(_buf, 1, L, stdin); if (S == T) return EOF; } return *S++; } void readi(int &X) { for (c = _gc(); c < '0' || c > '9'; c = _gc()) ; X = c & 15; for (c = _gc(); c >= '0' && c <= '9'; X = X * 10 + (c & 15), c = _gc()) ; } } // namespace INPUT using INPUT::readi; int N; int l, r, u, d; int main() { readi(N); puts("YES"); for (int i = 1; i <= N; ++i) readi(l), readi(u), readi(r), readi(d), printf("%d\n", ((l & 1) << 1) + (u & 1) + 1); return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
import java.math.*; import java.util.*; public class itachi { public static void main(String[] args){ Scanner in = new Scanner(System.in); int maxn = 100005; int n = in.nextInt(); System.out.println("YES"); for (int i = 1; i <= n; i++){ int x1 = in.nextInt(), y1 = in.nextInt(), x2 = in.nextInt(), y2 = in.nextInt(); int ans = (2 * (x1 % 2) + y1 % 2) + 12; System.out.println(ans % 4 + 1); } } }
JAVA
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
n = int (input()) print ('YES') for i in range (0, n): a, b, c, d = [int(x) for x in (input().split())] print (1 + a%2 + (b%2) * 2)
PYTHON3
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
from sys import stdin t = int(stdin.readline()) print 'YES' for _ in xrange(t): x1,y1,x2,y2 = map(int,stdin.readline().split()) x = abs(min(x1,x2)) y = abs(min(y1,y2)) print 1 + 2*(x&1) + (y&1)
PYTHON
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class B_Round_395_Div1 { public static long MOD = 1000000007; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); out.println("YES"); for(int i = 0; i < n; i++){ int x1 = in.nextInt(); int y1 = in.nextInt(); int x2 = in.nextInt(); int y2 = in.nextInt(); int v = ((Math.abs(x1) % 2) + (Math.abs(y1) % 2)*2) + 1; out.println(v); } out.close(); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new // BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new // FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
JAVA
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7, mod = 1e9 + 7, inf = 1e5 + 7; const long long linf = (long long)1e18 + 7; const long double eps = 1e-15, pi = 3.141592; const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; int t; inline int get(int x, int y) { if (x % 2 && y % 2) return 1; if (x % 2 && y % 2 == 0) return 2; if (x % 2 == 0 && y % 2) return 3; if (x % 2 == 0 && y % 2 == 0) return 4; } int main() { cin >> t; cout << "YES\n"; while (t--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; cout << get(x1, y1) << '\n'; } exit(0); }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } int main() { int n; printf("YES\n"); scanf("%d", &n); for (int i = 0; i < n; i++) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); printf("%d\n", ((12 + 2 * (x1 % 2) + (y1 % 2)) % 4) + 1); } return 0; }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
p = int(input()) print("YES") for i in range(p): a, b, c, d = [abs(int(i)) for i in input().split()] if a % 2 == 0: print("1" if b % 2 == 0 else "2") else: print("3" if b % 2 == 0 else "4")
PYTHON3
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, x1, y1; cin >> n; cout << "YES" << endl; while (n--) { cin >> x >> y >> x1 >> y1; cout << (x & 1 ? 0 : 1) + (y & 1 ? 0 : 2) + 1 << endl; } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << "YES" << endl; for (int i = 0; i < n; i++) { int x, y, xx, yy; cin >> x >> y >> xx >> yy; cout << (x & 1) + 2 * (y & 1) + 1 << endl; } }
CPP
764_D. Timofey and rectangles
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other. Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible. Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length <image> The picture corresponds to the first example Input The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of rectangles. n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 ≀ x1 < x2 ≀ 109, - 109 ≀ y1 < y2 ≀ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle. It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other. Output Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color. Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≀ ci ≀ 4) β€” the color of i-th rectangle. Example Input 8 0 0 5 3 2 -1 5 0 -3 -4 2 -1 -1 -1 2 0 -3 0 0 5 5 2 10 3 7 -3 10 2 4 -2 7 -1 Output YES 1 2 2 3 2 2 4 1
2
10
function main() { var all = [[2,1], [3,4]] var n = Number(readline()); print("YES"); for(var i=0;i<n;i++){ var line = readline().split(' ').map(Number); var x = Math.abs(line[0]); var y = Math.abs(line[1]); print(all[x%2][y%2]); } } main()
JAVA