Блог пользователя oursaco

Автор oursaco, история, 3 года назад, По-английски

Sorry for the weak pretests on A :(. We hope you enjoyed the round nonetheless.

1904A - Forked!

Idea: Apple_Method
Preparation: Apple_Method/oursaco
Analysis: lunchbox

Solution
Code

1904B - Collecting Game

Idea: oursaco
Preparation: oursaco
Analysis: lunchbox

Solution
Code

1904C - Array Game

Idea: lunchbox
Preparation: lunchbox
Analysis: lunchbox

Solution
Code

1904D1 - Set To Max (Easy Version) / 1904D2 - Set To Max (Hard Version)

Idea: oursaco
Preparation: oursaco
Analysis: oursaco

Hint 1
Hint 2
Solution
Code

1904E - Tree Queries

Idea: Apple_Method
Preparation: oursaco

Editorial 1:
Analysis: oursaco

Thanks to errorgorn for the solution!

Hint 1
Hint 2
Solution
Code

Editorial 2:
Analysis: willy108

Hint 1
Hint 2
Hint 3
Solution
Code

1904F - Beautiful Tree

Idea: Apple_Method
Preparation: oursaco
Analysis: Apple_Method

Hint
Solution
Code
Разбор задач Codeforces Round 914 (Div. 2)
  • Проголосовать: нравится
  • +135
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

i love oursaco

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

after this contest i realised, that chess tasks are not for me)) i solved B easily, mb im stupid for A idk

»
3 года назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

Got TLE in B on system testing. Did two wrong submissions also on B. I was hoping to be blue today. But missed the chance.

»
3 года назад, скрыть # |
 
Проголосовать: нравится -12 Проголосовать: не нравится

What's wrong with this solution for A? 236554300

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I liked D2 on how simple its implementation really was. Even though I implemented sparse table and range update segment trees, realizing it only required monotonic stack was pretty cool.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone tell me why did I get TLE here ? https://codeforces.me/contest/1904/submission/236554358.

I think the complexity here is O(N*N*log(N)). Am I wrong?

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Thanks for contest

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone try hacking this solution to D2?

»
3 года назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Loved this competition! My solution to problem C first failed on system testing, but then suddenly the verdict changed) Also, my friend’s solution failed on the same test, but it changed the verdict after the final results! Can someone please explain how this could be, because I just don't understand how system testing works on Codeforces. Sorry for my poor language...)

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can somebody help me with the reason of tle on d2(hard version) on test case 25 .here's my submission — https://codeforces.me/contest/1904/submission/236593367

»
3 года назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

Cool Contest!

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For D1/D2, I had a solution I'd like to share.

Firstly, if any position exists such that a[i] > b[i], then obviously there's no solution.

Then, I iterated over the numbers from 1 to n, and for each i, I tried to find all the intervals centered around a position j such that a[j] = i. I then tried to extend it as much to the left and right as possible without messing any conditions up. I found that it you did this for all numbers from 1 to n in increasing order, then you'll either construct a valid solution or find that it's impossible to do so. You can optimize this a bit for D2 by binary searching how far to the left and right you'll extend your current position to and using range assignment and range query operations to check if the intervals you're using are okay (You can do that with a lazy segment tree).

In essence, it's similar to the official solution except you actually construct the solution in this case instead of determining if it's possible. Here's my submission: 236596139

It barely passes in time lol

»
3 года назад, скрыть # |
 
Проголосовать: нравится +12 Проголосовать: не нравится

It will be better if F offers stronger samples.

I finished coding and passed samples in 15 min but got WA on #3, and failed to debug in remaining 30 min.

Anyway, A~D are enjoyable.

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Nice problems. D2 systests were pretty weak though.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In problem E, how are we handling the case when path is outside of current subtree?

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    If you reroot the tree to $$$x$$$ correctly, then each leaf in the segment tree will store the distance of a node to $$$x$$$ and this isn't limited to just $$$x$$$'s subtree. Doing a max query on all the non blocked intervals will then find the maximum distance to all reachable nodes.

»
3 года назад, скрыть # |
 
Проголосовать: нравится +23 Проголосовать: не нравится

Was the TL and ML for problem F a little tight in this contest? I see a lot of submissions here which have taken > 3 seconds.

I have roughly the same solution as the editorial, except that my graph is built with edges denoting >= relationships (instead of > relationships), then I find the SCCs of the graph, and the remainder of the construction is roughly the same. Or is there something else extremely slow about my code? I could get it to pass very narrowly with pragmas though.

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Maybe to solve the memmory problem,you should use tree chain dissection.

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    But the time complexity is O(log^2n) However

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится +16 Проголосовать: не нравится

    I ran into a similar problem. I tried implementing the editorial's idea and got TLE (later cloned the contest and checked that it passed in 4788ms), then changed how i did toposort and barely got AC.

    What seems to have happened is that they only had 1 AC solution that passed in 2s (1793ms in my testing) and used the standard "make the TL 2x the model solution", but the way they get the nodes to draw the edges is particularly smart, so it's very easy to make something inneficient compared to it and end up with double or triple the constant factor.

    By the way I did some testing with your code, it passed in 5038ms and 567200KB. Changing the bound for g from MXN * MXK * 3 to MXN * MXK * 2+20 makes it pass the memory limit (482900KB) and changing its type from vector to basic_string also makes it easily pass TL (2761ms).

    • »
      »
      »
      3 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      Thanks a lot for looking into this!

      I had no idea that there was such a substantial improvement possible with basic_string instead of vectors, thanks for pointing that out too!

»
3 года назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

E is a great problem, thank you for such a masterpiece!

»
3 года назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

Can someone help me understand the solution for D.Why does there always exist a valid sequence of operations if we can satisfy the conditions for each element independently

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How to solve problem E if the queries given online?

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone help me in C? The first test gives me MLE and WA but in my computer gives me a correct answer 236711122

»
3 года назад, скрыть # |
← Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

In Problem F, can anyone explain how they are achieving this exactly: "Given a path and a node, add a directed edge from the node to every node in that path"? I can't understand how they can add edges to (or from) all the nodes in the path, won't that take linear time?

Also, in this sentence "we can repeatedly add an edge from that node to the largest node we can" what does "largest" node mean?

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    Imagine creating a binary lifting on the tree. You have $$$bin[i][0] = par[i]$$$, $$$bin[i][1] = par[par[i]]$$$, etc. Here, $$$bin[i][0]$$$ is an auxiliary node that has edges to $$$i$$$ and $$$par[i]$$$. $$$bin[i][1]$$$ is an auxiliary node that has edges to $$$bin[i][0]$$$ and $$$bin[par[i]][0]$$$. Note that this is equivalent to $$$bin[i][1]$$$ having edges to $$$i$$$, $$$par[i]$$$, and $$$par[par[i]]$$$. Similar to how you always jump up the largest power of 2 that fits when you perform binary lifting, you will always add an edge to the auxiliary node with the largest power of 2 that fits. This will always require at most $$$log N$$$ edge additions.

»
3 года назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

There is also a $$$O(nlogn)$$$ online sol for E (although with high constant).

Take the euler tour of the tree (the version where you insert again every time you go to a new node, exactly like this image taken from https://cp-algorithms.com/graph/lca.html ):

$$$\begin{array}{|l|c|c|c|c|c|c|c|c|c|c|c|c|c|} \hline \text{Vertices:} & 1 & 2 & 5 & 2 & 6 & 2 & 1 & 3 & 1 & 4 & 7 & 4 & 1 \\ \hline \text{Heights:} & 1 & 2 & 3 & 2 & 3 & 2 & 1 & 2 & 1 & 2 & 3 & 2 & 1 \\ \hline \end{array}$$$

Lemma: the length of the diameter of a tree is $$$\max(height[a] - 2\cdot height[b] + height[c])$$$ for $$$a \leq b \leq c$$$, being $$$height[i]$$$ the height of the $$$i-th$$$ node in the euler tour. In that case, if $$$v[i]$$$ is the $$$i-th$$$ node in the euler tour, the endpoints of the diameter will be $$$v[a]$$$ and $$$v[c]$$$ and their lca will be $$$v[b]$$$. Proof is an exercise to the reader ;)

Using that lemma, we can ban segments like the solution 1 and if $$$x$$$ is the source node of our query, the answer will be $$$\max(height[a] - 2\cdot height[b] + height[c])$$$ for $$$a \leq b \leq c = x$$$ or for $$$x = a \leq b \leq c$$$, which can be done with a segtree. You just need to remember to rollback your updates after every query. Code: https://codeforces.me/contest/1904/submission/236721097

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hi can someone help me in figuring out why my two pointer approach for problem C is failing?

https://codeforces.me/contest/1904/submission/236751570

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Or can someone share their two pointer approach for problem c?

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can someone please explain what does |a| notation mean in problem C? is it the length of array?

if so, why sorting array in this problem doesn't violate the condition of must pick some (i,j) such that 1≤i<j≤|a|?

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    You are correct. |a| means the length of the array a.

    Essentially, in each operation, you can pick two different elements (can be equal) from the array and compute the absolute differences and put the result back to the array.

    So the order of the elements in the array does not matter and you are free to reorder the array (including sorting it).

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain me how edges are created in "problem F" in simple way and why are they created in that way(thought process)?

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hi thanks for this contest and I hope this is not necro-posting but for $$$B$$$, don't you think it is enough to check if $$$p_i \geq a_{i+1}$$$ and then set the answer of $$$i$$$ equal to the answer of $$${i + 1}$$$? I have an implementation which you can check out 237039030.

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

My submission to problem F. Getting a TLE, unable to figure out why. Its similar to others', like MridulAhi (link). Can someone help?

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

good D2 and E

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone tell me why did we multiply 4001 in this solution

https://codeforces.me/contest/1904/submission/236534459

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    It's not "multiply $$$4001$$$". As the official solution we need set<pair<int,int>>. But in fact we can use set, because every point with small $$$x, y$$$ can be "hashed" by $$$1000x + y$$$. Why it is true? Ok, let $$$(1000x + y)$$$ = $$$(1000z + t)$$$. Then $$$(y - t) = 0 \bmod 1000$$$. But if $$$y, t \leq 100$$$, then $$$y = t$$$ and so $$$x = z$$$. So in fact we just need to use $$$(x, y) \lt - \gt (k * x + y)$$$ for a big $$$k$$$.

»
3 года назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone tell , in problem 3 why this code is not passing the test case 35 of test case 2 . https://codeforces.me/contest/1904/submission/237695706

  • »
    »
    3 года назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +5 Проголосовать: не нравится

    I think it is because your num might not be initialized for some test cases and it is reading the old value from the last test case.

    Removing it will now TLE on test 4: 237886642

    This is probably because you need to sort and erase elements from vector for each iteration of b[]

    Slightly change your code without changing your algorithm can get it accepted: 237887308

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem B

Can anybody explain me why is the last sentence of tutorial is correct? If we have a [3, 8, 4, 5], then sorted it gonna be [3, 4, 5, 8] p — prefix sum [3, 7, 12, 20]. Lets calculate answer for i = 2, pi = 7 and largest aj (pi >= aj) is 5 and j = 2. But the correct answer is 3 because we can collect all 3 numbers if started with 4 -> 3, 5, 8

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    The tutorial is correct. The answer is not j, but answer to j.

    If you start with 5, you can collect all other three. So if you start with 4, since prefix sum is 7, and it is greater than 5, you will have the same answer as if you start with 5, which is also three.

    So it is better to calculate the answer to larger j, then smaller j.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Task E is really cool!

Both of the intended solutions are really cute, the continuous interval is also really cute. Thank you for this task!

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

help please, what is wrong? Why tle? https://codeforces.me/contest/1904/submission/269838822

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone tell me where it might be failing 279271278

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Manmath_H 281079206 ~~~~~~~~~~~~ void solve() { int a,b; cin >> a >> b; int x,y; cin >> x >> y; int p, q; cin >> p >> q;

int delx[2] = {a, -a};
int dely[2] = {b, -b};

set<pair<int,int>> st;
set<pair<int,int>> stt;

for(int i: delx){
    for(int j: dely){
        st.insert({x+i, y+j});
        st.insert({x+j, y+i});
        stt.insert({p+i, q+j});
        stt.insert({p+j, q+i});
    }
}
int cnt = 0;
for(auto it : st){
    for(auto jt : stt) {
        if(it == jt)    cnt++;
    }
}
cout << cnt << endl;

} ~~~~~~~~~~~~~~

»
16 месяцев назад, скрыть # |
← Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

Deleted .