poj3468(线段树)

题目连接:http://poj.org/problem?id=3468

线段树功能:update:成段增减 query:区间求和。

分析:需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define M 1000000000
#define maxn 111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
LL sum[maxn<<2];
LL col[maxn<<2];
void Pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void Pushdown(int rt,int m)
{
    if(col[rt])
    {
        col[rt<<1]+=col[rt];
        col[rt<<1|1]+=col[rt];
        sum[rt<<1]+=col[rt]*(m-(m>>1));
        sum[rt<<1|1]+=col[rt]*(m>>1);
        col[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    col[rt]=0;
    if(l==r)
    {
        scanf("%lld",&sum[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    Pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        col[rt]+=c;
        sum[rt]+=(LL)(r-l+1)*c;
        return;
    }
    Pushdown(rt,r-l+1);
    int m=(l+r)>>1;
    if(L<=m)update(L,R,c,lson);
    if(m<R)update(L,R,c,rson);
    Pushup(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return sum[rt];
    Pushdown(rt,r-l+1);
    int m=(r+l)>>1;
    LL res=0;
    if(L<=m)res+=query(L,R,lson);
    if(R>m)res+=query(L,R,rson);
    return res;
}
int main()
{
    int n,m;
    char op[10];
    while(scanf("%d%d",&n,&m)>0)
    {
        build(1,n,1);
        while(m--)
        {
            int a,b,c;
            scanf("%s",op);
            if(op[0]==‘Q‘)
            {
                scanf("%d%d",&a,&b);
                printf("%lld\n",query(a,b,1,n,1));
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,c,1,n,1);
            }
        }
    }
    return 0;
}

时间: 2024-10-09 22:08:10

poj3468(线段树)的相关文章

poj3468 线段树 or splay

poj3468  裸线段树.因为在熟悉splay 所以就用splay交了一发...开始用的scanf()!==2 居然TLE了...然后我就当单组测试数据做的 然后就过了  囧TZ #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <cmath> using namespac

poj3468 线段树

http://poj.org/problem?id=3468 题目链接, 很经典的线段树的应用, 这里复习一下, 再写一遍, 代码如下: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100000 + 10; int N, Q; int a[maxn]; struct Segment{ int l, r; long long

poj3468 线段树+lazy标记

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92921   Accepted: 28910 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

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. Input The firs

POJ-3468(线段树+区间更新+区间查询)

A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是update函数里面还需要计算sum数组.因为这里查询的时候是直接用sum查询结点. //区间更新,区间查询 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #inc

poj3468(线段树-区间修改)

模板题: #include<cstdio> #include<cstring> #define ll long long const int N = 100000 + 10; ll sum[N << 2]; ll addv[N << 2]; int num[N]; int n,q; void pushUp(int u) { sum[u] = sum[u * 2] + sum[u * 2 + 1]; } void build(int u, int L, int

POJ3468(线段树区间维护)

#include<cstdio> #define lson n<<1,l,mid #define rson (n<<1)|1,mid+1,r #define gmid (a[n].l+a[n].r)>>1 using namespace std; const int MAX_N=100005; typedef long long LL; struct node{ int l,r; LL sum,lazy; }a[MAX_N<<2]; void P

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

POJ3468(KB7-C 线段树)

A Simple Problem with Integers Time Limit: 5000MS  Memory Limit: 131072K Total Submissions: 108903   Accepted: 33919 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