您的位置:首页 > 其它

hdoj 1698 Just a Hook 【线段树 区间修改】【线段树 + lazy思想】

2015-05-15 21:03 441 查看

Just a Hook

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 20406 Accepted Submission(s): 10221



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.



区间修改(702ms):

#include <cstdio>
#include <algorithm>
#define INF 0x7f7f7f7f
#define Max 600000
#include <cstdlib>
using namespace std;
int c[Max];
int setv[Max], sumv[Max];
int _sum;
int a, b, v;//记录查询区间 修改值 
bool flagv[Max];//标记 
void maintain(int o, int L, int R)//维护 
{
    if (flagv[o]) 
    {
        sumv[o] = (R-L+1)*setv[o];
    }
    else if (R > L)
    {
        sumv[o] = sumv[o*2] + sumv[o*2+1];
    } 
}
void build(int o, int L, int R)//建树 
{
    if (L == R) 
    {
        //scanf("%lld", c+o);//输入n个数 
        sumv[o] = setv[o] = c[o] = 1;
    }
    else 
    {
        int M = L + (R-L)/2; 
        build(o<<1, L, M);
        build(o*2+1, M+1, R);
        maintain(o, L, R);
    }
    flagv[o] = false;//置0 
}
inline void pushdown(int o) 
{
    if (flagv[o]) 
    {
        setv[o<<1] = setv[o<<1|1] = setv[o];
        flagv[o<<1] = flagv[o<<1|1] = true;
        flagv[o] = false;
    }
}
void update(int o, int L, int R)//更新 
{
    if (a <= L && b >= R) 
    {
        setv[o] = v;
        flagv[o] = true;
    }
    else 
    {
        int M = L + (R-L)/2; 
        pushdown(o);
        if (a <= M) update(o<<1, L, M); 
        else maintain(o<<1, L, M);
        if (b > M) update(o<<1|1, M+1, R); 
        else maintain(o<<1|1, M+1, R);
    }
    maintain(o, L, R);
}
void query(int o, int L, int R) //查询 
{
    if (flagv[o])
    {
         _sum += setv[o]*(min(b, R) - max(a, L) + 1);
    }
    else if (a <= L && b >= R) 
    {
        _sum += sumv[o];
    }
    else 
    {
        int M = L + (R-L)/2; 
        if (a <= M) query(o<<1, L, M); 
        if (b > M) query(o<<1|1, M+1, R);
    }
}
int main() 
{
    int t, n, m, j=1; 
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d", &n);
        build(1, 1, n);
        scanf("%d", &m);
        while(m--)
        {
            scanf("%d%d%d", &a, &b, &v);
            update(1, 1, n);
        }
        _sum=0;
        a = 1;
        b = n;
        query(1, 1, n);
        printf("Case %d: The total value of the hook is %d.\n", j++, _sum);
    }
    return 0;
}


线段树 + lazy(764ms) :

#include <cstdio>  
#define MAX 100000+10
int col[MAX<<2];  
int sum[MAX<<2];  
void PushUp(int o) 
{  
    sum[o] = sum[o<<1] + sum[o<<1|1];  
}  
void PushDown(int o, int m) 
{  
    if (col[o]) 
    {  
        col[o<<1] = col[o<<1|1] = col[o];  
        sum[o<<1] = (m - (m >> 1)) * col[o];  
        sum[o<<1|1] = (m >> 1) * col[o];  
        col[o] = 0;  
    }  
}  
void build(int o, int l, int r)//建树 
{  
    col[o] = 0;  
    sum[o] = 1;  
    if (l == r) return ;  
    int mid = (l + r) >> 1;  
    build(o<<1,l, mid);  
    build(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) 
    {  
        col[o] = v;  
        sum[o] = v * (r - l + 1);  
        return ;  
    }  
    PushDown(o , r - l + 1);  
    int mid = (l + r) >> 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 query(int o, int l, int r, int L, int R) //查询 
{
    if(L <= l && R >= r)
    {
        return sum[o];
    }
    PushDown(o, r-l+1);
    int mid = (r+l) >> 1;
    int res = 0;
    if(L <= mid) 
    res += query(o<<1, l, mid, L, R);
    if(R > mid)
    res += query(o<<1|1, mid+1 , r, L, R);
    return res; 
}
int main() 
{  
    int t , n , m, j=1;  
    int a, b, v;
    scanf("%d", &t);  
    while(t--)
    {  
        scanf("%d%d", &n, &m);  
        build(1 , 1 , n);  
        while (m --) 
        {  
            scanf("%d%d%d", &a, &b, &v);  
            update(1 , 1 , n, a, b, v);  
        }  
        printf("Case %d: The total value of the hook is %d.\n",j++ , query(1, 1, n, 1, n));  
    }  
    return 0;  
}


还看到一个人用map 做的:

#include<cstdio>
#include<map>
#define N 100001
using namespace std;
map<int,int>m;
map<int,int>::iterator it,jt;
int a
,x
,y
,num
;
int main()
{
	int i,t,tt=0,n,q,ans;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&q);
		m.clear();
		for(i=1;i<=n;i++)
			m[i]=1;
		for(i=0;i<q;i++)
			scanf("%d%d%d",x+i,y+i,num+i);
		for(i=0;i<=n;i++)
			a[i]=1;
		for(i=q-1;i>=0;i--)
			for(it=m.lower_bound(x[i]);it!=m.end();)
				if(it->first<=y[i])
				{
					a[it->first]=num[i];
					jt=it++;
					m.erase(jt);
				}
				else
					break;
		ans=0;
		for(i=1;i<=n;i++)
			ans+=a[i];
		printf("Case %d: The total value of the hook is %d.n",++tt,ans);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: