commit a4a3f8e87a4d49d63182c3362361e70e53197ff3 Author: RinRi Date: Tue Apr 6 09:49:22 2021 +0600 recreating algo repository diff --git a/binary-search/binary-search.cpp b/binary-search/binary-search.cpp new file mode 100755 index 0000000..3f02d18 --- /dev/null +++ b/binary-search/binary-search.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std ; + +int main() +{ + int n ; + cin >> n ; + int a[n] ; + for(int i = 0 ; i < n ; ++i) cin >> a[i] ; + sort(a,a+n) ; + int x ; + cin >> x ; + int l = 0 , r = n ; + while(r-l>1) + { + int mid = (r+l)/2 ; + if(a[mid] <= x) + l = mid ; + else r = mid ; + } + if(a[l]==x) + cout << l ; + else + cout << -1 ; +} + + diff --git a/data-structures/dk-dereva.cpp b/data-structures/dk-dereva.cpp new file mode 100755 index 0000000..6b55424 --- /dev/null +++ b/data-structures/dk-dereva.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +#include +#include + +#define pb push_back +#define ll long long +#define ub upper_bound +#define lb lower_bound +#define sc(x) scanf("%I64d",&x) +#define pr(x) printf("%I64d\n",x) + +using namespace std ; + +const int MAXN = 1e9+1 ; +const int N = 1e6+1 ; +int n , m , k , x , y , z ; +int a[N] , d[30][N] , U[N] ; + +struct node +{ + int x, sz, y ; + node *l, *r ; + node (int X = 0) + { + x = X ; + sz = 1 ; + y = rand () ; + l = r = NULL ; + } +} *t ; + +typedef node* pnode ; + +int GetSz (pnode t) +{ + if (!t) + return 0 ; + return t -> sz ; +} + +int GetKey(pnode t) +{ + if (!t) + return 0 ; + return GetSz (t -> l) + 1 ; +} + +void upd (pnode t) +{ + if (!t) + return ; + t -> sz = GetSz (t -> l) + GetSz (t -> r) + 1 ; +} + +pnode Merge (pnode a , pnode b) +{ + if (!a) + return b ; + if (!b) + return a ; + if (a -> y > b -> y) + { + a -> r = Merge ( a -> r , b ); + upd (a) ; + return a ; + } + else + { + b -> l = Merge ( a , b -> l ) ; + upd (b) ; + return b ; + } +} + +void Split (pnode t , pnode &a , pnode &b , int x) +{ + if (!t) + return void (a = b = NULL); + int key = GetKey (t) ; + if (key <= x) + { + Split ( t -> r , t -> r , b , x - key ) ; + a = t ; + } + else + { + Split ( t -> l , a , t -> l , x ) ; + b = t ; + } + upd (a) ; + upd (b) ; +} + +void add (int x) +{ + pnode L , R , M ; + L = R = M = NULL ; + Split ( t , L , R , x ) ; + Split ( L , L , M , x - 1 ) ; + M = Merge ( new node ( x ) , M ) ; + L = Merge ( L , M ) ; + t = Merge ( L , R ) ; +} + +int main() +{ + +} diff --git a/data-structures/fenwick.cpp b/data-structures/fenwick.cpp new file mode 100755 index 0000000..e7f140f --- /dev/null +++ b/data-structures/fenwick.cpp @@ -0,0 +1,94 @@ +#include + +#define F first +#define S second +#define pb(x) push_back(x) +#define iosb ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) +#define BIT(x) __builtin_popcount(x) + +using namespace std ; + +const int MAXN = 1e6+1 ; +const int MaxN = 1e5+1 ; +const int N = 1e4+1 ; +const int M = 1e3+1 ; +const int MOD = 1e9+7 ; +const int inf = 2e9+7 ; +const long long INF = 2e18+7 ; + +int t[MaxN] , n , a[MaxN] ; + +int sum(int v) +{ + int res = 0 ; + + if(v==-1) + return 0 ; + + for(; v > 0 ; v = (v & (v + 1)) - 1) + res += t[v] ; + + return res ; +} + +void upd(int pos , int delta) +{ + a[pos] += delta ; + for( ; pos < n ; pos |= pos+1) + t[pos] += delta ; +} + +/* +int get_min(int v) +{ + int res = inf ; + + for (; v >= 0; v = (v & (v + 1)) - 1) + res = min(res , f[v]) ; + + return res ; +} + +void assign(int pos, int delta) +{ + a[pos] = delta ; + + for (; pos < n ; pos |= pos + 1) + f[pos] = min(f[pos] , delta) ; +} +*/ + +main() +{ +// freopen(".in" , "r" , stdin) ; +// freopen(".out" , "w" , stdout) ; + cin >> n ; + + for(int i = 0 ; i < n ; ++i) + { + int x ; + cin >> x ; + upd(i , x) ; + } + /* + for(int i = 0 ; i < n ; ++i) + f[i] = inf ; + + for(int i = 0 ; i < n ; ++i) + { + int t ; + cin >> t ; + assign(i , t) ; + } + */ + int q ; + cin >> q ; + while(q--) + { + int l , r ; + cin >> l >> r ; + cout << sum(r-1) - sum(l-2) << '\n' ; + } + return 0 ; +} + diff --git a/data-structures/painters.cpp b/data-structures/painters.cpp new file mode 100644 index 0000000..1c46ad9 --- /dev/null +++ b/data-structures/painters.cpp @@ -0,0 +1,103 @@ +/* +#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 = 8e6+123 ; +const I MOD = 1e9+7 ; +const I inf = 2e9+7 ; +const L INF = 2e18+7 ; + +struct item{ + int cnt = 0, pref = 0, suff = 0, sum = 0 ; +} t[MaxN] ; +int m[MaxN] ; + +void push(int v, int x, int y) +{ + if (m[v] != -1 && v*2+1 < 4000004) + { + m[v*2] = m[v*2+1] = m[v] ; + m[v] = -1 ; + + int z = (x+y)/2 ; + t[v*2].sum = (z*1ll-x*1ll+1ll) * (m[v*2]*1ll) ; + t[v*2].pref = m[v*2] ; + t[v*2].cnt = m[v*2] ; + t[v*2].suff = m[v*2] ; + t[v*2+1].sum = (y*1ll-z*1ll) * (m[v*2+1]*1ll) ; + t[v*2+1].pref = m[v*2+1] ; + t[v*2+1].cnt = m[v*2+1] ; + t[v*2+1].suff = m[v*2+1] ; + } +} + +void update(int l, int r, int c, int v, int x, int y) +{ + if (l <= x && y <= r) { + t[v].sum = c*1ll * (y*1ll-x*1ll+1ll) ; + t[v].cnt = c ; + t[v].pref = c ; + t[v].suff = c ; + m[v] = c ; + return ; + } + + if (y < l || r < x) + return ; + + push(v, x, y) ; + int z = (x+y)/2 ; + update(l, r, c, v * 2, x, z) ; + update(l, r, c, v * 2 + 1, z+1, y) ; + t[v].pref = t[v*2].pref, + t[v].suff = t[v*2+1].suff, + t[v].sum = t[v*2].sum + t[v*2+1].sum, + t[v].cnt = t[v*2].cnt + t[v*2+1].cnt - (t[v*2].suff && t[v*2+1].pref?1:0); +} + +int main() +{ + freopen("painter.in", "r", stdin) ; + freopen("painter.out", "w", stdout) ; + for(int i = 0 ; i < MaxN ; ++i) + m[i] = -1 ; + int n ; + cin >> n ; + while(n--) + { + char c ; + int l, r ; + cin >> c >> l >> r ; + if(c == 'W') c = 0 ; + else c = 1 ; + update(l+500001, l+r-1+500001, int(c), 1, 1, 2000000); + cout << t[1].cnt << ' ' << t[1].sum << '\n' ; + } +} +/* +7 +W 2 3 +B 2 2 +B 4 2 +B 3 2 +B 7 2 +W 3 1 +W 0 10 +*/ \ No newline at end of file diff --git a/data-structures/rycarski_turnir356a.cpp b/data-structures/rycarski_turnir356a.cpp new file mode 100755 index 0000000..27a5720 --- /dev/null +++ b/data-structures/rycarski_turnir356a.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +int n , m , add[1200004],t[1200004],l[300003],r[300003],x[300003]; + +void push(int v , int tl ,int tr) +{ + if(add[v] != 0) + { + if(tl != tr) + add[v * 2] = add[v] , + add[v * 2 + 1] = add[v] ; + t[v] = add[v] ; + add[v] = 0 ; + } +} +void update(int v,int tl, int tr , int l,int r ,int x) +{ + push(v,tl,tr) ; + if(tl > r || tr < l || l > r){ + return ; + } + if(l <= tl && tr <= r) + { + add[v] = x ; + push(v,tl,tr) ; + return ; + } + int mid = (tl + tr) / 2 ; + update(v * 2 , tl , mid , l ,r , x); + update(v * 2 + 1, mid + 1, tr , l , r , x); +} +int get(int v , int tl ,int tr , int pos) +{ + push(v,tl,tr) ; + if(tl == tr) + return t[v ]; + else + { + int mid = (tl + tr) / 2 ; + if(pos <= mid) + get(v * 2, tl ,mid , pos) ; + else + get(v * 2 + 1, mid + 1, tr, pos) ; + } +} +int main() +{ + cin >> n >> m ; + for(int i = 1 ; i <= m ; i++) cin >> l[i] >> r[i] >> x[i] ; + for(int i = m ; i >= 1 ; i--) + { + update(1,1,n,x[i]+1,r[i],x[i]) ; + update(1,1,n,l[i],x[i]-1,x[i]) ; + } + for(int i = 1; i <= n ; i++) + cout << get(1,1,n,i) << ' ' ; +} diff --git a/data-structures/segtreecolminupdx.cpp b/data-structures/segtreecolminupdx.cpp new file mode 100644 index 0000000..9c5966b --- /dev/null +++ b/data-structures/segtreecolminupdx.cpp @@ -0,0 +1,123 @@ +/* +#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 a[MaxN], n, m ; + +struct{ + L mn = INF, c = 0 ; +} t[MaxN*4] ; + +void build(int v, int tl, int tr) +{ + if(tl == tr) + { + t[v].mn = a[tl] ; + t[v].c = 1 ; + return ; + } + + int tm = (tl + tr) >> 1 ; + build(v*2, tl, tm) ; + build(v*2+1, tm+1, tr) ; + t[v].mn = min(t[v*2].mn, t[v*2+1].mn) ; + t[v].c = (t[v].mn==t[v*2].mn?t[v*2].c:0) + (t[v].mn==t[v*2+1].mn?t[v*2+1].c:0) ; +} + +void update(int v, int tl, int tr, int l, int x) +{ + if(tl == tr && tl == l) + { + t[v].mn = x ; + t[v].c = 1 ; + 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].mn = min(t[v*2].mn, t[v*2+1].mn) ; + t[v].c = (t[v].mn==t[v*2].mn?t[v*2].c:0) + (t[v].mn==t[v*2+1].mn?t[v*2+1].c:0) ; +} + +L get(int v, int tl, int tr, int l, int r) +{ + if(l <= tl && tr <= r) + return t[v].mn ; + + if(l > tr || r < tl) + return INF ; + + int tm = (tl + tr) >> 1 ; + return min(get(v*2, tl, tm, l, r), get(v*2+1, tm+1, tr, l, r)) ; +} + +L col(int v, int tl, int tr, int l, int r, int mn) +{ + if(l <= tl && tr <= r) + return (t[v].mn == mn?t[v].c:0) ; + + if(l > tr || r < tl) + return 0 ; + + int tm = (tl + tr) >> 1 ; + L cl = 0 ; + cl += col(v*2, tl, tm, l, r, mn) ; + cl += col(v*2+1, tm+1, tr, l, r, mn) ; + return cl ; +} + +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, r, x ; + cin >> type ; + if(type == 1) + { + cin >> l >> x ; + update(1, 0, n-1, l, x) ; + } + else + { + cin >> l >> r ; + int mn = get(1, 0, n-1, l, r-1), cl = col(1, 0, n-1, l, r-1, mn) ; + cout << mn << ' ' << cl << '\n' ; + } + } +} \ No newline at end of file diff --git a/data-structures/segtreekth.cpp b/data-structures/segtreekth.cpp new file mode 100644 index 0000000..8db3f76 --- /dev/null +++ b/data-structures/segtreekth.cpp @@ -0,0 +1,99 @@ +/* +#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 ; + +int t[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] = t[v*2] + t[v*2+1] ; +} + +void update(int v, int tl, int tr, int l) +{ + if(tl == tr && tl == l) + { + t[v] ^= 1; + return ; + } + + if(tr < l || l < tl) + return ; + + int tm = (tl + tr) >> 1 ; + update(v*2, tl, tm, l) ; + update(v*2+1, tm+1, tr, l) ; + t[v] = t[v*2] + t[v*2+1] ; +} + +int idx(int v, int tl, int tr, int k) +{ + if(tl == tr) + return tl ; + + int tm = (tl + tr) >> 1 ; + if(t[v*2] >= k) + return idx(v*2, tl, tm, k) ; + else + return idx(v*2+1, tm+1, tr, k-t[v*2]) ; +} + +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 ; + cin >> type ; + if(type == 1) + { + cin >> l ; + update(1, 0, n-1, l) ; + } + else + { + cin >> l ; + cout << idx(1, 0, n-1, l+1) << '\n' ; + } + } +} \ No newline at end of file diff --git a/data-structures/segtreeminupdx.cpp b/data-structures/segtreeminupdx.cpp new file mode 100644 index 0000000..c61408e --- /dev/null +++ b/data-structures/segtreeminupdx.cpp @@ -0,0 +1,99 @@ +/* +#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] = min(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] = min(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 INF ; + + int tm = (tl + tr) >> 1 ; + return min(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 >> m ; + for(int i = 0 ; i < n ; ++i) + cin >> a[i] ; + + build(1, 0, n-1) ; + + while(m--) + { + int type, l, r, x ; + cin >> type ; + if(type == 1) + { + cin >> l >> x ; + update(1, 0, n-1, l, x) ; + } + else + { + cin >> l >> r ; + cout << get(1, 0, n-1, l, r-1) << '\n' ; + } + } +} \ No newline at end of file diff --git a/data-structures/segtreesufpref.cpp b/data-structures/segtreesufpref.cpp new file mode 100644 index 0000000..b763e91 --- /dev/null +++ b/data-structures/segtreesufpref.cpp @@ -0,0 +1,96 @@ +/* +#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 a[MaxN], n, m ; + +struct{ + L pref = -INF, suff = -INF, sum = -INF, ans = -INF ; +} t[MaxN*4] ; + +void build(int v, int tl, int tr) +{ + if(tl == tr) + { + t[v].sum = a[tl] ; + t[v].ans = a[tl] ; + t[v].pref = a[tl] ; + t[v].suff = a[tl] ; + return ; + } + + int tm = (tl + tr) >> 1 ; + build(v*2, tl, tm) ; + build(v*2+1, tm+1, tr) ; + t[v].ans = max(max(t[v*2].ans, t[v*2+1].ans), t[v*2].suff + t[v*2+1].pref) ; + t[v].sum = t[v*2].sum + t[v*2+1].sum ; + t[v].pref = max(t[v*2].pref, t[v*2].sum + t[v*2+1].pref) ; + t[v].suff = max(t[v*2+1].suff, t[v*2].suff + t[v*2+1].sum) ; +} + +void update(int v, int tl, int tr, int l, int x) +{ + if(tl == tr && tl == l) + { + t[v].sum = x ; + t[v].ans = x ; + t[v].pref = x ; + t[v].suff = 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].ans = max(max(t[v*2].ans, t[v*2+1].ans), t[v*2].suff + t[v*2+1].pref) ; + t[v].sum = t[v*2].sum + t[v*2+1].sum ; + t[v].pref = max(t[v*2].pref, t[v*2].sum + t[v*2+1].pref) ; + t[v].suff = max(t[v*2+1].suff, t[v*2].suff + t[v*2+1].sum) ; +} + +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) ; + + cout << max(0ll, t[1].ans) << '\n' ; + while(m--) + { + int l, x ; + cin >> l >> x ; + update(1, 0, n-1, l, x) ; + cout << max(0ll, t[1].ans) << '\n' ; + } +} \ No newline at end of file diff --git a/data-structures/segtreesumupdlr.cpp b/data-structures/segtreesumupdlr.cpp new file mode 100644 index 0000000..f8fb8d3 --- /dev/null +++ b/data-structures/segtreesumupdlr.cpp @@ -0,0 +1,117 @@ +/* +#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 ; + +int t[4*MaxN], mod[4*MaxN], a[MaxN], n ; + +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] = t[v*2] + t[v*2+1] ; +} + +void push(int v, int tl, int tr) +{ + if(mod[v] != 0 && v*2+1 < 4*MaxN) + { + mod[v*2] = mod[v*2+1] = mod[v] ; + mod[v] = 0 ; + + int tm = (tl + tr) >> 1 ; + t[v*2] = (tm-tl+1) * mod[v*2] ; + t[v*2+1] = (tr-tm) * mod[v*2+1] ; + } +} + +void update(int v, int tl, int tr, int l, int r, int x) +{ + if(l <= tl && tr <= r) + { + t[v] = x * (tr-tl+1) ; + mod[v] = x ; + return ; + } + + if(tr < l || r < tl) + return ; + + push(v, tl, tr) ; + int tm = (tl + tr) >> 1 ; + update(v*2, tl, tm, l, r, x) ; + update(v*2+1, tm+1, tr, l, r, x) ; + t[v] = t[v*2] + t[v*2+1] ; +} + +int 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 ; + + push(v, tl, tr) ; + 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 = 1 ; i <= n ; ++i) + cin >> a[i] ; + + build(1, 1, n) ; + + int t ; + cin >> t ; + while(t--) + { + int type, l, r, x ; + cin >> type ; + if(type == 1) + { + cin >> l >> r >> x ; + update(1, 1, n, l, r, x) ; + } + else + { + cin >> l >> r ; + cout << get(1, 1, n, l, r) << '\n' ; + } + } +} \ No newline at end of file diff --git a/data-structures/segtreesumupdx.cpp b/data-structures/segtreesumupdx.cpp new file mode 100644 index 0000000..f01005d --- /dev/null +++ b/data-structures/segtreesumupdx.cpp @@ -0,0 +1,99 @@ +/* +#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] = 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] = 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 >> m ; + for(int i = 0 ; i < n ; ++i) + cin >> a[i] ; + + build(1, 0, n-1) ; + + while(m--) + { + int type, l, r, x ; + cin >> type ; + if(type == 1) + { + cin >> l >> x ; + update(1, 0, n-1, l, x) ; + } + else + { + cin >> l >> r ; + cout << get(1, 0, n-1, l, r-1) << '\n' ; + } + } +} \ No newline at end of file diff --git a/data-structures/sparse-table.cpp b/data-structures/sparse-table.cpp new file mode 100755 index 0000000..cbc2327 --- /dev/null +++ b/data-structures/sparse-table.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include +#include + +#define pb push_back +#define ub upper_bound +#define lb lower_bound +#define sc(x) scanf("%I64d",&x) +#define pr(x) printf("%I64d\n",x) +#define int long long + +using namespace std ; + +const int MAXN = 1e9+1 ; +const int N = 1e6+1 ; +int n , m , k , x , y , z , gt ; +int a[N] , d[30][N] , p[N] ; + +void build () +{ + for (int i = 1 ; i <= n ; ++i ) + d[0][i] = a[i] ; + for (int i = 2 ; i < N ; ++i ) + p[i] = p[i / 2] + 1; + for (int i = 1 ; i <= p[n] ; ++i ) + for (int j = 1 ; j <= n - (1 << p[i]) + 1 ; ++j ) + d[i][j] = min (d[i - 1][j] , d[i - 1][j + (1 << (i - 1))]) ; +} + +int get (int l , int r) +{ + int rr = max(r,l) ; + int ll = min(r,l) ; + int m = rr-ll + 1 ; + m = p[m] ; + return min (d[m][ll] , d[m][rr - (1 << m) + 1]) ; +} + +main() +{ + cin >> n >> m >> a[1] ; + for ( int i = 2 ; i <= n ; ++i ) + { + a[i] = (23*a[i-1]+21563) % 16714589 ; + } + build() ; + int l , r ; + cin >> l >> r ; + for ( int i = 2 ; i <= m ; ++i ) + { + gt = get(l,r) ; + l = ((17*l+751+gt+2*(i-1))%n)+1 ; + r = ((13*r+593+gt+5*(i-1))%n)+1 ; + } + gt = get(l,r) ; + cout << l << ' ' << r << ' ' << gt ; +} diff --git a/graphs/bridges.cpp b/graphs/bridges.cpp new file mode 100755 index 0000000..6eef9ba --- /dev/null +++ b/graphs/bridges.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include + +using namespace std ; + +map,int> mp ; + +int tin[20001] , fup[20001] , timer ; + +vector g[20001] , ans ; + +bool used[20001] ; + +void dfs(int v,int p = -1) +{ + used[v] = 1 ; + tin[v]=fup[v]=timer,timer++ ; + for(int i = 0 ; i < g[v].size() ; ++i) + { + int u = g[v][i] ; + if(u==p) + continue ; + if(used[u]) + fup[v]=min(fup[v],tin[u]) ; + else + { + dfs(u,v) ; + fup[v] = min(fup[v],fup[u]) ; + if(tin[v]pr ; + pr.first = v , pr.second = u ; + ans.push_back(mp[pr]) ; + } + } + } +} + +int main() +{ + int n , m ; + cin >> n >> m ; + for(int i = 0 ; i < m ; ++i) + { + pairpr ; + cin >> pr.first >> pr.second ; + mp[pr] = i+1 ; + pairpr1; + pr1.first = pr.second ; + pr1.second = pr.first ; + mp[pr1] = i+1 ; + g[pr.first].push_back(pr.second) ; + g[pr.second].push_back(pr.first) ; + } + for(int i = 0 ; i <= n ; ++i) + { + if(!used[i]) + dfs(i) ; + } + cout << ans.size() << '\n' ; + sort(ans.begin(),ans.end()) ; + for(int i = 0 ; i < ans.size() ; ++i) + { + cout << ans[i] << ' ' ; + } +} + diff --git a/graphs/cutpoints.cpp b/graphs/cutpoints.cpp new file mode 100755 index 0000000..4b4d4e8 --- /dev/null +++ b/graphs/cutpoints.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +using namespace std ; + +vector g[20001] ; set ans ; +bool used[20001] ; +int tin[20001] , fup[20001] , timer ; + +void dfs(int v, int p = -1) +{ + used[v] = 1 ; + tin[v]=fup[v]=timer ,++timer ; + int children = 0 ; + for(int i = 0 ; i < g[v].size() ; ++i) + { + int u = g[v][i] ; + if(u==p) + continue ; + if(used[u]) + fup[v]=min(fup[v],tin[u]) ; + else + { + dfs(u,v) ; + fup[v] = min(fup[v],fup[u]) ; + if(tin[v]<=fup[u]&&p!=-1) + { + ans.insert(v) ; + } + ++children ; + } + } + if(children>1&&p==-1) + { + ans.insert(v) ; + } +} + +int main() +{ + freopen("points.in","r",stdin) ; + freopen("points.out","w",stdout) ; + int n , m ; + cin >> n >> m ; + for(int i = 0 ; i < m ; ++i) + { + int l , r ; + cin >> l >> r ; + g[l].push_back(r) ; + g[r].push_back(l) ; + } + for(int i = 1 ; i <= n ; ++i) + if(!used[i]) dfs(i) ; + cout << ans.size() << '\n' ; + for (std::set::iterator i= ans.begin() ; i != ans.end() ; ++i) + { + cout << (*i) << ' ' ; + } +} diff --git a/graphs/dijkstra.cpp b/graphs/dijkstra.cpp new file mode 100755 index 0000000..2aaa375 --- /dev/null +++ b/graphs/dijkstra.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std ; + +const long long MaxN = 1e6 + 17; +const long long INF = 1e14 + 17; + +long long n , m ; +vector > g[MaxN] ; +set > S ; +long long s , f ; +long long d[MaxN] ; + +int main () +{ + scanf ("%d%d", &n, &m); + s = 1 ; + for (int i = 1; i <= m; ++ i) { + int x, y, w; + scanf ("%d%d%d", &x, &y, &w); + g[x].push_back (make_pair (y, w)); + g[y].push_back (make_pair (x, w)); + } + for (int i = 1 ; i <= n ; ++i) + d[i] = INF ; + d[s] = 0 ; + vector p(n+1) ; + S.insert (make_pair (0,s)) ; + while (!S.empty()) { + int v = S.begin() -> second; + S.erase (*S.begin()); + for (int i = 0 ; i < g[v].size() ; ++ i) { + int to = g[v][i].first; + int w = g[v][i].second; + if (d[v] + w < d[to]) + { + S.erase(make_pair(d[to] , to)) ; + d[to] = d[v] + w ; + p[to] = v ; + S.insert(make_pair(d[to] , to)) ; + } + } + } + if(d[n] == INF) + return (cout << -1 , 0) ; + vector ans ; + for(int v = n ; v != 1 ; v = p[v]) + { + if(p[v] == 0) + return(cout << -1,0) ; + ans.push_back(v) ; + } + ans.push_back(1) ; + for(int i = ans.size() - 1 ; i >= 0 ; --i) + cout << ans[i] << ' ' ; +} diff --git a/graphs/lca.cpp b/graphs/lca.cpp new file mode 100755 index 0000000..76a17b3 --- /dev/null +++ b/graphs/lca.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std ; + +vector g[500001] ; + +int n , m , lvl[500001] , used[500001] , k , up[500001][21] ; + +void dfs(int v , int p) +{ + used[v] = 1 ; + up[v][0] = p ; + for (int jump = 20 ; jump >= 1 ; --jump) + up[v][jump] = up[up[v][jump-1]][jump-1] ; + for (int i = 0 ; i < g[v].size() ; ++i) + { + int to = g[v][i] ; + if (!used[to]) + { + lvl[to] = lvl[v] + 1 ; + dfs(to, v) ; + } + } +} + +int lca(int a , int b) +{ + if (lvl[a] < lvl[b]) + swap(a,b) ; + for (int jump = 20 ; jump >= 0 ; --jump) + if (lvl[up[b][jump]] >= lvl[a]) + b = up[b][jump] ; + if (a == b) + return b ; + for (int jump = 20 ; jump >= 0 ; --jump) + if (up[a][jump] != up[b][jump]) + a = up[a][jump] , + b = up[b][jump] ; + return up[a][0] ; +} + +main() +{ + cin >> n ; + vector >z ; + for (int i = 0 ; i < n ; ++i) + { + string s ; + cin >> s ; + int a , b ; + cin >> a >> b; + if(s=="ADD") + g[a].push_back(b) ; + else + z.push_back(make_pair(a,b)) ; + } + lvl[1] = 1 ; + dfs(1, 1) ; + for (int i = 0 ; i < z.size() ; ++i) + cout << lca(z[i].first,z[i].second) << '\n' ; +} + diff --git a/graphs/maxflow.cpp b/graphs/maxflow.cpp new file mode 100755 index 0000000..b66e18a --- /dev/null +++ b/graphs/maxflow.cpp @@ -0,0 +1,71 @@ +#include +#define pb push_back +#define int long long +using namespace std ; +const long long MAXN = 1e9+1 ; +const int N = (int)(2e4) + 256 ; +int n , m , timer , used[N] , lim ; +struct edge{ + int v , u , c , f ; +}; +vector e ; +vector g[N] ; +inline void addEdge(int v , int u , int c ) +{ + e.pb({v,u,c,0}) ; + g[v].pb(e.size()-1) ; + e.pb({u,v,0,0}) ; + g[u].pb(e.size()-1) ; +} +int dfs ( int v , int t , int pushed = MAXN , int p = -1 ) +{ + if (used[v] == timer || pushed < lim) return 0; + if (!pushed || v == t) return pushed; + + used[v] = timer ; + for ( int i = 0 ; i < g[v].size() ; ++i ) + { + int id = g[v][i] ; + int to = e[id].u , f = e[id].f , c = e[id].c ; + if ( p == to || f == c ) continue ; + int nxt = dfs ( to , t , min ( pushed , c - f) , v); + if ( nxt > 0 ) + { + e[id].f += nxt ; + e[id^1].f -= nxt ; + return nxt ; + } + } + return 0 ; +} +inline int fordf(int s,int t) +{ + int res = 0 ; + lim = 1 << 30 ; + while ( lim > 0 ) + { + ++timer ; + int pushed = dfs(s,t) ; + if ( !pushed ) + { + lim >>= 1 ; + continue ; + } + res += pushed ; + } + return res ; +} +main() +{ + freopen("flow.in","r",stdin) ; + freopen("flow.out","w",stdout) ; + cin >> n >> m ; + for ( int i = 0 ; i < m ; ++i ) + { + int x , y , c ; + cin >> x >> y >> c ; + addEdge(x,y,c) ; + } + cout << fordf(1,n) << endl ; + return 0 ; +} diff --git a/graphs/unionday.cpp b/graphs/unionday.cpp new file mode 100644 index 0000000..a1ed79e --- /dev/null +++ b/graphs/unionday.cpp @@ -0,0 +1,73 @@ +/* +#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 float INF = 1e9+7 ; + +pair a[5001] ; +int p[5001] ; +float g[5001][5001], cost ; + +int32_t main() +{ + freopen("unionday.in" , "r" , stdin) ; + freopen("unionday.out" , "w" , stdout) ; + int n ; + cin >> n ; + for(int i = 0 ; i < n ; ++i) + { + cin >> a[i].F >> a[i].S ; + for(int j = 0 ; j < i ; ++j) + { + float d = (a[i].F-a[j].F)*(a[i].F-a[j].F)+(a[i].S-a[j].S)*(a[i].S-a[j].S) ; + d = sqrt(d) ; + g[i][j] = d ; + g[j][i] = d ; + } + } + vector used (n); + vector min_e (n, INF); + vector sel_e (n, -1); + min_e[0] = 0; + for (int i=0; i> s >> c ; + + + + int cnt = 0 ; + for(int i = 0 ; i < s.size() ; ++i) + if(s[i] == '1') + cnt ++ ; + + for(L i = 1, j = cnt-1; i <= s.size() ; i++) + { + ans[i] = c ; + for(L j = 1 ; j < i ; ++j) + { + ans[i]+=binpow(c, j)*pascal[i][j+1]; + ans[i]%=MOD; + } + if(s[s.size()-i] == '1') + anss+=ans[i]*binpow(c, j--), anss %= MOD ; + } + + cout << anss+1 ; +} \ No newline at end of file diff --git a/math/meet-in-the-middle.cpp b/math/meet-in-the-middle.cpp new file mode 100755 index 0000000..560c729 --- /dev/null +++ b/math/meet-in-the-middle.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +typedef long long ll; +const int maxn = (int) 3e5 + 1; + +int n, k; +ll ans; +int a[maxn], p, u[maxn]; +int main(){ + cin >> n >> k; + for(int i = 0; i < n; i++){ + cin >> a[i]; + } + p = n / 2; + for(int i = 0; i < (1 << p); i++){ + ll res = 0; + for(int j = 0; j < p; j++){ + if((i >> j) & 1){ + res += a[j]; + } + } + u[i] = res % k; + ans = max(ans, res % k); + } + sort(u, u + (1 << p)); + n -= p; + for(int i = 0; i < (1 << n); i++){ + ll res = 0; + for(int j = 0; j < n; j++){ + if((i >> j) & 1){ + res += a[j + p]; + } + } + res %= k; + ans = max(ans, res); + int l = 0, r = (1 << p) - 1; + while(r - l > 1){ + int mid = (l + r) / 2; + if(u[mid] + res >= k){ + r = mid; + } + else { + l = mid; + } + } + int p1 = 0; + if(u[l] + res >= k){ + p1 = l; + } + else { + p1 = r; + } + ans = max(ans, (u[(1 << p) - 1] + res) % k); + ans = max(ans, (u[p1 - 1] + res) % k); + } + cout << ans; +} diff --git a/math/pascaltriangle.cpp b/math/pascaltriangle.cpp new file mode 100755 index 0000000..d82ff0c --- /dev/null +++ b/math/pascaltriangle.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std ; + +long long c[200][20000] ; + +main() +{ + freopen("combination.in","r",stdin) ; + freopen("combination.out","w",stdout) ; + int n , k ; + cin >> n >> k; + for(int i = 0 ; i <= n ; ++i) + { + c[i][0] = 1 ; c[i][i] = 1 ; + for(int j = 1 ; j < i ; ++j) + c[i][j] = c[i-1][j-1]+c[i-1][j] ; + } + cout << c[n][k] ; +} diff --git a/math/two-turtles.cpp b/math/two-turtles.cpp new file mode 100644 index 0000000..962fd66 --- /dev/null +++ b/math/two-turtles.cpp @@ -0,0 +1,118 @@ +#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 ; + +char c[3001][3001] ; +L dp[3001][3001], pd[3001][3001] ; +bool ok[3001][3001], ok2[3001][3001] ; + +int32_t main() +{ + iosb ; + int n, m ; + cin >> n >> m ; + for(int i = 1 ; i <= n ; ++i) + for(int j = 1 ; j <= m ; ++j) + cin >> c[i][j] ; + + dp[2][1] = 1, pd[1][2] = 1 ; + if(c[2][1]!='#') + ok[2][1] = 1, ok2[2][1] = 1 ; + if(c[1][2]!='#') + ok[1][2] = 1, ok2[1][2] = 1 ; + for(int i = 2 ; i <= n ; ++i) + for(int j = 1 ; j <= m ; ++j) + { + if(i==2&&j==1) + continue ; + if(i==2) + { + if(c[i][j] == '#') + ok[i][j] = 0 ; + else if(!ok[i][j-1]) + ok[i][j] = 0 ; + else + dp[i][j] = dp[i][j-1], dp[i][j]%=MOD, ok[i][j] = 1 ; + continue ; + } + if(j==1) + { + if(c[i][j] == '#') + ok[i][j] = 0 ; + else if(!ok[i-1][j]) + ok[i][j] = 0 ; + else + dp[i][j] = dp[i-1][j], dp[i][j]%=MOD, ok[i][j] = 1 ; + continue ; + } + if(c[i][j] == '#') + ok[i][j] = 0 ; + else if(!ok[i][j-1] && !ok[i-1][j]) + ok[i][j] = 0 ; + else + dp[i][j] = dp[i][j-1] + dp[i-1][j], dp[i][j]%=MOD, ok[i][j] = 1 ; + } + + for(int i = 1 ; i <= n ; ++i) + for(int j = 2 ; j <= m ; ++j) + { + if(i==1&&j==2) + continue ; + if(i==1) + { + if(c[i][j] == '#') + ok2[i][j] = 0 ; + else if(!ok2[i][j-1]) + ok2[i][j] = 0 ; + else + pd[i][j] = pd[i][j-1], pd[i][j]%=MOD, ok2[i][j] = 1 ; + continue ; + } + if(j==2) + { + if(c[i][j] == '#') + ok2[i][j] = 0 ; + else if(!ok2[i-1][j]) + ok2[i][j] = 0 ; + else + pd[i][j] = pd[i-1][j], pd[i][j]%=MOD, ok2[i][j] = 1 ; + continue ; + } + if(c[i][j] == '#') + ok2[i][j] = 0 ; + else if(!ok2[i][j-1] && !ok2[i-1][j]) + ok2[i][j] = 0 ; + else + pd[i][j] = pd[i][j-1] + pd[i-1][j], pd[i][j]%=MOD, ok2[i][j] = 1 ; + } + + if(ok[n][m-1] && ok2[n-1][m]) + { + L ans = (((dp[n][m-1]*pd[n-1][m])%MOD)-((dp[n-1][m]*pd[n][m-1])%MOD)) ; + if(ans<0) + ans = MOD+ans ; + cout << ans << '\n' ; + } + else + cout << 0 << '\n' ; +} \ No newline at end of file diff --git a/other/ioi-ideas.pdf b/other/ioi-ideas.pdf new file mode 100755 index 0000000..500a1a7 Binary files /dev/null and b/other/ioi-ideas.pdf differ