import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class sia { BufferedReader rd; int n; long[] a; long[] s; NavigableMap<Integer, long[]> u; sia() throws IOException { rd = new BufferedReader(new InputStreamReader(System.in)); compute(); } private void compute() throws IOException { long m = longarr()[1]; a = longarr(); sort(a); n = a.length; s = new long[n+1]; for(int i=0;i<n;i++) { s[i+1] = s[i] + a[i]; } u = new TreeMap<>(); u.put(0, new long[] { 0, 0 }); StringBuilder buf = new StringBuilder(); for(int i=0;i<m;i++) { long[] c = longarr(); long d = c[0]; long b = c[1]; int g = getFirstIndexWithValueGreaterThan(b, d); long sum = 0; if(g < n) { Map.Entry<Integer, long[]> floor = u.floorEntry(g); List<Integer> toRemove = new ArrayList<>(); boolean first = true; int lastIndex = g; long prevDay = 0; long prevHeight = 0; for(Map.Entry<Integer, long[]> e: u.tailMap(floor.getKey()).entrySet()) { if(first) { first = false; } else { int width = e.getKey() - lastIndex; sum += (d - prevDay) * (s[e.getKey()] - s[lastIndex]) + width*prevHeight; lastIndex = e.getKey(); } toRemove.add(e.getKey()); prevDay = e.getValue()[0]; prevHeight = e.getValue()[1]; } int width = n - lastIndex; sum += (d - prevDay) * (s[n] - s[lastIndex]) + width*prevHeight; sum -= (n-g)*b; int firstJ = floor.getKey().equals(g)?0:1; for(int j=firstJ;j<toRemove.size();j++) { u.remove(toRemove.get(j)); } u.put(g, new long[] { d, b }); } if(i > 0) { buf.append('\n'); } buf.append(sum); } out(buf); } private void sort(long[] a) { int[] c = new int[1000010]; for(long x: a) { c[(int)x]++; } int p = 0; for(int i=0;i<1000010;i++) { for(int j=0;j<c[i];j++) { a[p] = i; p++; } } } private int getFirstIndexWithValueGreaterThan(long b, long d) { int min = 0; int max = n; while(max - min > 1) { int mid = (min + max) / 2; if(getHeight(mid,d) <= b) { min = mid+1; } else { max = mid; } } if(min < n && getHeight(min, d) > b) { return min; } return max; } private long getHeight(int x, long d) { Map.Entry<Integer, long[]> floor = u.floorEntry(x); long baseDay = floor.getValue()[0]; long baseHeight = floor.getValue()[1]; return baseHeight + a[x]*(d - baseDay); } private long[] longarr() throws IOException { return longarr(rd.readLine()); } private long[] longarr(String s) { String[] q = split(s); int n = q.length; long[] a = new long[n]; for(int i=0;i<n;i++) { a[i] = Long.parseLong(q[i]); } return a; } public String[] split(String s) { if(s == null) { return new String[0]; } int n = s.length(); int start = -1; int end = 0; int sp = 0; boolean lastWhitespace = true; for(int i=0;i<n;i++) { char c = s.charAt(i); if(isWhitespace(c)) { lastWhitespace = true; } else { if(lastWhitespace) { sp++; } if(start == -1) { start = i; } end = i; lastWhitespace = false; } } if(start == -1) { return new String[0]; } String[] res = new String[sp]; int last = start; int x = 0; lastWhitespace = true; for(int i=start;i<=end;i++) { char c = s.charAt(i); boolean w = isWhitespace(c); if(w && !lastWhitespace) { res[x++] = s.substring(last,i); } else if(!w && lastWhitespace) { last = i; } lastWhitespace = w; } res[x] = s.substring(last,end+1); return res; } private boolean isWhitespace(char c) { return c==' ' || c=='\t'; } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new sia(); } }
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class sia { BufferedReader rd; int n; long[] a; long[] s; NavigableMap<Integer, long[]> u; sia() throws IOException { rd = new BufferedReader(new InputStreamReader(System.in)); compute(); } private void compute() throws IOException { long m = longarr()[1]; a = longarr(); sort(a); n = a.length; s = new long[n+1]; for(int i=0;i<n;i++) { s[i+1] = s[i] + a[i]; } u = new TreeMap<>(); u.put(0, new long[] { 0, 0 }); StringBuilder buf = new StringBuilder(); for(int i=0;i<m;i++) { long[] c = longarr(); long d = c[0]; long b = c[1]; int g = getFirstIndexWithValueGreaterThan(b, d); long sum = 0; if(g < n) { Map.Entry<Integer, long[]> floor = u.floorEntry(g); List<Integer> toRemove = new ArrayList<>(); boolean first = true; int lastIndex = g; long prevDay = 0; long prevHeight = 0; for(Map.Entry<Integer, long[]> e: u.tailMap(floor.getKey()).entrySet()) { if(first) { first = false; } else { int width = e.getKey() - lastIndex; sum += (d - prevDay) * (s[e.getKey()] - s[lastIndex]) + width*prevHeight; lastIndex = e.getKey(); } toRemove.add(e.getKey()); prevDay = e.getValue()[0]; prevHeight = e.getValue()[1]; } int width = n - lastIndex; sum += (d - prevDay) * (s[n] - s[lastIndex]) + width*prevHeight; sum -= (n-g)*b; int firstJ = floor.getKey().equals(g)?0:1; for(int j=firstJ;j<toRemove.size();j++) { u.remove(toRemove.get(j)); } u.put(g, new long[] { d, b }); } if(i > 0) { buf.append('\n'); } buf.append(sum); } out(buf); } private void sort(long[] a) { int[] c = new int[1000010]; for(long x: a) { c[(int)x]++; } int p = 0; for(int i=0;i<1000010;i++) { for(int j=0;j<c[i];j++) { a[p] = i; p++; } } } private int getFirstIndexWithValueGreaterThan(long b, long d) { int min = 0; int max = n; while(max - min > 1) { int mid = (min + max) / 2; if(getHeight(mid,d) <= b) { min = mid+1; } else { max = mid; } } if(min < n && getHeight(min, d) > b) { return min; } return max; } private long getHeight(int x, long d) { Map.Entry<Integer, long[]> floor = u.floorEntry(x); long baseDay = floor.getValue()[0]; long baseHeight = floor.getValue()[1]; return baseHeight + a[x]*(d - baseDay); } private long[] longarr() throws IOException { return longarr(rd.readLine()); } private long[] longarr(String s) { String[] q = split(s); int n = q.length; long[] a = new long[n]; for(int i=0;i<n;i++) { a[i] = Long.parseLong(q[i]); } return a; } public String[] split(String s) { if(s == null) { return new String[0]; } int n = s.length(); int start = -1; int end = 0; int sp = 0; boolean lastWhitespace = true; for(int i=0;i<n;i++) { char c = s.charAt(i); if(isWhitespace(c)) { lastWhitespace = true; } else { if(lastWhitespace) { sp++; } if(start == -1) { start = i; } end = i; lastWhitespace = false; } } if(start == -1) { return new String[0]; } String[] res = new String[sp]; int last = start; int x = 0; lastWhitespace = true; for(int i=start;i<=end;i++) { char c = s.charAt(i); boolean w = isWhitespace(c); if(w && !lastWhitespace) { res[x++] = s.substring(last,i); } else if(!w && lastWhitespace) { last = i; } lastWhitespace = w; } res[x] = s.substring(last,end+1); return res; } private boolean isWhitespace(char c) { return c==' ' || c=='\t'; } private static void out(Object x) { System.out.println(x); } public static void main(String[] args) throws IOException { new sia(); } } |