栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3468 A Simple Problem wit...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3468 A Simple Problem wit...

#include <cstring>#include <cstdio>#include <cstdlib>#include <iostream>using namespace std;const    int        maxn = 100001;struct cnode{    int        l, r;    long long nsum, inc;    cnode    *pleft, *pright;};int        n, q, ncount;cnode    tree[maxn * 3];void buildtree(cnode *proot, int s, int e){    proot->l = s;    proot->r = e;    proot->inc = 0;    proot->nsum = 0;    if (s != e)    {        ncount++;        proot->pleft = tree + ncount;        ncount++;        proot->pright = tree + ncount;        buildtree(proot->pleft, s, (s + e) / 2);        buildtree(proot->pright, (s + e) / 2 + 1, e);    }}void insert(cnode *proot, int s, int e, long long increase){    int        mid;    if (s > proot->r || e < proot->l)        return;    s = max(s, proot->l);    e = min(e, proot->r);    mid = (proot->l + proot->r) / 2;    if (s == proot->l && e == proot->r)    {        proot->inc += increase;        return;    }    proot->nsum += (e - s + 1) * increase;    insert(proot->pleft, s, e, increase);    insert(proot->pright, s, e, increase);}long long query(cnode *proot, int s, int e){    int        mid;    if (s > proot->r || e < proot->l)        return 0;    s = max(s, proot->l);    e = min(e, proot->r);    mid = (proot->l + proot->r) / 2;    if (proot->l == s && proot->r == e)        return proot->nsum + proot->inc * (proot->r - proot->l + 1);    long long inc_value = proot->inc * (e - s + 1);    return query(proot->pleft, s, e) + query(proot->pright, s, e) + inc_value;}int main(){    int        i, h, s, e;    char    ch;    scanf("%d%d", &n, &q);    ncount = 0;    buildtree(tree, 1, n);    for (i = 0; i < n; i++)    {        scanf("%d", &h);        insert(tree, i + 1, i + 1, h);    }    for (i = 0; i < q; i++)    {        getchar();        scanf("%c", &ch);        if (ch == 'C')        { scanf("%d%d%d", &s, &e, &h); insert(tree, s, e, h);        }        else        { scanf("%d%d", &s, &e); printf("%lldn", query(tree, s, e));        }    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378012.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号