#include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PII pair <int, int> const int N = 250 * 1000 + 7; const int T = 1 << 18; const int INF = 1e9 + 7; int n; int P[2][N]; int tree[2][T + T + 7][3]; int place[2][N]; PII order[2][N]; void refresh(int t, int p){ tree[t][p][1] = min(tree[t][p + p][1], tree[t][p + p + 1][1]) + tree[t][p][0]; tree[t][p][2] = max(tree[t][p + p][2], tree[t][p + p + 1][2]) + tree[t][p][0]; } void update(int t, int p, int v){ p += T; for(int j = 0; j < 3; ++j) tree[t][p][j] = v; while(p > 1){ p >>= 1; tree[t][p][0] = 0; refresh(t, p); } } int get(int t, int p){ p += T; int ret = 0; while(p){ ret += tree[t][p][0]; p >>= 1; } return ret; } int getLeft(int t, int v){ int cur = 1, up = 0; while(cur < T){ up += tree[t][cur][0]; cur += cur; if(tree[t][cur + 1][1] + up < v) ++cur; } return cur - T; } int getRight(int t, int v){ int cur = 1, up = 0; while(cur < T){ up += tree[t][cur][0]; cur += cur; if(tree[t][cur][2] + up <= v) ++cur; } return cur - T; } void add(int t, int s, int e, int v, int cur = 1, int from = 0, int to = T - 1){ if(s > e) return; if(s <= from && to <= e){ for(int i = 0; i < 3; ++i) tree[t][cur][i] += v; return; } int mid = (from + to) / 2; if(s <= mid) add(t, s, e, v, cur + cur, from, mid); if(mid < e) add(t, s, e, v, cur + cur + 1, mid + 1, to); refresh(t, cur); } void solve(int id){ for(int j = 0; j < 2; ++j) for(int i = 1; i <= n; ++i) if(i != id){ int t = place[j][i]; if(place[j][i] > place[j][id]) --t; update(j, t, P[j][i]); } update(0, 0, -INF); update(1, 0, -INF); for(int j = 0; j < 2; ++j) for(int i = 1; i <= n; ++i){ if(i == id) continue; int curPlace = place[j][i]; if(place[j][i] > place[j][id]) --curPlace; int curV = get(j, curPlace); // printf("%d %d -> %d\n", j, i, curV); // printf("%d %d\n", getLeft(j, curV), getRight(j, curV)); add(j, 1, getLeft(j, curV), 1); add(j, getRight(j, curV), n - 1, -1); } LL res = 0LL; for(int i = 1; i <= n; ++i) if(i != id){ int t1 = place[0][i], t2 = place[1][i]; if(place[0][i] > place[0][id]) --t1; if(place[1][i] > place[1][id]) --t2; res += 1LL * get(0, t1) * get(1, t2); } printf("%lld\n", res + 1LL * P[0][id] * P[1][id]); } int main(){ scanf("%d", &n); for(int i = 1; i <= n; ++i) for(int j = 0; j < 2; ++j){ scanf("%d", &P[j][i]); order[j][i] = {P[j][i], i}; } for(int j = 0; j < 2; ++j) sort(order[j] + 1, order[j] + n + 1); for(int j = 0; j < 2; ++j) for(int i = 1; i <= n; ++i) place[j][order[j][i].nd] = i; for(int j = 0; j < 2; ++j) for(int i = n; i < T; ++i) update(j, i, INF); // solve(2); for(int i = 1; i <= n; ++i) solve(i); return 0; }
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; #define st first #define nd second #define PII pair <int, int> const int N = 250 * 1000 + 7; const int T = 1 << 18; const int INF = 1e9 + 7; int n; int P[2][N]; int tree[2][T + T + 7][3]; int place[2][N]; PII order[2][N]; void refresh(int t, int p){ tree[t][p][1] = min(tree[t][p + p][1], tree[t][p + p + 1][1]) + tree[t][p][0]; tree[t][p][2] = max(tree[t][p + p][2], tree[t][p + p + 1][2]) + tree[t][p][0]; } void update(int t, int p, int v){ p += T; for(int j = 0; j < 3; ++j) tree[t][p][j] = v; while(p > 1){ p >>= 1; tree[t][p][0] = 0; refresh(t, p); } } int get(int t, int p){ p += T; int ret = 0; while(p){ ret += tree[t][p][0]; p >>= 1; } return ret; } int getLeft(int t, int v){ int cur = 1, up = 0; while(cur < T){ up += tree[t][cur][0]; cur += cur; if(tree[t][cur + 1][1] + up < v) ++cur; } return cur - T; } int getRight(int t, int v){ int cur = 1, up = 0; while(cur < T){ up += tree[t][cur][0]; cur += cur; if(tree[t][cur][2] + up <= v) ++cur; } return cur - T; } void add(int t, int s, int e, int v, int cur = 1, int from = 0, int to = T - 1){ if(s > e) return; if(s <= from && to <= e){ for(int i = 0; i < 3; ++i) tree[t][cur][i] += v; return; } int mid = (from + to) / 2; if(s <= mid) add(t, s, e, v, cur + cur, from, mid); if(mid < e) add(t, s, e, v, cur + cur + 1, mid + 1, to); refresh(t, cur); } void solve(int id){ for(int j = 0; j < 2; ++j) for(int i = 1; i <= n; ++i) if(i != id){ int t = place[j][i]; if(place[j][i] > place[j][id]) --t; update(j, t, P[j][i]); } update(0, 0, -INF); update(1, 0, -INF); for(int j = 0; j < 2; ++j) for(int i = 1; i <= n; ++i){ if(i == id) continue; int curPlace = place[j][i]; if(place[j][i] > place[j][id]) --curPlace; int curV = get(j, curPlace); // printf("%d %d -> %d\n", j, i, curV); // printf("%d %d\n", getLeft(j, curV), getRight(j, curV)); add(j, 1, getLeft(j, curV), 1); add(j, getRight(j, curV), n - 1, -1); } LL res = 0LL; for(int i = 1; i <= n; ++i) if(i != id){ int t1 = place[0][i], t2 = place[1][i]; if(place[0][i] > place[0][id]) --t1; if(place[1][i] > place[1][id]) --t2; res += 1LL * get(0, t1) * get(1, t2); } printf("%lld\n", res + 1LL * P[0][id] * P[1][id]); } int main(){ scanf("%d", &n); for(int i = 1; i <= n; ++i) for(int j = 0; j < 2; ++j){ scanf("%d", &P[j][i]); order[j][i] = {P[j][i], i}; } for(int j = 0; j < 2; ++j) sort(order[j] + 1, order[j] + n + 1); for(int j = 0; j < 2; ++j) for(int i = 1; i <= n; ++i) place[j][order[j][i].nd] = i; for(int j = 0; j < 2; ++j) for(int i = n; i < T; ++i) update(j, i, INF); // solve(2); for(int i = 1; i <= n; ++i) solve(i); return 0; } |