Advertisement
a53

secvcost

a53
Nov 20th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <ctime>
  4. #include <vector>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8.  
  9.  
  10. int genTest(int n, int m, int testNr) {
  11. string inputFile = "input.";
  12. if (testNr < 10)
  13. inputFile.push_back('0');
  14. inputFile += to_string(testNr);
  15. ofstream out(inputFile);
  16.  
  17. vector<int> arr(int(5e6));
  18. for (int i = 0; i < arr.size(); ++i)
  19. arr[i] = i + 1;
  20. for (int i = 1; i < arr.size(); ++i)
  21. swap(arr[i], arr[rand() % i]);
  22.  
  23. for (int i = 0; i < arr.size(); i += 5000) {
  24. for (int nr = 1; nr <= 3; ++nr) {
  25. int p1, p2, p3;
  26. p1 = i + rand() % 4000;
  27. p2 = p1 + rand() % 1000;
  28. p3 = rand() % 2;
  29. if (0 <= p1 && p1 <= p2 && p2 < n) {
  30. sort(arr.begin() + p1, arr.begin() + p2 + 1);
  31. if (p3 == 1)
  32. reverse(arr.begin() + p1, arr.begin() + p2 + 1);
  33. }
  34. }
  35. }
  36.  
  37. out << n << " " << m << "\n";
  38. for (int i = 0; i < n; ++i)
  39. out << arr[i] << " ";
  40. out << "\n";
  41.  
  42. for (int i = 1; i <= m; ++i) {
  43. int type = rand() % 8, x, y;
  44. if (n <= 100) {
  45. x = rand() % n + 1;
  46. y = rand() % n + 1;
  47. if (x > y)
  48. swap(x, y);
  49. } else if (type == 0) {
  50. x = rand() % (n - 100) + 1;
  51. y = x + rand() % 100;
  52. } else if (type == 1) {
  53. x = rand() % (n - n / 4 - 10) + 1;
  54. y = x + n / 4 + rand() % 6;
  55. } else if (type == 2) {
  56. x = rand() % (n - n / 3 - 10) + 1;
  57. y = x + n / 3 + rand() % 6;
  58. } else {
  59. x = rand() % (n - n / 2 - 10) + 1;
  60. y = x + n / 2 + rand() % 6;
  61. }
  62. y = min(y, n);
  63. //assert(1 <= x && x <= y && y <= n);
  64. out << x << " " << y << "\n";
  65. }
  66.  
  67. return testNr;
  68. }
  69.  
  70. void testGeneration(int a, int b) {
  71. for (int test = a; test <= b; ++test) {
  72. if (test <= 2) {
  73. genTest(12, 25, test);
  74. continue;
  75. }
  76. if (test <= 5) {
  77. genTest(500, 500, test);
  78. continue;
  79. }
  80. if (test <= 10) {
  81. genTest(10000, 10000, test);
  82. continue;
  83. }
  84. if (test <= 20) {
  85. genTest(200000, 200000, test);
  86. continue;
  87. }
  88. genTest(10, 10, test);
  89. }
  90. }
  91.  
  92. vector<long long> solve(int nrTest) {
  93. string inputFile = "secvcost.in";
  94. if (nrTest >= 0) {
  95. inputFile = "input.";
  96. if (nrTest < 10)
  97. inputFile.push_back('0');
  98. inputFile += to_string(nrTest);
  99. }
  100. ifstream in(inputFile);
  101. vector<long long> ans;
  102.  
  103. int n, m; in >> n >> m;
  104. vector<int> arr(n + 1);
  105. for (int i = 1; i <= n; ++i)
  106. in >> arr[i];
  107.  
  108. vector<int> lefPos(n + 1), rigPos(n + 1), stk;
  109.  
  110. stk.assign(1, 0);
  111. for (int i = 1; i <= n; ++i) {
  112. while (stk.size() != 1 && arr[stk.back()] < arr[i])
  113. stk.pop_back();
  114. lefPos[i] = stk.back() + 1;
  115. stk.push_back(i);
  116. }
  117.  
  118. stk.assign(1, n + 1);
  119. for (int i = n; i >= 1; --i) {
  120. while (stk.size() != 1 && arr[stk.back()] < arr[i])
  121. stk.pop_back();
  122. rigPos[i] = stk.back() - 1;
  123. stk.push_back(i);
  124. }
  125. vector<long long> psm(n + 1);
  126. long long avgLen = 0;
  127. for (int i = 1; i <= n; ++i) {
  128. avgLen += rigPos[i] - lefPos[i] + 1;
  129. psm[i] = psm[i - 1] + 1LL * arr[i] * (rigPos[i] - lefPos[i] + 1);
  130. }
  131. avgLen /= n;
  132. while (m--) {
  133. int x, y; in >> x >> y;
  134. ans.push_back(psm[y] - psm[x - 1]);
  135. }
  136. //cerr << avgLen << endl;
  137. return ans;
  138. }
  139.  
  140. const int DIM = 200005;
  141.  
  142. int arr[DIM];
  143. long long psm[DIM];
  144.  
  145. void solveN3() {
  146. ifstream in("secvcost.in");
  147. ofstream out("secvcost.out");
  148. int n, m;
  149. in >> n >> m;
  150.  
  151. for (int i = 1; i <= n; ++i) {
  152. in >> arr[i];
  153. }
  154. while (m--) {
  155. int x, y;
  156. in >> x >> y;
  157. long long ans = 0;
  158. for (int i = x; i <= y; ++i) {
  159. ans += psm[i];
  160. int p1 = i, p2 = i;
  161. while (p1 > 1 && arr[p1 - 1] <= arr[i])
  162. --p1;
  163. while (p2 < n && arr[p2 + 1] <= arr[i])
  164. ++p2;
  165. ans += 1LL * arr[i] * (p2 - p1 + 1);
  166. }
  167. out << ans << "\n";
  168. }
  169. }
  170. void solveN2() {
  171. ifstream in("secvcost.in");
  172. ofstream out("secvcost.out");
  173. int n, m;
  174. in >> n >> m;
  175.  
  176. for (int i = 1; i <= n; ++i) {
  177. in >> arr[i];
  178. }
  179. for (int i = 1; i <= n; ++i) {
  180. int p1 = i, p2 = i;
  181. while (p1 > 1 && arr[p1 - 1] <= arr[i])
  182. --p1;
  183. while (p2 < n && arr[p2 + 1] <= arr[i])
  184. ++p2;
  185. psm[i] = 1LL * arr[i] * (p2 - p1 + 1);
  186. }
  187. while (m--) {
  188. int x, y;
  189. in >> x >> y;
  190. long long ans = 0;
  191. for (int i = x; i <= y; ++i)
  192. ans += psm[i];
  193. out << ans << "\n";
  194. }
  195. }
  196.  
  197. void solveTest(int test) {
  198. vector<long long> ans = solve(test);
  199. string outputFile = "secvcost.out";
  200. if (test>= 0) {
  201. outputFile = "output.";
  202. if (test < 10)
  203. outputFile.push_back('0');
  204. outputFile += to_string(test);
  205. }
  206. ofstream out(outputFile);
  207. for (long long x : ans)
  208. out << x << "\n";
  209. }
  210.  
  211. int main(void) {
  212. srand(unsigned(time(0)));
  213. //testGeneration(1, 20);
  214. // for (int i = 1; i <= 20; ++i)
  215. // solveTest(i);
  216. solveTest(-1);
  217. return 0;
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement