【hdu】1698 just a hook中国語題意&題解

11134 ワード

Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
题意:1つのシーケンスに、初期値はすべて1で、多くのテストデータがあって、各データシーケンスの长さはnで、mの操作があって、各操作はx,y,cを与えて、[x,y]区间の値をcに変えることを表して、最后にシーケンスの総和の出力フォーマットを寻ねます:Case_1:_The_total_value_of_the_hook_is_num下線はスペースです
题解:とても明らかな線分の木、時間を節約するため、修正の操作はlazyで表記するべきで、ある区間の値がすべてlazyに変えられることを表して、木の細部は多くて、詳しくコードを参照してください
push_up関数は統計サブリーフの値の総和push_であるdown関数はlazy値をサブリーフbuild関数に渡す初期化注意いつでもtre[id].vは区間の値の和を表す
コードは次のとおりです.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#define lson id*2
#define rson id*2+1
#define yy int mid=(l+r)/2
using namespace std;
struct node{int v;int lazy;} tre[900000];
inline void push_up(int id)
{
        tre[id].v=tre[lson].v+tre[rson].v;return ;
}
inline void push_down(int id,int l,int r)
{
        if (l>r) return ;
        if (tre[id].lazy==0)    return ;
        yy;
        tre[lson].lazy=tre[id].lazy;
        tre[rson].lazy=tre[id].lazy;
        tre[lson].v=tre[lson].lazy*(mid+1-l);
        tre[rson].v=tre[rson].lazy*(r-mid);
        tre[id].lazy=0;
        return ;
}
int ans;
inline void add(int id,int l,int r,int L,int R,int val)
{
        if (l>r || L>R) return ;
        if (l>=L && r<=R)
        {
                tre[id].lazy=val;
                tre[id].v=val*(r-l+1);
                return ;
        }
        yy;
        push_down(id,l,r);
        if (L<=mid)     add(lson,l,mid,L,R,val);
        if (R>=mid+1)   add(rson,mid+1,r,L,R,val);
        push_up(id);

}
inline void que(int id,int l,int r,int L,int R)
{
        if (l>r || L>R) return ;
        if (l>=L && R>=r)
        {
                ans+=tre[id].v;
                return ;
        }
        yy;
        push_down(id,l,r);
        if (L<=mid)     que(lson,l,mid,L,R);
        if (R>=mid+1)   que(rson,mid+1,r,L,R);
        push_up(id);
        return;

}



inline void build(int id,int l,int r)
{
        if (l>r) return ;
        if (l==r)       {tre[id].lazy=0;tre[id].v=1;return ;}
        yy;
        tre[id].lazy=0;
        build(lson,l,mid);
        build(rson,mid+1,r);
        push_up(id);
        return ;

}

int a,b,c,d,n,m,t;



int main()
{
cin>>t;
int num=0;
while(num<t)
{
num++;
scanf("%d",&n);

build(1,1,n);

scanf("%d",&m);
for (int i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(1,1,n,a,b,c);
}
cout<<"Case "<<num<<": The total value of the hook is "<<tre[1].v<<"."<<endl;
}
}