HDU-1698 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. 
InputThe 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. 
OutputFor 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.

        这个题目是一个线段树的线段查询的题目,他里面所有的操作都是对一个小线段进行操作,和点查询的题目差距很大,昨天写这个题的时候用点查询的方法来建树和查询,导致程序跑到一半就崩溃了,跑不下去。后面翻了翻书才发现,线段查询和点查询的操作不一样,从建树,到修改,再到区间查询的方法全部都得修改。一个晚上就写完了这一道水题,喵大概是废了。这个题目是线段的区间修改,区间查询。所以依旧要用上lazy思想,否则会tle。好不容易写出来题目,然后能运行到最终结果,然后兴奋的提交,wa掉了,整只喵都半崩溃了。后面怀疑是忘记清零了,然后把样例复制粘贴了两遍,发现后面的答案是前面的两倍,主函数加入了一个ans=0就

以下附上题目代码,第一次写这种线段树,代码有些糟糕,求各位巨佬勿喷:

#include
#include
using namespace std;

const int maxn=1e5+10;

struct trees{
    int left;
    int right;
    int value;
    int culmid(){
        return (left+right)>>1;
    }
}tree[maxn<<2];

void build(int x,int y,int num){
    tree[num].left=x;
    tree[num].right=y;
    tree[num].value=1;
    if(x==y){
        return ;
    }
    int mid=tree[num].culmid();
    build(x,mid,num<<1);
    build(mid+1,y,(num<<1)+1);
}

void update(int x,int y,int val,int num){
    if(tree[num].left==x&&tree[num].right==y){
        tree[num].value=val;
        return ;
    }
    if(tree[num].value>=1){
        tree[num<<1].value=tree[num].value;
        tree[(num<<1)+1].value=tree[num].value;
        tree[num].value=-1;
    }
    int mid=tree[num].culmid();
    if(mid>=y)
        update(x,y,val,num<<1);
    else if(mid

你可能感兴趣的:(线段树)