Guest User

Untitled

a guest
Apr 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <tuple>
  3. #include <map>
  4. using namespace std;
  5. map<string, tuple<int, int, int>> commands;
  6.  
  7. int main() {
  8. int tc;
  9. cin >> tc;
  10. while (tc--) {
  11. int n, c;
  12. cin >> n;
  13. cin >> c;
  14. while (c--) {
  15. int type;
  16. cin >> type;
  17. if (type == 0) {
  18. int p, q, v;
  19. cin >> p;
  20. cin >> q;
  21. cin >> v;
  22. string key = to_string(p) + "#" + to_string(q) + "#" + to_string(c);
  23. commands[key] = make_tuple(p, q, v);
  24. } else {
  25. int p, q;
  26. cin >> p;
  27. cin >> q;
  28. int sum = 0;
  29. for (auto com: commands) {
  30. // porque e inclusivo
  31. int nbr_elements;
  32. int begin, end, value;
  33. begin = get<0>(com.second);
  34. end = get<1>(com.second);
  35. value = get<2>(com.second);
  36. if (p > end || q < begin) {
  37. // se o comeco for maior que o fim
  38. continue;
  39. }
  40.  
  41. if (p >= begin && q <= end) {
  42. // numero de elementos dentro desse conjunto.
  43. sum += (q - p + 1) * value;
  44. } else if (p < begin && q > end) {
  45. // o conjunto esta contido dentro do range,
  46. // ele inteiro entra no resultado final
  47. sum += (end - begin + 1) * value;
  48. } else {
  49. if (p < begin && q <= end) {
  50. sum += (q - begin + 1) * value;
  51. }
  52. if (p >= begin && q > end ) {
  53. sum += (end - p + 1) * value;
  54. }
  55. }
  56. }
  57. cout << sum << endl;
  58. }
  59. }
  60. }
  61. }u
Add Comment
Please, Sign In to add comment