BZOJ3212 Pku3468 A Simple Problem with Integers 题解
题目大意:
一个数列,有两个操作:1.修改操作,将一段区间内的数加上c;2.查询操作,查询一段区间内的数的和。
思路:
线段树裸题,区间修改、区间查询,维护和以及加上的数,由于无序,不需要向下推标记,只需在子树更新完之后更新根节点即可。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; long long sum[],mor[]; void up(int cur,int t)
{
sum[cur]=sum[cur<<]+sum[cur<<|]+t*mor[cur];
} void add(int L,int R,int l,int r,int x,int cur)
{
if (L>=l && R<=r)
{
mor[cur]+=x;
sum[cur]+=(R-L+)*x;
return;
}
int mid=L+R>>;
if (l<=mid) add(L,mid,l,r,x,cur<<);
if (r>mid) add(mid+,R,l,r,x,cur<<|);
up(cur,R-L+);
} long long ask(int L,int R,int l,int r,int cur)
{
if (L>=l && R<=r) return sum[cur];
int mid=L+R>>;
long long ans=mor[cur]*(r-l+);
if (l<=mid) ans+=ask(L,mid,l,min(mid,r),cur<<);
if (r>mid) ans+=ask(mid+,R,max(l,mid+),r,cur<<|);
return ans;
} int main()
{
int n,m,i,a,b,c;
scanf("%d%d",&n,&m);
for (i=;i<=n;i++) scanf("%d",&a),add(,n,i,i,a,);
for (i=;i<=m;i++)
{
char ch=getchar();
while (ch<'A' || ch>'Z') ch=getchar();
if (ch=='Q') scanf("%d%d",&a,&b),printf("%lld\n",ask(,n,a,b,));
else scanf("%d%d%d",&a,&b,&c),add(,n,a,b,c,);
}
return ;
}
BZOJ3212 Pku3468 A Simple Problem with Integers 题解的更多相关文章
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- bzoj3212 Pku3468 A Simple Problem with Integers 线段树
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2046 So ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- bzoj3212 pku3468 A Simple Problem with Integers
一个有初值的数列.区间加.区间查 用线段树直接水过 然而并没有1A,主要是做题太快没看规模结果没注意线段树要用longlong建 卧槽怎么可以这么坑爹,害得我看见wa心慌了,还以为连线段树都要跪 一开 ...
- 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers
线段树入门题…… 因为poj原来的代码莫名RE,所以丧病地写了区间修改的分块…… 其实就是块上打标记,没有上传下传之类. #include<cstdio> #include<cmat ...
- 3212: Pku3468 A Simple Problem with Integers
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1053 So ...
- bzoj 3212 Pku3468 A Simple Problem with Integers
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Description You ...
- bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作
Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2173 Solved: ...
- POJ3468:A Simple Problem with Integers——题解
http://poj.org/problem?id=3468 实现一个线段树,能够做到区间修改和区间查询和. 明显板子题. #include<cstdio> #include<cma ...
随机推荐
- nginx 配一个简单的静态文件服务器 和一个虚似机
下面是个图片服务器: server { listen ; server_name img.xxx.xxx.com; root /data/site/img.xxx.xxx.com; access_lo ...
- HTML5学习之画布和SVG(四)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 【翻译三】java-并发之线程对象和实现
Thread Objects Each thread is associated with an instance of the class Thread. There are two basic s ...
- 如何减少JS的全局变量污染
A,唯一变量 B,闭包
- maven 依赖查询
该文章源地址:http://xiejianglei163.blog.163.com/blog/static/1247276201362733217604/ 为方便个人使用,转载于此处. http:// ...
- hdu 4069 福州赛区网络赛I DLC ***
再遇到一个DLC就刷个专题 #include <stdio.h> #include <string.h> #include <iostream> #include ...
- Java Socket编程(转)
Java Socket编程 对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket.服务端和客户端之间通过Socket建立连接,之后它们就可以进行通信了.首 ...
- 【转】从RGB色转为灰度色算法
----本文摘自作者ZYL910的博客 一.基础 对于彩色转灰度,有一个很著名的心理学公式: Gray = R*0.299 + G*0.587 + B*0.114 二.整数算法 而实际应用时,希望避 ...
- barabasilab-networkScience学习笔记2-图理论
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- ios本地推送
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate //无论程序在 ...