#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <cassert> #include <stack> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // head /*const int N=222; int n,a[N],b[N],dp[N][N],pre[N][N],p[N][N],__; int d[N]; int main() { while (1) { n=100; rep(i,0,n) { a[i]=rand()%10; b[i]=rand()%1000; } rep(i,0,n) rep(j,i+1,n) if (a[i]>a[j]) { swap(a[i],a[j]); swap(b[i],b[j]); } rep(i,1,n+2) d[i]=0; dp[0][0]=0; memset(dp,0xa0,sizeof(dp)); rep(i,1,n+1) { rep(j,0,i+1) { dp[i][j]=dp[i-1][j]; if (j>=1&&dp[i-1][j-1]+j*a[i-1]+b[i-1]>=dp[i][j]) { dp[i][j]=dp[i-1][j-1]+j*a[i-1]+b[i-1]; } } rep(j,1,i+1) if (j*a[i-1]+b[i-1]>=d[j]) { for (int k=i;k>j;k--) { assert(k*a[i-1]+b[i-1]>=d[k]); d[k]=d[k-1]+a[i-1]; } d[j]=j*a[i-1]+b[i-1]; break; } } rep(j,1,n+1) assert(dp[n][j]-dp[n][j-1]==d[j]); printf("Case #%d: ",++__); puts("Correct!"); } } */ struct node { node *s[2],*f; ll fg,key; int sz; void clear() { s[0]=s[1]=f=0;} bool dir() { return f->s[1]==this;} void setc(node *p,int d) { if (p) p->f=this; s[d]=p;} void setf(ll d) { fg+=d; key+=d;} void push() { if (fg) { rep(i,0,2) if (s[i]) s[i]->setf(fg); fg=0; } } void upd() { sz=1; rep(i,0,2) if (s[i]) sz+=s[i]->sz; } }; const int N=1001000; node pool[N],*cur=pool; node *newnode() { return cur++; } const ll inf=1ll<<62; struct Splaytree { node *rt; ll ret=0; void init() { rt=newnode(); rt->sz=1; rt->key=inf; } void rot(node *x) { node *p=x->f; bool d=x->dir(); if (p->f) p->f->setc(x,p->dir()); else x->f=p->f; p->setc(x->s[!d],d); x->setc(p,!d); if (p==rt) rt=x; p->upd(); } void splay(node *x,node *f=NULL) { node *q=x; stack<node*> sta; while (1) { sta.push(q);if (!q->f) break; q=q->f; } while (!sta.empty()) sta.top()->push(),sta.pop(); while (x->f!=f) { if (x->f->f==f) rot(x); else if (x->dir()==x->f->dir()) rot(x->f),rot(x); else rot(x),rot(x); } x->upd(); } node *prev(node *x) { assert(x); splay(x); node *p=x->s[0]; if (!p) return 0; while (p->s[1]) p=p->s[1]; return p; } node *succ(node *x) { assert(x); splay(x); node *p=x->s[1]; if (!p) return 0; while (p->s[0]) p=p->s[0]; return p; } void insert(node *x,node *p) { if (!rt) { rt=p; return; } splay(x); if (!x->s[1]) { x->setc(p,1);x->upd();} else { splay(succ(x),x); x->s[1]->setc(p,0); x->s[1]->upd(); x->upd(); } } void insert(node *x) { if (!rt) rt=x; else { node *p=rt; while (p->s[1]) p=p->s[1]; p->setc(x,1);} splay(x); } node* extract(node *l,node *r) { node *pl=prev(l),*pr=succ(r); assert(pl); assert(pr); splay(pl);splay(pr,pl); node *p=pr->s[0]; return p; } node* split(node *l,node *r) { node *p=extract(l,r); p->f->s[p->dir()]=0; p->f->upd(); p->f=0; return p; } void dfs_debug(node *x) { x->push(); if (x->s[0]) dfs_debug(x->s[0]); if (ret==-inf) ret=0; else printf("%lld\n",ret+=x->key); if (x->s[1]) dfs_debug(x->s[1]); } void output() { ret=-inf; dfs_debug(rt);} void add(ll a,ll b) { node *p=rt,*q=NULL; int rk=-2,pos=0; while (p) { p->push(); int pl=1+(p->s[0]?p->s[0]->sz:0); if (p->key>=a*(rk+pl)+b) rk+=pl,pos=rk,q=p,p=p->s[1]; else p=p->s[0]; } node *r=newnode(); r->key=a*(pos+1)+b; r->sz=1; insert(q,r); splay(r); node *s=succ(r); if (s) { splay(s,r); s->setf(a); } } }T; int n; ll a,b; pair<ll,ll> p[N]; inline ll getint() { ll ret=0;bool ok=0; for(;;) { int c=getchar(); if(c>='0'&&c<='9')ret=(ret<<3)+ret+ret+c-'0',ok=1; else if(ok)return ret; } } int main() { scanf("%d",&n); rep(i,0,n) { a=getint(); b=getint(); p[i]=mp(a,b); } sort(p,p+n); T.init(); rep(i,0,n) T.add(p[i].fi,p[i].se); T.output(); }
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <cassert> #include <stack> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007; ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} // head /*const int N=222; int n,a[N],b[N],dp[N][N],pre[N][N],p[N][N],__; int d[N]; int main() { while (1) { n=100; rep(i,0,n) { a[i]=rand()%10; b[i]=rand()%1000; } rep(i,0,n) rep(j,i+1,n) if (a[i]>a[j]) { swap(a[i],a[j]); swap(b[i],b[j]); } rep(i,1,n+2) d[i]=0; dp[0][0]=0; memset(dp,0xa0,sizeof(dp)); rep(i,1,n+1) { rep(j,0,i+1) { dp[i][j]=dp[i-1][j]; if (j>=1&&dp[i-1][j-1]+j*a[i-1]+b[i-1]>=dp[i][j]) { dp[i][j]=dp[i-1][j-1]+j*a[i-1]+b[i-1]; } } rep(j,1,i+1) if (j*a[i-1]+b[i-1]>=d[j]) { for (int k=i;k>j;k--) { assert(k*a[i-1]+b[i-1]>=d[k]); d[k]=d[k-1]+a[i-1]; } d[j]=j*a[i-1]+b[i-1]; break; } } rep(j,1,n+1) assert(dp[n][j]-dp[n][j-1]==d[j]); printf("Case #%d: ",++__); puts("Correct!"); } } */ struct node { node *s[2],*f; ll fg,key; int sz; void clear() { s[0]=s[1]=f=0;} bool dir() { return f->s[1]==this;} void setc(node *p,int d) { if (p) p->f=this; s[d]=p;} void setf(ll d) { fg+=d; key+=d;} void push() { if (fg) { rep(i,0,2) if (s[i]) s[i]->setf(fg); fg=0; } } void upd() { sz=1; rep(i,0,2) if (s[i]) sz+=s[i]->sz; } }; const int N=1001000; node pool[N],*cur=pool; node *newnode() { return cur++; } const ll inf=1ll<<62; struct Splaytree { node *rt; ll ret=0; void init() { rt=newnode(); rt->sz=1; rt->key=inf; } void rot(node *x) { node *p=x->f; bool d=x->dir(); if (p->f) p->f->setc(x,p->dir()); else x->f=p->f; p->setc(x->s[!d],d); x->setc(p,!d); if (p==rt) rt=x; p->upd(); } void splay(node *x,node *f=NULL) { node *q=x; stack<node*> sta; while (1) { sta.push(q);if (!q->f) break; q=q->f; } while (!sta.empty()) sta.top()->push(),sta.pop(); while (x->f!=f) { if (x->f->f==f) rot(x); else if (x->dir()==x->f->dir()) rot(x->f),rot(x); else rot(x),rot(x); } x->upd(); } node *prev(node *x) { assert(x); splay(x); node *p=x->s[0]; if (!p) return 0; while (p->s[1]) p=p->s[1]; return p; } node *succ(node *x) { assert(x); splay(x); node *p=x->s[1]; if (!p) return 0; while (p->s[0]) p=p->s[0]; return p; } void insert(node *x,node *p) { if (!rt) { rt=p; return; } splay(x); if (!x->s[1]) { x->setc(p,1);x->upd();} else { splay(succ(x),x); x->s[1]->setc(p,0); x->s[1]->upd(); x->upd(); } } void insert(node *x) { if (!rt) rt=x; else { node *p=rt; while (p->s[1]) p=p->s[1]; p->setc(x,1);} splay(x); } node* extract(node *l,node *r) { node *pl=prev(l),*pr=succ(r); assert(pl); assert(pr); splay(pl);splay(pr,pl); node *p=pr->s[0]; return p; } node* split(node *l,node *r) { node *p=extract(l,r); p->f->s[p->dir()]=0; p->f->upd(); p->f=0; return p; } void dfs_debug(node *x) { x->push(); if (x->s[0]) dfs_debug(x->s[0]); if (ret==-inf) ret=0; else printf("%lld\n",ret+=x->key); if (x->s[1]) dfs_debug(x->s[1]); } void output() { ret=-inf; dfs_debug(rt);} void add(ll a,ll b) { node *p=rt,*q=NULL; int rk=-2,pos=0; while (p) { p->push(); int pl=1+(p->s[0]?p->s[0]->sz:0); if (p->key>=a*(rk+pl)+b) rk+=pl,pos=rk,q=p,p=p->s[1]; else p=p->s[0]; } node *r=newnode(); r->key=a*(pos+1)+b; r->sz=1; insert(q,r); splay(r); node *s=succ(r); if (s) { splay(s,r); s->setf(a); } } }T; int n; ll a,b; pair<ll,ll> p[N]; inline ll getint() { ll ret=0;bool ok=0; for(;;) { int c=getchar(); if(c>='0'&&c<='9')ret=(ret<<3)+ret+ret+c-'0',ok=1; else if(ok)return ret; } } int main() { scanf("%d",&n); rep(i,0,n) { a=getint(); b=getint(); p[i]=mp(a,b); } sort(p,p+n); T.init(); rep(i,0,n) T.add(p[i].fi,p[i].se); T.output(); } |