#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <climits> #include <queue> #include <vector> //#define PRINT using namespace std; const int MAX = 500001; typedef long long int lli; int n, m; int t[MAX]; lli sum[MAX]; lli total = 0, d, b; int lastIx = 0; struct Tree { int x, y; lli subRemoves; lli lastReturn; lli lastD, lastB; }; Tree tree[5*MAX]; void createTree(int x, int y, int tr){ if( x > y ) {tree[tr].x = -1; return;} tree[tr].x = x; tree[tr].y = y; tree[tr].subRemoves = 0; tree[tr].lastB = tree[tr].lastD = 0; createTree(x+1, (x+1+y)/2, tr*2); createTree((x+1+y)/2+1, y, tr*2+1); lastIx = max(lastIx,tr); } lli locate(lli d, lli b, lli lastD, lli lastB, int tr) { if(tree[tr].x == -1) { tree[tr].lastB = b; tree[tr].lastB = d; return 0; } int curr = tree[tr].x; int last = tree[tr].y; if(lastD > tree[tr].lastD) { tree[tr].subRemoves = 0; } if( lastD < tree[tr].lastD) { lastD = tree[tr].lastD; lastB = tree[tr].lastB; } int totalGrowth = (d-lastD) * t[curr] + lastB; if ( totalGrowth > b ) { lli totalSegmentGrowth = (last-curr+1)*lastB+(d-lastD)*(sum[curr]-sum[last+1]); lli totalSegmentValueAfterTheCurrentCut = (last-curr+1)*b; tree[tr].lastB = b; tree[tr].lastD = d; lli result = totalSegmentGrowth - totalSegmentValueAfterTheCurrentCut - tree[tr].subRemoves; tree[tr].subRemoves = 0; tree[tr].lastReturn = result; return result; } else { lli rightSide = locate(d,b,lastD,lastB,tr*2+1); lli leftSide = 0; if (tree[tr*2+1].lastD == d) { leftSide = locate(d,b,lastD,lastB,tr*2); } tree[tr].subRemoves += rightSide+leftSide; tree[tr].lastReturn = rightSide + leftSide; return rightSide+leftSide; } } void printTree() { #ifndef PRINT return; #endif // PRINT printf("\n"); int i = 0; do{ i++; int j = i; while ((j&1) == 0) j>>=1; if (j == 1) printf("\n"); //if( i == 100000) break; printf("[x=%i,y=%i,d=%lli,b=%lli,r=%lli,lr=%lli]", tree[i].x, tree[i].y, tree[i].lastD, tree[i].lastB,tree[i].subRemoves,tree[i].lastReturn); }while(i != lastIx); printf("\n"); } int main(){ scanf("%i %i", &n, &m); for(int i = 0; i < n; ++i) { scanf("%i ", &t[i]); } sort(t,t+n); for(int i = n-1; i>= 0; --i) { total += t[i]; sum[i] = total; } #ifdef PRINT for(int i = 0; i < n; ++i) printf("(%i %i)\n ",i, t[i]);printf("\n"); for(int i = 0; i < n; ++i) printf("(%i %lli)\n ",i, sum[i]);printf("\n"); #endif // PRINT createTree(0,n-1,1); printTree(); for(int k = 0; k < m; ++k) { scanf("%lli %lli", &d, &b); #ifdef PRINT printf("%lli %lli\n", d, b); #endif // PRINT lli cut = locate(d,b,tree[1].lastD,tree[1].lastB,1); printf("%lli\n", cut); //if( k > 137 && k < 142) printTree(); } 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 | #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <climits> #include <queue> #include <vector> //#define PRINT using namespace std; const int MAX = 500001; typedef long long int lli; int n, m; int t[MAX]; lli sum[MAX]; lli total = 0, d, b; int lastIx = 0; struct Tree { int x, y; lli subRemoves; lli lastReturn; lli lastD, lastB; }; Tree tree[5*MAX]; void createTree(int x, int y, int tr){ if( x > y ) {tree[tr].x = -1; return;} tree[tr].x = x; tree[tr].y = y; tree[tr].subRemoves = 0; tree[tr].lastB = tree[tr].lastD = 0; createTree(x+1, (x+1+y)/2, tr*2); createTree((x+1+y)/2+1, y, tr*2+1); lastIx = max(lastIx,tr); } lli locate(lli d, lli b, lli lastD, lli lastB, int tr) { if(tree[tr].x == -1) { tree[tr].lastB = b; tree[tr].lastB = d; return 0; } int curr = tree[tr].x; int last = tree[tr].y; if(lastD > tree[tr].lastD) { tree[tr].subRemoves = 0; } if( lastD < tree[tr].lastD) { lastD = tree[tr].lastD; lastB = tree[tr].lastB; } int totalGrowth = (d-lastD) * t[curr] + lastB; if ( totalGrowth > b ) { lli totalSegmentGrowth = (last-curr+1)*lastB+(d-lastD)*(sum[curr]-sum[last+1]); lli totalSegmentValueAfterTheCurrentCut = (last-curr+1)*b; tree[tr].lastB = b; tree[tr].lastD = d; lli result = totalSegmentGrowth - totalSegmentValueAfterTheCurrentCut - tree[tr].subRemoves; tree[tr].subRemoves = 0; tree[tr].lastReturn = result; return result; } else { lli rightSide = locate(d,b,lastD,lastB,tr*2+1); lli leftSide = 0; if (tree[tr*2+1].lastD == d) { leftSide = locate(d,b,lastD,lastB,tr*2); } tree[tr].subRemoves += rightSide+leftSide; tree[tr].lastReturn = rightSide + leftSide; return rightSide+leftSide; } } void printTree() { #ifndef PRINT return; #endif // PRINT printf("\n"); int i = 0; do{ i++; int j = i; while ((j&1) == 0) j>>=1; if (j == 1) printf("\n"); //if( i == 100000) break; printf("[x=%i,y=%i,d=%lli,b=%lli,r=%lli,lr=%lli]", tree[i].x, tree[i].y, tree[i].lastD, tree[i].lastB,tree[i].subRemoves,tree[i].lastReturn); }while(i != lastIx); printf("\n"); } int main(){ scanf("%i %i", &n, &m); for(int i = 0; i < n; ++i) { scanf("%i ", &t[i]); } sort(t,t+n); for(int i = n-1; i>= 0; --i) { total += t[i]; sum[i] = total; } #ifdef PRINT for(int i = 0; i < n; ++i) printf("(%i %i)\n ",i, t[i]);printf("\n"); for(int i = 0; i < n; ++i) printf("(%i %lli)\n ",i, sum[i]);printf("\n"); #endif // PRINT createTree(0,n-1,1); printTree(); for(int k = 0; k < m; ++k) { scanf("%lli %lli", &d, &b); #ifdef PRINT printf("%lli %lli\n", d, b); #endif // PRINT lli cut = locate(d,b,tree[1].lastD,tree[1].lastB,1); printf("%lli\n", cut); //if( k > 137 && k < 142) printTree(); } return 0; } |