Sokol080808's blog

By Sokol080808, history, 10 months ago, translation, In English

1843A - Sasha and Array Coloring

Idea: Sokol080808, prepared: molney

Tutorial
Solution
Rate the problem

1843B - Long Long

Idea: EJIC_B_KEDAX, prepared: molney

Tutorial
Solution
Rate the problem

1843C - Sum in Binary Tree

Idea: Sokol080808, prepared: molney

Tutorial
Solution
Rate the problem

1843D - Apple Tree

Idea: EJIC_B_KEDAX, prepared: Vladosiya

Tutorial
Solution
Rate the problem

1843E - Tracking Segments

Idea: meowcneil, prepared: meowcneil

Tutorial
Solution
Rate the problem

1843F1 - Omsk Metro (simple version)

Idea: EJIC_B_KEDAX, prepared: Sokol080808

Tutorial
Solution
Rate the problem

1843F2 - Omsk Metro (hard version)

Idea: EJIC_B_KEDAX, prepared: Sokol080808

Tutorial
Solution
Rate the problem
  • Vote: I like it
  • +172
  • Vote: I do not like it

»
10 months ago, # |
  Vote: I like it +2 Vote: I do not like it

Good contest. Ah Why I didn't think of int overflow

  • »
    »
    10 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    You should try to read statements more carefully. At the end of the output section in B was stated:

    "Pay attention that an answer may not fit in a standard integer type, so do not forget to use 64-bit integer type."

    And also, the problem name was a hint :)

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    i had the same problem but then realized the problem name is literally long long

»
10 months ago, # |
  Vote: I like it +53 Vote: I do not like it

Once again, thank you for this fun contest! :D

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Why does my code output 0 in all queries in D problem? Submission Id: 210522059 ~~~~~ void dfs(int apple){

visited[apple] = 1;

for(auto it = adj[apple].begin();it<adj[apple].end();it++){
    if(!visited[*it]){
        dfs(*it);
        leaves[apple]+=leaves[*it];
    }
    // else leaves[apple]+=leaves[*it];
}
if(adj[apple].empty()){
    leaves[apple]++;
    // cout<<"Leaves["<<apple<<"] is : "<<leaves[apple]<<"\n";
    // visited[apple] = 0;
    return;
}
// cout<<"Leaves["<<apple<<"] is : "<<leaves[apple]<<"\n";
// visited[apple] = 0;
return;

}

~~~~~

  • »
    »
    10 months ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    It is because of the way you are reading the graphs as input, you are making the larger vertex number the child and the smaller one the parent which is not always true

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You using directed graph instead of undirected graph. While you run dfs only for vertex 1. Consider an graph:

    5 
    1 3
    3 2
    2 4
    5 2
    

    where your vector list represent like that

    1 -> 3

    2 -> 3 , 4 , 5

    Now see your dfs start from 1 then 3 and then it was stopped. because there are no connection from 3. So leaves array are not changed. Just changes in directed to undirected, your code will pass the cases. if you use undirected graph then the vector list look like —

    1 -> 3

    2 -> 3, 4, 5

    3 -> 1, 2

    4 -> 2

    5 -> 2

    then your dfs will run for all the vertex, and compute leaves array.

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      When I remove the conditions I get a segmentation fault and it then outputs 0 on everything.

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      When I remove the conditions I get a segmentation fault and it then outputs 0 on everything., does using the vector of vectors cause the issue??

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Furthermore when I keep the conditional statements, the code outputs correctly up to test case 4

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You may have a look at this implementation 210483045

»
10 months ago, # |
  Vote: I like it +19 Vote: I do not like it

I've learned to use binary lifting to store more information thanks to the problem F, really good contest

  • »
    »
    9 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    can you explain how you have used binary lifting

    • »
      »
      »
      9 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      For each node, let's store for it some informations, consider the path start from the node to its 2^k-th parents, we store the maximum prefix, suffix, the sum of whole path, and the maximum sum, we can efficiently use these informations to combine the path with another path.

      To get similar with this concept, I suggest you to have a look at the Segment Tree course in Codeforces Edu section, which also have this concept but for an array.

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

nice round,hope for the positive del:)

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone tell me why my code was giving TLE for 1843D - Apple Tree. All I did was globally define the graph and cnt array similar to the tutorial solution and it worked then.

This is my initial submission: 210536302. This is the final accepted submission: 210538893

  • »
    »
    10 months ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    Passing the whole graph as an argument for the helper function each time is costly. C++ basically has to make a new copy of it each time you call that function. After making g global, it no longer has to do this. Alternatively, you could've passed a pointer to the graph as an argument.

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

For problem E, why don’t you need a prefix sum for every possible array. I understand the binary search part but just building the prefix sums would tle right?

  • »
    »
    10 months ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Analyzing the complexity : You binary search on q ( O(log(q)) ) and to check if a certain value is valid, you build the prefix sum array and then iterate through segments ( O(m + n (+q depending on impl) ) ). Combining these, you get O(log(q)(m+n)). If you were to build all prefix sum arrays before hand, that would take you O(q*n) time ( TLE ), because for each query, you would have to build prefix-suns. A way around this is to use a segment tree to achieve O(qlog(n)) for precomputation!

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      Thank you for your explanation. I forgot we could build the prefix sums inside of the binary search :(.

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    As we are populating the prefix array while we are doing binary search, the maximum number of times we would have to create the prefix array would be O(log Q) and creating each prefix array would take O(N) time, thus total time complexity will be well under limits reaching only O(N*log Q) for prefix array creation and O(M*log Q) for checking every range given to us, making the total time complexity O((N+M)*log Q).

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thank you for your explanation. I forgot we could build the prefix sums inside of the binary search :(.

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

For the problem C, I tried to solve the problem by find the values from the vertex 1, and using the bit value of 'N' to decide to go for either left subtree or right subtree, but I couldn't solve it :( . Does there exist any solution by using the bit value of 'N' to find either to go left or right ?

»
10 months ago, # |
Rev. 3   Vote: I like it -11 Vote: I do not like it
map<int,vector<pair<int/*second node*/,int/*weight*/>>>graph;
pair<int,int> segments[2][N];
int vis[N];
void dfs(int node,int sum,int mx_sum,int mn_sum){
    vis[node]=1;
    for(auto child:graph[node]) {
        if (!vis[child.first]) {
            if (child.second == 1) {
                int val = sum >= 1 ? sum : 0;
                segments[1][child.first].second = mn_sum;
                segments[1][child.first].first = max(mx_sum, val + 1);
                dfs(child.first, val + 1, max(mx_sum, val + 1), mn_sum);
            } else {
                int val = sum <= -1 ? sum : 0;
                segments[1][child.first].second = min(val - 1, mn_sum);
                segments[1][child.first].first = mx_sum;
                dfs(child.first, val - 1, mx_sum, min(mn_sum, val - 1));
            }
        }
    }
}
void solve() {
    int cnt=1;
    int n,size;cin>>n;
    vector<pair<int,int>>queries;
    graph.clear();
    for(int i=0;i<n;++i){
        char t;
        cin>>t;
        if(t=='?') {
            int a, b, val;
            cin>>a>>b>>val;
            queries.emplace_back(b,val);
        }
        else{
            int node,weight;cin>>node>>weight;
            graph[node].emplace_back(cnt+1,weight);
            graph[++cnt].emplace_back(node,weight);
        }
    }
    for(int i=1;i<=cnt;++i)vis[i]=0;
    for(int i=1;i<=cnt;++i)
        segments[1][i]={1,0};
    dfs(1,1,1,0);
    size=queries.size();
    for(int i=0;i<size;++i){
        int b=queries[i].first,weight=queries[i].second;
        cout<<(weight>=segments[1][b].second&&weight<=segments[1][b].first?"YES\n":"NO\n");
    }
}

can someone provide me any hints to detect the error in my code in problem F1?

i fall on test 3 on line 11301st expected yes found no

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    same error bro

    • »
      »
      »
      10 months ago, # ^ |
      Rev. 2   Vote: I like it -8 Vote: I do not like it
      + 1 1
      + 2 -1
      + 3 1
      + 2 -1
      + 4 1 
      + 2 -1
      

      and the query is

      ? 1 6 3

      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        should be no only for this

        • »
          »
          »
          »
          »
          10 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          actually it's yes because there is a subsegment from 1 to 6 that actually add up to 3

          node 1 ==> 1 node 2 ==> 1 node 3 ==> -1 node 4 ==> 1 node 6 ==> 1

          this adds up to 3

          • »
            »
            »
            »
            »
            »
            10 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            yes bro spot on. Actually i counted max positive sum on a path as only th length of consecutive ones and set it to 0 again when -1 came in between while this is nt crct as evident feom this tc.such a fool i am

            • »
              »
              »
              »
              »
              »
              »
              10 months ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              Ayo bro i did the same XD , i totally forget that not only the consecutive needs to be added Actually this problem is just literally find the maximum and minimum sum of subsegments in array At least we learnt new something today Hope for cyan next div me and you <3

»
10 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

210560633 why this is giving me a wa although i have gone with the correct logic

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I don't know how the tree given in the example in question D is drawn, can someone explain it to me, thank you very much.

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This is the first testcase:

    5 1 2 3 4 5 3 3 2 4 3 4 5 1 4 4 1 3

    The first number is n, so there are n — 1 edges with follow. Let us start with the first edge. "1 2" means an edge is drawn between vertices 1 and 2 (depicted by a line connected 1 and 2). We do the same for edges "3 4", "5 3", and "3 2".

»
10 months ago, # |
  Vote: I like it +7 Vote: I do not like it

My solution for F2 https://youtu.be/FZJxWhOoxo0

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I was not able to make it in this round as I planned. But hope things will be great next time.

»
10 months ago, # |
  Vote: I like it +1 Vote: I do not like it

E can be solved with wavelet tree:

create an array, initially all INF. then for the ith update, set array[index] = i

now for each segment, the (segment len/2 + 1)th-smallest number is either INF, or the id of the query which makes it beautiful

210572758

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Any solution possible with — segment trees concept only ?

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yes, Check my submission 210447265, (By using Segment tree + Binary Search approach)

    • »
      »
      »
      10 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      It boils down to finding k-th smallest element in a range, there's a lot of ways of doing it: wavelet tree, persistent segment tree, parallel binary search...

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I solved E during the contest using wavelet tree too, I'm looking for the (len/2 + 1 + (number of zeros))th-smallest in each segment

    my submission: 210413964

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I guess wavlet tree is better than the Editorial solution

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I tried a persistent segment tree approach in E but got mle. Since I copied the implementation from cpp-algorithms I didn't know how to delete all the nodes to free up space after each tc. Does anyone uses persistent segment trees at all for cp? It used 200mbs for trivial test cases so I wonder if they are useful in cp contests at all?

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

AnyOne tried to use — segment trees ? for problem E

»
10 months ago, # |
Rev. 3   Vote: I like it -8 Vote: I do not like it

Time Complexity for E would be : O((q+n+m)⋅logq)

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

In F1 and F2 the realize that you only need to find maximum and minimum sum can create make everything much easier.

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Quite instructive contest. Thank you.

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I am struggling hard to figure out what's wrong with my code for problem D.210612596 Unlike other solutions, I have only connected nodes u and v from u to v such that u < v. I am guessing that's the bug but I am not sure exactly how.

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    What's your logic to add edge u->v only if u < v ??

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      From what I thought after reading the testcases is that when an edge v1, v2 is given and v1 < v2, then that means v1 is the parent node of v2, otherwise v2 is the parent node. So I made a directed adjacency list such that adj[v] contains only the nodes which are it's children. Using this, finding the leafnodes is simple as adj[v] will be empty if v is leafnode.

      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        "From what I thought after reading the testcases is that when an edge v1, v2 is given and v1 < v2, then that means v1 is the parent node of v2, otherwise v2 is the parent node."

        That's the problem: You assumed something that wasn't guranteed in the statement. The assumption that smaller vertices are parents of larger ones is incorrect. You need to treat the given tree as a graph of undirected edges and find the parent nodes yourself (if you need them).

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Does anyone solve it using sqrt decomposition for problem E?

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

You can also solve F2 with HLD for the same reason, if you could not figure out (like me) how to do it with binlifts. Code: https://codeforces.com/contest/1843/submission/210677217

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I'm trying the HLD approach but I'm keeping it WA at test 3, here is my submission: 210725677. Could you help me to point out where am I missing, thanks.

  • »
    »
    7 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Hi I am curious how did you avoid stack overflow when running dfs? I tried HLD approach but still stuck at testcase 3 with Runtime error due to stack overflow

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

good problems

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Did not understand the editorial of 1843E - Tracking Segments. Can anyone help me understand it please..

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You can try to write some problems about binary search, it's can help you

»
10 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Can someone explain why this doesn't work? I get wrong answer error for testcase 8

#include <bits/stdc++.h>

using namespace std;

int dfs(vector<int>*T,int* pos, int node,int par){


    if(T[node].size()==1 && T[node][0]==par){
        pos[node]=1;
        return 1;
    }

    int paths=0;

    for(auto n:T[node]){
        if(n!=par){
            paths+=dfs(T,pos,n,node);
        }
    }


    pos[node]=paths;
    return paths;

}

int main(){

    int t;
    cin>>t;
    
    while(t--){
        int n;
        cin>>n;

        vector<int> T[n];
        int pos[n] = {0};

        for(int i=0;i<n-1;i++){

            int u,v;
            cin>>u>>v;

            T[u-1].emplace_back(v-1);
            T[v-1].emplace_back(u-1);
        }

 
        dfs(T,pos,0,-1);

        int Q;
        cin>>Q;
        unsigned long long  int ans;
        while(Q--){
            int s,t;
            cin>>s>>t;
            ans=pos[s-1]*pos[t-1];
            cout<<ans<<'\n';
        }

    }

    return 0;
}
  • »
    »
    10 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    your pos array is int type. You need to change it to long long or type cast it before multiplying. Because after multiplying, it is returning int and then the int is getting converted into long long.

    quick fix should be ans = (long long int)pos[s-1] * pos[t-1];

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anybody tell me why in E in the first test when we change 5-th element in array on 1, it doesn't count as a solution? I mean that we have a segment (5 5) and if we change 5-th element we have 0 zeroes and 1 one...

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    You have to only consider the segments given in the input and not any segment.

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thank you, but i meant that in input we have (5, 5) segment and it fits with the first query...

      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        No that is actually n and m.

        • »
          »
          »
          »
          »
          10 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Oh, that's dumb... thank you, didn't even noticed it...

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

nice contest

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Iam suprised at how people can come up with the greedy algorithm so fast , the only explanation is they must have seen a question like that before.

»
10 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Can someone help me debug my solution for F2? https://codeforces.com/contest/1843/submission/211222013

WA on the third test (180th token)

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

211316049 Can anyone tell me why this code results in Exit code is-1073741571.thanks.

»
10 months ago, # |
  Vote: I like it +23 Vote: I do not like it

An even shorter solution for C: 2*x - popcount(x)

  • »
    »
    10 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Is this formula derived purely from observation or is there some intuition to it?

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Intuition is what I used to find this solution, but it can be easily proven using basic number theory.

      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Can you give some hints about the intuition behind it?

        • »
          »
          »
          »
          »
          10 months ago, # ^ |
            Vote: I like it +3 Vote: I do not like it

          Consider the contribution of each bit to the answer $$$\sum \lfloor \frac{n}{2^k} \rfloor$$$. If the $$$j$$$-th bit is set in $$$n$$$, the $$$(j-1)$$$-th bit will be set in $$$\lfloor \frac{n}{2} \rfloor$$$, the $$$(j-2)$$$-th bit will be set in $$$\lfloor \frac{n}{4} \rfloor$$$ and so on until $$$(j-j)=0$$$-th bit is set. We also know that $$$\sum_{0 \le k \le j} 2^k = 2^{j+1}-1$$$.

          Putting everything together, you get $$$2 \cdot n - (\text{no. of set bits in } n)$$$.

»
10 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Hi everyone.

Can anyone please explain why my code is giving WRONG_ANSWER on problem D?

from collections import defaultdict

def solve(tree, node):
	if len(tree[node]) == 0:
		return 1
	
	sm = 0
	for child in tree[node]:
		sm += solve(tree, child)
	
	return sm

for _ in range(int(input())):
	n = int(input())
	
	tree = defaultdict(set)
	
	for _ in range(n - 1):
		u, v = map(int, input().split())
		
		if v < u:
			u, v = v, u

		tree[u].add(v)
	
	q = int(input())
	
	for _ in range(q):
		x, y = map(int, input().split())
		res = solve(tree, x) * solve(tree, y)
		print(res)

211679589

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I am getting stack overflow error for my submission:https://codeforces.com/contest/1843/submission/211281258 I have set maxim recursion limit for python what should i do, please hep

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone pls tell me why in sol for F2 the parent of LCA is also merged??

Thanks in Advanced.

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone tell why this solution 212190931 or this 212172518 shows memory limit exceeded for problem F1

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone please tell why this solution gives TLE on test case 5

https://codeforces.com/contest/1843/submission/213606130

Thanks

»
9 months ago, # |
  Vote: I like it 0 Vote: I do not like it

why my code works for first 4 cases but then gives different result, even though the logic is correct. code

  • »
    »
    9 months ago, # ^ |
      Vote: I like it +10 Vote: I do not like it

    Instead of int try using long long, because answer may be greater than the size of int (2^31 = 2147483648) 214366788

»
9 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I solved E using K-th order statistics on segment. Let's sort queries by X. For each segment (let it be [l, r]) we use binary search to find all queries, laying on it. Easy to understand these queries form a segment (let this segment be [l1, r1]). Suppose K = (r-l+1) / 2 + 1 (it's the number of 1 for segment to become beautiful). Then we are to find on segment [l1, r1] the number of query, which would be on position K, if we sorted these queries by their numbers.

P.S. Yeah, you can tell me I'm sick)

»
6 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Hello, I am able to understand the D. Apple Tree logic. When i coded it in python i'm getting runtime error in python https://codeforces.com/contest/1843/submission/230310579. but the same code when converted using gpt passes the judge. What could be the error here. I think recursion is the issue here. but writing loop solution i'm not able to do. can i make this python solution pass the judge?

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

when will Div3 change by not givving horrible implementation in last question They just dump it inot div3