【lightoj - 1183 - Computing Fast Average(如此水题,竟然也会错?!)】
Time Limit: 2 second(s) | Memory Limit: 64 MB |
Given an array of integers (0 indexed), you have to perform two types of queries in the array.
- 1 i j v - change the value of the elements from ith index to jth index to v.
- 2 i j - find the average value of the integers from ith index to jth index.
You can assume that initially all the values in the array are 0.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case contains two integers: n (1 ≤ n ≤ 105), q (1 ≤ q ≤ 50000), where n denotes the size of the array. Each of the next q lines will contain a query of the form:
1 i j v (0 ≤ i ≤ j < n, 0 ≤ v ≤ 10000)
2 i j (0 ≤ i ≤ j < n)
Output
For each case, print the case number first. Then for each query of the form '2 i j' print the average value of the integers from i to j. If the result is an integer, print it. Otherwise print the result in 'x/y' form, where x denotes the numerator and y denotes the denominator of the result and x and y are relatively prime.
Sample Input |
Output for Sample Input |
1 10 6 1 0 6 6 2 0 1 1 1 1 2 2 0 5 1 0 3 7 2 0 1 |
Case 1: 6 16/3 7 |
Note
Dataset is huge. Use faster i/o methods.
1 // Project name : 1183 ( Computing Fast Average ) 2 // File name : main.cpp 3 // Author : iCoding 4 // E-mail : honi.linux@gmail.com 5 // Date & Time : Thu Aug 9 15:38:43 2012 6 7 8 #include <iostream> 9 #include <stdio.h> 10 #include <string> 11 #include <cmath> 12 #include <algorithm> 13 using namespace std; 14 15 /*************************************************************************************/ 16 /* data */ 17 18 #ifndef MAXN 19 #define MAXN 100100 20 #endif 21 22 #ifndef CHANGE 23 #define CHANGE 1 24 #endif 25 26 #ifndef QUERY 27 #define QUERY 2 28 #endif 29 30 struct Node 31 { 32 int left; 33 int right; 34 int value; 35 }; 36 37 Node iMap[MAXN*4]; 38 39 int n, k; 40 41 /*************************************************************************************/ 42 /* procedure */ 43 44 void debug() 45 { 46 printf("--debug msg--\n"); 47 } 48 void iCreatMap(int iStart, int iEnd, int iID) 49 { 50 iMap[iID].left = iStart; 51 iMap[iID].right = iEnd; 52 iMap[iID].value = 1; 53 54 if (iStart == iEnd) 55 { 56 return; 57 } 58 else 59 { 60 int iMid = (iStart + iEnd) / 2; 61 iCreatMap(iStart, iMid, iID*2); 62 iCreatMap(iMid+1, iEnd, iID*2+1); 63 } 64 } 65 66 void iChangeValue(int iStart, int iEnd, int iID, int iValue) 67 { 68 if (iMap[iID].left == iStart && iMap[iID].right == iEnd) 69 { 70 iMap[iID].value = iValue; 71 } 72 else 73 { 74 if (iMap[iID].value != -1) 75 { 76 iMap[iID*2] .value = iMap[iID].value; 77 iMap[iID*2+1].value = iMap[iID].value; 78 iMap[iID].value = -1; 79 } 80 int iMid = (iMap[iID].left + iMap[iID].right) / 2; 81 if (iMid >= iEnd) 82 { 83 iChangeValue(iStart, iEnd, iID * 2, iValue); 84 } 85 else if (iMid + 1 <= iStart) 86 { 87 iChangeValue(iStart, iEnd, iID * 2 + 1, iValue); 88 } 89 else 90 { 91 iChangeValue(iStart, iMid, iID * 2, iValue); 92 iChangeValue(iMid + 1, iEnd, iID * 2 + 1, iValue); 93 } 94 } 95 } 96 97 int iQuery(int iStart, int iEnd, int iID) 98 { 99 if (iMap[iID].value != -1) 100 { 101 return (iEnd - iStart + 1) * iMap[iID].value; 102 } 103 else 104 { 105 int iMid = (iMap[iID].left + iMap[iID].right) / 2; 106 if (iMid >= iEnd) 107 { 108 return iQuery(iStart, iEnd, iID * 2); 109 } 110 else if (iMid + 1 <= iStart) 111 { 112 return iQuery(iStart, iEnd, iID * 2 + 1); 113 } 114 else 115 { 116 return iQuery(iStart, iMid, iID * 2) + iQuery(iMid + 1, iEnd, iID * 2 + 1); 117 } 118 } 119 } 120 121 int iGCD(int a, int b) 122 { 123 int tmp; 124 while (b) 125 { 126 tmp = a % b; 127 a = b; 128 b = tmp; 129 } 130 return a; 131 } 132 133 void iShowResult(int a, int b) 134 { 135 int gcd = iGCD(a, b); 136 a /= gcd; 137 b /= gcd; 138 if (b == 1) 139 { 140 printf("%d\n", a); 141 } 142 else 143 { 144 printf("%d/%d\n", a, b); 145 } 146 } 147 /*************************************************************************************/ 148 /* main */ 149 int main() 150 { 151 int iT; 152 scanf("%d", &iT); 153 154 for (int iCaseCount = 1; iCaseCount <= iT; iCaseCount++) 155 { 156 printf("Case %d:\n", iCaseCount); 157 scanf("%d%d", &n, &k); 158 iCreatMap(0, n-1, 1); 159 160 while (k--) 161 { 162 int iInstru; 163 scanf("%d", &iInstru); 164 if (iInstru == CHANGE) 165 { 166 int iStart; 167 int iEnd; 168 int iValue; 169 scanf("%d%d%d", &iStart, &iEnd, &iValue); 170 iChangeValue(iStart, iEnd, 1, iValue); 171 } 172 if (iInstru == QUERY) 173 { 174 int iStart; 175 int iEnd; 176 scanf("%d%d", &iStart, &iEnd); 177 int iSum = iQuery(iStart, iEnd, 1); 178 179 // undone 180 //printf("%d\n", iSum); 181 iShowResult(iSum, iEnd - iStart + 1); 182 } 183 } 184 185 } 186 return 0; 187 } 188 189 // end 190 // Code by Sublime text 2 191 // iCoding@CodeLab
啊,,罪过罪过。。我把和之前做的一个题目给混淆起来了。。。这里默认初始值是0 啊。。而以前做的是1,这么不小心。。。害我还不知道哪里错了。。。又浪费了不少时间啊。。。
贴个代码,认个错吧。
1 // Project name : 1183 ( Computing Fast Average ) 2 // File name : main.cpp 3 // Author : iCoding 4 // E-mail : honi.linux@gmail.com 5 // Date & Time : Thu Aug 9 15:38:43 2012 6 7 8 #include <iostream> 9 #include <stdio.h> 10 #include <string> 11 #include <cmath> 12 #include <algorithm> 13 using namespace std; 14 15 /*************************************************************************************/ 16 /* data */ 17 18 #ifndef MAXN 19 #define MAXN 100100 20 #endif 21 22 #ifndef CHANGE 23 #define CHANGE 1 24 #endif 25 26 #ifndef QUERY 27 #define QUERY 2 28 #endif 29 30 struct Node 31 { 32 int left; 33 int right; 34 int value; 35 }; 36 37 Node iMap[MAXN*4]; 38 39 int n, k; 40 41 /*************************************************************************************/ 42 /* procedure */ 43 44 void debug() 45 { 46 printf("--debug msg--\n"); 47 } 48 void iCreatMap(int iStart, int iEnd, int iID) 49 { 50 iMap[iID].left = iStart; 51 iMap[iID].right = iEnd; 52 iMap[iID].value = 0; 53 54 if (iStart == iEnd) 55 { 56 return; 57 } 58 else 59 { 60 int iMid = (iStart + iEnd) / 2; 61 iCreatMap(iStart, iMid, iID*2); 62 iCreatMap(iMid+1, iEnd, iID*2+1); 63 } 64 } 65 66 void iChangeValue(int iStart, int iEnd, int iID, int iValue) 67 { 68 if (iMap[iID].left == iStart && iMap[iID].right == iEnd) 69 { 70 iMap[iID].value = iValue; 71 } 72 else 73 { 74 if (iMap[iID].value != -1) 75 { 76 iMap[iID*2] .value = iMap[iID].value; 77 iMap[iID*2+1].value = iMap[iID].value; 78 iMap[iID].value = -1; 79 } 80 int iMid = (iMap[iID].left + iMap[iID].right) / 2; 81 if (iMid >= iEnd) 82 { 83 iChangeValue(iStart, iEnd, iID * 2, iValue); 84 } 85 else if (iMid + 1 <= iStart) 86 { 87 iChangeValue(iStart, iEnd, iID * 2 + 1, iValue); 88 } 89 else 90 { 91 iChangeValue(iStart, iMid, iID * 2, iValue); 92 iChangeValue(iMid + 1, iEnd, iID * 2 + 1, iValue); 93 } 94 } 95 } 96 97 int iQuery(int iStart, int iEnd, int iID) 98 { 99 if (iMap[iID].value != -1) 100 { 101 return (iEnd - iStart + 1) * iMap[iID].value; 102 } 103 else 104 { 105 int iMid = (iMap[iID].left + iMap[iID].right) / 2; 106 if (iMid >= iEnd) 107 { 108 return iQuery(iStart, iEnd, iID * 2); 109 } 110 else if (iMid + 1 <= iStart) 111 { 112 return iQuery(iStart, iEnd, iID * 2 + 1); 113 } 114 else 115 { 116 return iQuery(iStart, iMid, iID * 2) + iQuery(iMid + 1, iEnd, iID * 2 + 1); 117 } 118 } 119 } 120 121 int iGCD(int a, int b) 122 { 123 int tmp; 124 while (b) 125 { 126 tmp = a % b; 127 a = b; 128 b = tmp; 129 } 130 return a; 131 } 132 133 void iShowResult(int a, int b) 134 { 135 if (a % b == 0) 136 { 137 printf("%d\n", a / b); 138 } 139 else 140 { 141 int gcd = iGCD(a, b); 142 a /= gcd; 143 b /= gcd; 144 printf("%d/%d\n", a, b); 145 } 146 } 147 /*************************************************************************************/ 148 /* main */ 149 int main() 150 { 151 int iT; 152 scanf("%d", &iT); 153 154 for (int iCaseCount = 1; iCaseCount <= iT; iCaseCount++) 155 { 156 printf("Case %d:\n", iCaseCount); 157 scanf("%d%d", &n, &k); 158 iCreatMap(0, n-1, 1); 159 160 while (k--) 161 { 162 int iInstru; 163 scanf("%d", &iInstru); 164 if (iInstru == CHANGE) 165 { 166 int iStart; 167 int iEnd; 168 int iValue; 169 scanf("%d%d%d", &iStart, &iEnd, &iValue); 170 iChangeValue(iStart, iEnd, 1, iValue); 171 } 172 if (iInstru == QUERY) 173 { 174 int iStart; 175 int iEnd; 176 scanf("%d%d", &iStart, &iEnd); 177 int iSum = iQuery(iStart, iEnd, 1); 178 179 // undone 180 //printf("%d\n", iSum); 181 iShowResult(iSum, iEnd - iStart + 1); 182 } 183 } 184 185 } 186 return 0; 187 } 188 189 // end 190 // Code by Sublime text 2 191 // iCoding@CodeLab