#include <bits/stdc++.h> using namespace std; typedef long long int LL; #define PII pair <int, int> #define PLL pair <LL, LL> #define mp make_pair #define st first #define nd second #define pb push_back const int N = 1e6 + 7; const LL INF = 10000000000000000LL; struct sqr{ LL add; int cnt; vector <PII> V; sqr(){ add = 0LL; cnt = 0; V.clear(); } }; int n; int psize, pcnt; LL answer; PLL fun[N]; sqr Get[3007]; int BS(int a, int cnt){ int p = 0, k = Get[a].V.size() - 1; while(p < k){ int m = (p + k + 1) / 2; if(Get[a].V[m].st <= cnt) p = m; else k = m - 1; } return Get[a].V[p].nd; } PLL Get_max(){ PLL res = mp(-1, -1); for(int i = 1; i <= pcnt; ++i){ if(Get[i].V.size() == 0) continue; int cnt = Get[i].cnt; int who = BS(i, cnt); if(res.nd < fun[who].nd + Get[i].add + 1LL * cnt * fun[who].st) res = mp(who, fun[who].nd + Get[i].add + 1LL * cnt * fun[who].st); } return res; } void akt(int a, int v){ int from = (a - 1) * psize + 1; int to = min(a * psize, n); for(int i = from; i <= to; ++i) fun[i].nd += Get[a].add + fun[i].st * (LL)Get[a].cnt; for(int i = from; i < v; ++i) fun[i].nd += fun[v].st; for(int i = v + 1; i <= to; ++i) fun[i].nd += fun[i].st; for(int i = 1; i < a; ++i) Get[i].add += fun[v].st; for(int i = a + 1; i <= pcnt; ++i) Get[i].cnt++; } LL get_point(LL a1, LL b1, LL a2, LL b2){ // printf("%lld %lld %lld %lld\n", a1, b1, a2, b2); if(b2 >= b1) return -1; LL x = (b1 - b2) / (a2 - a1); if((b1 - b2)%(a2 - a1) == 0) return x; return x + 1; } void construct(int a){ Get[a].cnt = Get[a].add = 0; Get[a].V.clear(); int from = (a - 1) * psize + 1; int to = min(psize * a, n); for(int i = from; i <= to; ++i){ if(fun[i].st < 0) continue; LL v = -1; while(Get[a].V.size() > 0){ int last = Get[a].V[Get[a].V.size() - 1].nd; v = get_point(fun[last].st, fun[last].nd, fun[i].st, fun[i].nd); // printf("%lld\n", v); if(v > (LL)Get[a].V[Get[a].V.size() - 1].st) break; Get[a].V.pop_back(); } if(v > n) continue; Get[a].V.pb(mp(Get[a].V.size() == 0 ? 0 : v, i)); } // printf("%d : ", a); // for(auto i: Get[a].V) // printf(": %d %d :", i.st, i.nd); // puts(""); } int main(){ scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%lld %lld", &fun[i].st, &fun[i].nd); sort(fun + 1, fun + n + 1); // for(int i = 1; i <= n; ++i) // printf("%lld %lld\n", fun[i].st, fun[i].nd); psize = max((int)sqrt(n / 3), 1); pcnt = (n + psize - 1) / psize; // printf("%d %d\n", psize, pcnt); fun[0] = mp(-1, -1); for(int i = 1; i <= pcnt; ++i) construct(i); for(int i = 1; i <= n; ++i){ PLL v = Get_max(); answer += v.nd; akt((v.st + psize - 1) / psize, v.st); fun[v.st] = mp(-1, -INF); construct((v.st + psize - 1) / psize); printf("%lld\n", answer); } 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 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; #define PII pair <int, int> #define PLL pair <LL, LL> #define mp make_pair #define st first #define nd second #define pb push_back const int N = 1e6 + 7; const LL INF = 10000000000000000LL; struct sqr{ LL add; int cnt; vector <PII> V; sqr(){ add = 0LL; cnt = 0; V.clear(); } }; int n; int psize, pcnt; LL answer; PLL fun[N]; sqr Get[3007]; int BS(int a, int cnt){ int p = 0, k = Get[a].V.size() - 1; while(p < k){ int m = (p + k + 1) / 2; if(Get[a].V[m].st <= cnt) p = m; else k = m - 1; } return Get[a].V[p].nd; } PLL Get_max(){ PLL res = mp(-1, -1); for(int i = 1; i <= pcnt; ++i){ if(Get[i].V.size() == 0) continue; int cnt = Get[i].cnt; int who = BS(i, cnt); if(res.nd < fun[who].nd + Get[i].add + 1LL * cnt * fun[who].st) res = mp(who, fun[who].nd + Get[i].add + 1LL * cnt * fun[who].st); } return res; } void akt(int a, int v){ int from = (a - 1) * psize + 1; int to = min(a * psize, n); for(int i = from; i <= to; ++i) fun[i].nd += Get[a].add + fun[i].st * (LL)Get[a].cnt; for(int i = from; i < v; ++i) fun[i].nd += fun[v].st; for(int i = v + 1; i <= to; ++i) fun[i].nd += fun[i].st; for(int i = 1; i < a; ++i) Get[i].add += fun[v].st; for(int i = a + 1; i <= pcnt; ++i) Get[i].cnt++; } LL get_point(LL a1, LL b1, LL a2, LL b2){ // printf("%lld %lld %lld %lld\n", a1, b1, a2, b2); if(b2 >= b1) return -1; LL x = (b1 - b2) / (a2 - a1); if((b1 - b2)%(a2 - a1) == 0) return x; return x + 1; } void construct(int a){ Get[a].cnt = Get[a].add = 0; Get[a].V.clear(); int from = (a - 1) * psize + 1; int to = min(psize * a, n); for(int i = from; i <= to; ++i){ if(fun[i].st < 0) continue; LL v = -1; while(Get[a].V.size() > 0){ int last = Get[a].V[Get[a].V.size() - 1].nd; v = get_point(fun[last].st, fun[last].nd, fun[i].st, fun[i].nd); // printf("%lld\n", v); if(v > (LL)Get[a].V[Get[a].V.size() - 1].st) break; Get[a].V.pop_back(); } if(v > n) continue; Get[a].V.pb(mp(Get[a].V.size() == 0 ? 0 : v, i)); } // printf("%d : ", a); // for(auto i: Get[a].V) // printf(": %d %d :", i.st, i.nd); // puts(""); } int main(){ scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%lld %lld", &fun[i].st, &fun[i].nd); sort(fun + 1, fun + n + 1); // for(int i = 1; i <= n; ++i) // printf("%lld %lld\n", fun[i].st, fun[i].nd); psize = max((int)sqrt(n / 3), 1); pcnt = (n + psize - 1) / psize; // printf("%d %d\n", psize, pcnt); fun[0] = mp(-1, -1); for(int i = 1; i <= pcnt; ++i) construct(i); for(int i = 1; i <= n; ++i){ PLL v = Get_max(); answer += v.nd; akt((v.st + psize - 1) / psize, v.st); fun[v.st] = mp(-1, -INF); construct((v.st + psize - 1) / psize); printf("%lld\n", answer); } return 0; } |