import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class sia { static int n, m; static long[] a, sum, lastCutH, lastCutD; static int[] lastCutI; static int lastCutLength = 1; static long d, b; public static void main(String[] args) { n = R.i(); m = R.i(); a = new long[n]; for (int i = 0; i < n; i++) { a[i] = R.l(); } Arrays.sort(a); sum = new long[n + 1]; sum[n] = 0; for (int i = n - 1; i >= 0; i--) { sum[i] = sum[i + 1] + a[i]; } lastCutI = new int[n + 1]; lastCutH = new long[n + 1]; lastCutD = new long[n + 1]; lastCutI[0] = 0; lastCutH[0] = 0; lastCutD[0] = 0; lastCutLength = 1; int ftc; int lc; for (int j = 0; j < m; j++) { d = R.l(); b = R.l(); if (currentHeight(n - 1) <= b) { W.in(0); } else { ftc = firstToCut(0, n - 1); lc = lastCut(ftc, 0, lastCutLength - 1); int currentI = ftc; int nextI; BigInteger result = BigInteger.ZERO; for (int x = lc; x < lastCutLength; x++) { nextI = (x < lastCutLength - 1) ? lastCutI[x + 1] : n; result = result .add(BigInteger.valueOf(sum[currentI] - sum[nextI]).multiply(BigInteger.valueOf(d - lastCutD[x]))) .add(BigInteger.valueOf(lastCutH[x] - b).multiply(BigInteger.valueOf(nextI - currentI))); currentI = nextI; } if (lastCutI[lc] == ftc) { lastCutD[lc] = d; lastCutH[lc] = b; lastCutLength = lc + 1; } else { lastCutI[lc + 1] = ftc; lastCutD[lc + 1] = d; lastCutH[lc + 1] = b; lastCutLength = lc + 2; } W.sn(result.toString()); } } W.flush(); } static int lastCut(int i, int left, int right) { if (left == right) return left; int middle = (left + right) / 2 + ((left + right) % 2); if (lastCutI[middle] > i) { return lastCut(i, left, middle - 1); } else { return lastCut(i, middle, right); } } static long currentHeight(int i) { int lc = lastCut(i, 0, lastCutLength - 1); long lch = lastCutH[lc]; long lcd = lastCutD[lc]; return a[i] * (d - lcd) + lch; } static int firstToCut(int left, int right) { if (left == right) return left; int middle = (left + right) / 2; long ch = currentHeight(middle); if (ch <= b) { return firstToCut(middle + 1, right); } else { return firstToCut(left, middle); } } static class R { static InputStream is=new BufferedInputStream(System.in); static int b='\n'; static void next(){try{b=is.read();}catch(Exception e){}} static void skip(){while(b=='\n'||b==' '||b=='\r'||b=='\t')next();} static char c(){skip();char c=(char)b;next();return c;} static int i(){skip();int i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} static long l(){skip();long i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} } static class W { static OutputStream os=new BufferedOutputStream(System.out,4096); static void b(int b){try{os.write(b);}catch(Exception e){}} static void c(char c){b(c);} static void cs(char c){c(c);space();} static void cn(char c){c(c);newLine();} static void i(int i){if(i>9)i(i/10);b('0'+(i%10));} static void is(int i){i(i);space();} static void in(int i){i(i);newLine();} static void l(long l){if(l>9)l(l/10);b('0'+(int)(l%10));} static void ls(long l){l(l);space();} static void ln(long l){l(l);newLine();} static void s(String s){try{os.write(s.getBytes());}catch(Exception e){}} static void ss(String s){s(s);space();} static void sn(String s){s(s);newLine();} static void tn(boolean b){sn(b?"TAK":"NIE");} static void space(){b(' ');} static void newLine(){b('\n');} static void flush(){try{os.flush();}catch(Exception e){}}; } static class T { static List<String> s=new ArrayList<String>(); static List<Long> t=new ArrayList<Long>(); static void t(String n){s.add(n);t.add(System.currentTimeMillis());} static void err(){t.add(System.currentTimeMillis());System.err.println();for(int i=0;i<s.size();i++){System.err.println(s.get(i)+": "+(t.get(i+1)-t.get(i)));}System.err.println("TOTAL: "+(t.get(t.size() - 1)-t.get(0)));System.err.flush();} } }
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 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class sia { static int n, m; static long[] a, sum, lastCutH, lastCutD; static int[] lastCutI; static int lastCutLength = 1; static long d, b; public static void main(String[] args) { n = R.i(); m = R.i(); a = new long[n]; for (int i = 0; i < n; i++) { a[i] = R.l(); } Arrays.sort(a); sum = new long[n + 1]; sum[n] = 0; for (int i = n - 1; i >= 0; i--) { sum[i] = sum[i + 1] + a[i]; } lastCutI = new int[n + 1]; lastCutH = new long[n + 1]; lastCutD = new long[n + 1]; lastCutI[0] = 0; lastCutH[0] = 0; lastCutD[0] = 0; lastCutLength = 1; int ftc; int lc; for (int j = 0; j < m; j++) { d = R.l(); b = R.l(); if (currentHeight(n - 1) <= b) { W.in(0); } else { ftc = firstToCut(0, n - 1); lc = lastCut(ftc, 0, lastCutLength - 1); int currentI = ftc; int nextI; BigInteger result = BigInteger.ZERO; for (int x = lc; x < lastCutLength; x++) { nextI = (x < lastCutLength - 1) ? lastCutI[x + 1] : n; result = result .add(BigInteger.valueOf(sum[currentI] - sum[nextI]).multiply(BigInteger.valueOf(d - lastCutD[x]))) .add(BigInteger.valueOf(lastCutH[x] - b).multiply(BigInteger.valueOf(nextI - currentI))); currentI = nextI; } if (lastCutI[lc] == ftc) { lastCutD[lc] = d; lastCutH[lc] = b; lastCutLength = lc + 1; } else { lastCutI[lc + 1] = ftc; lastCutD[lc + 1] = d; lastCutH[lc + 1] = b; lastCutLength = lc + 2; } W.sn(result.toString()); } } W.flush(); } static int lastCut(int i, int left, int right) { if (left == right) return left; int middle = (left + right) / 2 + ((left + right) % 2); if (lastCutI[middle] > i) { return lastCut(i, left, middle - 1); } else { return lastCut(i, middle, right); } } static long currentHeight(int i) { int lc = lastCut(i, 0, lastCutLength - 1); long lch = lastCutH[lc]; long lcd = lastCutD[lc]; return a[i] * (d - lcd) + lch; } static int firstToCut(int left, int right) { if (left == right) return left; int middle = (left + right) / 2; long ch = currentHeight(middle); if (ch <= b) { return firstToCut(middle + 1, right); } else { return firstToCut(left, middle); } } static class R { static InputStream is=new BufferedInputStream(System.in); static int b='\n'; static void next(){try{b=is.read();}catch(Exception e){}} static void skip(){while(b=='\n'||b==' '||b=='\r'||b=='\t')next();} static char c(){skip();char c=(char)b;next();return c;} static int i(){skip();int i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} static long l(){skip();long i=0;while(b>='0'&&b<='9'){i=10*i+(b-'0');next();}return i;} } static class W { static OutputStream os=new BufferedOutputStream(System.out,4096); static void b(int b){try{os.write(b);}catch(Exception e){}} static void c(char c){b(c);} static void cs(char c){c(c);space();} static void cn(char c){c(c);newLine();} static void i(int i){if(i>9)i(i/10);b('0'+(i%10));} static void is(int i){i(i);space();} static void in(int i){i(i);newLine();} static void l(long l){if(l>9)l(l/10);b('0'+(int)(l%10));} static void ls(long l){l(l);space();} static void ln(long l){l(l);newLine();} static void s(String s){try{os.write(s.getBytes());}catch(Exception e){}} static void ss(String s){s(s);space();} static void sn(String s){s(s);newLine();} static void tn(boolean b){sn(b?"TAK":"NIE");} static void space(){b(' ');} static void newLine(){b('\n');} static void flush(){try{os.flush();}catch(Exception e){}}; } static class T { static List<String> s=new ArrayList<String>(); static List<Long> t=new ArrayList<Long>(); static void t(String n){s.add(n);t.add(System.currentTimeMillis());} static void err(){t.add(System.currentTimeMillis());System.err.println();for(int i=0;i<s.size();i++){System.err.println(s.get(i)+": "+(t.get(i+1)-t.get(i)));}System.err.println("TOTAL: "+(t.get(t.size() - 1)-t.get(0)));System.err.flush();} } } |