POJ3468---线段树模版--A Simple Problem with Integers

#include<stdio.h>
#include<algorithm>
#include<string.h>
#define ll __int64
#define M 100007
using namespace std;
struct node
{
    ll l,r,mid,val,mark;
}tree[M<<2];
ll s[M];
void build(ll left,ll right,ll i)//建树
{
    tree[i].l=left;tree[i].r=right;
    tree[i].mid=(left+right)>>1;tree[i].mark=0;
    if(left==right){tree[i].val=s[left]; return;}
    build(left,tree[i].mid,i*2);
    build(tree[i].mid+1,right,i*2+1);
    tree[i].val=tree[i*2].val+tree[i*2+1].val;
}
void update(int pos,ll val,int i)//点更新
{
    tree[i].val+=val;
    if(tree[i].l==tree[i].r)return;
    if(pos<=tree[i].mid)update(pos,val,i*2);
    else update(pos,val,i*2+1);
}
//ll query(int left,int right,int i)//点查询
//{
//    if(tree[i].l==left&&tree[i].r==right)return tree[i].val;
//    if(right<=tree[i].mid)query(left,right,i*2);
//    else if(left>tree[i].mid)query(left,right,i*2+1);
//    else return query(left,tree[i].mid,i*2)+query(tree[i].mid+1,right,i*2+1);
//}
void update(int left,int right,ll val,int i)//区间更新
{
    if(tree[i].l==left&&tree[i].r==right){tree[i].mark+=val;return;}
    tree[i].val+=val*(right-left+1);
    if(tree[i].mid<left)update(left,right,val,2*i+1);
    else if(tree[i].mid>=right)update(left,right,val,2*i);
    else
    {
        update(left,tree[i].mid,val,2*i);
        update(tree[i].mid+1,right,val,2*i+1);
    }
}
ll query(int left,int right,int i)//区间查询
{
    if(tree[i].l==left&&tree[i].r==right)  return tree[i].val+tree[i].mark*(right-left+1);
    if(tree[i].mark!=0)
    {
        tree[i*2].mark+=tree[i].mark;tree[i*2+1].mark+=tree[i].mark;
        tree[i].val+=(tree[i].r-tree[i].l+1)*tree[i].mark;tree[i].mark=0;
    }
    if(tree[i].mid>=right){return query(left,right,i*2);}
    else if(tree[i].mid<left){return query(left,right,i*2+1);}
    else{return query(left,tree[i].mid,i*2)+query(tree[i].mid+1,right,i*2+1);}
}
int main()
{
    ll n,m,i,j,k;
    ll l,f,num;
    char str[5];
    while(scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%I64d",&s[i]);
        build(0,n,1);

        for(i=0;i<m;i++)
        {
          //  printf("i=%I64d",i);
            scanf("%s",&str);
            if(str[0]=='Q')
            {
                scanf("%I64d%I64d",&l,&f);
                printf("%I64d\n",query(l,f,1));
            }
            if(str[0]=='C')
            {
                scanf("%I64d%I64d%I64d",&l,&f,&num);
                update(l,f,num,1);
            }
        }

    }
    return 0;
}
时间: 2024-10-12 13:28:44

POJ3468---线段树模版--A Simple Problem with Integers的相关文章

【POJ3468】【zkw线段树】A Simple Problem with Integers

Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval. In

POJ3468 A Simple Problem with Integers 【线段树】+【成段更新】

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 57666   Accepted: 17546 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ-3468 A Simple Problem with Integers(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 94899   Accepted: 29568 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ-3468 A Simple Problem with Integers(线段树、段变化+段查询、模板)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 116441   Accepted: 36178 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type o

poj3468 A Simple Problem with Integers 线段树区间更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97722   Accepted: 30543 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

POJ3468 A Simple Problem with Integers ( 线段树)

题目: A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 55817   Accepted: 16839 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One typ

A Simple Problem with Integers(POJ-3468)(线段树)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 71540   Accepted: 22049 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 83959   Accepted: 25989 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. Yo

poj3468A Simple Problem with Integers(线段树的区域更新)

http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. 线段树功能:update:成段增减 query:区间求和 #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; #d