Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@hotman78

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

More than 1 year has passed since last update.

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

可換性を必要とします
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)

2
Help us understand the problem. What is going on with this article?
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.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?