HDU - 1698 (modified section, the pickup query, the lazy flag update section)

The meaning of problems

Give you a big range, the initial value of the interval are both 1, and then given a series of modification operations, query operation.
Modification operation to modify the value k (1,2,3) on the interval l ~ r, the sum of the values on the query asked how many sections l ~ r.

Explanation

Using the tree line, summed version, but the use of labeled lazy, lazy marker interval only point in line with the update interval. Then in the query in writing before the update down, a lazy update of pushdown. pushdown lazy values obtained utilizing lazy child node, while assigned to (or in addition) a value corresponding to a value range should have two subranges nodes.
Since the update of the subintervals in the range, a lazy re-assignment of the current, even if the front of the lazy also been assigned, but the results after the update subject, thus reducing the update, the lazy effect reached.
In the query, but also need to use range queries, lazy just pushdown down, minimizing unnecessary operations to get the result.

#include <cstdio>
int num[100001*4];
int lazy[100001*4];
void bulit(int l, int r,int ret){
	int m = (l+r)/2;
	lazy[ret] = 0;
	if(r == l){
		num[ret] = 1;
		return ;
	}
	bulit(l, m, ret*2);
	bulit(m+1, r, ret*2+1);
	num[ret] = num[ret*2] + num[ret*2+1];
}
void pushdown(int l, int r, int ret){
	if(lazy[ret]){
		lazy[ret*2] = lazy[ret*2+1] = lazy[ret];
		int m = (l+r)/2;
		num[ret*2] = (m-l+1)*lazy[ret]; 
		num[ret*2+1] = (r-(m+1)+1)*lazy[ret];
		lazy[ret] = 0;
	}
}
void change(int L, int R, int l, int r, int key, int ret){
	int m = (l+r)/2;
	if(L<= l&& r<= R){
		lazy[ret] = key;
		num[ret] = (r-l+1)*key;
		return ;
	}
	pushdown(l, r, ret);
	if(L<= m) change(L, R, l, m, key, ret*2);
	if(R> m) change(L, R, m+1, r, key, ret*2+1);
	num[ret] = num[ret*2] + num[ret*2+1];
}
int ans = 0;
int query(int L, int R, int l, int r, int ret){
	int ans = 0;
	int m = (l+r)/2;
	if(L <= l && r <= R){
        return num[ret];
    }
    pushdown(l, r, ret);
    ans = 0;
    m = (l+r)>>1;
    if(L <= m) ans += query(L, R, l, m, ret*2);
    if(R >= m+1) ans += query(L, R, m+1, r, ret*2+1); 
    return ans;
}
int main(){
	int t;
	int n, m;
	int r, l, v;
	scanf("%d", &t);
	int cont = 1;
	while(t--){	
		scanf("%d %d", &n,&m);
		bulit(1, n, 1);
		for(int i = 0; i < m; i++)
		{
			scanf("%d %d %d", &l, &r, &v);
			change(l, r, 1, n, v,1);
		}
		printf("Case %d: The total value of the hook is %d.\n",cont++, query(1,n,1,n,1));
	}	
	return 0;
} 
Published 52 original articles · won praise 2 · Views 855

Guess you like

Origin blog.csdn.net/qq_44714572/article/details/103933695