zoukankan      html  css  js  c++  java
  • hdoj 1698 Just a Hook【线段树区间修改】

    Just a Hook

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 23137    Accepted Submission(s): 11600


    Problem Description
    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.
     题意:输入t代表有t组测试数据,然后一个数字n代表n个英雄,接下来数字q代表接下来有q行,每行三个整数a,b,c,代表将区间a到b的值全部换为c,最后问总分数,注意  初始分数为1
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    #define MAX 100100
    #define INF 0x3f3f3f
    using namespace std;
    int sum[MAX<<2];
    int change[MAX<<2];
    void pushup(int o)
    {
    	sum[o]=sum[o<<1]+sum[o<<1|1];
    }
    void pushdown(int o,int m)
    {
    	if(change[o])
    	{
    		change[o<<1]=change[o<<1|1]=change[o];
    		sum[o<<1]=change[o]*(m-(m>>1));
    		sum[o<<1|1]=change[o]*(m>>1);
    		change[o]=0;
    	}
    }
    void gettree(int o,int l,int r)
    {
    	sum[o]=1;change[o]=0;
    	if(l==r)
    	    return ;
    	int mid=(l+r)>>1;
    	gettree(o<<1,l,mid);
    	gettree(o<<1|1,mid+1,r);
    	pushup(o);
    }
    void update(int o,int l,int r,int L,int R,int v)
    {
    	if(L<=l&&R>=r)
    	{
    		change[o]=v;
    		sum[o]=v*(r-l+1);
    		return ;
    	}
    	pushdown(o,r-l+1);
    	int mid=(r+l)>>1;
    	if(L<=mid)
    	    update(o<<1,l,mid,L,R,v);
    	if(R>mid)
    	    update(o<<1|1,mid+1,r,L,R,v);
        pushup(o);
    }
    int find(int o,int l,int r,int L,int R)
    {
    	if(L<=l&&R>=r)
    	{
    		return sum[o];
    	}
    	pushdown(o,r-l+1);
    	int ans=0;
    	int mid=(r+l)>>1;
    	if(L<=mid)
    	    ans+=find(o<<1,l,mid,L,R);
    	if(R>mid)
    	    ans+=find(o<<1|1,mid+1,r,L,R);
    	return ans;
    }
    int main()
    {
    	int t,k,i,j;
    	int n,m;
    	k=1;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%d%d",&n,&m);
    		int a,b,c;
    		gettree(1,1,n);
    		while(m--)
    		{
    			scanf("%d%d%d",&a,&b,&c);
    			update(1,1,n,a,b,c);
    		}
    		printf("Case %d: The total value of the hook is ",k++);
    		printf("%d.
    ",find(1,1,n,1,n));
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Go中的interface学习
    Go中的命名规范
    Go中的结构体
    Go中的文件读写
    Go包管理工具dep
    SpringBoot中读取配置文件的几种方式
    Go标准库--net/http学习
    centos7通过yum安装docker
    scala之构造器详解
    解决!!-- krb5-libs.x86_64被卸载,yum不能使用,ssh不能连接
  • 原文地址:https://www.cnblogs.com/tonghao/p/4758952.html
Copyright © 2011-2022 走看看