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
|
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define Mid(x,y) ((x + y) >> 1) #define Lson(x) (x << 1) #define Rson(x) (x << 1 | 1) #define MAXN 100010
struct Node{ int left; int right; int mark; int sum; }node[MAXN << 2];
void push_up(int pos) { node[pos].sum = node[Lson(pos)].sum + node[Rson(pos)].sum; }
void push_down(int pos) { if(node[pos].mark){ node[Lson(pos)].mark = node[pos].mark; node[Rson(pos)].mark = node[pos].mark; node[Lson(pos)].sum = node[Lson(pos)].mark * (node[Lson(pos)].right - node[Lson(pos)].left + 1); node[Rson(pos)].sum = node[Rson(pos)].mark * (node[Rson(pos)].right - node[Rson(pos)].left + 1); node[pos].mark = 0; } }
void build_tree(int left, int right, int pos) { node[pos].left = left; node[pos].right = right; node[pos].mark = 1; if(left == right){ node[pos].sum = 0; return ; } int mid = Mid(left, right); build_tree(left, mid, Lson(pos)); build_tree(mid + 1, right, Rson(pos)); push_up(pos); }
void update(int left, int right, int value, int pos) { if(left <= node[pos].left && node[pos].right <= right){ node[pos].mark = value; node[pos].sum = value * (node[pos].right - node[pos].left + 1); return ; } push_down(pos); int mid = Mid(node[pos].left, node[pos].right); if(right <= mid) update(left, right, value, Lson(pos)); else if(left > mid) update(left, right, value, Rson(pos)); else{ update(left, mid, value, Lson(pos)); update(mid + 1, right, value, Rson(pos)); } push_up(pos); }
int main() { int T, n, q, x, y, value, Case = 1; scanf("%d", &T); while(T--){ scanf("%d%d", &n, &q); build_tree(1, n, 1); while(q--){ scanf("%d%d%d", &x, &y, &value); update(x, y, value, 1); } printf("Case %d: The total value of the hook is %d.\n" , Case++ , node[1].sum); } return 0; }
|