Comments (1)
// 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)
- Consistent Use of Infinity HOT 3
- Unclear usage for HopCroft Karp HOT 1
- Make all graphs use one-indexed notation HOT 2
- Use online-judge-verify-helper to test our library HOT 2
- Treaps: Augmentable Binary Search Trees
- Crude way to track verified status of codes HOT 1
- A Foray into DP Optimizations HOT 1
- Persistent segtree implementation
- Link cut tree implementation HOT 1
- Nice implementation of dynamic convex hull
- Interesting segment tree template supporting multi-operations HOT 1
- Code for finding diameter of tree
- Matrix multiplication and geometric sum (finite terms) code HOT 2
- Cool template for HLD
- Making a implicit segtree that actually gets AC HOT 1
- MCMF tested implementation HOT 2
- [WIP] Treaps tested implementation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from algorithms-notebook.