#include <bits/stdc++.h> using namespace std; int wr[1000006]; int giv[1000006]; vector<int> lep[1000006]; const int S = 1048576; int sc[S * 2]; int query(int w, int p, int k, int pp, int kk) { if (pp > k || kk < p) { return 0; } if (pp <= p && kk >= k) { return sc[w]; } int p1 = query(w * 2, p, (p + k) / 2, pp, kk), p2 = query(w * 2 + 1, (p + k) / 2 + 1, k, pp, kk); return p1 + p2; } void ins(int w) { w += S - 1; while (w) { sc[w]++; w /= 2; } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; long long mus = 0, cm = 0; for (int i = 1; i <= n; i++) cin >> wr[i]; cout << n + n + 1 << ' '; int ma = 0; long long w = 0; lep[0].push_back(0); for (int i = 1; i <= n; i++) { if (wr[n - i + 1] == n) break; ma = max(wr[n - i + 1], ma); int ler = max(ma * 2 + 1 - n - i, 0); lep[ler].push_back(i); } ma = 0; for (int i = 0; i <= n; i++) { if (wr[i] == n) break; ma = max(wr[i], ma); for (auto j : lep[i]) { ins(j + 1); // cout << "ADD " << j << '\n'; } // cout << i << ' ' << w << ' ' << max(ma * 2 + 1 - n - i, 0) << ' '; w += query(1, 1, S, max(ma * 2 + 1 - n - i, 0) + 1, n); // cout << w << '\n'; // giv[i] = max(ma * 2 + 1 - n - i, 0); // cout << giv[i] << ' '; } cout << w << '\n'; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <bits/stdc++.h> using namespace std; int wr[1000006]; int giv[1000006]; vector<int> lep[1000006]; const int S = 1048576; int sc[S * 2]; int query(int w, int p, int k, int pp, int kk) { if (pp > k || kk < p) { return 0; } if (pp <= p && kk >= k) { return sc[w]; } int p1 = query(w * 2, p, (p + k) / 2, pp, kk), p2 = query(w * 2 + 1, (p + k) / 2 + 1, k, pp, kk); return p1 + p2; } void ins(int w) { w += S - 1; while (w) { sc[w]++; w /= 2; } } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; long long mus = 0, cm = 0; for (int i = 1; i <= n; i++) cin >> wr[i]; cout << n + n + 1 << ' '; int ma = 0; long long w = 0; lep[0].push_back(0); for (int i = 1; i <= n; i++) { if (wr[n - i + 1] == n) break; ma = max(wr[n - i + 1], ma); int ler = max(ma * 2 + 1 - n - i, 0); lep[ler].push_back(i); } ma = 0; for (int i = 0; i <= n; i++) { if (wr[i] == n) break; ma = max(wr[i], ma); for (auto j : lep[i]) { ins(j + 1); // cout << "ADD " << j << '\n'; } // cout << i << ' ' << w << ' ' << max(ma * 2 + 1 - n - i, 0) << ' '; w += query(1, 1, S, max(ma * 2 + 1 - n - i, 0) + 1, n); // cout << w << '\n'; // giv[i] = max(ma * 2 + 1 - n - i, 0); // cout << giv[i] << ' '; } cout << w << '\n'; } |