From 4b356de1dabb7db1186bcead0fde6b4309fac9b7 Mon Sep 17 00:00:00 2001 From: RinRi Date: Wed, 7 Apr 2021 10:09:45 +0600 Subject: [PATCH] add segment tree various implementations --- data-structures/segtreefirstabove.cpp | 110 ++++++++++++++++++++++++++ data-structures/segtreeinversions.cpp | 71 +++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 data-structures/segtreefirstabove.cpp create mode 100644 data-structures/segtreeinversions.cpp diff --git a/data-structures/segtreefirstabove.cpp b/data-structures/segtreefirstabove.cpp new file mode 100644 index 0000000..9341cbb --- /dev/null +++ b/data-structures/segtreefirstabove.cpp @@ -0,0 +1,110 @@ +/* +#pragma GCC target ("avx2") +#pragma GCC optimize ("Ofast") +#pragma GCC optimize ("unroll-loops") +*/ +#include "bits/stdc++.h" + +#define iosb ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define BIT(x) __builtin_popcount(x) +#define all(x) x.begin() , x.end() +#define F first +#define S second +#define pb push_back + +using namespace std ; + +typedef unsigned long long UL ; +typedef long long L ; +typedef string T ; +typedef int I ; + +const I MaxN = 1e5+1 ; +const I MOD = 1e9+7 ; +const I inf = 2e9+7 ; +const L INF = 2e18+7 ; + +L t[4*MaxN], mod[4*MaxN], a[MaxN], n, m ; + +void build(int v, int tl, int tr) +{ + if(tl == tr) + { + t[v] = a[tl] ; + return ; + } + + int tm = (tl + tr) >> 1 ; + build(v*2, tl, tm) ; + build(v*2+1, tm+1, tr) ; + t[v] = max(t[v*2], t[v*2+1]) ; +} + +void update(int v, int tl, int tr, int l, int x) +{ + if(tl == tr && tl == l) + { + t[v] = x ; + return ; + } + + if(tr < l || l < tl) + return ; + + int tm = (tl + tr) >> 1 ; + update(v*2, tl, tm, l, x) ; + update(v*2+1, tm+1, tr, l, x) ; + t[v] = max(t[v*2], t[v*2+1]) ; +} + +int above(int v, int tl, int tr, int l, int x) +{ + if(l > tr) + return -1 ; + + if(tl == tr) + { + if(x > t[v]) + return -1 ; + return tl ; + } + + int tm = (tl + tr) >> 1 ; + if(t[v*2] >= x) + { + int ab = above(v*2, tl, tm, l, x) ; + if(ab == -1) + return above(v*2+1, tm+1, tr, l, x) ; + } + else + return above(v*2+1, tm+1, tr, l, x) ; +} + +int32_t main(int argc, char *argv[]) +{ + if(argc > 1) + freopen(argv[1], "r", stdin) ; + //freopen(".in" , "r" , stdin) ; + //freopen(".out" , "w" , stdout) ; + cin >> n >> m ; + for(int i = 0 ; i < n ; ++i) + cin >> a[i] ; + + build(1, 0, n-1) ; + + while(m--) + { + int type, l, x ; + cin >> type ; + if(type == 1) + { + cin >> l >> x ; + update(1, 0, n-1, l, x) ; + } + else + { + cin >> x >> l ; + cout << above(1, 0, n-1, l, x) << '\n' ; + } + } +} \ No newline at end of file diff --git a/data-structures/segtreeinversions.cpp b/data-structures/segtreeinversions.cpp new file mode 100644 index 0000000..96c261e --- /dev/null +++ b/data-structures/segtreeinversions.cpp @@ -0,0 +1,71 @@ +/* +#pragma GCC target ("avx2") +#pragma GCC optimize ("Ofast") +#pragma GCC optimize ("unroll-loops") +*/ +#include "bits/stdc++.h" + +#define iosb ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) +#define BIT(x) __builtin_popcount(x) +#define all(x) x.begin() , x.end() +#define F first +#define S second +#define pb push_back + +using namespace std ; + +typedef unsigned long long UL ; +typedef long long L ; +typedef string T ; +typedef int I ; + +const I MaxN = 1e5+1 ; +const I MOD = 1e9+7 ; +const I inf = 2e9+7 ; +const L INF = 2e18+7 ; + +L t[4*MaxN], mod[4*MaxN], a[MaxN], n, m ; + +void update(int v, int tl, int tr, int l, int x) +{ + if(tl == tr && tl == l) + { + t[v] = x ; + return ; + } + + if(tr < l || l < tl) + return ; + + int tm = (tl + tr) >> 1 ; + update(v*2, tl, tm, l, x) ; + update(v*2+1, tm+1, tr, l, x) ; + t[v] = t[v*2] + t[v*2+1] ; +} + +L get(int v, int tl, int tr, int l, int r) +{ + if(l <= tl && tr <= r) + return t[v] ; + + if(l > tr || r < tl) + return 0 ; + + int tm = (tl + tr) >> 1 ; + return get(v*2, tl, tm, l, r) + get(v*2+1, tm+1, tr, l, r) ; +} + +int32_t main(int argc, char *argv[]) +{ + if(argc > 1) + freopen(argv[1], "r", stdin) ; + //freopen(".in" , "r" , stdin) ; + //freopen(".out" , "w" , stdout) ; + cin >> n ; + for(int i = 0 ; i < n ; ++i) + { + cin >> a[i] ; + cout << get(1, 1, n, a[i], n) << ' ' ; + update(1, 1, n, a[i], 1) ; + } +} \ No newline at end of file