Submission #1172851

#TimeUsernameProblemLanguageResultExecution timeMemory
1172851khangai11Vudu (COCI15_vudu)C++20
42 / 140
1098 ms59028 KiB
#include <iostream> #include<string> #include<algorithm> #include<functional> #include<cmath> #include<queue> #include<vector> #include<map> #include<stack> #include<list> #include<deque> #include<set> #include<unordered_set> #include<unordered_map> #include<numeric> #include<bitset> #include<iomanip> #include<cstdlib> #include<time.h> #include <functional> #include <chrono> #include <thread> #include <fstream> #include <random> using namespace std; #ifdef _DEBUG #define prnt(a) cout<<#a<<"="<<a<<endl #else #define prnt(a) (void)0 #endif // _DEBUG #ifdef _MSC_VER # include <intrin.h> # define __builtin_popcount __popcnt #endif #define ull unsigned long long #define ll int #define ld long double #define INF (1LL<<30) #define INFLL (1LL<<62) #define MOD 1000000007 #define MOD2 998244353 #define rep(i,st,en) for(ll i=(st);i<(en);++i) #define vld vector<ld> #define vll vector<ll> #define vvll vector<vll> #define vi vector<int> #define vvi vector<vi> #define vb vector<bool> #define vvb vector<vb> #define pii pair<int,int> #define pll pair<ll,ll> #define vpii vector<pii> #define vpll vector<pll> #define VS vector<string> #define MY_PI 3.141592653589793238462643383279502884L #define all(v) (v).begin(), (v).end() #define rd(...) __VA_ARGS__; read(__VA_ARGS__) #define rdv(value,...) value(__VA_ARGS__);cin >> value template <class T> auto& operator>>(istream& is, vector<T>& xs) { for (auto& x : xs) is >> x; return is; } template <class T> auto& operator<<(ostream& os, vector<T>& xs) { int sz = xs.size(); rep(i, 0, sz) os << xs[i] << " \n"[i + 1 == sz]; return os; } template <class T, class Y> auto& operator<<(ostream& os, pair<T, Y>& xs) { os << "{" << xs.first << ", " << xs.second << "}"; return os; } template <class T, class Y> auto& operator>>(istream& is, vector<pair<T, Y>>& xs) { for (auto& [x1, x2] : xs) is >> x1 >> x2; return is; } template <class ...Args> auto& read(Args & ...args) { return (cin >> ... >> args); } #define write(...) writemy(__VA_ARGS__);cout<<"\n" void writemy() {} template <typename Head, class ...Args> void writemy(const Head& head, const Args & ...args) { cout << head << " "; writemy(args...); } class UnionFindTree { public: vector<ll> parent; vector<ll> union_size; vector<ll> w; int len; int nn; ll value = 0; UnionFindTree(int n) { len = n; nn = n; parent.resize(n + 1, 0); w.resize(n + 1, 0); rep(i, 0, n + 1) { w[i] = i; } union_size.resize(n + 1, 1); value = 0; rep(i, 0, n + 1) parent[i] = i; } ll root(int a) { if (parent[a] == a) return a; parent[a] = root(parent[a]); return parent[a]; } void setWeight(int a, ll we) { w[a] = we; } ll getWeight(int a) { int ra = root(a); return w[ra]; } bool join(int a, int b) { if (a<0 || a>nn) return false; if (b<0 || b>nn) return false; int ra = root(a); int rb = root(b); if (ra == rb) return false; if (ra > rb) { parent[rb] = ra; union_size[ra] += union_size[rb]; w[ra] ^= w[rb]; } else { parent[ra] = rb; union_size[rb] += union_size[ra]; w[rb] ^= w[ra]; } len--; return true; } ll size(int a) { return union_size[root(a)]; } }; template <typename T> class segmentTree { public: vector<T> v; int n; T(*func)(T, T); T defval; segmentTree(int s, T(*f)(T, T)) { n = 1; while (n < s) n *= 2; v.resize(2 * n, defval); func = f; } segmentTree(int s, T(*f)(T, T), T defaultValue) { n = 1; while (n < s) n *= 2; defval = defaultValue; v.resize(2 * n, defval); func = f; } /// <summary> /// use this before calculateTree() /// index starts 0 /// </summary> void setNode(int ind, T val) { v[ind + n - 1] = val; } void calculateTree() { for (int i = n - 2; i >= 0; i--) v[i] = func(v[i * 2 + 1], v[i * 2 + 2]); } /// <summary> /// add val to value[index] /// index starts 0 /// </summary> void addValue(int ind, T val) { updateNode(ind, val + v[ind + n - 1]); } T getValue(int ind) { return v[ind + n - 1]; } /// <summary> /// set value[ind] to val /// index starts 0 /// </summary> void updateNode(int ind, T val) { v[ind + n - 1] = val; for (int i = (ind + n - 2) / 2; i != 0; i = (i - 1) / 2) { v[i] = func(v[i * 2 + 1], v[i * 2 + 2]); } v[0] = func(v[1], v[2]); } /// <summary> /// query sum of [l,r] from [st,en] range /// </summary> /// <returns></returns> T queryInternal(int ind, int st, int en, int l, int r) { if (st >= l && en <= r) return v[ind]; if (l > en || r < st) return defval; int mid = st + (en - st) / 2; return func(queryInternal(ind * 2 + 1, st, mid, l, r), queryInternal(ind * 2 + 2, mid + 1, en, l, r)); } /// <summary> /// returns sum between [l,r] /// index starts 0 /// </summary> T query(int l, int r) { if (l > r) return defval; return queryInternal(0, 0, n - 1, l, r); } /// <summary> /// returns minimum x which is SUM(0,x) >= sum /// ind should be 0 /// </summary> int querySumIndex(int ind, ll sum) { int left, right; left = ind * 2 + 1; right = left + 1; if (ind >= n - 1) { return (ind - n + 1); } //if (v[ind] < sum) // return 0; if (v[left] >= sum) { return querySumIndex(left, sum); } else { return querySumIndex(right, sum - v[left]); } } }; template <typename T> T my_gcd(T a, T b) { return gcd(a, b); } template <typename T> T my_min(T a, T b) { return min(a, b); } template <typename T> T my_max(T a, T b) { return max(a, b); } template <typename T> T my_and(T a, T b) { return (a & b); } template <typename T> T my_xor(T a, T b) { return (a ^ b); } template <typename T> T my_or(T a, T b) { return (a | b); } template <typename T> T my_sum(T a, T b) { return (a + b); } template <typename T> T my_sum_mod(T a, T b) { return (a + b) % MOD; } /// <summary> /// index starts 0 /// </summary> class SparseTable { public: vvll table; ll(*func)(ll, ll); ll deep = 0; SparseTable(vector<ll> vec, ll(*f)(ll, ll)) { this->func = f; ll s = vec.size(); deep = floor(log2(s)); table.resize(deep + 1); table[0].resize(s); rep(i, 0, s) table[0][i] = vec[i]; rep(k, 1, deep + 1) { ll g = pow(2, k - 1); table[k].resize(s); rep(i, 0, s - (g * 2 - 1)) { table[k][i] = f(table[k - 1][i], table[k - 1][i + g]); } } } /// <summary> /// index starts 0 /// </summary> /// <param name="st">index of start pos</param> /// <param name="size">size of query</param> /// <returns></returns> ll query(ll st, ll size) { ll g = floor(log2(size)); ll ret = this->func(table[g][st], table[g][st + size - pow(2, g)]); return ret; } }; /// <summary> /// index starts 1 /// </summary> class BITree { public: vector<ll> v; int sz; BITree(int n) { v.resize(n + 1, 0); sz = n; } void add(int ind, ll val) { int i = ind; while (i <= sz) { v[i] += val; i += (i & (-i)); } } ll query(int ind) { ll r = 0; int i = ind; if (i > sz) i = sz; while (i > 0) { r += v[i]; i -= (i & (-i)); } return r; } }; void solve(ll test) { ll rd(n); vll rdv(a, n); ll rd(p); vll s(n + 1, 0); rep(i, 1, n + 1) { s[i] = s[i - 1] + a[i - 1] - p; } map<ll, ll> mp; rep(i, 0, n + 1) { mp[s[i]] = 1; } ll ind = 0; for (auto& v : mp) { v.second = ind++; } BITree t(ind + 1); long long ans = 0; rep(i, 0, n + 1) { ans += t.query(mp[s[i]]+1); t.add(mp[s[i]]+1, 1); } cout << ans << "\n"; } int main() { //initFacts(100, MOD2); //findPrimes(400000); //freopen("four_in_a_burrow_input.txt", "r", stdin); //freopen("output.txt", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int test = 1; //cin >> test; for (int t = 1; t <= test; t++) solve(t); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...