#include <bits/stdc++.h> using namespace std; class Tree{ private: int numLeaves; vector<short>d; void Tadd(int l,int p,short k){ if(l==p)d[l]|=k; else{ if(l&1)d[l++]|=k; if(!(p&1))d[p--]|=k; if(l<=p)Tadd(l/2,p/2,k); } } public: Tree(const int& n){ numLeaves=1; while(numLeaves<n)numLeaves<<=1; d.assign(numLeaves*2,0); } void add(const int& l,const int& p,const short& k){Tadd(l+numLeaves-1,p+numLeaves-1,1<<(k-1));} int wyn(short k=0,int x=1){ k|=d[x]; if(x>=numLeaves)return k==3; if(!(k&4)) return wyn(k,2*x)+wyn(k,2*x+1); return 0; } }; int main(){ int n,m,l,r; short k; cin >> n >> m; Tree tree(n); for(int i=0;i<m;i++){ cin >> l >> r >> k; tree.add(l,r,k); } cout << tree.wyn() << endl; }
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 | #include <bits/stdc++.h> using namespace std; class Tree{ private: int numLeaves; vector<short>d; void Tadd(int l,int p,short k){ if(l==p)d[l]|=k; else{ if(l&1)d[l++]|=k; if(!(p&1))d[p--]|=k; if(l<=p)Tadd(l/2,p/2,k); } } public: Tree(const int& n){ numLeaves=1; while(numLeaves<n)numLeaves<<=1; d.assign(numLeaves*2,0); } void add(const int& l,const int& p,const short& k){Tadd(l+numLeaves-1,p+numLeaves-1,1<<(k-1));} int wyn(short k=0,int x=1){ k|=d[x]; if(x>=numLeaves)return k==3; if(!(k&4)) return wyn(k,2*x)+wyn(k,2*x+1); return 0; } }; int main(){ int n,m,l,r; short k; cin >> n >> m; Tree tree(n); for(int i=0;i<m;i++){ cin >> l >> r >> k; tree.add(l,r,k); } cout << tree.wyn() << endl; } |