|
| 1 | +//https://codeforces.com/contest/61/submission/356643204 |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +#define int long long |
| 6 | +#define ll long long |
| 7 | +#define ull unsigned long long |
| 8 | +#define ld long double |
| 9 | +#define pii pair<int,int> |
| 10 | +#define vi vector<int> |
| 11 | +#define vvi vector<vector<int>> |
| 12 | +#define vpi vector<pair<int,int>> |
| 13 | +#define all(x) (x).begin(), (x).end() |
| 14 | + |
| 15 | + |
| 16 | +const long long MAXN = 2e6; |
| 17 | +const long long MOD = 1e9 + 7; |
| 18 | +const int INF = 1e18; |
| 19 | +const ld EPS = 1e-9; |
| 20 | + |
| 21 | + |
| 22 | +#define fastio ios::sync_with_stdio(false); cin.tie(nullptr); |
| 23 | + |
| 24 | +#ifndef ONLINE_JUDGE |
| 25 | + #define debug(x) cerr << #x << " = " << x << "\n"; |
| 26 | +#else |
| 27 | + #define debug(x) |
| 28 | +#endif |
| 29 | + |
| 30 | +// Utility functions |
| 31 | +int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } |
| 32 | +int lcm(int a, int b) { return a / gcd(a, b) * b; } |
| 33 | +int mod_add(int a, int b, int m=MOD) { return ((a % m) + (b % m) + m) % m; } |
| 34 | +int mod_sub(int a, int b, int m=MOD) { return ((a % m) - (b % m) + m) % m; } |
| 35 | +int mod_mul(int a, int b, int m=MOD) { return ((a % m) * (b % m)) % m; } |
| 36 | + |
| 37 | +//Binary Exponentiation |
| 38 | +int binexp(int a, int b, int m=MOD) { |
| 39 | + int res = 1; |
| 40 | + a %= m; |
| 41 | + while(b > 0) { |
| 42 | + if(b & 1) res = (res * a) % m; |
| 43 | + a = (a * a) % m; |
| 44 | + b >>= 1; |
| 45 | + } |
| 46 | + return res; |
| 47 | +} |
| 48 | + |
| 49 | +int mod_inv(int a, int m=MOD) { |
| 50 | + return binexp(a, m - 2, m); |
| 51 | +} |
| 52 | + |
| 53 | +long long fac[MAXN + 1]; |
| 54 | +long long inv[MAXN + 1]; |
| 55 | + |
| 56 | +long long exp(long long x, long long n, long long m) { |
| 57 | + x %= m; |
| 58 | + long long res = 1; |
| 59 | + while (n > 0) { |
| 60 | + if (n % 2 == 1) { res = res * x % m; } |
| 61 | + x = x * x % m; |
| 62 | + n /= 2; |
| 63 | + } |
| 64 | + return res; |
| 65 | +} |
| 66 | + |
| 67 | +void factorial() { |
| 68 | + fac[0] = 1; |
| 69 | + for (long long i = 1; i <= MAXN; i++) { fac[i] = fac[i - 1] * i % MOD; } |
| 70 | +} |
| 71 | + |
| 72 | +void inverses() { |
| 73 | + inv[MAXN] = exp(fac[MAXN], MOD - 2, MOD); |
| 74 | + for (long long i = MAXN; i >= 1; i--) { inv[i - 1] = inv[i] * i % MOD; } |
| 75 | +} |
| 76 | + |
| 77 | +long long choose(long long n, long long r) { |
| 78 | + if (r > n)return 0ll; |
| 79 | + return (fac[n] * inv[r] % MOD * inv[n - r] % MOD) % MOD; |
| 80 | +} |
| 81 | + |
| 82 | +long long catalan(long long n) { |
| 83 | + return (exp(n + 1, MOD - 2, MOD) % MOD * choose(2 * n, n) % MOD) % MOD; |
| 84 | +} |
| 85 | + |
| 86 | +//Returns all prime numbers <=N |
| 87 | +vector<int> sieve(int n) { |
| 88 | + vector<bool> prime(n + 1, true); |
| 89 | + for (int p=2;p*p<=n;p++) { |
| 90 | + if (prime[p] == true) { |
| 91 | + |
| 92 | + for (int i=p*p;i<=n;i+=p) |
| 93 | + prime[i] = false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + vector<int> res; |
| 98 | + for (int p = 2; p <= n; p++){ |
| 99 | + if (prime[p]){ |
| 100 | + res.push_back(p); |
| 101 | + } |
| 102 | + } |
| 103 | + return res; |
| 104 | +} |
| 105 | + |
| 106 | +//Prime factors of all numbers upto N |
| 107 | +vector<vector<int>> primefactors(int N) |
| 108 | +{ |
| 109 | + vvi pfac(N + 1); |
| 110 | + for (int i=2;i<=N;i++){ |
| 111 | + if (!pfac[i].empty()) |
| 112 | + continue; |
| 113 | + |
| 114 | + for (int j = i; j <= N; j += i) |
| 115 | + pfac[j].push_back(i); |
| 116 | + } |
| 117 | + |
| 118 | + return pfac; |
| 119 | +} |
| 120 | + |
| 121 | +//smallest prime factor of a number |
| 122 | +int spf(int n) { |
| 123 | + if (n % 2 == 0) return 2; |
| 124 | + for (int i = 3; i * i <= n; i += 2) { |
| 125 | + if (n % i == 0) return i; |
| 126 | + } |
| 127 | + return n; |
| 128 | +} |
| 129 | + |
| 130 | +int power2(int p) |
| 131 | +{ |
| 132 | + int v=1ll<<p; |
| 133 | + return v; |
| 134 | +} |
| 135 | + |
| 136 | +//Sort functions |
| 137 | + |
| 138 | +void sort(vector<int>&a) |
| 139 | +{ |
| 140 | + sort(a.begin(),a.end()); |
| 141 | +} |
| 142 | + |
| 143 | +void psort(vector<pair<int,int>>&a) |
| 144 | +{ |
| 145 | + sort(a.begin(),a.end()); |
| 146 | +} |
| 147 | + |
| 148 | +void rsort(vector<int>&a) |
| 149 | +{ |
| 150 | + sort(a.rbegin(),a.rend()); |
| 151 | +} |
| 152 | + |
| 153 | +void rpsort(vector<pair<int,int>>&a) |
| 154 | +{ |
| 155 | + sort(a.rbegin(),a.rend()); |
| 156 | +} |
| 157 | + |
| 158 | +//2-D Vector Declaration: |
| 159 | +//vvi mat(n,vi(m)); // nxm matrix, all initialized to 0 |
| 160 | + |
| 161 | + |
| 162 | +/***************Code***************/ |
| 163 | +struct SegTree { |
| 164 | +public: |
| 165 | + |
| 166 | + SegTree (int _n) : n (_n) { |
| 167 | + tree.resize(4*n, 0); |
| 168 | + } |
| 169 | + |
| 170 | + int query (int x, int y) { |
| 171 | + return query (x, y, 0, n-1, 0); |
| 172 | + } |
| 173 | + |
| 174 | + void update (int ind, int val) { |
| 175 | + update (ind, val, 0, n-1, 0); |
| 176 | + } |
| 177 | + |
| 178 | +private: |
| 179 | + |
| 180 | + vector<int> tree; |
| 181 | + int n; |
| 182 | + |
| 183 | + int query (int x, int y, int l, int r, int i) { |
| 184 | + if (r < x || l > y) return 0; |
| 185 | + if (l >= x && r <= y) return tree[i]; |
| 186 | + |
| 187 | + int m = (l+r) >> 1; |
| 188 | + return ( |
| 189 | + query (x, y, l, m, i*2+1) + |
| 190 | + query (x, y, m+1, r, i*2+2) |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + void update (int ind, int val, int l, int r, int i) { |
| 195 | + if (l == r) { |
| 196 | + tree[i] += val; |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + int m = (l+r) >> 1; |
| 201 | + if (m >= ind) update (ind, val, l, m, i*2+1); |
| 202 | + else update (ind, val, m+1, r, i*2+2); |
| 203 | + |
| 204 | + tree[i] = tree[i*2+1] + tree[i*2+2]; |
| 205 | + } |
| 206 | +}; |
| 207 | + |
| 208 | +void solve() { |
| 209 | + int n,i; |
| 210 | + cin>>n; |
| 211 | + vi a(n); |
| 212 | + vpi b(n); |
| 213 | + for(i=0;i<n;i++) |
| 214 | +{ |
| 215 | + cin>>a[i]; |
| 216 | + b[i].first=a[i]; |
| 217 | + b[i].second=i; |
| 218 | +} |
| 219 | + psort(b); |
| 220 | + |
| 221 | + map <int,int> pos; |
| 222 | + for(i=0;i<n;i++) |
| 223 | + pos[b[i].first]=i; |
| 224 | + |
| 225 | + SegTree tree(n); |
| 226 | + int result = 0; |
| 227 | + |
| 228 | + for(i=0;i<n;i++) |
| 229 | + { |
| 230 | + int ps=pos[a[i]]; |
| 231 | + |
| 232 | + int gl=tree.query(ps+1,n-1); |
| 233 | + int sl=i-gl; |
| 234 | + int sr=ps-sl; |
| 235 | + result+=gl*sr; |
| 236 | + tree.update(ps,1); |
| 237 | + } |
| 238 | + cout<<result<<endl; |
| 239 | +} |
| 240 | + |
| 241 | + |
| 242 | +signed main() { |
| 243 | + fastio; |
| 244 | + int t = 1; |
| 245 | + //cin >> t; |
| 246 | + while (t--) { |
| 247 | + solve(); |
| 248 | + } |
| 249 | + return 0; |
| 250 | +} |
0 commit comments