题目链接

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

思路

线段树 区间更新 模板题 在赋初始值的时候,按点更新区间就可以

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream> using namespace std;
typedef long long LL; const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6; const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7; int n, q;
LL anssum; struct Node
{
LL l, r;
LL addv, sum;
}tree[maxn << 2]; void maintain(int id)
{
if (tree[id].l >= tree[id].r)
return;
tree[id].sum = tree[id << 1].sum + tree[id << 1 | 1].sum;
} void pushdown(int id)
{
if (tree[id].l >= tree[id].r)
return;
if (tree[id].addv)
{
int tmp = tree[id].addv;
tree[id << 1].addv += tmp;
tree[id << 1 | 1].addv += tmp;
tree[id << 1].sum += (tree[id << 1].r - tree[id << 1].l + 1) * tmp;
tree[id << 1 | 1].sum += (tree[id << 1 | 1].r - tree[id << 1 | 1].l + 1) * tmp;
tree[id].addv = 0;
}
} void build(int id, LL l, LL r)
{
tree[id].l = l;
tree[id].r = r;
tree[id].addv = 0;
tree[id].sum = 0;
if (l == r)
{
tree[id].sum = 0;
return;
}
LL mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
maintain(id);
} void updateAdd(int id, LL l, LL r, LL val)
{
if (tree[id].l >= l && tree[id].r <= r)
{
tree[id].addv += val;
tree[id].sum += (tree[id].r - tree[id].l + 1) * val;
return;
}
pushdown(id);
LL mid = (tree[id].l + tree[id].r) >> 1;
if (l <= mid)
updateAdd(id << 1, l, r, val);
if (mid < r)
updateAdd(id << 1 | 1, l, r, val);
maintain(id);
} void query(int id, LL l, LL r)
{
if (tree[id].l >= l && tree[id].r <= r)
{
anssum += tree[id].sum;
return;
}
pushdown(id);
LL mid = (tree[id].l + tree[id].r) >> 1;
if (l <= mid)
query(id << 1, l, r);
if (mid < r)
query(id << 1 | 1, l, r);
maintain(id);
} int main()
{
scanf("%d %d", &n, &q);
build(1, 1, n);
for (LL i = 1; i <= n; i++)
{
LL num;
cin >> num;
updateAdd(1, i, i, num);
}
char id;
LL x, y;
LL val;
while (q--)
{
scanf(" %c", &id);
if (id == 'C')
{
scanf("%lld %lld %lld", &x, &y, &val);
updateAdd(1, x, y, val);
}
else if (id == 'Q')
{
scanf("%lld %lld", &x, &y);
anssum = 0;
query(1, x, y);
cout << anssum << endl;
}
}
}

POJ 3468 A Simple Problem with Integers 【线段树】的更多相关文章

  1. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  2. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  3. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  4. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  5. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

  8. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  9. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  10. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

随机推荐

  1. CentOS上使用Squid+Stunnel搭建代理服务器教程

    这篇文章主要介绍了CentOS上使用Squid+Stunnel搭建代理服务器教程,同时文中也介绍了用户认证的方法,适合于多用户共同使用代理,这种功能在国内用还是比较exciting的~需要的朋友可以参 ...

  2. Eclipse 工作空间(Workspace)

    Eclipse 工作空间(Workspace) eclipse 工作空间包含以下资源: 项目 文件 文件夹 项目启动时一般可以设置工作空间,你可以将其设置为默认工作空间,下次启动后无需再配置: 工作空 ...

  3. SQL.Cookbook 读书笔记4 插入更新和删除

    第四章 插入更新和删除 4.1 插入数据 ,'PROGRA','NEW YOURK'); 4.2 从一个表向另一个表中复制 insert into dept_east(deptno,dname,loc ...

  4. WINDOWS 7.1 SDK 安装失败

    错误提示: Please refer to Samples\Setup\HTML\ConfigDetails.htm document for further information. 原因:本机上安 ...

  5. Java数据类型的转换:隐式(自动)转换与强制转换

    原文链接:http://java.chinaitlab.com/base/725590.html 一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double ...

  6. JS中的动态合集与静态合集

    JS的动态合集 前言 DOM是JavaScript重要组成部分,在DOM中有三个特别的集合分别是NodeList(节点的集合),NamedNodeMap(元素属性的集合)和HTMLCollection ...

  7. 【BZOJ3029】守卫者的挑战 概率+背包

    [BZOJ3029]守卫者的挑战 Description 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过.“我,Nizem, ...

  8. 1624 取余最长路(set)

    1624 取余最长路 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 佳佳有一个n*m的带权矩阵,她想从(1,1)出发走到(n,m)且只能往右往下移动,她能得到的娱 ...

  9. SharePoint服务器端对象模型 之 访问网站和列表数据(Part 4)

    (四)栏/字段 SharePoint中的字段(中文版中叫做"栏")与传统的数据栏类似,也有不同类型的区别,不过SharePoint中内置的栏类型除了按照数据类型(如数字.日期和时间 ...

  10. 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType

    回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...