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

Автор Sokol080808, история, 3 года назад, По-русски

1843A - Саша и раскраска массива

Идея: Sokol080808, разработка: molney

Разбор
Решение
Оценка задачи

1843B - Лонг лонг

Идея: EJIC_B_KEDAX, разработка: molney

Разбор
Решение
Оценка задачи

1843C - Сумма в бинарном дереве

Идея: Sokol080808, разработка: molney

Разбор
Решение
Оценка задачи

1843D - Яблоня

Идея: EJIC_B_KEDAX, разработка: Vladosiya

Разбор
Решение
Оценка задачи

1843E - Слежка за отрезками

Идея: meowcneil, разработка: meowcneil

Разбор
Решение
Оценка задачи

1843F1 - Метрополитен в Омске (простая версия)

Идея: EJIC_B_KEDAX, разработка: Sokol080808

Разбор
Решение
Оценка задачи

1843F2 - Метрополитен в Омске (сложная версия)

Идея: EJIC_B_KEDAX, разработка: Sokol080808

Разбор
Решение
Оценка задачи
Разбор задач Codeforces Round 881 (Div. 3)
  • Проголосовать: нравится
  • +172
  • Проголосовать: не нравится

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

Автокомментарий: текст был обновлен пользователем Sokol080808 (предыдущая версия, новая версия, сравнить).

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

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

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

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

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

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;

}

~~~~~

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

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

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

nice round,hope for the positive del:)

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

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

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

    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.

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

      Okay, thanks!

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

      Hi, I know its been a long time after this contest but can you help me understand why my solution is giving WA on test4. My approach and the editorial approach seems similar. Sub:288232403

      thanks

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

        Hey! Sorry about a late reply, I hope it's still relevant.

        The idea seems correct. I believe your code does not calculate the child[i] correctly. It would be easier to use a DFS here.

        parent[i] seems to be okay, but, since in the last loop you iterate over nodes from n to 1 (for(int i=n;i>=1;i--){...}), it is possible that, when performing child[parent[i]]+=child[i], child[i] is still not fully computed (Meaning that there exist j such that j < i, parent[j] = i).

        I hope this makes sense. Feel free to ask if you have more questions.

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

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?

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

    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!

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

    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).

»
3 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится -11 Проголосовать: не нравится
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

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

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.

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

    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".

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

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

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

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

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

AnyOne tried to use — segment trees ? for problem E

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

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

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

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.

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

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

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

      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.

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

        "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).

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

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://mirror.codeforces.com/contest/1843/submission/210677217

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

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

WA on the third test (180th token)

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

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

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

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

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

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

Thanks in Advanced.

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

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

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

Thanks

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

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

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

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)

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

i am stupid, i used persistence segment tree to solve E and solution of tutorial just

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

My code can pass F2, but it fails on F1...?

There are no test cases in F2 with all '+' characters.

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

.