Skip to content

Commit

Permalink
Added pbds code
Browse files Browse the repository at this point in the history
  • Loading branch information
puneet1747 committed Oct 8, 2021
1 parent 354fd0c commit e0e63f0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Data Structures/Ordered-Statistic-Tree(pbds).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define bolt \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define endl "\n"

#define int long long
const int N = 2e5 + 5;

#define T pair<int, int>
typedef tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define ll long long

// find1_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;

/*1. Number of elements strictly greater than a[i]
int cnt = sz(curr) - (int)curr.order_of_key({a[i]+1,0});
2. Number of elements strictly less than a[i]
int cnt = (int)curr.order_of_key({a[i],i});
3. Use insert like this : curr.insert({a[id], id});
4. Finding the k -th smallest element in the set using * because
find_by_order returns an iterator
auto k-th_smallest_value = *(o_set.find_by_order(k - 1))
It is (k - 1) since it uses zero indexes
5. Erasing : curr.erase({a[i], i});*/

/*
Example :
Problem : https://codeforces.com/contest/1042/problem/D
Solution :
*/

ll a[200005], n, k, prefix[200005], ans;

signed main()
{
#ifdef ONLINE_JUDGE
bolt;
#endif
cin >> n >> k;
oset s;
s.insert({0, 0});
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
prefix[i] = prefix[i - 1] + a[i];
int now = s.order_of_key({prefix[i] - k, 1e9});
ans += s.size() - now;
s.insert({prefix[i], i});
}
cout << ans;
return 0;
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This Repo is For those who want to contribute in an open Source Repo for Hacktob
├── Data Structures
│   ├── DSU.cpp
│   ├── Mo's-algorithm.cpp
│   ├── Ordered-Statistic-Tree(pbds).cpp
├── Divide and conquer
│   ├── maximum-subarray-sum-O(nlogn).cpp
│   ├── merge-sort-O(nlogn).cpp
Expand Down

0 comments on commit e0e63f0

Please sign in to comment.