|
| 1 | +/* |
| 2 | +Submission Link: |
| 3 | +https://codeforces.com/contest/61/submission/356381771 |
| 4 | +*/ |
| 5 | + |
| 6 | +/* |
| 7 | +Problem: Enemy is weak |
| 8 | +Link: https://codeforces.com/contest/61/problem/E |
| 9 | +Author: Krishna200608 |
| 10 | +
|
| 11 | +Short Problem Statement: |
| 12 | + Given distinct integers, count how many triplets (i, j, k) exist |
| 13 | + such that i < j < k and the values strictly decrease: |
| 14 | + a[i] > a[j] > a[k]. |
| 15 | +
|
| 16 | +Approach: |
| 17 | + We first compress the values to handle large numbers easily. |
| 18 | + Treat each element as the middle of a triplet. |
| 19 | + For every position, count bigger elements on the left . |
| 20 | + and count smaller elements on the right. |
| 21 | + A Fenwick Tree helps do these counts efficiently . |
| 22 | + The final answer is the sum of left multiplied by right for all positions. |
| 23 | +
|
| 24 | +Time Complexity: O(N log N) |
| 25 | +Space Complexity: O(N) |
| 26 | +*/ |
| 27 | + |
| 28 | + |
| 29 | +#include <bits/stdc++.h> |
| 30 | +using namespace std; |
| 31 | + |
| 32 | +#define MOD 1000000007 |
| 33 | +const int M = 1e9 + 7; |
| 34 | +const double PI = acos(-1.0); |
| 35 | +#define INF 1e18 |
| 36 | +#define pb push_back |
| 37 | +#define ppb pop_back |
| 38 | +#define ll long long |
| 39 | +#define no cout << "NO" << endl |
| 40 | +#define yes cout << "YES" << endl |
| 41 | +#define ff first |
| 42 | +#define ss second |
| 43 | +#define inn(x) int x; cin >> x |
| 44 | +#define ill(x) ll x; cin >> x |
| 45 | +#define all(x) x.begin(), x.end() |
| 46 | +#define in(a) for (int i = 0; i < (int)a.size(); i++) cin >> a[i] |
| 47 | +#define out(a) for (int i = 0; i < (int)a.size(); i++) cout << a[i] << " " |
| 48 | +typedef vector<int> vi; |
| 49 | +typedef vector<ll> vll; |
| 50 | +#define ceil_div(n, x) (((n) % (x) == 0) ? ((n) / (x)) : ((n) / (x) + 1)) |
| 51 | +#define debug(x) cout << "x -> " << x << endl |
| 52 | +#define outt(x) cout << x << endl |
| 53 | +#define endl "\n" |
| 54 | + |
| 55 | +const int sz = 1000005; |
| 56 | +int bit[sz]; |
| 57 | +int nn; |
| 58 | + |
| 59 | +void upd(int i, int x) { |
| 60 | + for (; i <= nn; i += i & -i) |
| 61 | + bit[i] += x; |
| 62 | +} |
| 63 | + |
| 64 | +int qry(int i) { |
| 65 | + int res = 0; |
| 66 | + for (; i > 0; i -= i & -i) |
| 67 | + res += bit[i]; |
| 68 | + return res; |
| 69 | +} |
| 70 | + |
| 71 | +void solve() { |
| 72 | + inn(n); |
| 73 | + nn = n; |
| 74 | + |
| 75 | + vi a(n); |
| 76 | + in(a); |
| 77 | + |
| 78 | + vi b = a; |
| 79 | + sort(all(b)); |
| 80 | + |
| 81 | + for (int i = 0; i < n; i++) { |
| 82 | + a[i] = lower_bound(all(b), a[i]) - b.begin() + 1; |
| 83 | + } |
| 84 | + |
| 85 | + vll le(n), ri(n); |
| 86 | + |
| 87 | + for (int i = 0; i < n; i++) { |
| 88 | + int smaller = qry(a[i]); |
| 89 | + int total = i; |
| 90 | + le[i] = total - smaller; |
| 91 | + upd(a[i], 1); |
| 92 | + } |
| 93 | + |
| 94 | + memset(bit, 0, sizeof(bit)); |
| 95 | + |
| 96 | + for (int i = n - 1; i >= 0; i--) { |
| 97 | + int smaller = qry(a[i] - 1); |
| 98 | + ri[i] = smaller; |
| 99 | + upd(a[i], 1); |
| 100 | + } |
| 101 | + |
| 102 | + ll ans = 0; |
| 103 | + for (int i = 0; i < n; i++) { |
| 104 | + ans += le[i] * ri[i]; |
| 105 | + } |
| 106 | + |
| 107 | + outt(ans); |
| 108 | +} |
| 109 | + |
| 110 | +signed main() { |
| 111 | + ios_base::sync_with_stdio(false); |
| 112 | + cin.tie(nullptr); |
| 113 | + cout.tie(nullptr); |
| 114 | + |
| 115 | + auto begin = chrono::high_resolution_clock::now(); |
| 116 | + |
| 117 | + int t = 1; |
| 118 | + while (t--) { |
| 119 | + solve(); |
| 120 | + } |
| 121 | + |
| 122 | + auto end = chrono::high_resolution_clock::now(); |
| 123 | + auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end - begin); |
| 124 | + cerr << "Time measured: " << elapsed.count() * 1e-6 << "ms"; |
| 125 | + |
| 126 | + return 0; |
| 127 | +} |
0 commit comments