#include <cstdio> int n,m; const int K = 3; struct Tree { int mask; int counts[1<<K]; void mix_leaf(int mask){ this->counts[this->mask]--; this->mask |= mask; this->counts[this->mask]++; } }; const int N = 1 << 20; Tree tree[2*N+2]; void print_tree(int level, int index){ if(index >= 2*N - 1){ return; } print_tree(level + 1, index*2 + 2); for(int i = 0; i < level; ++i){ printf("\t"); } printf("%d %d [", index, tree[index].mask); for(int i = 0; i < (1<<K); ++i){ printf("%d ", tree[index].counts[i]); } printf("]\n"); print_tree(level + 1, index*2 + 1); } void fix_node(int i){ Tree& now = tree[i]; Tree& left = tree[i*2 + 1]; Tree& right = tree[i*2 + 2]; const int mask = now.mask; for(int i = 0; i < (1<<K); ++i){ now.counts[i] = 0; } for(int i = 0; i < (1<<K); ++i){ now.counts[i | mask] += left.counts[i] + right.counts[i]; } } void insert(int l, int r, int k){ int il = l + N - 1 - 1; int ir = r + N - 1 - 1; int mask = 1 << k; //printf("insert mask %d from %d to %d\n", mask, l, r); //printf("%d %d\n", il, ir); tree[il].mix_leaf(mask); tree[ir].mix_leaf(mask); if(il != ir){ if( il % 2 == 1) { tree[il+1].mix_leaf(mask); } if( ir % 2 == 0) { tree[ir-1].mix_leaf(mask); } //printf("%d %d\n", il, ir); il = (il - 1)/2; fix_node(il); ir = (ir - 1)/2; fix_node(ir); //print_tree(0,0); while( (il - 1)/2 != (ir - 1)/2){ // printf("%d %d\n", il, ir); if( il % 2 == 1) { tree[il+1].mask |= mask; fix_node(il+1); } if( ir % 2 == 0) { tree[ir-1].mask |= mask; fix_node(ir-1); } il = (il - 1)/2; fix_node(il); ir = (ir - 1)/2; fix_node(ir); } } int index = (il - 1)/2; while(index){ fix_node(index); index = (index - 1)/2; } fix_node(0); //print_tree(0,0); } void prepare_tree(){ for(int i = N-1; i < 2*N; ++i){ tree[i].counts[0] = 1; } for(int i = N-2; i >=- 0 ; --i){ fix_node(i); } } int main(){ scanf("%d %d", &n, &m); prepare_tree(); //print_tree(0,0); while(m--){ int l, r, k; scanf("%d %d %d", &l, &r, &k); insert(l, r, k - 1); } printf("%d\n", tree[0].counts[3]); }
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 129 130 | #include <cstdio> int n,m; const int K = 3; struct Tree { int mask; int counts[1<<K]; void mix_leaf(int mask){ this->counts[this->mask]--; this->mask |= mask; this->counts[this->mask]++; } }; const int N = 1 << 20; Tree tree[2*N+2]; void print_tree(int level, int index){ if(index >= 2*N - 1){ return; } print_tree(level + 1, index*2 + 2); for(int i = 0; i < level; ++i){ printf("\t"); } printf("%d %d [", index, tree[index].mask); for(int i = 0; i < (1<<K); ++i){ printf("%d ", tree[index].counts[i]); } printf("]\n"); print_tree(level + 1, index*2 + 1); } void fix_node(int i){ Tree& now = tree[i]; Tree& left = tree[i*2 + 1]; Tree& right = tree[i*2 + 2]; const int mask = now.mask; for(int i = 0; i < (1<<K); ++i){ now.counts[i] = 0; } for(int i = 0; i < (1<<K); ++i){ now.counts[i | mask] += left.counts[i] + right.counts[i]; } } void insert(int l, int r, int k){ int il = l + N - 1 - 1; int ir = r + N - 1 - 1; int mask = 1 << k; //printf("insert mask %d from %d to %d\n", mask, l, r); //printf("%d %d\n", il, ir); tree[il].mix_leaf(mask); tree[ir].mix_leaf(mask); if(il != ir){ if( il % 2 == 1) { tree[il+1].mix_leaf(mask); } if( ir % 2 == 0) { tree[ir-1].mix_leaf(mask); } //printf("%d %d\n", il, ir); il = (il - 1)/2; fix_node(il); ir = (ir - 1)/2; fix_node(ir); //print_tree(0,0); while( (il - 1)/2 != (ir - 1)/2){ // printf("%d %d\n", il, ir); if( il % 2 == 1) { tree[il+1].mask |= mask; fix_node(il+1); } if( ir % 2 == 0) { tree[ir-1].mask |= mask; fix_node(ir-1); } il = (il - 1)/2; fix_node(il); ir = (ir - 1)/2; fix_node(ir); } } int index = (il - 1)/2; while(index){ fix_node(index); index = (index - 1)/2; } fix_node(0); //print_tree(0,0); } void prepare_tree(){ for(int i = N-1; i < 2*N; ++i){ tree[i].counts[0] = 1; } for(int i = N-2; i >=- 0 ; --i){ fix_node(i); } } int main(){ scanf("%d %d", &n, &m); prepare_tree(); //print_tree(0,0); while(m--){ int l, r, k; scanf("%d %d %d", &l, &r, &k); insert(l, r, k - 1); } printf("%d\n", tree[0].counts[3]); } |