jdoj 2526 农场土地

题目描述:传送门

思路:

线段树区间更新,但是Pushdown函数与标准模板略有不同;
有两种方法:
1:线段树维护区间已经升级为黑土地的个数,最后乘上单价
2:线段树维护区间已经升级为黑土地的总价钱;
然而写push_down(num)时只打了push_down ,为什么没有告诉我编译错误QAQ

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct your
{
    int x,y,add;
    long long sum;
}a[410000];
int k,n,m;
char s[2];
void build(int dx,int dy,int num)
{
    a[num].x=dx,a[num].y=dy;
    if(dx==dy)return ;
    int mid=(dx+dy)>>1;
    build(dx,mid,num<<1);
    build(mid+1,dy,num<<1|1);
}
void push_down(int num)
{
    a[num<<1].add=1;
    a[num<<1].sum=a[num<<1].y-a[num<<1].x+1;
    a[num<<1|1].add=1;
    a[num<<1|1].sum=a[num<<1|1].y-a[num<<1|1].x+1;
    a[num].add=0;
}
void update(int dx,int dy,int num)
{
    if(dx==a[num].x&&a[num].y==dy)
    {
        a[num].add=1;
        a[num].sum=a[num].y-a[num].x+1;
        return;
    }
    if(a[num].add)push_down(num);
    int mid=(a[num].x+a[num].y)>>1;
    if(dy<=mid)update(dx,dy,num<<1);
    else if(dx>mid)update(dx,dy,num<<1|1);
    else
    {
        update(dx,mid,num<<1);
        update(mid+1,dy,num<<1|1);
    }
    a[num].sum=a[num<<1].sum+a[num<<1|1].sum;
}
long long check(int dx,int dy,int num)
{
    if(dx==a[num].x&&a[num].y==dy)return a[num].sum;
    if(a[num].add)push_down(num);
    int mid=(a[num].x+a[num].y)>>1;
    if(dy<=mid) return check(dx,dy,num<<1);
    if(dx>mid) return check(dx,dy,num<<1|1);
    return check(dx,mid,num<<1)+check(mid+1,dy,num<<1|1);   
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    build(1,n,1);
    for(int i=1;i<=m;i++)    
    {
        scanf("%s",&s[0]);
        int x,y;
        scanf("%d%d",&x,&y);
        if(s[0]=='B') update(x,y,1);
        else printf("%lld\n",check(x,y,1)*k);
    }
} 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct your
{
    int x,y,add;
    long long sum;
}a[410000];
int k,n,m;
char s[2];
void build(int dx,int dy,int num)
{
    a[num].x=dx,a[num].y=dy;
    if(dx==dy) return;
    int mid=(dx+dy)>>1;
    build(dx,mid,num<<1);
    build(mid+1,dy,num<<1|1);
}
void push_down(int num)
{
    a[num<<1].add=a[num].add;
    a[num<<1].sum=(a[num<<1].y-a[num<<1].x+1)*a[num].add;
    a[num<<1|1].add=a[num].add;
    a[num<<1|1].sum=(a[num<<1|1].y-a[num<<1|1].x+1)*a[num].add;
    a[num].add=0;
}
void update(int dx,int dy,int val,int num)
{
    if(dx==a[num].x&&a[num].y==dy)
    {
        a[num].add=val;
        a[num].sum=(a[num].y-a[num].x+1)*val;
        return;
    }
    if(a[num].add)push_down(num);
    int mid=(a[num].x+a[num].y)>>1;
    if(dy<=mid)update(dx,dy,val,num<<1);
    else if(dx>mid)update(dx,dy,val,num<<1|1);
    else
    {
        update(dx,mid,val,num<<1);
        update(mid+1,dy,val,num<<1|1);
    }
    a[num].sum=a[num<<1].sum+a[num<<1|1].sum;
}
long long check(int dx,int dy,int num)
{
    if(dx==a[num].x&&a[num].y==dy)return a[num].sum;
    if(a[num].add)push_down(num);
    int mid=(a[num].x+a[num].y)>>1;
    if(dy<=mid) return check(dx,dy,num<<1);
    if(dx>mid) return check(dx,dy,num<<1|1);
    return check(dx,mid,num<<1)+check(mid+1,dy,num<<1|1);   
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    build(1,n,1);
    for(int i=1;i<=m;i++)    
    {
        scanf("%s",&s[0]);
        int x,y;
        scanf("%d%d",&x,&y);
        if(s[0]=='B') update(x,y,k,1);
        else printf("%lld\n",check(x,y,1));
    }
}

 

发表评论

邮箱地址不会被公开。 必填项已用*标注