### NBAH's blog

By NBAH, history, 6 years ago, translation,

## A

We know that time=distance/speed. For each car we should find timei, than if it is less than answer we should update it.

Time Complexity: O(n).

Solution

## B

Consider c[x] the number of stores in which the price per drink is x. We calculate this array prefix sum. Then the following options:

1) If the current amount of money m is larger than the size of the array with the prefix sums than answer is n.

2) Otherwise, the answer is c[m].

Time Complexity: O(n+q).

Solution

## C

We will solve the problem with the help of dynamic programming. dp[i][j] is the minimum amount of energy that should be spent to make first i strings sorted in lexicographical order and i-th of them will be reversed if j = 1 and not reversed if j = 0. dp[i][j] is updated by dp[i - 1][0] and dp[i - 1][1]. It remains to verify that the i-th string is lexicographically greater than (i - 1)-th (if j = 1 then we should check reversed i-th string, similar to (i - 1)-th). Then we update dp[i][j] = min(dp[i][j], dp[i - 1][0] + c[i] * j), dp[i][j] = min(dp[i][j], dp[i - 1][1] + j * c[i]). The answer is a minimum of dp[n][0] and dp[n][1].

Time Complexity: O(n+sum_length).

Solution

## D

Let's store each number in binary system (each number consists of 32 bits, 0 or 1) in such a data structure as trie.The edges will be the bits 1 and 0, and the vertices will be responsible for whether it is possible to pass the current edge. To reply to a query like "? X" will descend the forest of high-order bits to whether the younger and now we can look XOR in the i-th bit to get one, if we can, then move on, otherwise we go to where we can go.

Time Complexity: O(q*log(10^9)).

Solution

## E

Let's surround the matrix with the frame of elements. In each element of the matrix, and frame we need to store value, the number of the right element and the number of down element. When a request comes we should change only values of the elements along the perimeter of rectangles.

Time Complexity: O(q*(n+m)).

Solution

• +37

 » 6 years ago, # |   +29 Problem B can be easy solved using Binary search
•  » » 6 years ago, # ^ |   +3 I think it's so classical binary search problem you can copy it's code simply from google!
•  » » 6 years ago, # ^ |   0 will it not be the case then complexity is more. log(n) for each search. so time complexity qlog(n), also for sorting nlog(n). so finally time complexity = max(nlog(n), qlog(n)).
•  » » » 6 years ago, # ^ |   +3 Yeah, but the idea is more obvious to some of us, and the code is really really simple, too.
•  » » » » 6 years ago, # ^ |   0 http://www.codeforces.com/contest/706/submission/19794976 Got tle using binary search !
•  » » » » » 6 years ago, # ^ |   +4 Binary Search in cpp worked just fine!
•  » » » » » 6 years ago, # ^ |   0 The problem is here while(index >=0 && x.get(index)==p+1) index--; 
•  » » » » » » 6 years ago, # ^ |   0 What if there are multiple values for key in binary search ( java) ?? What does the method return ? ( i assumed any one of those values)
•  » » » » » » » 6 years ago, # ^ |   0 It randomly returns any index of the number you are currently searching for
•  » » » » » » » » 6 years ago, # ^ |   0 So there is no workaround for finding the end range of a value other than lineaely traversing in one direction after bin. searching ????
•  » » » » » » » » » 3 years ago, # ^ |   0 I guess you can easily modify the normal binary search to return first value greater than the target. Hint: https://www.geeksforgeeks.org/first-strictly-greater-element-in-a-sorted-array-in-java/
•  » » » » » » 6 years ago, # ^ |   0 i tried it with binary search but whhhy only 4 tests there is help ?
•  » » » » » 3 years ago, # ^ |   0 https://codeforces.com/contest/706/submission/61466118 Check this i just found the upper bound with STL inbuilt function. Worked for me!
•  » » » » » » 2 months ago, # ^ |   0 beautiful
•  » » » 6 years ago, # ^ |   0 shouldn't the complexity be n * log(n) + q * log(n) ?
•  » » 6 years ago, # ^ |   0 Yes.I think so.
•  » » 6 years ago, # ^ |   0 data aint sorted :|
•  » » 6 years ago, # ^ |   0 I don't even have to implement binary search. Just straight using sort() and upper_bound() function in c++.
•  » » » 5 years ago, # ^ |   0 same
•  » » 6 years ago, # ^ |   0 No , i tried to solve it by binry search and I got TLE.
•  » » » 6 years ago, # ^ |   +1 I got accepted with binary search on bits.
•  » » » » 6 years ago, # ^ | ← Rev. 2 →   0 Yes i just saw an answer solved by binary search. I was actually using simple iteration to check how many elemants were equal to the given query because of which i was getting TLE.
•  » » » 6 years ago, # ^ |   -6 Your hands are crooked.
•  » » » » 6 years ago, # ^ |   0 Excuse me, why would you say that?
•  » » » » » 6 years ago, # ^ |   +14 I mean that if your solution with binary search gets TL, you do something wrong.
•  » » » » » » 6 years ago, # ^ |   0 Yes, I already said that in one of my above comments.
 » 6 years ago, # |   +20 Don't understand the approach for E ! Can someone explain it better ?
•  » » 6 years ago, # ^ | ← Rev. 3 →   +53 This problem is an exercise in Linked Lists: store the matrix as a 2D array of elements each of which has links to its upper, lower, left and right neighbours. To swap two rectangles you only need to consider their borders. Tear the rectangles off from the matrix by erasing the needed links (i.e. erase all upper neighbour links to tear the upper border of a rectangle) and stitch them back in each other's spots. Note that you lose the ability to ask for a cell in (r, c) by a 2D array lookup but should instead start from the upper left corner and walk r down and c right links.To avoid null pointers, surround the matrix by a frame of fake cells, as the editorial suggests.Example solution: http://codeforces.com/contest/706/submission/19808792
•  » » 6 years ago, # ^ |   +40 One thing to note about swapping submatrices is that the actual submatrix "structure" doesnt change, take a look at the following masterpiece (orange is left neighbour, red is right, blue is up, and green is down).so say i swap the top left 2x2 submatrix(1,2,5,6) with bottom right 2x2 submatrix(11,12,15,16). the INTERNAL arrows never change, because the actual submatrices still have the same elements. However, the perimeter arrows change, because we now have new neighbours due to swapping matrices. In order to fix the structure, all we have to do is swap the corresponding perimeter arrows, so for example 15 green arrow will point at 9, 12 red arrow will point at 3 etc. hope that helps!
•  » » » 6 years ago, # ^ | ← Rev. 3 →   0 I received TLE on test case 54, I wonder if I am missing some optimization of the linked list or it's just because of poor implementation?http://codeforces.com/contest/706/submission/19831761UPD: Turns out it's just because of poor implementation, changed the iteration method to reduce iteration time and it managed to slithered through the TL. http://codeforces.com/contest/706/submission/19832612
•  » » » 6 years ago, # ^ |   0 thanks for the further explanation
•  » » 6 years ago, # ^ |   0 Help with solving E needed, I got TLE on test 10 all the time, could anyone explane why does it give TLE? Check the last submission pls http://codeforces.com/submissions/GuralTOO
•  » » » 6 years ago, # ^ |   0 Got rid of TLE, by simply using scanf/printf now have trouble with WA..
•  » » » » 6 years ago, # ^ |   0 Check your nested for loops, some of them are for looping n then n, rather than n then m (which explains why you get WA on that testcase, because its the first one where n
•  » » » » » 6 years ago, # ^ |   0 Man, thank you a lot, was typing fast, so didn't notice.. that fault, thank you again
 » 6 years ago, # |   0 why this submission: 19811194 gets memory limit exceeded?? It's memory complexity is O(q lg MAXa) which is ok for this problem!
•  » » 6 years ago, # ^ |   0 i am not sure but maybe this is the reason :you are right about O(q*logMax) but here you have a constant like 2*5=10. 2 because of creating both nodes at each step not just one. 5 because of struct size (pointer size is 32 bit).
 » 6 years ago, # |   +23 Thanks for a really awesome problem set. Really enjoyed the contest.
 » 6 years ago, # | ← Rev. 2 →   +7 Isn't complexity of D just O(q)? That log factor is constant.Btw, u can solve D without any data stucture, finding bits from most significant one with lowerbound on set. The main idea is the same, but u don't have to know trie. Here's my code — 19807490, complexity is O(q*log(q)).
•  » » 6 years ago, # ^ |   0 please be more specific on your binary search on set please?
•  » » » 6 years ago, # ^ | ← Rev. 2 →   0 Ok, look. First of all we have l = 0, r = maximal value of unsigned int. y = bitwise negation of x — best-case scenario for xor, our reference point. We look over bits of y from 31th to 0th.If pth bit is set (y & (1<
•  » » » » 4 years ago, # ^ |   0 This is a beautiful solution :)
 » 6 years ago, # |   +4 In the explanation of problem D, what does RRF mean?
 » 6 years ago, # |   0 i have problem with time complexity analysis of problem C. let us do worst time complexity analysis. for each case say we have to reverse . reverse function has linear time complexity and for each case we are reversing hence time complexity should be O(n^2) or 10^5 * 10^5 = 10^10.i think then this algorithm should get time limit exceeded verdict in test case where reverse permutation of a string of length close to 10^5 is used.
•  » » 6 years ago, # ^ |   +5 From the problem statement: The total length of these strings doesn't exceed 100 000.
•  » » » 6 years ago, # ^ | ← Rev. 2 →   +5 Yeah: the reverses and comparisons will alltogether be O(n), or amortized O(1). Also imagine that your worst case was possible, it would mean that each string has 10^5 characters, which means you couldn't even input the input without getting TLE.
 » 6 years ago, # | ← Rev. 3 →   -8 I have a solution for problem C that does not need DP 19812078:It basically "tries" both the string and its reverse against all possible previous choices. class Choice(String s, Long cost) choices := List.of(new Choice("", 0)) for (cost <- costs) { s := readInput() r := s.reverse() current := List.of(new Choice(s, 0), new Choice(r, cost)) next := new Map() withDefault Long.MAX for(c1 <- choices) { for(c2 <- current) { if (c1.s.isEmpty || c1.s <= c2.s) { next[c2.s] = min(next[c2.s], c1.cost + c2.cost) } } } choices = next.map((k, v) => new Choice(k, v)) } if (choices.isEmpty) print(-1) else print(choices.values.min) 
•  » » 6 years ago, # ^ |   +1 So you only store the last row in your DP
•  » » » 6 years ago, # ^ |   +1 Exactly: it's still the same logic as the regular DP.
•  » » » » 6 years ago, # ^ | ← Rev. 2 →   -13 Ah, you are right — now I see the similarity. Atleast when I was writing the code, I did not think I was doing DP :)
•  » » » » » 6 years ago, # ^ |   0 Brute force would be if you also went all the way back to the first string when trying an option for the current one. Using the fact that the previous string is enough (and storing the corresponding scores) makes it DP :))
 » 6 years ago, # | ← Rev. 3 →   +5 Hi, last night I misunderstood the problem C: they said "total length of the strings <= 100 000" but I thought "length of each string <= 100 000" So I came up with the idea "how to compare 2 strings only in O(log(length(string)), and we also don't have to reverse it in O(length)", based on binary search. Thus, the general complexity is O(qlog(length)) So I wrote a "compare(string s1, string s2, int type)" function: type = 1: compare non-reverse s1 and non-reverse s2 type = 2: compare non-reverse s1 and reverse s2 type = 3: compare reverse s1 and non-reverse s2 type = 4: compare reverse s1 and reverse s2 Then I submitted and got WA, so I hope somebody could take a look at my submission and point out what I was wrong. I'm sure I got wrong in my "compare" function because when I use standard "compare" and "reverse" functions of C++, I got AC ( of course the complexity now is O(length) ).Ty in advance.My submission: 19808155
•  » » 6 years ago, # ^ | ← Rev. 2 →   +6 Binary searching like this is wrong, because comparing the middle of two strings doesn't tell you anything about the characters before it or after it. You should compare entire prefixes of the strings, and to do so efficiently you'd need hashing, but that means that before the binary search you'd already do O(length) operations for the hashing.
•  » » » 6 years ago, # ^ |   0 You're right, thanks
 » 6 years ago, # |   0 How do we prove the solution to B is correct ?
•  » » 6 years ago, # ^ |   0 By greedy, you want to buy the X-cheapest items available anyways.
 » 6 years ago, # |   0 What is RRF in problem D?
 » 6 years ago, # |   0 Problem C: I have one doubt.Let's suppose the given order of string is : Let's call them by name A, B, C A B Cif we needed to reverse B (rB) to be lexicographically smaller than or equal to B, then we can't check for B with C, we need to check for rB with C only. I.e. we can't reverse the reversed string for next comparisions. How is this case handled in the dp solution? Can anyone explain please?
•  » » 6 years ago, # ^ | ← Rev. 3 →   0 That is the reason why we have to add the "state index" 0/1 to our dp array (that's what I call it)As you said, dp[i] is not enough, because we would not know if (i-1)-th string is reversed or not to calculate exactly dp[i].But everything will be easier if we have the state for each string we're considering: dp[i][0] means the minimum result if we don't reverse i-th string dp[i][1] means the minimum result if we reverse i-th string Thus, the (i-1)-th string will also have 2 states: reversed or not. dp[C][0], dp[C][1] will be calculated based on dp[B][0] (B) and dp[B][1] (rB)
 » 6 years ago, # |   0 I still dont understand prob C. Is the dp actually like brute force? wont it take O(2^n)?also, in prob D I tried something like putting in integer form in list. I dont understand what the editorial says when it says to put in binary.(I am new to codeforce!!)
•  » » 6 years ago, # ^ |   0 dp isn't brute force. You update dp[i][0] and dp[i][1] in O(s[i].length). So total complexity is O(sumLength).
•  » » 6 years ago, # ^ |   +5 The point of dp is to memorize previous choices that won't be affected by later choices, so that we could save the time for computing the previous choices again when we proceed to later choices.In problem C the states store the value of previous computed strings, since it is guaranteed that it all the previous strings have been ordered in lexicographical order (or just set the value to INF if it is impossible to do so), you only have to compare the latest string to the reversed or the original string to check the minimum costs of following the lexicographical order.For problem D I can't find your submission so I don't know what exactly got you, but I suppose that you have completely no idea of what the editorial is doing.The solution is to build a tree/trie, that stores integers in binary form (eg: 5=101(2), 7=111(2) ). You have to maintain the amount of values in the subtree in order to answer the value. If you don't know about Trie, or even some other tree structures like BIT(fenwick tree), read them before solving this problem, or else you will have absolutely no idea what I am explaining.After building the trie of the multiset, you have to check 1. if there is an edge to the left child(0) and the right child(1), and if the left/right child is empty (cnt==0). Make the optimal steps upon visiting each nodes, and sum up the values of xor for the query results.
 » 6 years ago, # | ← Rev. 2 →   0 I'm still learning DP, so confused about problem C.We both know if we want apply DP to some problem, the problem must meet several conditions.One is that it must have optimal sub-structure. One is that the sub-problem has non-aftereffect property.So here is my question, if we already solve the first i query, but the i+1 th string is smaller than the i th but the cost c[i+1] is too big, then we should not reverse the i+1 th but modify the first i string, how's that meet the optimal sub-structrue and non-aftereffect property? Because the i+1 th string has a big influence on the answer we've already calculated before.I don't know where I get wrong, thanks for your help.(sorry for poor English
 » 6 years ago, # |   0 I have solve C using dp , but getting wrong answer. Can someone help me to debug.... I use dp1[] for reverse string and dp[] for simple string. ~~~~~ ..#define For(i,st,ed) for(i=st;i<=ed;i++)const ll mod=1000000007;//Mainint main() { ios_base::sync_with_stdio(false);cin.tie(0); int test=1,i,j,n,tt; //cin>>test; For(tt,1,test){ cin>>n; string st[n+1]; ll dp[n+1],dp1[n+1],a[n+1]; string s,s1; ScanA(a,n); dp[1]=0; dp1[1]=a[1]; For(i,1,n) cin>>st[i]; For(i,2,n){ s.assign(st[i]); s1.assign(st[i-1]); reverse(s1.begin(),s1.end()); dp[i]=dp1[i]=-1; ll x=-1; if(s.compare(st[i-1])>=0 && dp[i-1]!=-1) x=dp[i-1]; if(s.compare(s1)>=0 && dp1[i-1]!=-1) if(x!=-1) x=min(x,dp1[i-1]); else x=dp1[i-1]; dp[i]=x; x=-1; reverse(s.begin(),s.end()); if(s.compare(st[i-1])>=0 && dp[i-1]!=-1) x=dp[i-1]+a[i]; if(s.compare(s1)>=0 && dp1[i-1]!=-1) if(x!=-1) x=min(x,dp1[i-1]+a[i]); else x=dp1[i-1]+a[i]; dp1[i]=x; } ll x=mod; if(dp[n]!=-1) x=dp[n]; if(dp1[n]!=-1) x=min(x,dp1[n]); if(x==mod) x=-1; cout<
•  » » 6 years ago, # ^ |   0 Change const ll mod=1000000007; to const ll mod=100000000000007; A bigger number so to say. As the sum of the numbers in the dp array can exceed 10^9+7.
•  » » 6 years ago, # ^ |   0 Side note: My personal way to set the INF for long long is (1LL<<60), I have also seen some pretty good ways such as "1234567890123456789012345LL"
•  » » » 6 years ago, # ^ |   0 Or just use scientific notation, eg: 1e9 + 7 or 1e18. I think it's more understandable and not as much error-prone as "1234567890123456789012345LL"
•  » » » » 6 years ago, # ^ |   0 1e9+7 and 1e18 are without doubt great ways to do it too, it's just a personal preference after all. =D
 » 6 years ago, # |   0 Explain prob D more properly plz . Whats RRF?
•  » » 6 years ago, # ^ | ← Rev. 4 →   +7 19825442 is my commented solution(C++) for you. You can do it easily with map. Time Complexity: O(q*31*log2(10^5)).more explained submission: 19825804
 » 6 years ago, # |   0 Can anyone please explain the D solution in more detail or from another perspective?
•  » » 6 years ago, # ^ |   +6 Quora — TrieThe link explains some tricks using tries, including what you need to solve D
 » 6 years ago, # |   0 What does the announcement mean ? "The lexicographical order is not required to be strict, i.e. two equal strings nearby ARE ALLOWED."
•  » » 6 years ago, # ^ |   0 That means it's still ok if s1<=s2 , it's not required for s2 to be strictly than s1 (i.e. s2>s1) to be considered as being ordered lexicographic-ally.
•  » » » 6 years ago, # ^ |   0 ohh, thanks.
 » 6 years ago, # |   0 Link for Solution of problem C gives error
•  » » 6 years ago, # ^ |   0 click top-right link!!
»
6 years ago, # |
-25

# include <bits/stdc++.h>

using namespace std;

int main() { int n; scanf("%d",&n); int a[n]; for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); int q; scanf("%d",&q); while(q--) { int m; scanf("%d",&m); int ans=int(upper_bound(a,a+n,m)-lower_bound(a,a+n,a[0])); printf("%d\n",ans); } }

//My solution using C++ STL .

 » 6 years ago, # |   0 Solution for D with std::multiset: just like in solution with trie, for the ith bit, first we try to get 1, if it's not possible we will get 0. But we can check this using lower_bound.Code: 19826426
 » 6 years ago, # |   0 The solution for problem E is just beautiful :)
 » 6 years ago, # | ← Rev. 2 →   0 These are the first few lines of Test #7 100 ? 1 + 1 + 4 ? 5 ... My code prints 1 4 But judge prints 1 5 Please explain how output is 5 for second line. I got 4 by computing: max(5 XOR 1, 5 XOR 4) = max(4, 1) = 4 Thanks in anticipation. 
•  » » 6 years ago, # ^ |   0 5^0 = 5
•  » » » 6 years ago, # ^ |   0 Jeez! Thanks.
•  » » 6 years ago, # ^ |   +5 "You are given q queries and a multiset A, initially containing only integer 0"the set initially has a 0 and 5 XOR 0 = 5
•  » » » 6 years ago, # ^ |   0 Thank you. Now AC.
 » 6 years ago, # |   0 If anyone is interested in practicing problems similar to problem D, here is a question I realized that the solution is quite similar to problem D.282E — Sausage maximization
 » 6 years ago, # | ← Rev. 2 →   0 I just want to make something sure about problem E. a)Swapping two rectangles like the red and green one is not allowed, right?b)And if such an operation were allowed, would there be an efficient method to solve this problem?update: solved it. So, I am clear about (a). So if anyone could tell me about (b)...
•  » » 6 years ago, # ^ |   0 I have never implemented this but I think this may work.1) Keep two vectors of coordinates of the perimeter, ordered corresponding to their position.2) When swapping, check if the block next to the swap is occupied by another block. If no, just do the operation required for E. If yes, check for the corresponding position in the original block, and link it to it.Keeping the difference in position of the blocks should help speed up the look-up process to O(1), so the whole algorithm should work in O(n).This is not applicable for overlapped blocks though.
 » 6 years ago, # |   0 There is error shown by hosting site . Please rectify it .
 » 6 years ago, # | ← Rev. 2 →   0 Does anyone have an idea for problem E in O(qnlogn) ? With data structures like segment tree?
•  » » 3 years ago, # ^ |   0 yeah that what i thinked of i think it's possible to do it with 2d segment tree with lazy propagation and it will be q*log^2(n)
 » 6 years ago, # |   0 The solution link for C seems to be broken. Can you fix it?
 » 6 years ago, # | ← Rev. 5 →   0 For the 1-dimensional version of Problem E, is there a solution with faster than linear time swaps?So in the 1-dimensional version, instead of swapping rectangles in a matrix we're swapping ranges in an array: each task is (a, c, w) and we want to swap the values in [a, a + w) with the values in [c, c + w).And with n items and q swap queries, we want an O(q * logn + n) algorithm, or just anything faster than O(qn).With the linked list solution in the post (which is super cool to me!), in the 1-d case it takes O(n) to find the rectangles, even though it's an O(1) swap after findingI was trying to do something with a binary search tree: each range corresponds to <= logn nodes in the tree, so maybe you could swap ranges by swapping only those logn nodes somehow, and their sub-trees would automatically come with them.The problem is that the ranges don't "line up": I somehow managed to convinced myself this wasn't really a problem, but I was totally off, and it is a serious issue, as far as I can tell.Anyway, yeah, is there a quicker solution to the range-swapping problem? I.e. like an O(n) size data structure on n items which supports fast range swaps?EDIT: answer to question = yes treapweaker guarantees than with treap i'd guess, but i think a skip-list would work too with high probability. You can find ranges quickly and then edit O(# lists) = O(log N) pointers.
•  » » 6 years ago, # ^ |   0 Treap makes swaps in O(logN)
•  » » » 6 years ago, # ^ | ← Rev. 6 →   0 EDIT: I'm not sure if what I said below really even makes sense actually; I'll think about this for a bit/try to understand treaps better.EDIT2: Okay I sort of get how the treap might work, although I'm still not sure how it'll necessarily stay balanced after lots of swaps (with high probability I guess? or it happens only after many swaps, so you can amortize any rebalances if they're needed or something). Thanks a lot for the response! I didn't know about treaps, they're really cool!But I don't see how a treap would do it though. This is how I imagine a treap approach would go:You construct a treap on [0, n), say as a balanced BST (and then assign the corresponding priorities).And then on a query (a, b, w) with a < b, you split the treap into 5 smaller treaps, t1=[0, a), t2=[a, a+w), t3=[a+w, b), t4=[b, b+w) and t5=[b+w, n)Then we merge them back together in the order t1, t4, t3, t2, t5.Splits and merges are fast, so this should be fast..But: naively, in order for the treaps to be merged correctly, you'd need to update the priorities of every node in t4 & t2, which would take linear time.Maybe there's a way to store these updates to the priorities in a more succinct/clever way, but I don't see it. (of course I might be missing something!)
 » 6 years ago, # | ← Rev. 4 →   0 Hello friends ! :D I would like to talk with you about problem D which i found very interesting as i've learned new data structure(trie)... I've failed on task 9 for some reason even tho i am pretty sure my logic is good.I wrote it in C language and i will explain to you what i've did to solve this problem in hope that someone will help me find flaw in my logic...http://codeforces.com/contest/706/submission/19910611I've constructed an trie,for every input,31 bits,i do not "re add" numbers that are already there which i track using a self made map.In structure i have int "cnt" which helps me with knowing how many unique inputs are there in specific node which will help me later when i delete a node from trie.The way i find the answer is that im trying to locate(if exists) an node that has different bit from the one i am looking at atm for example number 11011 i would start by finding a bit different then 1(0) etc etc. untill i reach the end(i think this part of logic is fine).But the tricky part comes when deleting the specific number..The way i solved it is that i kept track of how many specific numbers i added with '+' command,after i use '-' command i act only if the number in set == 0. In that case i traverse trough the trie using that number's bits,the same moment i reach the node that has "cnt" == 1 which means that the node is being used only by that unique number,i just release it from the trie with everything connected to it (as it was everything connected to the number i wanted to delete).Thanks so much ! Cheers !UPD: I found out bug,the problem was that if for example trie is like this | | | / \ And if if first stub belongs to first and second number and if i want to remove 2nd number i remove right most node but when it is time to remove the first one i only remove the left most one as i did not mark that i am finished with main stub(as cnt of it should be 1 instead of 2).Thanks anyways.
 » 6 years ago, # | ← Rev. 2 →   0 Why this solution of problem D got RE? Help me, please.
 » 6 years ago, # | ← Rev. 2 →   0 problem D : more clear code : http://codeforces.com/contest/706/submission/19944188 this post is awesome : https://threads-iiith.quora.com/Tutorial-on-Trie-and-example-problems
 » 6 years ago, # | ← Rev. 3 →   0 edit : found my mistake
 » 6 years ago, # |   0 Hi guys, I have implemented problem E by Python, anyway got TLE at test 10, can anyone help on thishttp://codeforces.com/contest/706/submission/19963732Thanks in advance
 » 6 years ago, # |   0 hey guys I'm getting WA on test 91 in problem D and i have no idea why. can anyone help me? this is my submission http://codeforces.com/contest/706/submission/20033141 i would really appreciate it
 » 6 years ago, # | ← Rev. 2 →   0 Hello everyone!Can you help me why I get TLE for Problem D? Here is my submission : http://www.codeforces.com/contest/706/submission/20083814 I used binary search between lo and hi, which are iterators that contains maximum values. But it got TLE on test#2. I think it's because of case when lo > hi, but I can't find out when.Thanks in advance
•  » » 6 years ago, # ^ |   +8 std :: lower_bound works in linear time for multiset\set so use s.lower_bound(x).
•  » » » 6 years ago, # ^ |   0 Oh! I didn't know that! Really appreciate your answer _index :)
 » 6 years ago, # |   0 Please fix problem D solution!!
 » 6 years ago, # |   0 Solution links to problem C and D broken. Any fixes? Please tell.
 » 6 years ago, # |   0 I solved D but when I submitted it in the system it told me that i got a wrong answer on test 1 . It told me my answer is '0 0 0 0 ' but in my computer it runs well. Why?....
 » 6 years ago, # |   0 Nice Editorial :)
 » 4 years ago, # |   0 I have encountered pastie.org to be unavailable a number of times.
 » 4 years ago, # |   0 Can some help me with the tutorial of problem D? I don't seem to understand this statement, "To reply to a query like "? X" will descend the forest of high-order bits to whether the younger and now we can look XOR in the i-th bit to get one, if we can, then move on, otherwise we go to where we can go."Thanks in advance.
 » 4 years ago, # |   0 I wrote about Problem C in this post here. It's an enjoyable DP question.
 » 4 years ago, # |   0 please someone help me to get what is wrong in my approach with problem C. link to my code.. [(http://codeforces.com/contest/706/submission/39018222)]
 » 3 years ago, # | ← Rev. 2 →   0 Hello Everyone, For the Problem C, I was getting Wrong Answer on test case 18 when I was setting dp[i][0] and dp[i-1][1] to LONG_MAX but when I set it to (1LL<<62) then I was getting Accepted verdict. But (1LL<<62) is less than LONG_MAX. Can anyone explain me the reason for it. Thank You in Advance. WA with LONG_MAX https://codeforces.com/contest/706/submission/48478924 AC with (1LL<<62) https://codeforces.com/contest/706/submission/48479243Update: I found out that on my system LONG_MAX is 2^63-1 and is equal to LONG_LONG_MAX but on online judge it is different and LONG_MAX is 2^32-1
•  » » 23 months ago, # ^ |   0 Thanks. Same issue.
 » 3 years ago, # |   0 Why wrong answer on Test case 5 of problem B? https://codeforces.com/contest/706/submission/50317848
 » 3 years ago, # |   0 Why my solution fails on test case 8 ? submissionAnyone?
 » 3 years ago, # |   0 I simply used count array. and then used prefix sum array logic it worked perfectly!! But is their any efficient solution for this problem
 » 3 years ago, # | ← Rev. 2 →   +6 Why am I getting this message when I click on the solution link in problem E.404 Not Found
 » 2 years ago, # |   0 For question D:https://codeforces.com/contest/706/problem/DCan someone explain logic behind this submission https://codeforces.com/contest/706/submission/19826426 ? It seems extremely simplistic and doesn't seem to use tries.
 » 2 years ago, # |   0 I did problem C without DP.But I got wrong answer on test case-8. This is my submisson: 79961667
 » 2 years ago, # |   0 Problem C I am not able to pass test case 8 on the juge onwards. what's wrong with my logic ? `#define int ll const int mxN = 2e5 + 10; int n; int C[mxN]; void solve() { cin >> n; for (int i = 0; i < n; ++i) cin >> C[i]; vector> is(n, vector(2)); vector> dp(n, vector(2, INF)); for (int i = 0; i < n; ++i) { cin >> is[i][0]; is[i][1] = is[i][0]; reverse(is[i][1].begin(), is[i][1].end()); } dp[0][0] = 0; dp[0][1] = C[0]; for(int i=0; i
 » 2 years ago, # |   0 In B upper_bound works but not binary search TLE 81591607 AC 81594037
 » 2 years ago, # |   0 https://codeforces.com/contest/706/submission/84959926 Please help,I am trying to do with tree,and not able to understand why MLE is coming on last test case
»
23 months ago, # |
-8

# include<bits/stdc++.h>

using namespace std; int main() { int a,b,n,temp; double d,res,time; vector x,y,v; cin>>a>>b; cin>>n; for(int i=0;i<n;i++) { cin>>temp; x.push_back(temp); cin>>temp; y.push_back(temp); cin>>temp; v.push_back(temp); } d=(double)(a-x[0])*(a-x[0]) + (b-y[0])*(b-y[0]); d=(double)sqrt(d); time=(double)d/v[0]; res=time; for(int i=0;i<n;i++) { d=(double)(a-x[i])*(a-x[i]) + (b-y[i])*(b-y[i]); d=(double)sqrt(d); time=(double)d/v[i]; if(time<res){ res=time; } } cout<<res<<setprecision(10); return 0; } //wrong answer on test 41...dont no why???

 » 22 months ago, # |   0 Used binary search in this question with a little modification but am getting some wrong answers in 5th test case. Looks like some edge case is missing, can anyone please help me out?? here is my submission:https://codeforces.com/contest/706/submission/91194271
•  » » 5 months ago, # ^ |   0 Don't use binary search technique because it will get TLE, try to use upper_bound built in function to solve it.
 » 17 months ago, # |   0 How can solve problem C using recursive-DP?
 » 15 months ago, # | ← Rev. 2 →   0 Please help Me I am getting WA on Problem C TC-8 link :https://codeforces.com/contest/706/submission/110875540 Update: Got AC by using a user defined string comparator insted of integer compairing operator
 » 3 months ago, # |   0 Nice Could someone code problem c in cpp