Help us understand the problem. What is going on with this article?

【競プロ】セグ木は2冪で配列を取らなくていいという話と30行実装

えびちゃんありがとうの章

可換性を必要とします
f(s,node[--r]);をf(node[--r],s);に変えると可換性を要求せずにすむらしいです(凄い)

気持ち

IMG_20200221_171036.jpg

確かに上手く行きそう

実装

非再帰です/可換性を要求しません

segment.cpp
template<typename T>
struct segment{
    vector<T> node;
    int n;
    segment(int n):n(n){node.assign(n*2,e);}
    T get(int l,int r){
        l+=n;r+=n;
        T s=e,t=e;
        while(l<r){
            if(l&1)s=f(s,node[l++]);
            if(r&1)t=f(node[--r],t);
            l/=2;r/=2;
        }
        return f(s,t);
    }
    void update(int t,T x){
        t+=n;
        node[t]=f(node[t],x);
        while(t>1)t/=2,node[t]=f(node[t*2],node[t*2+1]);
    }
    void change(int t,T x){
        t+=n;
        node[t]=x;
        while(t>1)t/=2,node[t]=f(node[t*2],node[t*2+1]);
    }
    T e={1,0};
    T f(const T& s,const T& t){
        return {t.first*s.first,t.first*s.second+t.second};
    }
};

verify

Range Sum Query(AOJ)
タコヤキオイシクナール(atcoder)

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした