hdu 1698 线段树成段更新
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <queue> 7 #include <vector> 8 9 #define maxn 130050 10 #define lson l,mid,u<<1 11 #define rson mid+1,r,u<<1|1 12 using namespace std; 13 14 const int INF = 0x3f3f3f; 15 16 int seg[maxn<<2]; 17 int pau[maxn<<2]; 18 19 void Push_UP(int u){ 20 seg[u] =seg[u<<1] + seg[u<<1|1]; 21 } 22 void Push_Down(int l,int r,int u){ 23 if(pau[u]){ 24 int mid = (l + r)/2; 25 pau[u<<1] = pau[u<<1|1] = pau[u]; 26 seg[u<<1] = (mid - l + 1) * pau[u]; 27 seg[u<<1|1] = (r - mid) * pau[u]; 28 pau[u] = 0; 29 } 30 } 31 void build(int l,int r,int u){ 32 if(l == r){ 33 seg[u] = 1; 34 return; 35 } 36 int mid = (l + r)/2; 37 build(lson); 38 build(rson); 39 Push_UP(u); 40 } 41 void Update(int L,int R,int num,int l,int r,int u){ 42 if(L <= l && r <= R){ // 这个地方要注意!! 43 pau[u] = num; 44 seg[u] = num * (r - l + 1); 45 return; 46 } 47 Push_Down(l,r,u); 48 int mid = (l + r)/2; 49 if(L <= mid) Update(L,R,num,lson); 50 if(R > mid) Update(L,R,num,rson); 51 Push_UP(u); 52 } 53 54 int main() 55 { 56 if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} 57 int T; 58 cin>>T; 59 for(int t=1;t<=T;t++){ 60 int N,Q; 61 cin>>N; 62 build(1,N,1); 63 cin>>Q; 64 memset(pau,0,sizeof(pau)); 65 while(Q--){ 66 int X,Y,Z; 67 scanf("%d%d%d",&X,&Y,&Z); 68 Update(X,Y,Z,1,N,1); 69 } 70 printf("Case %d: The total value of the hook is %d.\n",t,seg[1]); 71 } 72 }