Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include <cstdio> #include <algorithm> //#include <iostream> using namespace std; struct cut{ int idx; long long time; long long height; cut(int i, long long t, long long h) : idx(i) , time(t) , height(h) {} cut() : idx(0) , time(0) , height(0) {} }; bool revcmp(const int& a, const int& b){ return b < a; } int speeds[500001]; long long sums[500001]; cut cuts[500001]; long long totalgrass; int ntiers; int last_cut_tier_idx(long long t, long long h){ // TAK - ostatni -> kroimy wszystko // cokolwiek innego - trzeba szuka� mi�dzy ostatnim TAK i pierwszym NIE int p = 0; int k = ntiers; int n; while(true){ n = (p + k) / 2; cut c = cuts[n]; if((long long)speeds[c.idx] * (t - c.time) + c.height > h){ // TRZEBA CI�� if(n == 0){ //tniemy wszystko return -1; }else{ k = n - 1; } }else{ // NIE TRZEBA CI�� if(n == ntiers - 1){ return n; } cut c2 = cuts[n + 1]; if((long long)speeds[c2.idx] * (t - c2.time) + c2.height > h){ // tutaj jest pierwsze NT return n; }else{ p = n + 1; } } } } /* v-orig_p orig_k-v TTTTTTTTTTTTTTTTTTTTTNNNNNNNNNNNNNNNNNNNN ^cuts[n + 1] cuts[n]^ */ int last_cut_idx(long long t, long long h, int tier_idx){ cut c = cuts[tier_idx]; int orig_p; // orig_p - 1 JEST NA TAK int orig_k; // orig_k + 1 JEST NA NIE if(tier_idx == ntiers - 1){ orig_p = 0; }else{ cut c2 = cuts[tier_idx + 1]; orig_p = c2.idx + 1; } int k = c.idx; int p = orig_p; int n; while(true){ n = (p + k) / 2; if((long long)speeds[n] * (t - c.time) + c.height > h){ // TAK p = n + 1; }else{ // NIE if(n == orig_p || (long long)speeds[n - 1] * (t - c.time) + c.height > h){ // n jest pierwszym NIE // n - 1 jest ostatnim TAK return n - 1; } k = n - 1; } } } int main(){ int n, m; scanf("%d %d", &n, &m); for(int i = 0; i < n; ++i){ scanf("%d", &speeds[i]); } sort(speeds, speeds + n, revcmp); sums[0] = speeds[0]; for(int i = 1; i < n; ++i){ sums[i] = sums[i - 1] + speeds[i]; } cuts[0] = cut(n - 1, 0, 0); ntiers = 1; for(int i = 0; i < m; ++i){ long long t; long long h; scanf("%lld %lld", &t, &h); int tier = last_cut_tier_idx(t, h); if(tier == -1){ //cout << "TIER: -1" << endl; long long totalgrowth = sums[n - 1] * t; long long grass = totalgrowth - totalgrass - (long long)n * h; //cout << sums[n - 1] << " " << t << " ___ " << totalgrowth << " " << totalgrass << endl; printf("%lld\n", grass); totalgrass += grass; ntiers = 1; cuts[0] = cut(n - 1, t, h); }else{ //cout << "TIER: " << cuts[tier].idx << ", X = "; int idx = last_cut_idx(t, h, tier); //cout << idx << endl; // idx - tniemy traw� od 0 do idx w��cznie // idziemy po cuts od ko�ca, ca�e przedzia�y obcinamy i przesuwamy koniec cutsa. // po obliczeniach wstawiamy (idx, t, h) do cuts. long long grass = 0; if(idx != -1){ int orig_ntiers = ntiers - 1; for(int j = ntiers - 1; j >= 0; --j){ long long hb = cuts[j].height; long long tb = cuts[j].time; //cout << idx << ": Trying " << cuts[j].idx << endl; int p, k; if(j == orig_ntiers){ p = 0; }else{ p = cuts[j + 1].idx + 1; } if(cuts[j].idx <= idx){ //cout << "...Yup\n"; k = cuts[j].idx; --ntiers; }else{ //cout << "...Nope\n"; k = idx; j = -1; // end loop } //cout << "p(" << p << "), k(" << k << ")\n"; long long growth = sums[k] - sums[p] + speeds[p]; // works for p = 0, unlike sums[p - 1] grass += growth * (t - tb) - (long long)(k - p + 1) * (h - hb); } // wstawiamy nowe koszenie do tablycy cuts[ntiers] = cut(idx, t, h); ++ntiers; } totalgrass += grass; printf("%lld\n", grass); //cout << "Total: " << totalgrass << endl; } } 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 | #include <cstdio> #include <algorithm> //#include <iostream> using namespace std; struct cut{ int idx; long long time; long long height; cut(int i, long long t, long long h) : idx(i) , time(t) , height(h) {} cut() : idx(0) , time(0) , height(0) {} }; bool revcmp(const int& a, const int& b){ return b < a; } int speeds[500001]; long long sums[500001]; cut cuts[500001]; long long totalgrass; int ntiers; int last_cut_tier_idx(long long t, long long h){ // TAK - ostatni -> kroimy wszystko // cokolwiek innego - trzeba szuka� mi�dzy ostatnim TAK i pierwszym NIE int p = 0; int k = ntiers; int n; while(true){ n = (p + k) / 2; cut c = cuts[n]; if((long long)speeds[c.idx] * (t - c.time) + c.height > h){ // TRZEBA CI�� if(n == 0){ //tniemy wszystko return -1; }else{ k = n - 1; } }else{ // NIE TRZEBA CI�� if(n == ntiers - 1){ return n; } cut c2 = cuts[n + 1]; if((long long)speeds[c2.idx] * (t - c2.time) + c2.height > h){ // tutaj jest pierwsze NT return n; }else{ p = n + 1; } } } } /* v-orig_p orig_k-v TTTTTTTTTTTTTTTTTTTTTNNNNNNNNNNNNNNNNNNNN ^cuts[n + 1] cuts[n]^ */ int last_cut_idx(long long t, long long h, int tier_idx){ cut c = cuts[tier_idx]; int orig_p; // orig_p - 1 JEST NA TAK int orig_k; // orig_k + 1 JEST NA NIE if(tier_idx == ntiers - 1){ orig_p = 0; }else{ cut c2 = cuts[tier_idx + 1]; orig_p = c2.idx + 1; } int k = c.idx; int p = orig_p; int n; while(true){ n = (p + k) / 2; if((long long)speeds[n] * (t - c.time) + c.height > h){ // TAK p = n + 1; }else{ // NIE if(n == orig_p || (long long)speeds[n - 1] * (t - c.time) + c.height > h){ // n jest pierwszym NIE // n - 1 jest ostatnim TAK return n - 1; } k = n - 1; } } } int main(){ int n, m; scanf("%d %d", &n, &m); for(int i = 0; i < n; ++i){ scanf("%d", &speeds[i]); } sort(speeds, speeds + n, revcmp); sums[0] = speeds[0]; for(int i = 1; i < n; ++i){ sums[i] = sums[i - 1] + speeds[i]; } cuts[0] = cut(n - 1, 0, 0); ntiers = 1; for(int i = 0; i < m; ++i){ long long t; long long h; scanf("%lld %lld", &t, &h); int tier = last_cut_tier_idx(t, h); if(tier == -1){ //cout << "TIER: -1" << endl; long long totalgrowth = sums[n - 1] * t; long long grass = totalgrowth - totalgrass - (long long)n * h; //cout << sums[n - 1] << " " << t << " ___ " << totalgrowth << " " << totalgrass << endl; printf("%lld\n", grass); totalgrass += grass; ntiers = 1; cuts[0] = cut(n - 1, t, h); }else{ //cout << "TIER: " << cuts[tier].idx << ", X = "; int idx = last_cut_idx(t, h, tier); //cout << idx << endl; // idx - tniemy traw� od 0 do idx w��cznie // idziemy po cuts od ko�ca, ca�e przedzia�y obcinamy i przesuwamy koniec cutsa. // po obliczeniach wstawiamy (idx, t, h) do cuts. long long grass = 0; if(idx != -1){ int orig_ntiers = ntiers - 1; for(int j = ntiers - 1; j >= 0; --j){ long long hb = cuts[j].height; long long tb = cuts[j].time; //cout << idx << ": Trying " << cuts[j].idx << endl; int p, k; if(j == orig_ntiers){ p = 0; }else{ p = cuts[j + 1].idx + 1; } if(cuts[j].idx <= idx){ //cout << "...Yup\n"; k = cuts[j].idx; --ntiers; }else{ //cout << "...Nope\n"; k = idx; j = -1; // end loop } //cout << "p(" << p << "), k(" << k << ")\n"; long long growth = sums[k] - sums[p] + speeds[p]; // works for p = 0, unlike sums[p - 1] grass += growth * (t - tb) - (long long)(k - p + 1) * (h - hb); } // wstawiamy nowe koszenie do tablycy cuts[ntiers] = cut(idx, t, h); ++ntiers; } totalgrass += grass; printf("%lld\n", grass); //cout << "Total: " << totalgrass << endl; } } return 0; } |