#include <cstdio> #include <vector> #include <algorithm> #define MAXAR 500002 int AR; int FIELD[MAXAR]; long long SUM[MAXAR]; struct CUTT { int pos; long long h; long long day; }; int XCPOS = 0; CUTT XC[MAXAR]; long long mieszaj(long long xday, long long xh) { int xcp = XCPOS; CUTT old = {AR,0,0}; CUTT cur = XC[xcp]; long long sum = 0; long long curh = cur.h + FIELD[cur.pos]*(xday-cur.day); while (curh > xh) { long long delta = (SUM[cur.pos] - SUM[old.pos]) * (xday-cur.day); delta -= (xh-cur.h) * (old.pos - cur.pos); sum += delta; if (xcp == 0) { CUTT cuttt = {0, xh, xday}; XC[xcp] = cuttt; XCPOS = xcp; return sum; } --xcp; old = cur; cur = XC[xcp]; curh = cur.h + FIELD[cur.pos]*(xday-cur.day); } int a,b,c; a = cur.pos; b = old.pos; while (a!=b) { c = (a+b)>>1; long long ch = cur.h + FIELD[c] * (xday - cur.day); if (ch > xh) b = c; else if (ch <= xh) a = c + 1; } long long ch = cur.h + FIELD[a] * (xday - cur.day); long long delta = (SUM[a] - SUM[old.pos]) * (xday - cur.day); delta -= (xh - cur.h) * (old.pos - a); sum += delta; if (sum != 0) { ++xcp; CUTT cuttt = {a, xh, xday}; XC[xcp] = cuttt; XCPOS = xcp; } return sum; } int main() { int c; scanf("%d%d", &AR, &c); for (int i=0; i<AR; ++i) { scanf("%d", &FIELD[i]); } std::sort(&FIELD[0], &FIELD[AR]); FIELD[AR] = 0; SUM[AR] = 0; long long sum = 0; for (int i = AR-1; i >= 0; --i) { SUM[i] = SUM[i+1] + FIELD[i]; } XCPOS = 0; CUTT cuttt = {0,0,0}; XC[0] = cuttt; for (int i = 0; i < c; ++i) { long long xday, xh; scanf("%lld%lld", &xday, &xh); printf("%lld\n", mieszaj(xday, xh)); } 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 | #include <cstdio> #include <vector> #include <algorithm> #define MAXAR 500002 int AR; int FIELD[MAXAR]; long long SUM[MAXAR]; struct CUTT { int pos; long long h; long long day; }; int XCPOS = 0; CUTT XC[MAXAR]; long long mieszaj(long long xday, long long xh) { int xcp = XCPOS; CUTT old = {AR,0,0}; CUTT cur = XC[xcp]; long long sum = 0; long long curh = cur.h + FIELD[cur.pos]*(xday-cur.day); while (curh > xh) { long long delta = (SUM[cur.pos] - SUM[old.pos]) * (xday-cur.day); delta -= (xh-cur.h) * (old.pos - cur.pos); sum += delta; if (xcp == 0) { CUTT cuttt = {0, xh, xday}; XC[xcp] = cuttt; XCPOS = xcp; return sum; } --xcp; old = cur; cur = XC[xcp]; curh = cur.h + FIELD[cur.pos]*(xday-cur.day); } int a,b,c; a = cur.pos; b = old.pos; while (a!=b) { c = (a+b)>>1; long long ch = cur.h + FIELD[c] * (xday - cur.day); if (ch > xh) b = c; else if (ch <= xh) a = c + 1; } long long ch = cur.h + FIELD[a] * (xday - cur.day); long long delta = (SUM[a] - SUM[old.pos]) * (xday - cur.day); delta -= (xh - cur.h) * (old.pos - a); sum += delta; if (sum != 0) { ++xcp; CUTT cuttt = {a, xh, xday}; XC[xcp] = cuttt; XCPOS = xcp; } return sum; } int main() { int c; scanf("%d%d", &AR, &c); for (int i=0; i<AR; ++i) { scanf("%d", &FIELD[i]); } std::sort(&FIELD[0], &FIELD[AR]); FIELD[AR] = 0; SUM[AR] = 0; long long sum = 0; for (int i = AR-1; i >= 0; --i) { SUM[i] = SUM[i+1] + FIELD[i]; } XCPOS = 0; CUTT cuttt = {0,0,0}; XC[0] = cuttt; for (int i = 0; i < c; ++i) { long long xday, xh; scanf("%lld%lld", &xday, &xh); printf("%lld\n", mieszaj(xday, xh)); } return 0; } |