zoukankan      html  css  js  c++  java
  • HDU1698——Just a Hook(线段树区间修改,区间求和)

    In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

    Now Pudge wants to do some operations on the hook.

    Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
    The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

    For each cupreous stick, the value is 1.
    For each silver stick, the value is 2.
    For each golden stick, the value is 3.

    Pudge wants to know the total value of the hook after performing the operations.

    You may consider the original hook is made up of cupreous sticks.

    Input
    The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
    For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.

    Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.

    Output

    For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

    Sample Input

    1
    10
    2
    1 5 2
    5 9 3

    Sample Output

    Case 1: The total value of the hook is 24.

    代码:

    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int MAXN = 100005;
    
    int Tree[MAXN*4];
    int change[MAXN*4];
    
    void Build(int temp,int left,int right){
    	if(left == right){
    		Tree[temp] = 1;
    		return ;
    	}
    	int mid = left + (right-left)/2;
    	Build(temp<<1,left,mid);
    	Build(temp<<1|1,mid+1,right);
    	Tree[temp] = Tree[temp<<1]+Tree[temp<<1|1];
    }
    
    void PushDown(int temp,int left,int right){
    	if(change[temp]){
    		change[temp<<1] = change[temp];
    		change[temp<<1|1] = change[temp];
    		int mid = left + (right-left)/2;
    		Tree[temp<<1] = (mid-left+1)*change[temp];
    		Tree[temp<<1|1] = (right-mid)*change[temp];
    		change[temp] = 0;
    	}
    }
    
    void Updata(int temp,int left,int right,int ql,int qr,int value){
    	if(ql<=left && qr>=right){
    		change[temp] = value;
    		Tree[temp] = value*(right-left+1);
    		return;
    	}
    	
    	PushDown(temp,left,right);
    	
    	int mid = left + (right-left)/2;
    	if(ql<=mid)Updata(temp<<1,left,mid,ql,qr,value);
    	if(qr>mid)Updata(temp<<1|1,mid+1,right,ql,qr,value);
    	
    	Tree[temp] = Tree[temp<<1]+Tree[temp<<1|1];
    }
    
    int query(int temp,int left,int right,int ql,int qr){
    	if(ql>right || qr<left)return 0;
    	if(ql<=left && qr>=right)return Tree[temp];
    	
    	PushDown(temp,left,right);
    	
    	int mid = left + (right-left)/2;
    	int ans = 0;
    	if(ql<=mid)ans += query(temp<<1,left,mid,ql,qr);
    	if(qr>mid)ans += query(temp<<1|1,mid+1,right,ql,qr);
    	return ans;
    }
    
    int main(){
    	
    	int T,N,M;
    	scanf("%d",&T);
    	for(int _=1 ; _<=T ; _++){
    		scanf("%d %d",&N,&M);
    		memset(change,0,sizeof(change));
    		memset(Tree,0,sizeof(Tree));	
    		Build(1,1,N);
    		int A,B,C;
    		while(M--){
    			scanf("%d %d %d",&A,&B,&C);
    			Updata(1,1,N,A,B,C);
    		}
    		printf("Case %d: The total value of the hook is %d.
    ",_,query(1,1,N,1,N));
    	}
    	
    	return 0;
    } 


  • 相关阅读:
    delphi 浮点 精度
    delphi 线程 TTask
    IOS 阻止 锁屏
    grideh SelectedRows Bookmark
    TBluetoothLE.OnDisconnectDevice
    TBluetoothLEDevice.UpdateOnReconnect
    判断字符串为空 为null
    delphi IOS 简单类型转换
    setKeepAliveTimeout
    IOS 后台之长时间任务 beginBackgroundTaskWithExpirationHandler 申请后台十分钟 600秒
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514128.html
Copyright © 2011-2022 走看看