#include <cstdio> #include <vector> #include <algorithm> #define FOR(i,b,e) for(int i=(b); i <= (e); ++i) #define FORD(i,b,e) for(int i=(b); i >= (e); --i) #define SIZE(c) (int) (c).size() #define FORE(i,c) FOR(i,0,SIZE(c)-1) #define PB push_back #define MP make_pair #define ST first #define ND second using namespace std; typedef long long int LLI; typedef pair < LLI , LLI > PLL; typedef vector < int > VI; typedef vector < LLI > VL; typedef vector < PLL > VPL; /*************************************************************************/ /* Returns the product of a and b as two parts */ PLL mul(LLI a, LLI b) { bool neg = ((a < 0) ^ (b < 0)); if (a < 0) a *= -1; if (b < 0) b *= -1; LLI M = (1LL << 30); LLI a1 = a >> 30, a0 = a%M, b1 = b >> 30, b0 = b%M; LLI hi = a1 * b1, lo = a0 * b0; a0 *= b1, b0 *= a1; hi += (a0 >> 30) + (b0 >> 30); a0 <<= 30, b0 <<= 30; if (a0 + lo < lo) ++hi; lo += a0; if (b0 + lo < lo) ++hi; lo += b0; if (neg) { hi *= -1; lo *= -1; } return MP(hi, lo); } /* Returns true iff ab >= cd */ bool comp(LLI a, LLI b, LLI c, LLI d) { return mul(a,b) >= mul(c,d); } /* Returns true iff (x,y,z) is convex upwards */ bool isBad(PLL x, PLL y, PLL z) { return comp(y.ND - x.ND, z.ST - y.ST, y.ST - x.ST, z.ND - y.ND); } /* Returns index of last element remaining after pushing to convex stack structure */ int getLastConvex(VPL &Stack, PLL elem) { int s = SIZE(Stack); while (s > 1 && isBad(Stack[s-2], Stack[s-1], elem)) s--; return s - 1; } const LLI INF = 1000000000000000001LL; /*************************************************************************/ int main() { int n, m; scanf("%d%d", &n, &m); VL T(n); FORE(i,T) scanf("%lld", &T[i]); sort(T.begin(), T.end()); VL Sum = T; FORD(i,n-2,0) Sum[i] += Sum[i+1]; Sum.PB(0); VPL Stack(1, MP(0,0)); while (m--) { LLI x, y; scanf("%lld%lld", &x, &y); PLL elem = MP(x,y); int ind = getLastConvex(Stack, elem); int s = SIZE(Stack); VI pos; FOR(i,ind,s-1) { PLL x, y; if (i > ind) { x = Stack[i-1]; y = Stack[i]; } else { x = Stack[ind]; y = elem; } LLI dx = y.ST - x.ST; LLI dy = y.ND - x.ND; pos.PB(upper_bound(T.begin(), T.end(), dy / dx) - T.begin()); } pos.PB(n); LLI ans = 0; FOR(i,ind,s-1) { int it = i - ind; int l = pos[it]; int r = pos[it + 1]; ans += (x - Stack[i].ST) * (Sum[l] - Sum[r]); ans += (r - l) * (Stack[i].ND - y); } printf("%lld\n", ans); while (SIZE(Stack) > ind + 1) Stack.pop_back(); Stack.PB(elem); } 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 | #include <cstdio> #include <vector> #include <algorithm> #define FOR(i,b,e) for(int i=(b); i <= (e); ++i) #define FORD(i,b,e) for(int i=(b); i >= (e); --i) #define SIZE(c) (int) (c).size() #define FORE(i,c) FOR(i,0,SIZE(c)-1) #define PB push_back #define MP make_pair #define ST first #define ND second using namespace std; typedef long long int LLI; typedef pair < LLI , LLI > PLL; typedef vector < int > VI; typedef vector < LLI > VL; typedef vector < PLL > VPL; /*************************************************************************/ /* Returns the product of a and b as two parts */ PLL mul(LLI a, LLI b) { bool neg = ((a < 0) ^ (b < 0)); if (a < 0) a *= -1; if (b < 0) b *= -1; LLI M = (1LL << 30); LLI a1 = a >> 30, a0 = a%M, b1 = b >> 30, b0 = b%M; LLI hi = a1 * b1, lo = a0 * b0; a0 *= b1, b0 *= a1; hi += (a0 >> 30) + (b0 >> 30); a0 <<= 30, b0 <<= 30; if (a0 + lo < lo) ++hi; lo += a0; if (b0 + lo < lo) ++hi; lo += b0; if (neg) { hi *= -1; lo *= -1; } return MP(hi, lo); } /* Returns true iff ab >= cd */ bool comp(LLI a, LLI b, LLI c, LLI d) { return mul(a,b) >= mul(c,d); } /* Returns true iff (x,y,z) is convex upwards */ bool isBad(PLL x, PLL y, PLL z) { return comp(y.ND - x.ND, z.ST - y.ST, y.ST - x.ST, z.ND - y.ND); } /* Returns index of last element remaining after pushing to convex stack structure */ int getLastConvex(VPL &Stack, PLL elem) { int s = SIZE(Stack); while (s > 1 && isBad(Stack[s-2], Stack[s-1], elem)) s--; return s - 1; } const LLI INF = 1000000000000000001LL; /*************************************************************************/ int main() { int n, m; scanf("%d%d", &n, &m); VL T(n); FORE(i,T) scanf("%lld", &T[i]); sort(T.begin(), T.end()); VL Sum = T; FORD(i,n-2,0) Sum[i] += Sum[i+1]; Sum.PB(0); VPL Stack(1, MP(0,0)); while (m--) { LLI x, y; scanf("%lld%lld", &x, &y); PLL elem = MP(x,y); int ind = getLastConvex(Stack, elem); int s = SIZE(Stack); VI pos; FOR(i,ind,s-1) { PLL x, y; if (i > ind) { x = Stack[i-1]; y = Stack[i]; } else { x = Stack[ind]; y = elem; } LLI dx = y.ST - x.ST; LLI dy = y.ND - x.ND; pos.PB(upper_bound(T.begin(), T.end(), dy / dx) - T.begin()); } pos.PB(n); LLI ans = 0; FOR(i,ind,s-1) { int it = i - ind; int l = pos[it]; int r = pos[it + 1]; ans += (x - Stack[i].ST) * (Sum[l] - Sum[r]); ans += (r - l) * (Stack[i].ND - y); } printf("%lld\n", ans); while (SIZE(Stack) > ind + 1) Stack.pop_back(); Stack.PB(elem); } return 0; } /*************************************************************************/ |