GithubHelp home page GithubHelp logo

Comments (1)

dixitgarg059 avatar dixitgarg059 commented on August 22, 2024

    //  memory: 4*n*log(n), n = size of array
    int N;
    int Nodes;
    int n;
    vector<int> seg, lp, rp;

    PersistentSegTree() {
        Nodes = 0;
        n = 0;
    }
    int Initialize(int n) {

        this->n = n;
        N = ceil(4 * n * log2(n)) + 1;
        seg.resize(N, 0), lp.resize(N, -1), rp.resize(N, -1);
        return build(0, n - 1);
    }
    int build(int ss, int se) {
        int pos = Nodes++;
        if (ss == se) {
            seg[pos] = 0;
            lp[pos] = -1;
            rp[pos] = -1;
            return pos;
        }
        int mid = (ss + se) / 2;
        lp[pos] = build(ss, mid);
        rp[pos] = build(mid + 1, se);
        seg[pos] = seg[lp[pos]] + seg[rp[pos]];
        return pos;
    }
    int update(int root, int ss, int se, int ind, int val) {
        // update a[ind] = val
        if (ind < ss || ind > se)
            return root;
        int pos = Nodes++;
        if (ss == se) {
            seg[pos] = val;
            return pos;
        }
        int mid = (ss + se) / 2;
        if (ind <= mid) {
            lp[pos] = update(lp[root], ss, mid, ind, val);
            rp[pos] = rp[root];
            seg[pos] = seg[lp[pos]] + seg[rp[pos]];
            return pos;
        } else {
            rp[pos] = update(rp[root], mid + 1, se, ind, val);
            lp[pos] = lp[root];
            seg[pos] = seg[lp[pos]] + seg[rp[pos]];
            return pos;
        }
    }
    ll query(int pos, int ss, int se, int l, int r) {
        if (l <= ss && r >= se) {
            return seg[pos];
        }
        if (l > se || r < ss)
            return 0;
        int mid = (ss + se) / 2;
        return query(lp[pos], ss, mid, l, r) + query(rp[pos], mid + 1, se, l, r);
    }
};

struct Distinct {
    vector<int> roots;
    int n;
    PersistentSegTree obj;

    Distinct(const v &arr) {
        // obj = obj1;
        int root = obj.Initialize(arr.size());
        n = arr.size();
        vector<int> next(n, n);
        map<ll, ll> mapp;
        for (int i = n - 1; i >= 0; i--) {
            if (mapp.find(arr[i]) != mapp.end())
                next[i] = mapp[arr[i]];
            mapp[arr[i]] = i;
        }
        for (int i = n - 1; i >= 0; i--) {
            root = obj.update(root, 0, n - 1, i, 1);
            if (next[i] != n) {
                root = obj.update(root, 0, n - 1, next[i], 0);
            }
            roots.pb(root);
        }
        reverse(all(roots));
    }
    ll count_distinct(int l, int r) { return obj.query(roots[l - 1], 0, n - 1, l - 1, r - 1); }
};```

from algorithms-notebook.

Related Issues (18)

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.