zoukankan      html  css  js  c++  java
  • POJ(3468)

    A Simple Problem with Integers

    http://poj.org/problem?id=3468

    #include <stdio.h>
    struct node
    {
        int l,r;
        __int64 sum;
        __int64 add;
    }tree[100000*3];
    void build(int left,int right,int root)
    {
        int mid=(left+right)/2;
        if(left==right)
        {
            tree[root].l=left;
            tree[root].r=right;
            tree[root].add=0;
            scanf("%I64d",&tree[root].sum);
            return ;
        }
        tree[root].l=left;
        tree[root].r=right;
        tree[root].add=0;
        build(left,mid,2*root);
        build(mid+1,right,2*root+1);
        tree[root].sum=tree[2*root].sum+tree[2*root+1].sum;
    }
    void modify(int left,int right,__int64 add,int root)
    {
        int mid=(tree[root].l+tree[root].r)/2;
        if(tree[root].l==left&&tree[root].r==right)
        {
            tree[root].sum+=add*(tree[root].r-tree[root].l+1);
            tree[root].add+=add;
            return;
        }
        if(tree[root].add)
        {
            tree[2*root].add+=tree[root].add;
            tree[2*root+1].add+=tree[root].add;
            tree[2*root].sum+=tree[root].add*(tree[2*root].r-tree[2*root].l+1);
            tree[2*root+1].sum+=tree[root].add*(tree[2*root+1].r-tree[2*root+1].l+1);
            tree[root].add=0;
        }
        if(right<=mid)
            modify(left,right,add,2*root);
        else if(left>=mid+1)
            modify(left,right,add,2*root+1);
        else
        {
            modify(left,mid,add,2*root);
            modify(mid+1,right,add,2*root+1);
        }
        tree[root].sum=tree[2*root].sum+tree[2*root+1].sum;
    }
    __int64 search(int left,int right,int root)
    {
        int mid=(tree[root].l+tree[root].r)/2;
        __int64 sum;
        if(left==tree[root].l&&right==tree[root].r)
            return sum=tree[root].sum;
        if(tree[root].add)
        {
            tree[2*root].add+=tree[root].add;
            tree[2*root+1].add+=tree[root].add;
            tree[2*root].sum+=tree[root].add*(tree[2*root].r-tree[2*root].l+1);
            tree[2*root+1].sum+=tree[root].add*(tree[2*root+1].r-tree[2*root+1].l+1);
            tree[root].add=0;
        }
        if(right<=mid)
            return sum=search(left,right,2*root);
        else if(left>=mid+1)
            return sum=search(left,right,2*root+1);
        else
            return sum=search(left,mid,2*root)+search(mid+1,right,2*root+1);
    }
    int main()
    {
        int n,q,i;
        char str[2];
        int a,b;
        __int64 c;
        scanf("%d%d",&n,&q);
        build(1,n,1);
        for(i=1;i<=q;i++)
        {
            scanf("%s%d%d",str,&a,&b);
            if(str[0]=='Q')
            {
                printf("%I64d\n",search(a,b,1));
            }
            else
            {
                scanf("%I64d",&c);
                modify(a,b,c,1);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    ES2017 新特性:Async Functions (异步函数)
    为什么 window.location.search 为空?
    vue-cli 构建Vue项目后的打包和发布
    Vue.js父子组件如何传值 通俗易懂
    vue 面试题(文章末尾还有其他链接)
    vue组件命名和传值 and 父子组件传值
    line-gradient 之渐变角度
    vue-router路由模式
    vue-router 去掉#
    记录vue项目上线遇到的一些问题
  • 原文地址:https://www.cnblogs.com/chiry/p/3261883.html
Copyright © 2011-2022 走看看