aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTa180m2020-06-21 15:16:42 -0500
committerTa180m2020-06-21 15:16:42 -0500
commit10ac6ca120cf467293420221f880d64caae88167 (patch)
tree7d46fdf9e203e5e6fc473973c9525e656d2550b0
parentdb45276f8479216c38188e7637d47f3417771fdc (diff)
Create seg_ordered_set_tree.cpp
-rw-r--r--Data Structures/seg_ordered_set_tree.cpp25
1 files changed, 25 insertions, 0 deletions
diff --git a/Data Structures/seg_ordered_set_tree.cpp b/Data Structures/seg_ordered_set_tree.cpp
new file mode 100644
index 0000000..b1d9005
--- /dev/null
+++ b/Data Structures/seg_ordered_set_tree.cpp
@@ -0,0 +1,25 @@
+#include <ext/pb_ds/assoc_container.hpp>
+#include <ext/pb_ds/tree_policy.hpp>
+using namespace std;
+using namespace __gnu_pbds;
+template<typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
+constexpr int MAXN = 100001;
+
+ordered_set<int> S[4 * MAXN];
+
+void update(int x, int y, int l = 0, int r = MAXN, int n = 1) {
+ if (l != r) {
+ int m = (l + r) >> 1;
+ x <= m ? update(x, y, l, m, n << 1) : update(x, y, m + 1, r, n << 1 | 1);
+ }
+ S[n].insert(y);
+}
+
+int query(int xl, int xr, int yl, int yr, int l = 0, int r = MAXN, int n = 1) {
+ if (l > r || l > xr || r < xl) return 0;
+ if (l >= xl && r <= xr) return S[n].order_of_key(yr + 1) - S[n].order_of_key(yl);
+ else {
+ int m = (l + r) >> 1;
+ return query(xl, xr, yl, yr, l, m, n << 1) + query(xl, xr, yl, yr, m + 1, r, n << 1 | 1);
+ }
+} \ No newline at end of file