|
| 1 | +//math, line sweeping |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#define ll long long |
| 5 | +ll sigma(ll x){ |
| 6 | + if (x <= 0) return 0; |
| 7 | + ll res = x*(x + 1); |
| 8 | + //printf("%lld\n",res>>1); |
| 9 | + return res >> 1; |
| 10 | +} |
| 11 | +int main(){ |
| 12 | + int i; |
| 13 | + int tc, T; |
| 14 | + int P, Q, B, G, W; |
| 15 | + for (scanf("%d", &T), tc = 1; tc <= T; tc++){ |
| 16 | + scanf("%d%d%d%d%d",&P,&Q,&B,&G,&W); |
| 17 | + |
| 18 | + ll ans = -1; |
| 19 | + int l = 0; |
| 20 | + int r = G - 1; |
| 21 | + while (l < G){ |
| 22 | + ll cnt = sigma(l) + sigma(r); |
| 23 | + if (r >= Q){ |
| 24 | + cnt += (r - Q + 1LL) * W; |
| 25 | + cnt += sigma(W - 1); |
| 26 | + } |
| 27 | + else{ |
| 28 | + int half = W - (W >> 1) - 1; |
| 29 | + if (half > Q - r - 1) half = Q - r - 1; |
| 30 | + cnt += sigma(half) + sigma(W - half - 1); |
| 31 | + } |
| 32 | + if (l >= P){ |
| 33 | + cnt += (l - P + 1LL)*B; |
| 34 | + cnt += sigma(B - 1); |
| 35 | + } |
| 36 | + else{ |
| 37 | + int half = B - (B >> 1) - 1; |
| 38 | + if (half > P - l - 1) half = P - l - 1; |
| 39 | + cnt += sigma(half) + sigma(B - half - 1); |
| 40 | + } |
| 41 | + if (ans<0 || ans>cnt) ans = cnt; |
| 42 | + ++l; |
| 43 | + --r; |
| 44 | + } |
| 45 | + l = 0; |
| 46 | + r = B - 1; |
| 47 | + while (l < B){ |
| 48 | + ll cnt = sigma(l) + sigma(r); |
| 49 | + int cpos = 0; |
| 50 | + if (r >= P){ |
| 51 | + cnt += (r - P + 1LL)*G; |
| 52 | + cnt += sigma(G - 1); |
| 53 | + cpos = r - P + G; |
| 54 | + } |
| 55 | + else{ |
| 56 | + int half = G - (G >> 1) - 1; |
| 57 | + if (half > P - r - 1) half = P - r - 1; |
| 58 | + cnt += sigma(half) + sigma(G - half - 1); |
| 59 | + cpos = G - half - 1; |
| 60 | + } |
| 61 | + if (cpos >= Q){ |
| 62 | + cnt += (cpos - Q + 1LL) * W; |
| 63 | + cnt += sigma(W - 1); |
| 64 | + } |
| 65 | + else{ |
| 66 | + int half = W - (W >> 1) - 1; |
| 67 | + if (half > Q - cpos - 1) half = Q - cpos - 1; |
| 68 | + cnt += sigma(half) + sigma(W - half - 1); |
| 69 | + } |
| 70 | + if (ans<0 || ans>cnt) ans = cnt; |
| 71 | + ++l; |
| 72 | + --r; |
| 73 | + } |
| 74 | + l = 0; |
| 75 | + r = W - 1; |
| 76 | + while (l < W){ |
| 77 | + ll cnt = sigma(l) + sigma(r); |
| 78 | + int cpos = 0; |
| 79 | + if (l >= Q){ |
| 80 | + cnt += (l - Q + 1LL)*G; |
| 81 | + cnt += sigma(G - 1); |
| 82 | + cpos = l - Q + G; |
| 83 | + } |
| 84 | + else{ |
| 85 | + int half = G - (G >> 1) - 1; |
| 86 | + if (half > Q - l - 1) half = Q - l - 1; |
| 87 | + cnt += sigma(half) + sigma(G - half - 1); |
| 88 | + cpos = G - half - 1; |
| 89 | + } |
| 90 | + if (cpos >= P){ |
| 91 | + cnt += (cpos - P + 1LL) * B; |
| 92 | + cnt += sigma(B - 1); |
| 93 | + } |
| 94 | + else{ |
| 95 | + int half = B - (B >> 1) - 1; |
| 96 | + if (half > P - cpos - 1) half = P - cpos - 1; |
| 97 | + cnt += sigma(half) + sigma(B - half - 1); |
| 98 | + } |
| 99 | + if (ans<0 || ans>cnt) ans = cnt; |
| 100 | + ++l; |
| 101 | + --r; |
| 102 | + } |
| 103 | + printf("#%d %lld\n",tc,ans); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
0 commit comments