Giter Site home page Giter Site logo

Comments (4)

the-hyp0cr1t3 avatar the-hyp0cr1t3 commented on August 23, 2024

Not sure if I understood your query correctly, but you have to modify SegmentTree::fun to perform whatever operation you need to.

T fun(T a, T b) { return std::min(a, b); }

from cc.

MdNihal05 avatar MdNihal05 commented on August 23, 2024

Thanks for considering this and Sorry that I was not clear
I know how to use this template as i learned classic segment trees

I provided this code as reference
Iam asking about the code in your repository
As I am learning lazy segment trees
https://github.com/the-hyp0cr1t3/CC/blob/master/%E6%9C%AB%20Snippets/Segment%20trees/Recursive%20%2B%20Range%20updates%20(lazy%20prop).cpp

Is there any generic function that I can write instead of changing every where as I did in above code

Or how should I do that ( where should I change for mins,max,sum,or gcd,LCM etc for any cumulative functions)

You can explain by taking any example
The default templete is for min i think?

I hope I am clear this time
Thanks in advance!

from cc.

the-hyp0cr1t3 avatar the-hyp0cr1t3 commented on August 23, 2024

Oh I see, thanks for clarifying. You'll have to make changes in the Node struct.

For example, for range max query and range sum update, it would look something like this:

const int64_t inf = 2e18;

struct Node {
    using V = int64_t;
    using L = int64_t;

    bool pending{false};
    V val; L lazy{};
    Node(V val = -inf): val(val) {}               // default value
    Node(const Node& l, const Node& r)            // merge two nodes
        : val(std::max(l.val, r.val)) {}
    void apply_lazy(int len) { val += lazy; }     // update node
    operator V() const { return val; }
};

Similarly, range gcd query and range assignment updates would look like:

struct Node {
    using V = int64_t;
    using L = struct Lazy {
        V lzy;
        Lazy(V lzy = 0): lzy(lzy) {}            // default value
        operator V() const { return lzy; }
        void operator+=(const Lazy& rhs) { lzy = rhs.lzy; }    // combine with other lazy
    };

    bool pending{false};
    V val; L lazy{};
    Node(V val = 0): val(val) {}                // default values
    Node(const Node& l, const Node& r)          // merge two nodes
        : val(std::gcd(l.val, r.val)) {}
    void apply_lazy(int len) { val = lazy; }    // update node
    operator V() const { return val; }
};

from cc.

MdNihal05 avatar MdNihal05 commented on August 23, 2024

hey Thanks !

from cc.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.