#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #define PB push_back #define ST first #define ND second using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using VII = vector<PII>; using LL = long long int; using ULL = unsigned long long int; const int MAXN = 1000000 + 10; int n; pair<LL, LL> values[MAXN]; // Treap implementation inspired from: https://tanujkhattar.wordpress.com/2016/01/10/treaps-one-tree-to-rule-em-all-part-2/ typedef struct node{ int prior, size; LL val, lazy; //value in array,info of segtree,lazy update struct node *l,*r; } node; typedef node* pnode; int sz(pnode t){ return t?t->size:0; } void upd_sz(pnode t){ if(t) t->size=sz(t->l)+1+sz(t->r); } void lazy(pnode t){ if(!t || t->lazy == 0) return; t->val += t->lazy;//operation of lazy if(t->l) t->l->lazy += t->lazy;//propagate lazy if(t->r) t->r->lazy += t->lazy; t->lazy = 0; } void split(pnode t,pnode &l,pnode &r,int pos,int add=0){ if (!t) { l = r = NULL; return; } lazy(t); int curr_pos = add + sz(t->l); if (curr_pos < pos) {//element at pos goes to "l" split(t->r,t->r,r,pos,curr_pos+1); l = t; } else { split(t->l,l,t->l,pos,add); r = t; } upd_sz(t); lazy(t); } void merge(pnode &t,pnode l,pnode r){//result/left/right array lazy(l); lazy(r); if (!l || !r) t = l?l:r; else if (l->prior > r->prior) { merge(l->r,l->r,r); t = l; } else { merge(r->l,l,r->l); t = r; } upd_sz(t); lazy(t); } pnode init(LL val){ pnode ret = (pnode)malloc(sizeof(node)); ret->prior = rand(); ret->size = 1; ret->val = val; ret->lazy = 0; return ret; } // End of copy. LL CountVal(int lvl, int pos) { return values[lvl].ND + (pos - 1) * values[lvl].ST; } int BinSearch(int lvl, pnode diffs) { LL val = 0; LL count_val; int add = 1, curr_pos; int last_good = sz(diffs) + 1; do { val += diffs->lazy; curr_pos = add + sz(diffs->l); count_val = CountVal(lvl, curr_pos); if (val + diffs->val < count_val) { last_good = min(last_good, curr_pos); diffs = diffs->l; } else { diffs = diffs->r; add = curr_pos + 1; } } while (diffs); return last_good; } LL differences[MAXN]; void BuildDifferences(pnode diffs, LL lazy, int add = 1) { if (!diffs) return; int curr_pos = add + sz(diffs->l); lazy += diffs->lazy; differences[curr_pos] = lazy + diffs->val; BuildDifferences(diffs->l, lazy, add); BuildDifferences(diffs->r, lazy, curr_pos + 1); } void Run(int lvl, pnode &diffs) { int pos = BinSearch(lvl, diffs); if (pos > sz(diffs)) { pnode right = init(CountVal(lvl, sz(diffs) + 1)); merge(diffs, diffs, right); return; } if (pos == 1) { pnode left = init(CountVal(lvl, 1)); if (diffs) diffs->lazy += values[lvl].ST; merge(diffs, left, diffs); return; } pnode left, right; split(diffs, left, right, pos - 1); if (right) right->lazy += values[lvl].ST; pnode middle = init(CountVal(lvl, pos)); pnode tmp; merge(tmp, left, middle); merge(diffs, tmp, right); } int main() { srand(0); scanf("%d", &n); LL ain, bin; for (int i = 1; i <= n; ++i) { scanf("%lld %lld", &bin, &ain); values[i] = {bin, ain}; } sort(values + 1, values + n + 1); pnode diffs = init(CountVal(1, 1)); for (int i = 2; i <= n; ++i) { Run(i, diffs); } BuildDifferences(diffs, 0); LL result = 0; for (int i = 1; i <= n; ++i) { result += differences[i]; printf("%lld\n", result); } 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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #define PB push_back #define ST first #define ND second using namespace std; using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; using VII = vector<PII>; using LL = long long int; using ULL = unsigned long long int; const int MAXN = 1000000 + 10; int n; pair<LL, LL> values[MAXN]; // Treap implementation inspired from: https://tanujkhattar.wordpress.com/2016/01/10/treaps-one-tree-to-rule-em-all-part-2/ typedef struct node{ int prior, size; LL val, lazy; //value in array,info of segtree,lazy update struct node *l,*r; } node; typedef node* pnode; int sz(pnode t){ return t?t->size:0; } void upd_sz(pnode t){ if(t) t->size=sz(t->l)+1+sz(t->r); } void lazy(pnode t){ if(!t || t->lazy == 0) return; t->val += t->lazy;//operation of lazy if(t->l) t->l->lazy += t->lazy;//propagate lazy if(t->r) t->r->lazy += t->lazy; t->lazy = 0; } void split(pnode t,pnode &l,pnode &r,int pos,int add=0){ if (!t) { l = r = NULL; return; } lazy(t); int curr_pos = add + sz(t->l); if (curr_pos < pos) {//element at pos goes to "l" split(t->r,t->r,r,pos,curr_pos+1); l = t; } else { split(t->l,l,t->l,pos,add); r = t; } upd_sz(t); lazy(t); } void merge(pnode &t,pnode l,pnode r){//result/left/right array lazy(l); lazy(r); if (!l || !r) t = l?l:r; else if (l->prior > r->prior) { merge(l->r,l->r,r); t = l; } else { merge(r->l,l,r->l); t = r; } upd_sz(t); lazy(t); } pnode init(LL val){ pnode ret = (pnode)malloc(sizeof(node)); ret->prior = rand(); ret->size = 1; ret->val = val; ret->lazy = 0; return ret; } // End of copy. LL CountVal(int lvl, int pos) { return values[lvl].ND + (pos - 1) * values[lvl].ST; } int BinSearch(int lvl, pnode diffs) { LL val = 0; LL count_val; int add = 1, curr_pos; int last_good = sz(diffs) + 1; do { val += diffs->lazy; curr_pos = add + sz(diffs->l); count_val = CountVal(lvl, curr_pos); if (val + diffs->val < count_val) { last_good = min(last_good, curr_pos); diffs = diffs->l; } else { diffs = diffs->r; add = curr_pos + 1; } } while (diffs); return last_good; } LL differences[MAXN]; void BuildDifferences(pnode diffs, LL lazy, int add = 1) { if (!diffs) return; int curr_pos = add + sz(diffs->l); lazy += diffs->lazy; differences[curr_pos] = lazy + diffs->val; BuildDifferences(diffs->l, lazy, add); BuildDifferences(diffs->r, lazy, curr_pos + 1); } void Run(int lvl, pnode &diffs) { int pos = BinSearch(lvl, diffs); if (pos > sz(diffs)) { pnode right = init(CountVal(lvl, sz(diffs) + 1)); merge(diffs, diffs, right); return; } if (pos == 1) { pnode left = init(CountVal(lvl, 1)); if (diffs) diffs->lazy += values[lvl].ST; merge(diffs, left, diffs); return; } pnode left, right; split(diffs, left, right, pos - 1); if (right) right->lazy += values[lvl].ST; pnode middle = init(CountVal(lvl, pos)); pnode tmp; merge(tmp, left, middle); merge(diffs, tmp, right); } int main() { srand(0); scanf("%d", &n); LL ain, bin; for (int i = 1; i <= n; ++i) { scanf("%lld %lld", &bin, &ain); values[i] = {bin, ain}; } sort(values + 1, values + n + 1); pnode diffs = init(CountVal(1, 1)); for (int i = 2; i <= n; ++i) { Run(i, diffs); } BuildDifferences(diffs, 0); LL result = 0; for (int i = 1; i <= n; ++i) { result += differences[i]; printf("%lld\n", result); } return 0; } |