#include <bits/stdc++.h> using namespace std; const int TREE_SIZE = 1<<20; set <int> tree[2*TREE_SIZE]; void update(int l, int r, int v) { l += TREE_SIZE - 1; r += TREE_SIZE + 1; while(l>>1 != r>>1) { if((l&1) == 0) tree[l+1].insert(v); if((r&1) == 1) tree[r-1].insert(v); l>>=1; r>>=1; } } set <int> query(int x) { x += TREE_SIZE; set <int> r; while(x) { if(!tree[x].empty()) r.insert(tree[x].begin(),tree[x].end()); x>>=1; } return r; } bool czy(int x) { auto s = query(x); return s.size() == 2 && s.count(1) && s.count(2); } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; while(m--) { int l, r, k; cin >> l >> r >> k; update(l,r,k); } int ans = 0; for(int i = 1; i <= n; i++) ans += czy(i); cout << ans; return 0; }
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 | #include <bits/stdc++.h> using namespace std; const int TREE_SIZE = 1<<20; set <int> tree[2*TREE_SIZE]; void update(int l, int r, int v) { l += TREE_SIZE - 1; r += TREE_SIZE + 1; while(l>>1 != r>>1) { if((l&1) == 0) tree[l+1].insert(v); if((r&1) == 1) tree[r-1].insert(v); l>>=1; r>>=1; } } set <int> query(int x) { x += TREE_SIZE; set <int> r; while(x) { if(!tree[x].empty()) r.insert(tree[x].begin(),tree[x].end()); x>>=1; } return r; } bool czy(int x) { auto s = query(x); return s.size() == 2 && s.count(1) && s.count(2); } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; while(m--) { int l, r, k; cin >> l >> r >> k; update(l,r,k); } int ans = 0; for(int i = 1; i <= n; i++) ans += czy(i); cout << ans; return 0; } |