#include <stdio.h> #include <stdlib.h> #define BUF_READ 1000000 #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; char buf_out[BUF_SIZE]; char *buf_out_cur; char *buf_out_end; void 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; } } void buf_out_init() { buf_out_cur = buf_out; buf_out_end = buf_out + BUF_SIZE; } void buf_out_flush() { fwrite( buf_out, 1, buf_out_cur - buf_out, stdout ); buf_out_cur = buf_out; } void buf_out_print(unsigned long long n) { char tmp_buf[22]; int pos, i; if (n == 0) { tmp_buf[0] = '0'; pos = 1; } else { pos = 0; while (n > 0) { tmp_buf[pos] = (n % 10) + '0'; pos++; n = n / 10; } } for(i = pos-1; i >= 0; --i) { *buf_out_cur++ = tmp_buf[i]; } *buf_out_cur++ = '\n'; if (buf_out_end - buf_out_cur < BUF_BORDER) { buf_out_flush(); } } void buf_findNextNumber() { while ( *buf_cur < '0' || *buf_cur > '9') { buf_cur++; } } void read_buffer_if_needed() { int i, d, bytes_read; if (file_finished == 1) { return; } d = buf_end - buf_cur; if (d > BUF_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_findNextNumber(); number = *buf_cur - '0'; buf_cur++; while( *buf_cur >= '0' && *buf_cur <= '9') { number = number*10 + *buf_cur - '0'; buf_cur++; } return number; } unsigned long long *a; unsigned long long n, m; typedef struct { unsigned long long p; unsigned long long prev_d; } t_pole; t_pole *pole; unsigned long long przelicz_pole_i_siano(unsigned long long d, unsigned long long b) { int i; unsigned long long siano; siano = 0; for(i = 0; i < n; ++i) { pole[i].p = pole[i].p + a[i] * (d - pole[i].prev_d); pole[i].prev_d = d; if (pole[i].p > b) { siano += (pole[i].p - b); pole[i].p = b; } else { break; } } return siano; } static int ull_comp(const void *p1, const void *p2) { unsigned long long left = *(const unsigned long long *) p1; unsigned long long right = *(const unsigned long long *) p2; return ((right > left) - (right < left)); } int main() { int i; unsigned long long b, d; buf_init(); buf_out_init(); n = buf_getNextNumber(); m = buf_getNextNumber(); a = (unsigned long long*) malloc(sizeof(unsigned long long) * n); pole = (t_pole*) malloc(sizeof(t_pole) * n); for(i = 0; i < n; ++i) { pole[i].p = 0; pole[i].prev_d = 0; } for(i = 0; i < n; ++i) { a[i] = buf_getNextNumber(); } qsort( a, n, sizeof(unsigned long long), ull_comp ); for(i = 0; i < m; ++i) { d = buf_getNextNumber(); b = buf_getNextNumber(); buf_out_print( przelicz_pole_i_siano(d, b) ); } buf_out_flush(); 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 | #include <stdio.h> #include <stdlib.h> #define BUF_READ 1000000 #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; char buf_out[BUF_SIZE]; char *buf_out_cur; char *buf_out_end; void 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; } } void buf_out_init() { buf_out_cur = buf_out; buf_out_end = buf_out + BUF_SIZE; } void buf_out_flush() { fwrite( buf_out, 1, buf_out_cur - buf_out, stdout ); buf_out_cur = buf_out; } void buf_out_print(unsigned long long n) { char tmp_buf[22]; int pos, i; if (n == 0) { tmp_buf[0] = '0'; pos = 1; } else { pos = 0; while (n > 0) { tmp_buf[pos] = (n % 10) + '0'; pos++; n = n / 10; } } for(i = pos-1; i >= 0; --i) { *buf_out_cur++ = tmp_buf[i]; } *buf_out_cur++ = '\n'; if (buf_out_end - buf_out_cur < BUF_BORDER) { buf_out_flush(); } } void buf_findNextNumber() { while ( *buf_cur < '0' || *buf_cur > '9') { buf_cur++; } } void read_buffer_if_needed() { int i, d, bytes_read; if (file_finished == 1) { return; } d = buf_end - buf_cur; if (d > BUF_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_findNextNumber(); number = *buf_cur - '0'; buf_cur++; while( *buf_cur >= '0' && *buf_cur <= '9') { number = number*10 + *buf_cur - '0'; buf_cur++; } return number; } unsigned long long *a; unsigned long long n, m; typedef struct { unsigned long long p; unsigned long long prev_d; } t_pole; t_pole *pole; unsigned long long przelicz_pole_i_siano(unsigned long long d, unsigned long long b) { int i; unsigned long long siano; siano = 0; for(i = 0; i < n; ++i) { pole[i].p = pole[i].p + a[i] * (d - pole[i].prev_d); pole[i].prev_d = d; if (pole[i].p > b) { siano += (pole[i].p - b); pole[i].p = b; } else { break; } } return siano; } static int ull_comp(const void *p1, const void *p2) { unsigned long long left = *(const unsigned long long *) p1; unsigned long long right = *(const unsigned long long *) p2; return ((right > left) - (right < left)); } int main() { int i; unsigned long long b, d; buf_init(); buf_out_init(); n = buf_getNextNumber(); m = buf_getNextNumber(); a = (unsigned long long*) malloc(sizeof(unsigned long long) * n); pole = (t_pole*) malloc(sizeof(t_pole) * n); for(i = 0; i < n; ++i) { pole[i].p = 0; pole[i].prev_d = 0; } for(i = 0; i < n; ++i) { a[i] = buf_getNextNumber(); } qsort( a, n, sizeof(unsigned long long), ull_comp ); for(i = 0; i < m; ++i) { d = buf_getNextNumber(); b = buf_getNextNumber(); buf_out_print( przelicz_pole_i_siano(d, b) ); } buf_out_flush(); return 0; } |