首页 > ACM题库 > HDU-杭电 > HDU 1698 Just a Hook-线段树-[解题报告] C++
2013
12-21

HDU 1698 Just a Hook-线段树-[解题报告] C++

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.

输入:

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.

输出:

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.

样例输入:

1
10
2
1 5 2
5 9 3

样例输出:

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

个人的标准写法。

/*
 线段树+修改区间+询问区间
 */
 #include<stdio.h>
 #include<string.h>
 #include<stdlib.h>
 #include<algorithm>
 #include<iostream>
 #include<queue>
 #include<vector>
 #include<map>
 #include<math.h>
 typedef long long ll;
 //typedef __int64 int64;
 const int maxn = 100005;
 const int maxm = 1005;
 const int inf = 0x7FFFFFFF;
 const double pi = acos(-1.0);
 const double eps = 1e-8;
 using namespace std;
 #define LEFT( x ) (x<<1)
 #define RIGHT( x ) ((x<<1)+1)
 struct node{
     int l,r,sum;
     int flag;
 }tree[ maxn<<2 ];
 int data[ maxn ];
 void pushup( int n ){
     tree[ n ].sum = tree[ LEFT( n ) ].sum+tree[ RIGHT( n ) ].sum;
     return ;
 }
 void pushdown( int n,int m ){//下标为n,这段区间有m个数
     if( tree[ n ].flag!=0 ){
         tree[ LEFT( n ) ].flag = tree[ RIGHT( n ) ].flag = tree[ n ].flag;
         tree[ LEFT( n ) ].sum =  tree[ n ].flag*(m-m/2);//tree[ n ].flag*((m+1)/2);
         tree[ RIGHT( n ) ].sum = tree[ n ].flag*(m/2);//tree[ n ].flag*( m-(m+1)/2 );
         tree[ n ].flag = 0;
     }
     return;
 }
 void build( int l,int r,int n ){
     if( l==r ){
         tree[ n ].sum = data[ l ];
         tree[ n ].flag = 0;
         tree[ n ].l=tree[ n ].r = l;
         return ;
     }
     tree[ n ].flag = 0;
     tree[ n ].l = l;
     tree[ n ].r = r;
     int mid;
     mid = (l+r)>>1;
     build( l,mid,LEFT( n ) );
     build( mid+1,r,RIGHT( n ) );
     pushup( n );
     return;
 }
 void update( int a,int b,int c,int l,int r,int n ){
     if( a==l&&b==r ){
         tree[ n ].flag = c;
         tree[ n ].sum = (r-l+1)*c;
         return ;
     }
     pushdown( n,r-l+1 );
     //printf("test\n");
     int mid;
     mid = (l+r)>>1;
     if( mid>=b ) update( a,b,c,l,mid,LEFT( n ) );
     else if( mid<a ) update( a,b,c,mid+1,r,RIGHT( n ));
     else {
         update( a,mid,c,l,mid,LEFT( n ) );
         update( mid+1,b,c,mid+1,r,RIGHT( n ) );
     }
     pushup( n );
     return ;
 }
 int query( int a,int b,int l,int r,int n ){
     if( a==l&&b==r ){
         return tree[ n ].sum;
     }
     pushdown( n,r-l+1 );
     int mid;
     mid = (l+r)>>1;
     if( mid>=b ) return query( a,b,l,mid,LEFT( n ) );
     else if( mid<a ) return query( a,b,mid+1,r,RIGHT( n ));
     else return query( a,mid,l,mid,LEFT( n ) )+query( mid+1,b,mid+1,r,RIGHT( n ));
 }
 int main(){
     int ca;
     scanf("%d",&ca);
     for( int t=1;t<=ca;t++ ){
         int n;
         scanf("%d",&n);
         for( int i=1;i<=n;i++ ){
             data[ i ] = 1;
         }
         build( 1,n,1 );
         int op;
         scanf("%d",&op);
         while( op-- ){
             int a,b,c;
             scanf("%d%d%d",&a,&b,&c);
             update( a,b,c,1,n,1 );
         }
         printf("Case %d: The total value of the hook is %d.\n",t,tree[ 1 ].sum);
     }
     return 0;
 }

 另一种写法。

/*
 线段树
 成段更新
 */
 #include<stdio.h>
 const int maxn = 100005;
 int sum[ maxn<<2 ];
 int col[ maxn<<2 ];
 #define lson l,mid,rt<<1
 #define rson mid+1,r,rt<<1|1
 
 void PushUp( int rt ){
     sum[ rt ]=sum[ rt<<1 ]+sum[ rt<<1|1 ];
     return ;
 }
 void PushDown( int rt,int m ){
     if( col[ rt ] ){
         col[ rt<<1 ]=col[ rt<<1|1 ]=col[ rt ];
         sum[ rt<<1 ]=(m-m/2)*col[ rt ];
         sum[ rt<<1|1 ]=(m/2)*col[ rt ];
         col[ rt ]=0;
     }
 }        
 
 void build( int l,int r,int rt ){
     sum[ rt ]=1;
     col[ rt ]=0;
     if( l==r ){
         return ;
     }
     int mid;
     mid=(l+r)>>1;
     build( lson );
     build( rson );
     PushUp( rt );
     return ;
 }
 
 void update( int a,int b,int c,int l,int r,int rt ){
     if( a<=l && b>=r ){
         col[ rt ]=c;
         sum[ rt ]=(r-l+1)*c;
         return ;
     }
     PushDown( rt,r-l+1 );
     int mid;
     mid=(l+r)>>1;
     if( a<=mid ) update( a,b,c,lson );
     if( b>mid ) update( a,b,c,rson );
     PushUp( rt );
     return ;
 }
 /*
 int query( int a,int b,int l,int r,int rt ){
     if( a==l && b==r ){
         return sum[ rt ];
     }
     int mid;
     mid=(l+r)>>1;
     int t1,t2;
     if( b<=mid ) return query( a,b,lson );
     else if( a>mid ) return query( a,b,rson );
     else return query( a,mid,lson )+query( mid+1,b,rson );
 }        
 */
 int main(){
     int T;
     scanf("%d",&T);
     for(int i=1;i<=T;i++ ){
         int n;
         scanf("%d",&n);
         build( 1,n,1 );
         int t;
         scanf("%d",&t);
         int a,b,c;
         while( t-- ){
             scanf("%d%d%d",&a,&b,&c);
             update( a,b,c,1,n,1 );
             //for( int j=1;j<=25;j++ )printf("j:%d  %d\n",j,sum[j]);
         }
         printf("Case %d: The total value of the hook is %d.\n",i,sum[ 1 ]/*query( 1,n,1,n,1 )*/ );
     }
     return 0;
 }

 

解题报告转自:http://www.cnblogs.com/justforgl/archive/2013/04/15/3023055.html


  1. 问题3是不是应该为1/4 .因为截取的三段,无论是否能组成三角形, x, y-x ,1-y,都应大于0,所以 x<y,基础应该是一个大三角形。小三角是大三角的 1/4.

  2. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }

  3. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }