### ch_egor's blog

By ch_egor, 6 years ago, translation,

If you notice typos or errors, please send a private message.

Author: cdkrot
Developer: ch_egor

You just needed to implement the actions described in statement.

If you solution failed, then you probably forgot to use 64-bit integers or made some small mistake.

Correct solution:

C++ code
Python code

Author: ch_egor
Developer: ch_egor

We need to sort an array with strange operations — namely, to swap elements with even and odd indices in subarray of even length. Note that we can change the 2 neighboring elements, simply doing our exchange action for subarray of length 2 containing these elements. Also, note that n ≤ 100, and it is permission to do 20 000 actions, therefore, we can write any quadratic sort, which changes the neighboring elements in each iteration (bubble sort for example).
Complexity O(n2)

C++ code
Python code

Bonus: Is anyone able to search the shortest solution of this problem? If yes, what complexity?

Author: cdkrot
Developer: themikemikovi4

In this problem we use the septimal number system. It is a very important limitation. Let's count how many digits are showed on the watch display and call it cnt. If cnt more than 7, the answer is clearly 0 (because of pigeonhole principle). If cnt is not greater than 7, then you can just bruteforces all cases.
Depending on the implementation it will be O(BASEBASEBASE), O(BASEBASE) or O(BASEBASE!), where BASE = 7. Actually the most simple implementation is just to cycle between all posible hour:minute combinations and check them.
In the worst case, it will work in O(BASEBASEBASE).

C++ code
Python code

Bonus: Suppose the base is not fixed. Solve a problem with n ≤ 109, m ≤ 109, BASE ≤ 109. Can you do it in ?

Author: cdkrot
Developers: ch_egor, cdkrot

There are many possible approaches.

Solution by cdkrot:

Look at the all candidates for the centroid of the vertices v subtree. The size of centroid subtree must be at least of the vertex v subtree size. (If it isn't, then after cutting the upper part will have too big size)

Choose the vertex with the smallest subtree size satisfying the constraint above. Let's prove, that this vertex is centroid indeed. If it isn't, then after cutting some part will have subtree size greater than of subtree size of query vertex. It isn't upper part (because of constraint above), it is one of our sons. Ouch, it's subtree less than of selected vertex, and it's still greater than of subtree size of query vertex. Contradiction.

So we find a centroid.
We write the euler tour of tree and we will use a 2D segment tree in order to search for a vertex quickly.
Complexity

C++ code

You can consider all answers by one in dfs using this idea. Use std::set for pair (subtree size, vertex number) and at each vertex merge sets obtained from children. (Also known as "merging sets" idea)
Thus we get the answers for all vertex and we need only output answers for queries.
Complexity

C++ code

Solution by ch_egor:
Solve it for all subtrees. We can solve the problem for some subtree, after solving the problem for all of it's children. Let's choose the heaviest child. Note that the centroid of the child after a certain lifting goes into our centroid. Let's model the lifting.
Thus we get the answers for all vertex and we need only output answers for queries.
Complexity O(n + q)

C++ code

Author: cdkrot
Developer: cdkrot

Let's say few words about ternary search. It works correctly, but too slow.
It's complexity is , where n = 105, and v = 1018.
Don't use it.

Solution.
1) Let's make binary search on answer
2) Consider areas of "good" points (with dist  ≤ mid) for each source point.
3) Intersect those areas and check for solution.

This area can be decsribed by following inequalities:
(You can achieve them if you expand modules in inequality "manhattan distance <= const")

.. ≤ x + y + z ≤ ..

.. ≤  - x + y + z ≤ ..

.. ≤ x - y + z ≤ ..

.. ≤ x + y - z ≤ ..

.. denote some constants.

If you intersect set of such system, you will get system of the same form, so let's learn how to solve such system.

Let's replace some variables:

a =  - x + y + z

b = x - y + z

c = x + y - z

Then:

x + y + z = a + b + c

x = (b + c) / 2

y = (a + c) / 2

z = (a + b) / 2

Now the system transforms into:

.. ≤ a ≤ ..

.. ≤ b ≤ ..

.. ≤ c ≤ ..

.. ≤ a + b + c ≤ ..

We need to check if the system has solution in integers, also the numbers a, b, c must be of the same parity (have equal remainder after division by 2). (This is required for x, y, z to be integers too)

Let's get rid of parity constraint. Bruteforce the parity of a, b, c (two cases)

Make following replacement: a = 2a' + r, b = 2b' + r, c = 2c' + r, where r = 0 or r = 1. Substitute in equations above, simplify, and gain the same system for a', b', c' without parity constraint.

And now the system can be solved greedy (At first set a, b, c with minimum, and then raise slowly if necessary).

Total complexity is .

C++ code

Bonus. This task can be solved in a lot of modifications, for example euclidian distance instead of manhattan, 2d instead of 3d, floats instead of integers. How to solve this varations?

Authors: platypus179, Endagorion
Developer: platypus179

Let's solve this problem with scanline. Go through all rows from left to right and maintain the array in which in j index we will store the number of points in a square with bottom left coordinate (i, j), where i is current row of scanline.

This takes O(MAXCORD2) time. Note that the set of squares that contain some of the shaded points is not very large, namely — if the point has coordinates (x, y), then the set of left bottom corners of square is defined as {(a, b)|x - k + 1 <  = a <  = x, y - k + 1 <  = b <  = y}. Let's consider each point (x, y) as the 2 events:
Add one to the all elements with indexes from y - k + 1 to y on the row x - k + 1 and take one at the same interval on the row x + 1. How to calculate answer? Suppose we update the value of a cell on the row a, and before it was updated the value x on the row b. Let add to the answer for the number of squares containing x points value a - b. We can implement the addition of the segment directly and have O(nk) for processing all the events that fit in time limit. To get rid of O(MAXCORD) memory, we need to write all interested in the coordinates before processing events (them no more than nk) and reduce the coordinates in the events. It takes time and O(nk) memory. Now we can execute the previous point in O(nk) memory.
Complexity is time and O(nk) memory.

C++ code

Bonus: time and O(n) memory.

C++ code

Authors: cdkrot, GlebsHP
Developers: ch_egor, cdkrot

Unfortunately, due to my, cdkrot, failure, it was possible to get Ac with O(nm) solution, I apologise for that.

We propose following solution:
Let's solve task with divide & conquer. At first let's lift l to the first index, where s was mentioned And lower the r to the last index, where t was mentioned. This will not affect answers, but will make implementation much more easy.

Let's look on all queries. For each query consider it's location relative to centre of edges array. If it's stricly on the left half or on the right half, then solve recursively (You need to implement function like solve(requests, l, r)).

How to answer on query, if it contains the centre? Let's precalculate two dp's:

dpr[i] =  bitset of vertices you can from to the vi or ui with l = m and r = i.

dpl[i] =  bitset of vertices you can go to starting from vi or ui with l = i and r = m - 1

vi and ui are vertices of i'th edge.

Using this dp the answer is yes if and ony if dpl[l][u] = true and dpr[r][u] = true for some u.

All above can be implemented using bitwise operations.
So the time is

C++ code

• +85

 » 6 years ago, # | ← Rev. 2 →   +9 In 685D - Kay and Eternity we can achieve shorter implementation using set from STL. My solution is similar to the one from editorial, but I have set with numbers +1 and -1, instead of a segment tree. I use the fact that iterating over set takes O(size), not O(size·log). Check details in my code. To store +1 and -1 sorted by y, I represented +1 by 2y and -1 by 2y + 1. This way, I don't need to store pairs, what would be slightly slower (and maybe it would give TLE). code for D#include using namespace std; long long ans[1005 * 1005]; int main() { int n, k; scanf("%d%d", &n, &k); vector> w; for(int i = 0; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); w.push_back(vector{x-k, y, 101}); w.push_back(vector{x, y, 102}); } sort(w.begin(), w.end()); multiset s; int x_prev = -42; for(vector & event : w) { int x = event[0]; if(!s.empty() && x_prev != x) { int mul = x - x_prev; assert(mul > 0); int y_prev = -42; int cnt = 0; for(int y : s) { assert(cnt >= 0); if(cnt) ans[cnt] += (y/2 - y_prev) * mul; if(y%2) --cnt; else ++cnt; y_prev = y / 2; } } int y = event[1]; int type = event[2]; if(type == 101) { s.insert(2 * (y - k)); s.insert(2 * (y) + (y >= 0 ? 1 : -1)); } else { s.erase(s.find(2 * (y - k))); s.erase(s.find(2 * (y) + (y >= 0 ? 1 : -1))); } x_prev = x; } for(int i = 1; i <= n; ++i) printf("%lld ", ans[i]); puts(""); return 0; } Oh, so O(nm) wasn't intended in 685D - Kay and Eternity. I'm surprised. Constraints and everything looked like it's what organizers wanted. Anyway, it didn't spoil a problem I think (though it made it easier). By the way, did you think that O(nm) won't pass or you didn't come up with this solution?
•  » » 6 years ago, # ^ |   0 We want to cut off solution. On this limits difference in TL near 3. But we write very unoptimal O(nm) solution and didn't know that it can pass.
•  » » » 6 years ago, # ^ |   0 Just wondering what was the difference in time between main solution and nm solution. Was it really at least 4 times?
•  » » » » 6 years ago, # ^ | ← Rev. 2 →   0 It was.It seems, than nm solution runned too slowly because of cache: my code was iterating over first dim of matrix, and it is not good.I was making scanline from left to right, and the right to left is better.Just reversing everything makes it run over second dim of matrix, as in Ac-ed solutions.
•  » » 6 years ago, # ^ |   0 I've written it, but it turned out, that implementation wasn't effecient enough.Actually the most effort was made to cut O(M2 / 32) solution (however it has pretty simple and nice idea beneath it).It was hard to cut it, and this way I accidently opened door to O(nm) solution.
•  » » » 6 years ago, # ^ |   0 Can u share idea behind O(M^2 / 32) solution too?
•  » » » » 6 years ago, # ^ | ← Rev. 3 →   +3 Solution in M2 / 32, for div1.E.Let's create a matrix of N x M size and create a graph node from each cell.{We will not use it in final code, but it is important for explanations}Let's add some edges in this graph:* (w, i) -> (w, i + 1) for all i, w * (vi, i) -> (ui, i) for all i * (ui, i) -> (vi, i) for all i vi, ui — are endpoints of i-th edge.How to answer on query L, R, S->T? We just need to check, that there is a path from (S, L) to (T, R)However N x M table is too big for us, let's shrink it.Take note, that it is enough to check reachability within (S, L') and (T, R'), where L' — smallest num, with L' ≥ L and (UL' = S or VL' = S) Аnd R' — been the biggest number, with R' ≤ R and (UR' = T or VR' = T)This way almost all vertexes can be eliminated from graph, they simply will not be used. It is enough to create a new copy of vertex only when it was mentioned in new edge. Also, we were adding this edges before: (ui, i) <-> (vi, i). We will compress this two vertexes into one, since the relations of reachability are same for themAfter following operation we have acyclic graph with M vertexes and O(M) edges.We can calculate the following dp on it:dp[v] — is bitset of reachable vertexes (vertexes of our supplementary graph).And so complexity is M2 / 32 for both memory and time.However memory complexity can be boosted (thanks to ch_egor):You can go with scanline over edges, answering queries in certain places.Quite often some state of dp will be not used till the end, so it can be destructed to rescue memory for next ones.My code (but without memory optimization) Spoiler// Copyright (C) 2016 Sayutin Dmitry. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; version 3 // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; If not, see . #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using std::cin; using std::cout; using std::cerr; using std::vector; using std::map; using std::array; using std::set; using std::string; using std::pair; using std::make_pair; using std::min; using std::abs; using std::max; using std::sort; using std::generate; using std::min_element; using std::max_element; template T input() { T res; cin >> res; return res; } const size_t MAX_N = ..; const size_t MAX_M = ..; template struct supervec { public: T data[sz]; size_t used = 0; T& operator[](size_t id) { return data[id]; } void push_back(T val) { data[used++] = val; } T* begin() { return data; } T* end() { return data + used; } }; supervec graph[MAX_M]; vector last[MAX_N]; std::bitset dp[MAX_M]; int main() { std::iostream::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); size_t n, m, q; cin >> n >> m >> q; assert(n <= MAX_N and m <= MAX_M); for (size_t i = 0; i != m; ++i) { size_t v, u; cin >> v >> u; --v, --u; if (not last[v].empty()) graph[i].push_back(last[v].back()); if (not last[u].empty()) graph[i].push_back(last[u].back()); last[v].push_back(i); last[u].push_back(i); } for (size_t i = 0; i != m; ++i) { for (size_t u: graph[i]) dp[i] or_eq dp[u]; dp[i][i] = true; } for (size_t i = 0; i != q; ++i) { size_t L, R, S, T; cin >> L >> R >> S >> T; --L, --R, --S, --T; bool res = false; std::vector::iterator it1, it2; it1 = std::lower_bound(last[S].begin(), last[S].end(), L); if (it1 == last[S].end()) goto end; L = *it1; it2 = std::upper_bound(last[T].begin(), last[T].end(), R); if (it2 == last[T].begin()) goto end; R = *std::prev(it2); res = dp[R][L]; end:; cout << (res ? "Yes\n" : "No\n"); } return 0; } ch_egor's code (memory optimization + dirt optimizations like fast io). Spoiler#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #pragma comment(linker, "/STACK:256000000") using namespace std; typedef long long int int64; typedef long double double80; const int INF = (1 << 29) + 5; const int64 LLINF = (1ll << 59) + 5; const int MOD = 1000 * 1000 * 1000 + 7; const int MAX_N = 1000 + 5; const int MAX_M = 200000 + 5; const int MAX_Q = 200000 + 5; /** Interface */ inline int readChar(); template inline T readInt(); template inline void writeInt(T x); inline void writeChar(int x); inline void writeWord(const char *s); inline void flush(); /** Read */ static const int buf_size = 20 * 1000 * 1000; inline int getChar() { static char buf[buf_size]; static int len = 0, pos = 0; if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin); if (pos == len) return -1; return buf[pos++]; } inline int readChar() { int c = getChar(); while (c <= 32) c = getChar(); return c; } template inline T readInt() { int s = 1, c = readChar(); T x = 0; if (c == '-') s = -1, c = getChar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); return s == 1 ? x : -x; } inline int64 readInt64() { int s = 1, c = readChar(); int64 x = 0; if (c == '-') s = -1, c = getChar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); return s == 1 ? x : -x; } /** Write */ static int write_pos = 0; static char write_buf[buf_size]; inline void writeChar(int x) { if (write_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; write_buf[write_pos++] = x; } inline void flush() { if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; } template inline void writeInt(T x) { if (x < 0) writeChar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = '0' + x % 10, x /= 10; while (n--) writeChar(s[n]); } inline void writeInt64(int64 x) { if (x < 0) writeChar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = '0' + x % 10, x /= 10; while (n--) writeChar(s[n]); } inline void writeWord(const char *s) { while (*s) writeChar(*s++); } struct edge { int v1; int v2; }; struct ask { int l; int r; int v1; int v2; int ptr; int dp_order_l; int dp_order_r; bool answer; }; bool cmp_order(const ask &a, const ask &b) { return a.dp_order_r < b.dp_order_r; } bool cmp_answer(const ask &a, const ask &b) { return a.ptr < b.ptr; } int n, m, q; int lasts[MAX_M][2]; int size_of_lasts[MAX_M]; int need_ptr[MAX_M]; edge edges[MAX_M]; ask asks[MAX_Q]; vector last_of_v[MAX_N]; bitset* dp[MAX_M]; int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); n = readInt(); m = readInt(); q = readInt(); memset(size_of_lasts, 0, sizeof(size_of_lasts)); for (int i = 0; i < m; ++i) { edges[i].v1 = readInt() - 1; edges[i].v2 = readInt() - 1; if (!last_of_v[edges[i].v1].empty()) { lasts[i][size_of_lasts[i]++] = last_of_v[edges[i].v1].back(); } if (!last_of_v[edges[i].v2].empty()) { lasts[i][size_of_lasts[i]++] = last_of_v[edges[i].v2].back(); } last_of_v[edges[i].v1].push_back(i); last_of_v[edges[i].v2].push_back(i); } for (int i = 0; i < q; ++i) { asks[i].l = readInt() - 1; asks[i].r = readInt() - 1; asks[i].v1 = readInt() - 1; asks[i].v2 = readInt() - 1; asks[i].answer = false; asks[i].dp_order_l = asks[i].dp_order_r = -1; asks[i].ptr = i; } int l, r; for (int i = 0; i < q; ++i) { l = lower_bound(last_of_v[asks[i].v1].begin(), last_of_v[asks[i].v1].end(), asks[i].l) - last_of_v[asks[i].v1].begin(); r = upper_bound(last_of_v[asks[i].v2].begin(), last_of_v[asks[i].v2].end(), asks[i].r) - last_of_v[asks[i].v2].begin(); if (l != last_of_v[asks[i].v1].size() && r != 0) { asks[i].dp_order_l = last_of_v[asks[i].v1][l]; asks[i].dp_order_r = last_of_v[asks[i].v2][r - 1]; } } sort(asks, asks + q, cmp_order); memset(need_ptr, 0, sizeof(need_ptr)); for (int i = 0; i < m; ++i) { for (int j = 0; j < size_of_lasts[i]; ++j) { ++need_ptr[lasts[i][j]]; } } int ptr = 0; for (int i = 0; i < m; ++i) { dp[i] = new bitset; for (int j = 0; j < size_of_lasts[i]; ++j) { (*dp[i]) |= (*dp[lasts[i][j]]); --need_ptr[lasts[i][j]]; if (need_ptr[lasts[i][j]] == 0) { delete dp[lasts[i][j]]; } } (*dp[i])[i] = true; while (ptr < q && asks[ptr].dp_order_r < i) { ++ptr; } while (ptr < q && asks[ptr].dp_order_r == i) { asks[ptr].answer = (*dp[asks[ptr].dp_order_r])[asks[ptr].dp_order_l]; ++ptr; } if (need_ptr[i] == 0) { delete dp[i]; } } sort(asks, asks + q, cmp_answer); for (int i = 0; i < q; ++i) { ((asks[i].answer) ? writeWord("Yes\n") : writeWord("No\n")); } flush(); fclose(stdin); fclose(stdout); return 0; } 
•  » » 6 years ago, # ^ |   0 Can you explain the O(nm) solution for Div-1 E.
•  » » » 6 years ago, # ^ |   +10 Go with scanline from right to left, and maintain following dp:dp[i][j] -- if L is current pos of scannline, the smallest R, which makes possible to go from i to j.While you go with scanline add edges to this dynamics (requires recalculating O(N) values), and answer the queries, when current position of scannline coincides with querie's L.
 » 6 years ago, # |   0 Why is the complexity of Div1B's second approach O(N * log²(N) + Q)? I know one of the log factors is due to the set usage; but why does the merging step also have O(log(N)) complexity? Shouldn't it be more?
•  » » 6 years ago, # ^ |   0 It is a common trick with merging sets.Let's always merge smaller set into larger, this way when element is merged from one set to another the size of the set in which it lives doubles at least.Hence each element will not be moved more, than times.
•  » » » 6 years ago, # ^ | ← Rev. 2 →   0 How come it doubles at least? and what do you mean by the set which it "lives" would you clarify more please?
•  » » » » 6 years ago, # ^ | ← Rev. 2 →   +3 The key idea is following: each elements starts in it's own std::set, and sometimes we ask sets to merge together.How to merge them? Select smaller set and add all those elements to larger set.Notice, that each time some element changes set, the set in which it lives grows at least two times (since large set  ≥  smaller).Hence, there can't be more than movements per element.If you need more info, read 600E - Lomsat gelral and it's editorial.
•  » » » » » 6 years ago, # ^ |   0 Got it thank you <3
 » 6 years ago, # |   +67 Lol, you considered O(nm) as bruteforce and as a model solution :|? Do you know that dividing as 32 in the most typical model of computations is in fact dividing by log :)? And in reality, not in plain theory, isn't that still more or less the same? There is no reason to apologise that O(nm) passed, because I doubt it is possible to make a distinction between those 2 solutions.
•  » » 6 years ago, # ^ |   +112 In Balkan OI 2014, there was a problem with N≤10000 and intended solution was N * log(N) + N2. Author of problem apologized after contest because of there are some AC with N2 + N2.This is even funnier than it.
•  » » » 6 years ago, # ^ | ← Rev. 2 →   0 Well — even though this is a programming contest, I think that the solution should focus on pure big O complexity.In this contest the model solution was O(nm log m) -> there is no O((nmlogm)/32) complexity.In case of Balkan OI the model solution was O(N^2). I don't understand apologizing for letting the solutions with the same or better big O complexity get AC.Furthermore I would be surprised if the O(nm) or O(N^2) solution didn't AC.Dividing by 32 can be a nice trick if your big O complexity is worse than the model one and you used that trick to speed up things a bit. That trick should never be required by the model solution.
•  » » » » 6 years ago, # ^ |   +5 I hope you may change your mind if you read my comment below.
•  » » 6 years ago, # ^ | ← Rev. 2 →   +36 I find O(stuff / 32) problems pretty dissatisfying in general. I personally don't find it interesting at all that you can speed up a problem by a factor of 32 or 64, and that this often constitutes the "main difficulty" in a problem.
•  » » » 6 years ago, # ^ |   +44 There was a time when I had a similar opinion as yours, but after I realized what this "/32" in fact really is, I think it is perfectly fine.Do you agree that we should be able to handle "a+b" operation in constant time? If we won't, because a and b binary notations are of logarithmic length, hence this operation requires operations then in 99% of problems we should put additional into complexity, because summed length of bits sequences we are dealing with is (previous complexity) . It would be rather inconvenient to put additional into complexities almost everywhere, because of arithmetic operations. Moreover that suits practical model, because as long as we are dealing with numbers from not very large range we perform arithmetic operations on bits sequences not longer than 64 as fast as we perform operations on shorter sequences and only reasonable way of extending that arrangement to arbitrarily large integers is to assume that we are able to perform operations on bits sequences of length in constant time (where R is range of numbers on input). Either we accept it as a fact that we are able to do this or we demand adding two numbers to take logarithmic time. There is no way to "ban cheating with dividing by 32" and have constant time addition. Thus, consequence of having addition in constant time is being able to perform operations on bits sequence of logarithmic length in constant time, because constant time addition is same kind of cheat as ability to xor sequences of length k in k/log k time. That enables you to sometimes perform operations on data of O(n) size in time which is for example a state of an art algorithm for transitive closure.tl;dr dividing by 32 is not optimization by a constant factor, it is optimization by a logarithmic factor Because of that I definitely don't consider such solutions as dirty tricks.
•  » » » » 6 years ago, # ^ | ← Rev. 2 →   +8 ok ok I had something here but then I understood what you're saying. That's reasonable. Further question: what do you think about the following problem that I might use on a future contest. You have 105 integers, each of size  ≤ 1010000. Find their sum and print it out.(ok the future contest part is a joke)
•  » » » » » 6 years ago, # ^ | ← Rev. 3 →   +5 Hm, yeah, I guess you didn't get my point :P. We are considering theoretical model. Do you want to multiply complexity of 99% of your solutions (those requiring any arithmetic operations) by factor coming from arithmetic operations? I guess no. If we stick to computations performed on a modern computer, following your reasoning, why do you write that algorithm of adding n numbers not larger than R is O(n), but not ? Bits notations of such numbers are of length and you are not able to shave off log factor, just 64 one. You may didn't realize this, but model when we are able to perform whole factor is exactly the model whole CF community communicates within. If you accept algorithm of adding n numbers from range R to be O(n) then you also accept algorithm to transitive closure. algorithm is in fact a better complexity, because it translates to in a theoretical model and fact that we need replace that log with 32 is only a technical barrier. I have a feeling that I am saying the same statements over and over and can't explain it much better :). EDIT: Oh, I see you changed your comment while I was writing this :).EDIT2: Regarding to your further question. I have to admit that I am not fully sure whether R I used so far should denote range of numbers we are dealing with. Maybe it should denote length of input or sth, but I am not sure right now.
•  » » » » » » 6 years ago, # ^ |   0 Oh, for transitive closure, you can do This is really surprising to me, as I thought the best savings you get in all cases is 64 or something (for all problems of this type).
•  » » 6 years ago, # ^ | ← Rev. 2 →   -8 Same here. I was just scan-reading the tutorial for a hint and the first thing I see is to iterate all n and m. I got an AC for iterating through all value of n and m, and I didn't even store the representation of values
 » 6 years ago, # |   0 can anyone plz explain the duv2D 3rd solution by ch_egor, what does big_subtree[] stand for and what is dfs_centroid calculating
•  » » 6 years ago, # ^ |   +3 The idea is that say the node u has more than one child and heaviest one is v, and say the centroid of v is vc, then the centroid of u will be on the way from vc towards the root. For leaf nodes, centroid are themselves, and for others, consider the lifting model, thus complexity being O(n) only.
•  » » » 6 years ago, # ^ |   0 Hi, Can u plz explain this->"heaviest one is v, and say the centroid of v is vc, then the centroid of u will be on the way from vc towards the root". Thanks.
•  » » » » 6 years ago, # ^ |   0 heaviest one means largest subtree among subtrees of children of u. Say the largest subtree among children subtrees of u is v and centroid of v is vc . Then you can get centroid of subtree of u by uplifting vc. If you draw some diagrams it will be more clear.
•  » » » 6 years ago, # ^ |   0 Correct me if I'm wrong, but is the centroid of u either itself or vc? And is that why the complexity is O(n)? Thanks in advance.
•  » » » » 6 years ago, # ^ | ← Rev. 3 →   +8 You are wrong. The centroid is either u or some vertex beetween u and vc.The amortized complexity is O(n). Think of it like, at every node, we do some lifting, lifting from centroid of big subtree (vc) to some nodes up (nvc : which will be the new centroid) . Now let's say you lift vc up to node nvc. Let's say the path is vc, parent[vc], parent[parent[vc]], ..... nvc.You will notice that for all nodes, these paths are disjoint. So this extra loop inside every node for lifting will be O(n) for all nodes combined.
•  » » » » » 6 years ago, # ^ |   0 I used almost similar idea, but instead of lifting up, I was trying to lift down. I start from the top of the tree, and find centroid for each node. For a node u, if it's parent's centroid c is in u's subtree, I try to lift down from c until I find the centroid, if c is not in u's subtree, I start searching from u node itself. The logic behind this is if u is the heavy child of it's parent, it must have it's parent's centroid in it's subtree, thus avoiding some steps and jumping directly to the centroid node for search. If u is not the heavy node, it's subtree is being explored for the first time, so it's not repeating. I got tle in 14th test case, can someone kindly point out my mistake here?http://codeforces.com/contest/685/submission/18730022
•  » » » » » » 6 years ago, # ^ | ← Rev. 3 →   +5 Your mistake here. Codebool isCentroid(int root, int center){ int i,child; if( (sub[root]-sub[center]) > (sub[root]/2) ) return false; for(i=0; i (sub[root]/2)) return false; } return true; } You check every vertex in O(deg(c)). 14th test looks like bamboo with tall and sunshine with size . The main vertex of sunshine is centroid of subtrees, because this your code works in .
•  » » 6 years ago, # ^ |   0 big_subtree[] stands for size of largest subtree of a node. The main concept of solution :- If we want to calculate centroid of a node . Then we find the largest subtree of that node. The centroid of largest subtree of a node when uplifted will become the centroid of the node. Reason behind :- If we remove a node then what are the connected components formed? 1) The connected components are subtrees of children of that node 2) The original tree(subtree) — subtree of node we are removing . It will be more clear if you draw some diagrams.
 » 6 years ago, # |   +3 While I drew diagrams and wrote equations for subtree sizes for certain cases to prove ch_egor's solution in the middle of the contest (and it passed :D), could someone give me a general, more formal proof for it?
•  » » 6 years ago, # ^ |   +5 consider vertex u, let v be the maxSize Child of u, let w be centroid of v, go along path from w to u, there are 2 case 1. size(v)<=size(u)/2, u itself is centroid of u. 2. size(v)>size(u)/2, along the path from w to v, the first vertex x satisfying the condition (size(x)>=size(u)/2) is centroid of u, such x must exist as x=v satisfy the condition. let prove case 2, x must be a centroid of u. if x=w, obviously true; if x!=w, let y be the preceding vectex of x along the path(y is a child of x), there are 3 types of components, 1.subtree(u) — subtree(x), obviously ok as sizeof(x)>=size(u)/2. 2.subtree(y), ok because size(y)
 » 6 years ago, # | ← Rev. 2 →   0 hello friends i wrote a coded up the div2 c problem and it is giving wrong answer on only two test cases(and possibly more not included in the test case file) could someone plz check what is wrong with the code it's simple recursion, here is the link code: http://codeforces.com/contest/685/submission/18694520. As you could clearly see in the code i cheated(of course i couldn't get it to submit during contest)...so plz help!!! i would be really grateful if someone could tell me what is wrong with the solution.
•  » » 6 years ago, # ^ |   0 It is curious because I compiled and ran your code locally removing the "cheating part" it works perfectly well in the two problematic test cases but if we check it on the server of codeforces it gives WA.
•  » » 6 years ago, # ^ |   0 I see now, the problem is with the function pow. It is defined for doubles and you are using for ints. Small roundoff errors give you trouble. It is better to implement your own powq function as in codelong long powq (long long a,long long b) { long long res=1; while (b) { if (b % 2) { res = (res * a); } a = (a * a); b /= 2; } return res; } Then it works properly
•  » » » 6 years ago, # ^ |   0 Thank you i will be forever in your debt
 » 6 years ago, # |   0 can the centroid for a vertex lie above a vertex v if yes then how (are we then considering the whole tree or just the subtree of a vertex) and how can we prove this model.
•  » » 6 years ago, # ^ |   0 The centroid of subtree at vertex v must be one of its children. Have a look at the problem statement ;)
 » 6 years ago, # |   +8 Div1 C is a harder version of GCJ 2008 R2 Problem C — Star Wars: https://code.google.com/codejam/contest/32001/dashboard#s=a&a=2
 » 6 years ago, # |   +8 An other solution for Div2 D/Div1 B: The algorithm for finding the centroid of a general tree is the following: If we are currently at node u and we are looking for the centroid, we find a child v of u with size[v] > N/2 and continue our search for the centroid from v. Now let us for every node u create a path containing all the nodes we have selected with this process. We have just done Heavy-Light decomposition to our tree, and every path we have created is a heavy path. So, when we want to find the centroid of the tree rooted at a node r, we can find it with a simple binary search in the heavy path which contains r. (Solving the problem with this method doesn't require knowledge of Heavy-Light Decomposition though) Code
•  » » 6 years ago, # ^ |   0 Nice! Got accepted using your approach, thanks bro.
•  » » » 6 years ago, # ^ |   0 You are welcome :)
 » 6 years ago, # |   0 guys how to do 359DIV2 B in shortest steps(bonus question propsed by ch_egor)i found a n^2 solution to it does a nlogn or n solution exist???please guys help will be appreciated
 » 6 years ago, # | ← Rev. 3 →   0 Can anyone please shed light on below line of code from Problem C editorial solution? How is this checking for unique combinations?  if (*std::max_element(used.begin(), used.end()) <= 1) ++ans; 
•  » » 6 years ago, # ^ |   0 used[v] = how much times this digit occured.If all digits occured no more than once, than this time all digits were distinct.
•  » » » 6 years ago, # ^ |   0 Thanks a lot for the explanation.
•  » » 6 years ago, # ^ |   0 max_element(used.begin(),used.end()) returns the iterator to the maximum element . We are checking if the value of the maximum element <= 1 . If there are any repetition the value of the maximum element >=2 else there are no repetition .
 » 6 years ago, # |   0 How can Div2C be solved when Base( <= 10^9 ) is not fixed ?
•  » » 6 years ago, # ^ | ← Rev. 2 →   0 Hint: solve in first.
•  » » » 6 years ago, # ^ |   0 Hey, i could think of a dp solution. Not so sure about complexity. Please check it 18702275
•  » » » » 6 years ago, # ^ | ← Rev. 2 →   0 Your complexity looks like O(BASE2 2BASE).
•  » » » » » 6 years ago, # ^ |   +3 I am still confused about the complexity part ! But at least now, can you share how to solve it in O(logN * logM) ?
•  » » » » » » 6 years ago, # ^ | ← Rev. 2 →   +3 I can share solution, I see ways to boost it (Drop base2 from complexity in trade of to more 's), still i don't want to implement this.The basic idea is that you bruteforce the common prefix of max hour and target, and same for minutes.When you fixed common prefix and first digit after it you just use combinatorics formula. P.S. This code can be used with different bases, however some things might be needed to adjust, e.g to avoid overflows. code// Copyright (C) 2016 Sayutin Dmitry. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; version 3 // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; If not, see . #include #include #include #include #include #include #include #include #include #include #include #include #include #include using std::cin; using std::cout; using std::cerr; using std::vector; using std::map; using std::array; using std::set; using std::string; using std::pair; using std::make_pair; using std::min; using std::abs; using std::max; using std::sort; using std::generate; using std::min_element; using std::max_element; template T input() { T res; cin >> res; return res; } #define BASE 7 vector get_digits(size_t v, size_t len) { vector res(len); for (size_t i = 0; i != len; ++i, v /= BASE) res[len - 1 - i] = v % BASE; return res; } size_t A[2 * BASE + 2][2 * BASE + 2]; void precalc_a() { for (size_t n = 0; n != 2 * BASE + 2; ++n) for (size_t k = 0; k != 2 * BASE + 2; ++k) { if (k == 0) A[n][k] = 1; else if (k > n) A[n][k] = 0; else A[n][k] = (A[n - 1][k - 1] * n); } } vector range(size_t a, size_t b) { vector res(b - a); std::iota(res.begin(), res.end(), a); return std::move(res); } int main() { std::iostream::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); precalc_a(); size_t n, m; cin >> n >> m; size_t len1 = 1, len2 = 1; for (size_t a = BASE; a < n; a *= BASE) len1 += 1; for (size_t b = BASE; b < m; b *= BASE) len2 += 1; vector dig1 = get_digits(n - 1, len1); vector dig2 = get_digits(m - 1, len2); vector used(BASE + 2, false); size_t ans = 0; for (size_t pref1 = 0; pref1 <= len1; ++pref1) { vector nused = used; vector last1 = (pref1 == len1 ? range(BASE, BASE + 1) : range(0, dig1[pref1])); for (size_t pref2 = 0; pref2 <= len2; ++pref2) { vector last2 = (pref2 == len2 ? range(BASE + 1, BASE + 2) : range(0, dig2[pref2])); size_t mult = 0; for (char ch1: last1) for (char ch2: last2) if (ch1 != ch2 and not nused[ch1] and not nused[ch2]) ++mult; size_t left = len1 + len2 - pref1 - pref2; size_t hasdigs = std::count(nused.begin(), nused.begin() + BASE, false); hasdigs -= (pref1 != len1) + (pref2 != len2); left -= (pref1 != len1) + (pref2 != len2); if (left < 2 * BASE + 2) ans += mult * A[hasdigs][left]; if (pref2 != len2) { if (nused[dig2[pref2]]) break; nused[dig2[pref2]] = true; } } if (pref1 != len1) { if (used[dig1[pref1]]) break; used[dig1[pref1]] = true; } } cout << ans << "\n"; return 0; } 
•  » » » » » » » 6 years ago, # ^ |   0 Tanks for answer, I'm so busy these days.
•  » » » » » » » » 6 years ago, # ^ |   0 Thank you. Both ! Maintain this generosity even when you're red :) And i like coordination between you two !
•  » » » » » » » » » 6 years ago, # ^ |   0 thank you :)
 » 6 years ago, # |   0 In DIV2D, "We write the euler tour of tree and we will use a 2D segment tree in order to search for a vertex quickly. ". How to do a query? Also please tell the properties of euler tour too.
•  » » 6 years ago, # ^ | ← Rev. 2 →   +3 Write the euler tour of tree, subtree of vi now takes some contigious segment of the tour.Select from this segment minimum number greater or equal to , why this works is explained in editorial.So you want to perform queries with three params: L, R, C — you can do this with 2d segment tree.
•  » » » 6 years ago, # ^ |   0 After we do the euler tour, each subtree becomes a subarray, right? Are they also ordered by some property like increasing subtree sizes? Or random?If they are random, I don't understand how we could use a 2D segment tree to find a number greater than X/2 in some subarray. Please give more details :)
•  » » » » 6 years ago, # ^ |   +6 After we do the euler tour, each subtree becomes a subarray, right? Are they also ordered by some property like increasing subtree sizes? Or random? Each subtree becomes an subarray, yes. You should write to the array the subtree sizes of vertexes.Yes, numbers are not sorted within given subarray, so 2d tree come to help.You can create segment tree, where each node contains sorted array of values of all leaves it contains. It can be done in time. Note, than each element lives in elements, so this is why it is , not more.Using this segment tree is easy: you split your request to list of nodes as usual, and in each node you make a binary search.
•  » » » » » 6 years ago, # ^ |   0 Thanks, that clears it up. Will try to implement it!
•  » » » » » 6 years ago, # ^ |   0 I got AC using this idea, thanks! Can you explain the second approach in a little more detail? What is "merging sets" idea?
•  » » » » » » 6 years ago, # ^ |   +11 It is a common idea.Checkout this problem 600E - Lomsat gelral and this editorial for it.
•  » » » » » 6 years ago, # ^ |   0 In each node of segment tree, are we storing a sorted array of that range and then using binary search to search for the value just greater than (subtree_size/2) ? If yes, then won't the cost of constructing such segment tree be O(n^{2}) ?
•  » » » » » » 6 years ago, # ^ |   +5 Do you know that merging two sorted arrays into one new can be done in O(len_of_first + len_of_second)? It is a key idea to know, may be you have encountered it before in mergesort algorithm. It is also implemented in c++ as std::merge.How to construct such tree? Construct it recursively: at first construct child nodes with same constructing function, and then merge sorted arrays of left and right nodes into array of this node.Why this is O(N log N)? Your recursion will have O(log) levels of recursion, and you will have O(N) work done on each level (it is same as in merge sort).
•  » » » » » » » 6 years ago, # ^ |   0 Thanks @cdkrot, I calculated the complexity in a wrong way and that's why was confused.
 » 6 years ago, # |   0 Can anyone tell me that how can I reduce complexity of my code for Div2D? I am not quite sure what's the complexity here :( Thnx in advance.My code: http://pastebin.com/HJNpnwyq
•  » » 6 years ago, # ^ |   +5 If I am not mistaken, you firstly check a node's heaviest child whether it is the centroid. If not, then you go down on the tree until you find it. However, it is not what is told in the tutorial and has a complexity of O(N^2). To make it correct, initialize your par variable with the centroid of your heaviest child and climb the tree upward by going to one's dad in each step. Please let me know if you need any further explaination :)
 » 6 years ago, # |   0 Can anyone please explain the Div2 C. Robbers' watch problem. I am not able to understand the problem statement clearly and how is the pigeonhole principle appyling in the solution. Please help. Thanks in advance.
•  » » 6 years ago, # ^ | ← Rev. 2 →   +1 (Refer the problem statement for terminology) Suppose there are h places for display in hour section and m places for minute section. Now, problem says that all digits in hour and minute section taken together should be distinct. Since, its septimal number system, so possible digits are 0,1,2,3,4,5,6, i.e. 7 distinct digits. So, if sum of places in hour and minute section is greater than 7 then it is impossible that all the digits are distinct because we have 7 digits available and places are more than 7.So, the answer should be zero in that case. This logic is formally called pigeonhole principle.For cases when total available places in hour and minute section are less than or equal to 7, we apply brute force approach. that is we take every possible value for hour(0 to n-1) and minute (0 to m-1), then convert them to base 7 and see if all digits are distinct are not. For this you can use an array for digits 0 to 6 and track the count of every digit while converting hour and minute to base 7. If any digit's count is greater than 1, then that combination of hour and minute is rejected.
•  » » » 6 years ago, # ^ |   0 Hi, thanks for the explanation, I have understood the problem and Pigeonhole principle and although one thing still confusing me is that->why do we need to track the count of every digit while converting to base 7, I mean why combination is rejected if the digit count exceeds 1. I know maybe I am asking very basic question. Sorry for inconvenience :)
•  » » » » 6 years ago, # ^ |   +1 Because the problem asks to find all those possible combination of hour and minute such that no digit appears more than once in the whole clock display. For eg, 02:23 (hr:min format) is not allowed since 2 appears more than once.
•  » » » » » 6 years ago, # ^ |   +1 Oh yes my bad, that was really easy :PAnyways Thank you very much for your help :)
 » 6 years ago, # |   0 I'm wondering how to solve div2/B if we have to minimize the number of operations ?
 » 6 years ago, # |   0 In DIV-2 D/DIV-1 B , the last solution taking O(n+q) time complexity to solve uses the line "Note that the centroid of the child after a certain lifting goes into our centroid. Let's model the lifting. ", for explanation which I am not able to understand, could someone please explain what is the lifting operation mentioned here and how is it helping ?
•  » » 6 years ago, # ^ | ← Rev. 2 →   0 I'll copy paste my explanation, ask more if you need anything: Of course, the centroid of the leaf is the leaf itself.What about the node?Let's find the size of every subtree.If for every node's child it's (child) subtree size <= size(node) / 2 than the node is the centroid.If not, then centroid of node is the children's centroid. It only fails one kind of test — where the path from the node to the centroid is longer than size(node)/2, then we have to find a node that is an ancestor(parent or parent's parent etc) of the centroid, that will be good for our node. The node has to be on the path from the centroid to our node. Why? Let's try a node that's below the centroid — then the size of our component is even bigger. So let's take another node that is above our centroid but not on the path between them. Then we have a component of size at least n/2, because if it wasn't, then we would've had another centroid computed for our son by that time. Try to draw a picture and it should be intuitive why it's that way.
•  » » » 6 years ago, # ^ | ← Rev. 2 →   0 Thanks for the reply, consider this example https://postimg.org/image/9v9wg9idt/ the centroid of the node (u) passes the test you mentioned bcoz it is at length 3 which is less than size(node)/2 but we will still need to find some ancestor as it is not the centroid of node (u) , but I think one way to quickly check if centroid of one of the child is centroid of the node as well is by checking the weight of centroid, it the weight of centroid is x then weight of the remaining part after removing it would be weight(node)-x-1, we can verify both the values and if they don't satisfy, we can look forward for ancestors. Also for the case in image the centroid of the node (u) would be v which is along the path from node to centroid, but still the centroid. Thanks again .
•  » » » » 6 years ago, # ^ | ← Rev. 2 →   0 In our case v subtree is bigger than half of u subtree size, so v centroid is our candidate. We have to check if v is centroid of u, so check the length of the path between u and v, it is 1, which is less than size of u subtree, thus v is u centroid. If length of path from u to v was longer than u subtree size then you would have to iterate through v ancestors. Sorry for mistakes, writing on mobile
•  » » » 6 years ago, # ^ |   0 "So let's take another node that is above our centroid but not on the path between them. Then we have a component of size at least n/2, because if it wasn't, then we would've had another centroid computed for our son by that time. Try to draw a picture and it should be intuitive why it's that way." — This is the part that I cannot understand. Could you elaborate a little bit more? I'm sorry to pester you.
•  » » » » 6 years ago, # ^ |   0 Sure, no problem! Imagine we're in vertex u, that has the child of size bigger than size(u)/2, let's name this child's as w and it's centroid as v. If v != w then we know, that v is in w subtree with size bigger than size(v)/2. Let's take vertex z that is not on the path between v and w and assume it's u centroid. Then there has to be a vertex c on that path that has both v and z as children in different subtrees (maybe not direct), but if v is w centroid, then it also has to be c centroid, so c subtree with v in it is bigger than size(c) > 2, so z cannot be a c centroid. Moreover, it cannot be u centroid. So we have a contradiction. It isn't beautifully written, I can't really put it into words, if you have any more questions feel free to PM me
•  » » » 19 months ago, # ^ |   0 Perfect!! 4 years old comment still helps!!. Thanks Mucosolvan
 » 6 years ago, # |   0 in DIV2, Problem D:Kay and Snowflake what i did was: 1.) Calculated subtree size for each node. And found parent for each node,stored them in an array. 2.) for query of node U ,i calculated the node with minimum subtree_size amongst those nodes(v) in subtree of U which have subtree_size(v) > (1/2)*subtree_size(U). But i faced TLE. And henceforth answer is the node satisfying the above condition. Please help me out i i can improve my complexity. THANKS IN ADVANCE 
•  » » 6 years ago, # ^ |   0 Your code's complexity is O(NQ), since you run dfs for each query.You should write something more effiecient, reread the editorial.P.S. It is old century to store graph in hand written lists, most people now use vector> graph(n); // graph[i] contains list of neighbours of i. It's just more simple to write.
 » 6 years ago, # | ← Rev. 3 →   0 Can someone explain what std::move does in div 2 D solution with merging sets? How would this solution work without it?
•  » » 6 years ago, # ^ |   0 In that particular code explicit std::move does nothing. I'd say it even makes things worse as it blocks potential Return Value Optimizations. However, move semantics (which are triggered on return anyway without explicit std::move) allow not to copy the set on return, but move it cheaply — something like returning a pointer, but more convenient.
 » 6 years ago, # | ← Rev. 2 →   +5 Can someone explain how the time-complexity is O(n+q) in Div2D Kay and Snowflake third approach. I am unable to understand O(n) part due to the below code. I think in worst case, it will take steps equivalent to its height thus making complexity > O(n).while (!is_centroid_of_subtree(v, c)) { c = prev_of[c]; }
•  » » 6 years ago, # ^ |   +5 Consider each edge. It has upper and lower ends. Lifting can be done only from lower to upper, and due to the DFS nature of solution it happens no more than once per edge.And num of edges is O(N) (N - 1 to be precise).
 » 6 years ago, # |   0 For Div1 D, can someone explain this in more detail from the editorial : "Suppose we update the value of a cell on the row a, and before it was updated the value x on the row b. Let add to the answer for the number of squares containing x points value a - b."
 » 6 years ago, # |   0 Hello, I have tried solving Div2 D/ Div1 B by selecting a child vertex which has subtree size > (1/2)*total_subtree_size of that node, but I am getting wrong answer on 3rd testcase. Can someone please point out what am I doing wrong ?Submission
•  » » 6 years ago, # ^ |   0 subtree size should be >= not > than half of total_subtree_size. btw floor division's also a problem. look out for that one.
 » 6 years ago, # | ← Rev. 3 →   0 .
 » 6 years ago, # |   0 can anyone explain what " lifting " means in div2D editorial?
•  » » 6 years ago, # ^ |   0 In this approache we choose centroid of the heaviest child. Then we are lifting it while considered vertex is not a centroid of subtree. In this case it means that we look at vertex if it is centroid of subtree do nothing else we look at parent of vertex. If parent of vertex is centroid do nothing else look at parent of parent... Do this while considered vertex is not centroid of subtree.
 » 6 years ago, # |   0 What is "manhattan distance <= const" equation? Wouldn't that be |x2 - x1| + |y1 - y1| + |z2 - z1| ≤ const ?
•  » » 6 years ago, # ^ | ← Rev. 2 →   0 Yes it is. One of (x_1, y_1, z_1), (x_2, y_2, z_2) is target point you are trying to find, and the other is source point given in test.Const is equal to mid value from binary search.
 » 6 years ago, # | ← Rev. 2 →   0 What is v in 686E - Optimal Point and what does v = 10 *  * 18 mean? Thank you.
•  » » 6 years ago, # ^ |   +3 v means bounds of coordinate. 10 *  * 18 is mistake, it means 1018. I fixed it now.
 » 6 years ago, # |   0 Could anyone explain how do we do binary search in 686E - Optimal Point?
•  » » 6 years ago, # ^ |   +4 Consider boolean function f(L) -- is there a point, which makes answer not worse, than L?We are searching for lowest x, such that f(x) = 1. Using binary search.
•  » » » 6 years ago, # ^ |   0 Why do you add +1 when calculation first of a',b',c' in "tr.c.first = DIV2(tr.c.first — r + 1);" ??Also in get_solution can you explain the final calculation part using delta ?? Couldnt get the logic .. Seems mathematical
•  » » » » 6 years ago, # ^ | ← Rev. 2 →   +1 Because we need to round in different ways. In one case we want to round down ("floor"), and in other case up ("ceil")Let me explain using example. Consider following inequality, solve in integers. a ≤ 2x ≤ bSince x is integer, we transform: And ceil operation can be written as floor operation: Also take note, that we don't divide using divide operator, it rounds toward zero, which is not good for us.
•  » » » » » 6 years ago, # ^ |   0 ok ! Also explain the final point calculation using delta
•  » » » » » » 6 years ago, # ^ | ← Rev. 2 →   +1 At first we make some comparisons, to make sure, that solution exists.After that we generate one of solutions. At first we take a, b, c at their minimum. However their sum can be too small, in this case we want it to be raised to minimum S.So we calculate delta -- how much we need to raise, and then we raise in such manner, that a, b, c will never be more, than maximum.
•  » » » » » » » 6 years ago, # ^ |   0 Thanks a lot mate ! Got it Accepted ! One last thing : That div2 function will make difference when dividing by 2 and taking floor of negative nos. right ?As -3/2 = -1 but div2(-3) = -2
•  » » » » » » » » 6 years ago, # ^ |   0 Yes, exactly. Div2 func always rounds down. Which matters for negative odds.
•  » » » 6 years ago, # ^ |   0 How do we count f(x)?
•  » » » » 6 years ago, # ^ | ← Rev. 3 →   +3 Mentioned function is simple boolean function -- have we such point or not?I believe, that this function is explained in editorial thoroughly. Try reading it again.If it will not help -- send me a PM.
 » 6 years ago, # |   0 In cdkrot code.... Is "std :: merge" working in the way smaller one attach to larger one? (O(logN))? I mean, std::merge(sm.begin(), sm.end(), big.begin(), big.end(), vec.begin(), vec.begin()) --> this will be sm---(attach)-->big and copied to vec?And I think copying time is somewhat costly isn't it?
•  » » 6 years ago, # ^ |   0 There are two solutions by me for centroids problem.One uses 2d segment tree, and other uses merging sets.std::merge is used in 2d segment tree solution, if you want merging sets solution, check the other code. (it contains the word "set" in the code).
•  » » » 6 years ago, # ^ |   0 Thanks for comment in person. After read all of solutions carefully for Div2-D, O(n+q) solution was what I really want. One of my goal was to solve like this kind of problem(which requires some sense and proof). But when solving problem D of any Div2, I failed 50% of them (like in this case...) The common point of failing (I think) is not how to prove but how to come up with "idea" (In this case, centroid of u must be on the line of centroid of v to u, v is child of u who makes sub[v]*2>=sub[u]). Could you give me any advice for accessing this kind of problems or training this kind?
•  » » » » 6 years ago, # ^ |   0 Don't know, really.May be you need to get more experience -- then write more contests. May be you need theory. May be something else...If you have opportunity to ask coach for help, than you should do it.
 » 6 years ago, # |   0 In Div 2 E (Optimal Point), the editorial says:-2) Consider areas of "good" points (with dist  ≤ mid) for each source point.What is mid here?
•  » » 6 years ago, # ^ |   0 If you know what binary search is, it should be obvious. If you don't know what binary search is, then you should learn it.
•  » » » 6 years ago, # ^ |   0 So what are L and R here?
•  » » » » 6 years ago, # ^ |   0 Do you know what the binary search is?
•  » » » » » 6 years ago, # ^ |   0 Yes.I apologize for bothering you. I was a bit confused by the editorial. After reading this editorial for a similar problem, I now understand the solution.
•  » » » » » » 6 years ago, # ^ |   0 Not a problem, feel free to ask.
 » 6 years ago, # |   0 Can someone explain how to solve div2/E (Optimal point) with ternary search?Thanks.
 » 2 years ago, # |   0 Can someone explain how to solve Div 1 A in O(log n log m)