1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include <algorithm> #include <cmath> #include <cstdio> #include <ctime> #include <cstring> #include <iostream> #include <queue> #include <map> #include <set> #include <string> #include <list> #include <forward_list> #include <stack> #include <unordered_set> #include <vector> #include <limits.h>
#define DISPLAY_A 0
using namespace std;
class Node { public: int left, right; int val;
Node(int left, int val, int right) : left(left), val(val), right(right) {}
Node(int left, int val) : left(left), val(val), right(left) {}
Node(int left) : left(left), val(0), right(left) {}
Node() : left(0), val(0), right(1) {}
bool operator<(Node node) const { return this->left < node.left; } };
int arr[100000 + 7];
int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n, m; int l, r, c; map<int, Node> tree; cin >> n; for (int i = 1; i <= n; i++) { cin >> arr[i]; } for (int i = 1; i <= n; i++) { int right = i; int i2 = i + 1; for (; i2 <= n; i2++) { if (arr[i2] != arr[i2 - 1]) { break; } else { right += 1; } } tree[i] = Node(i, arr[i], right); i = i2 - 1; } cin >> m; while (m--) { int ans = 0; cin >> l >> r >> c; auto it = tree.lower_bound(l); auto st = it; if (it->first != l) { st--; if (st->second.val != c) { if (st->second.right > r) { ans += r - l + 1; tree[r + 1] = Node(r + 1, st->second.val, st->second.right); } else { ans += st->second.right - l + 1; } st->second.right = l - 1; } else { l = st->first; if (r < st->second.right) { r = st->second.right; } } } for (; it->second.right <= r && it != tree.end();) { auto &node = it->second; if (node.val != c) { ans += node.right - node.left + 1; } it++; tree.erase(prev(it)); } auto et = it; if (et->first <= r && et != tree.end()) { if (et->second.val != c) { ans += r - et->first + 1; if (et->second.right - et->first > 0) { tree[r + 1] = Node(r + 1, et->second.val, et->second.right); } tree.erase(et); } else { r = et->second.right; tree.erase(et); } } tree[l] = Node(l, c, r); #if DISPLAY_A for (auto itt:tree) { for (int i = itt.second.left; i <= itt.second.right; i++) { cout << itt.second.val << " "; } } #endif cout << ans << endl; } return 0; }
|