线段树(区间修改,区间查询)

来源:互联网 发布:网络上终端设备的功能 编辑:程序博客网 时间:2024/05/19 02:18
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define lson (rt<<1)#define rson (rt<<1|1)#define lowbit(x) ((x)&(-(x)))using namespace std;typedef long long LL;const int MAXN=2e5+5;int val[MAXN];struct node{    int tot;    int left,right;    int mark;}tree[MAXN*4];int build(int rt,int left,int right){    tree[rt].mark=0;    tree[rt].left=left;    tree[rt].right=right;    if(left==right) return tree[rt].tot=val[left];    int mid=(left+right)/2;    int a=build(lson,left,mid);    int b=build(rson,mid+1,right);    return tree[rt].tot=a+b;}void update_mark(int rt){    if(tree[rt].mark)    {        tree[rt].tot=tree[rt].mark*(tree[rt].right-tree[rt].left+1);        if(tree[rt].left!=tree[rt].right)            tree[lson].mark=tree[rson].mark=tree[rt].mark;        tree[rt].mark=0;    }}int calc(int rt,int left,int right){    update_mark(rt);    if(tree[rt].left>right||tree[rt].right<left) return 0;    if(left<=tree[rt].left&&tree[rt].right<=right) return tree[rt].tot;    int a=calc(lson,left,right);    int b=calc(rson,left,right);    return a+b;}int update(int rt,int left,int right,int val){    update_mark(rt);    if(tree[rt].left>right||tree[rt].right<left) return tree[rt].tot;    if(tree[rt].left>=left&&tree[rt].right<=right)    {        tree[rt].mark=val;        return tree[rt].tot=val*(tree[rt].right-tree[rt].left+1);    }    int a=update(lson,left,right,val);    int b=update(rson,left,right,val);    return tree[rt].tot=a+b;}int main(){    int T;    scanf("%d",&T);    int cas=0;    while(T--)    {        int n,q;        scanf("%d%d",&n,&q);        for(int i=1;i<=n;++i)        {            val[i]=1;        }        build(1,1,n);        int x,y,z;        for(int i=1;i<=q;++i)        {            scanf("%d%d%d",&x,&y,&z);            update(1,x,y,z);        }        printf("Case %d: The total value of the hook is %d.\n",++cas,calc(1,1,n));    }    return 0;}
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define lson (rt<<1)#define rson (rt<<1|1)#define lowbit(x) ((x)&(-(x)))using namespace std;typedef long long LL;const int MAXN=1e5+5;int val[MAXN];struct node{    LL tot,add;    int left,right;}tree[MAXN*4];void pushup(int rt){    tree[rt].tot=tree[lson].tot+tree[rson].tot;}void pushdown(int rt,int m){    if(tree[rt].add)    {        tree[lson].add+=tree[rt].add;        tree[rson].add+=tree[rt].add;        tree[lson].tot+=(m-(m>>1))*tree[rt].add;        tree[rson].tot+=(m>>1)*tree[rt].add;        tree[rt].add=0;    }}void build(int rt,int left,int right){    tree[rt].add=0;    tree[rt].left=left;    tree[rt].right=right;    if(left==right) {tree[rt].tot=val[left];return;}    int mid=(left+right)>>1;    build(lson,left,mid);    build(rson,mid+1,right);    pushup(rt);}LL query(int rt,int left,int right){    if(left<=tree[rt].left&&tree[rt].right<=right) return tree[rt].tot;    pushdown(rt,tree[rt].right-tree[rt].left+1);    int mid=(tree[rt].left+tree[rt].right)>>1;    LL res=0;    if(left<=mid) res+=query(lson,left,right);    if(right>mid) res+=query(rson,left,right);    return res;}void update(int rt,int left,int right,int val){    if(left<=tree[rt].left&&tree[rt].right<=right)    {        tree[rt].add+=val;        tree[rt].tot+=(LL)val*(tree[rt].right-tree[rt].left+1);        return;    }    pushdown(rt,tree[rt].right-tree[rt].left+1);    int mid=(tree[rt].left+tree[rt].right)>>1;    if(left<=mid) update(lson,left,right,val);    if(right>mid) update(rson,left,right,val);    pushup(rt);}int main(){    int n,q;    while(scanf("%d%d",&n,&q)!=EOF)    {        for(int i=1;i<=n;++i)        {            scanf("%d",&val[i]);        }        build(1,1,n);        char op;        int x,y,z;        for(int i=1;i<=q;++i)        {            getchar();            scanf("%c",&op);            //cout<<"log: "<<op<<endl;            if(op=='Q')            {                scanf("%d%d",&x,&y);                printf("%lld\n",query(1,x,y));            }else            {                scanf("%d%d%d",&x,&y,&z);                update(1,x,y,z);            }        }    }    return 0;}
原创粉丝点击