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

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

It's been a while, but we are back with another Codeforces Gym!

Translated statements and my solutions are here, and statements are also added to the gym itself.

Today I will talk about various problems involving range queries in general, and segment trees in particular.

First, I describe simple problems with range queries, such as sum/min/max on a segment without any updates. It is essential to understand that sometimes you don't need any advanced data structures to process range queries. In some problems, you can write a for loop, use prefix sums, or precompute a sparse table. However, when update queries come into play, the need for better data structures arise, and here segment tree, and lazy segment tree come into play. Their applications can seem practically unlimited, but at the very end, we realize that there are some limitations. Because of that, we introduce our final data structure, which supports Split and Merge operations.

Watch the theory here, or skip straight to the practice session if you already know the theory.

  • Проголосовать: нравится
  • +132
  • Проголосовать: не нравится

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

Thank you nskybytskyi.

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

Starting in 15 minutes!

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

really excited!!!

thank you for putting in the effort to do such an amazing thing.

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

Can we please have more practice problems on segtrees? These are somewhat standard, lecture-style.

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

    I somewhat agree with you. The problems from this gym are designed to introduce people to the topic of range queries. If you already solved them you are welcome to the sequel gyms: first and second. Unfortunately, I haven't translated them just yet, so you will need the help of the google translate or something, and potentially https://2cyr.com/decode/?lang=en to fix the encoding. I will add English statements to the gyms once I translate the problems.

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

Hey guys, I have encountered a problem in cases problem Dynamic Sum Queries. My code fails for the second test case

Your code here...
#include <bits/stdc++.h>
using namespace std;

int n,q;
vector<long long> a(200005,0);
vector<long long> feenwick_tree(200005,0);

long long sum(int k){
    long long s=0;
    while(k>=1){
        s += feenwick_tree[k];
        k -= k&-k;
    }
    return s;
}

void add(int k, long long x){
    while(k<=n){
        feenwick_tree[k] += x;
        k += k&-k;
    }
}

int main(){
    cin>>n>>q;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++){
        add(i,a[i]);
    }
    while(q-->0){
        long long q,i,j;
        cin>>q>>i>>j;
        if(q==1){
            j = j-a[i];
            add(i,j);
        }
        else{
            long long x1 = sum(j);
            long long x2 = sum(i-1);
            if(i==1)
                cout<<x1<<"\n";
            else
                cout<<x1-x2<<"\n";
        }
    }
    return 0;
}

Thank you, in advance.

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

Hey nskybytskyi can you please make a contest mashup or question list from easy to hard so that we can try and maybe later you can make a editorial video!