#include <stdio.h> #define BUF_READ 5000000 #define BUF_BORDER 100 #define BUF_SIZE (BUF_READ+BUF_BORDER+1) char buf[BUF_SIZE]; char *buf_cur; char *buf_end; int file_finished; unsigned int bytes_write; int buf_init() { int bytes_read; bytes_read = fread( buf, 1, BUF_READ, stdin ); buf_cur = buf; buf_end = buf + bytes_read; if (bytes_read < BUF_READ) { file_finished = 1; } else { file_finished = 0; } return bytes_read; } void buf_findNextNumber() { while ( *buf_cur < '0' || *buf_cur > '9') { buf_cur++; } } void read_buffer_if_needed(int border) { int i, d, bytes_read; if (file_finished == 1) { return; } d = buf_end - buf_cur; if (d > border) { return; } for(i = 0; i < d; ++i) { buf[i] = buf_cur[i]; } bytes_read = fread( buf+d, 1, BUF_READ, stdin ); if (bytes_read < BUF_READ) { file_finished = 1; } buf_cur = buf; buf_end = buf+d+bytes_read; } unsigned long long buf_getNextNumber() { unsigned long long number = 0; read_buffer_if_needed(BUF_BORDER); buf_findNextNumber(); number = *buf_cur - '0'; buf_cur++; while( *buf_cur >= '0' && *buf_cur <= '9') { number = number*10 + *buf_cur - '0'; buf_cur++; } return number; } void buf_reset() { buf_cur = buf; bytes_write = 0; } void buf_write_ll(unsigned long long a) { char l_buf[24]; int i; i = 0; while(a > 0) { l_buf[i] = (a % 10) + '0'; a /= 10; i++; } if (i == 0) { i = 1; l_buf[0] = '0'; } i--; while(i >= 0) { *buf_cur++ = l_buf[i--]; bytes_write++; } *buf_cur = '\n'; buf_cur++; bytes_write++; } #define T_MAX_SIZE 200002 unsigned long long t[T_MAX_SIZE]; unsigned int d[T_MAX_SIZE]; unsigned long long t2[T_MAX_SIZE*2]; unsigned int n, m, n2; #define MAX_D 1000002 unsigned long long prev_res[MAX_D]; char prev_res_calc[MAX_D]; /* unsigned long long calcSum(unsigned int d) { unsigned long long sum = 0; unsigned long long fft = 0; unsigned int i; for (i = 0; i < n; ++i) { if (t[i] >= fft) { if (t[i] - fft >= d) { //sum += 0; fft = t[i]; } else { // t[i] - fft < d sum += (fft + d) - t[i]; fft += d; } } else { // t[i] < fft sum += (fft + d) - t[i]; fft += d; } } return sum; } */ unsigned long long calcSum(unsigned int d) { unsigned long long sum = 0; unsigned long long fft = 0; unsigned int i; for (i = 0; i < n2; i += 2) { if (t2[i] >= fft && t2[i] - fft >= d) { //sum += 0; fft = t2[i]; } else { // t[i] < fft or t[i] - fft < d fft += d; sum += fft - t2[i]; } if (t2[i+1] > 1) { unsigned long long cl = t2[i+1]-1; sum += (fft - t2[i])*cl + cl*(cl+1)*d/2; fft += cl*d; } } return sum; } void convert_t() { int i, i2; t2[0] = t[0]; t2[1] = 1; i2 = 0; for(i = 1; i < n; ++i) { if (t[i] == t2[i2]) { t2[i2+1]++; } else { i2 += 2; t2[i2] = t[i]; t2[i2+1] = 1; } } n2 = i2+1; } int main() { unsigned int i; unsigned long long result; n = (unsigned int) buf_getNextNumber(); m = (unsigned int) buf_getNextNumber(); for(i = 0; i < n; ++i) { t[i] = buf_getNextNumber(); } for(i = 0; i < m; ++i) { d[i] = (unsigned int) buf_getNextNumber(); } convert_t(); // for(i = 0; i < n2; i += 2) { // printf("%llu (%llu)\n", t2[i], t2[i+1]); // } buf_reset(); for(i = 0; i < m; ++i) { //printf("%llu\n", calcSum(d[i])); unsigned long long cd = d[i]; if (prev_res_calc[cd]) { buf_write_ll(prev_res[cd]); continue; } result = calcSum(cd); buf_write_ll(result); prev_res_calc[cd] = 1; prev_res[cd] = result; } *buf_cur = '0'; fwrite( buf, 1, bytes_write, stdout ); 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | #include <stdio.h> #define BUF_READ 5000000 #define BUF_BORDER 100 #define BUF_SIZE (BUF_READ+BUF_BORDER+1) char buf[BUF_SIZE]; char *buf_cur; char *buf_end; int file_finished; unsigned int bytes_write; int buf_init() { int bytes_read; bytes_read = fread( buf, 1, BUF_READ, stdin ); buf_cur = buf; buf_end = buf + bytes_read; if (bytes_read < BUF_READ) { file_finished = 1; } else { file_finished = 0; } return bytes_read; } void buf_findNextNumber() { while ( *buf_cur < '0' || *buf_cur > '9') { buf_cur++; } } void read_buffer_if_needed(int border) { int i, d, bytes_read; if (file_finished == 1) { return; } d = buf_end - buf_cur; if (d > border) { return; } for(i = 0; i < d; ++i) { buf[i] = buf_cur[i]; } bytes_read = fread( buf+d, 1, BUF_READ, stdin ); if (bytes_read < BUF_READ) { file_finished = 1; } buf_cur = buf; buf_end = buf+d+bytes_read; } unsigned long long buf_getNextNumber() { unsigned long long number = 0; read_buffer_if_needed(BUF_BORDER); buf_findNextNumber(); number = *buf_cur - '0'; buf_cur++; while( *buf_cur >= '0' && *buf_cur <= '9') { number = number*10 + *buf_cur - '0'; buf_cur++; } return number; } void buf_reset() { buf_cur = buf; bytes_write = 0; } void buf_write_ll(unsigned long long a) { char l_buf[24]; int i; i = 0; while(a > 0) { l_buf[i] = (a % 10) + '0'; a /= 10; i++; } if (i == 0) { i = 1; l_buf[0] = '0'; } i--; while(i >= 0) { *buf_cur++ = l_buf[i--]; bytes_write++; } *buf_cur = '\n'; buf_cur++; bytes_write++; } #define T_MAX_SIZE 200002 unsigned long long t[T_MAX_SIZE]; unsigned int d[T_MAX_SIZE]; unsigned long long t2[T_MAX_SIZE*2]; unsigned int n, m, n2; #define MAX_D 1000002 unsigned long long prev_res[MAX_D]; char prev_res_calc[MAX_D]; /* unsigned long long calcSum(unsigned int d) { unsigned long long sum = 0; unsigned long long fft = 0; unsigned int i; for (i = 0; i < n; ++i) { if (t[i] >= fft) { if (t[i] - fft >= d) { //sum += 0; fft = t[i]; } else { // t[i] - fft < d sum += (fft + d) - t[i]; fft += d; } } else { // t[i] < fft sum += (fft + d) - t[i]; fft += d; } } return sum; } */ unsigned long long calcSum(unsigned int d) { unsigned long long sum = 0; unsigned long long fft = 0; unsigned int i; for (i = 0; i < n2; i += 2) { if (t2[i] >= fft && t2[i] - fft >= d) { //sum += 0; fft = t2[i]; } else { // t[i] < fft or t[i] - fft < d fft += d; sum += fft - t2[i]; } if (t2[i+1] > 1) { unsigned long long cl = t2[i+1]-1; sum += (fft - t2[i])*cl + cl*(cl+1)*d/2; fft += cl*d; } } return sum; } void convert_t() { int i, i2; t2[0] = t[0]; t2[1] = 1; i2 = 0; for(i = 1; i < n; ++i) { if (t[i] == t2[i2]) { t2[i2+1]++; } else { i2 += 2; t2[i2] = t[i]; t2[i2+1] = 1; } } n2 = i2+1; } int main() { unsigned int i; unsigned long long result; n = (unsigned int) buf_getNextNumber(); m = (unsigned int) buf_getNextNumber(); for(i = 0; i < n; ++i) { t[i] = buf_getNextNumber(); } for(i = 0; i < m; ++i) { d[i] = (unsigned int) buf_getNextNumber(); } convert_t(); // for(i = 0; i < n2; i += 2) { // printf("%llu (%llu)\n", t2[i], t2[i+1]); // } buf_reset(); for(i = 0; i < m; ++i) { //printf("%llu\n", calcSum(d[i])); unsigned long long cd = d[i]; if (prev_res_calc[cd]) { buf_write_ll(prev_res[cd]); continue; } result = calcSum(cd); buf_write_ll(result); prev_res_calc[cd] = 1; prev_res[cd] = result; } *buf_cur = '0'; fwrite( buf, 1, bytes_write, stdout ); return 0; } |