Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <tuple>
- #include <map>
- using namespace std;
- map<string, tuple<int, int, int>> commands;
- int main() {
- int tc;
- cin >> tc;
- while (tc--) {
- int n, c;
- cin >> n;
- cin >> c;
- while (c--) {
- int type;
- cin >> type;
- if (type == 0) {
- int p, q, v;
- cin >> p;
- cin >> q;
- cin >> v;
- string key = to_string(p) + "#" + to_string(q) + "#" + to_string(c);
- commands[key] = make_tuple(p, q, v);
- } else {
- int p, q;
- cin >> p;
- cin >> q;
- int sum = 0;
- for (auto com: commands) {
- // porque e inclusivo
- int nbr_elements;
- int begin, end, value;
- begin = get<0>(com.second);
- end = get<1>(com.second);
- value = get<2>(com.second);
- if (p > end || q < begin) {
- // se o comeco for maior que o fim
- continue;
- }
- if (p >= begin && q <= end) {
- // numero de elementos dentro desse conjunto.
- sum += (q - p + 1) * value;
- } else if (p < begin && q > end) {
- // o conjunto esta contido dentro do range,
- // ele inteiro entra no resultado final
- sum += (end - begin + 1) * value;
- } else {
- if (p < begin && q <= end) {
- sum += (q - begin + 1) * value;
- }
- if (p >= begin && q > end ) {
- sum += (end - p + 1) * value;
- }
- }
- }
- cout << sum << endl;
- }
- }
- }
- }u
Add Comment
Please, Sign In to add comment