POJ3468 A Simple Problem with Integers
Description
给出了一个序列,你需要处理如下两种询问。
"C abc"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。
"Q ab" 询问[a, b]区间中所有值的和。
Input
第一行包含两个整数N, Q。1≤ N,Q ≤ 100000.
第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤1000000000)。
接下来Q行询问,格式如题目描述。
Output
对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
依旧是线段树操作,注意数据范围用long long
//#include<bits/stdc++.h>//POJ不吃这条
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
struct NODE{
int l,r;
long long sum,t;
}node[];
int data[];
int n,m;
void Build(int num,int left,int right){//以num为根建线段树
node[num].l=left;
node[num].r=right;
if(left==right){
node[num].sum=data[left];
return;
}
int mid=(left+right)/;
Build(num*,left,mid);
Build(num*+,mid+,right);
node[num].sum=node[num*].sum+node[num*+].sum;
return;
} void update(int num){//标记下传
node[num<<].t+=node[num].t;
node[num*+].t+=node[num].t;
node[num<<].sum+=(node[num<<].r-node[num<<].l+)*node[num].t;
node[num*+].sum+=(node[num*+].r-node[num*+].l+)*node[num].t;
node[num].t=;
return;
}
long long qu(int l,int r,int num){//求和
if(node[num].r<l || node[num].l>r)return ;
if(node[num].l>=l && node[num].r<=r)return node[num].sum;
if(node[num].t)update(num);
return qu(l,r,num<<)+qu(l,r,num*+);
}
void change(int l,int r,int c,int num){
if(node[num].r<l || node[num].l>r)return;
if(node[num].l>=l && node[num].r<=r){
node[num].sum+=c*(node[num].r-node[num].l+);
node[num].t+=c;
return;
}
if(node[num].t)update(num);
change(l,r,c,num<<);
change(l,r,c,num*+);
node[num].sum=node[num<<].sum+node[num*+].sum;
return;
}
int main(){
scanf("%d%d",&n,&m);
int i,j;
for(i=;i<=n;i++)scanf("%d",&data[i]);
Build(,,n);
char s;
int a,b,c;
for(i=;i<=m;i++)
{
cin>>s;
if(s=='C'){
scanf("%d%d%d",&a,&b,&c);
change(a,b,c,);
}
if(s=='Q'){
scanf("%d%d",&a,&b);
printf("%lld\n",qu(a,b,));
}
}
return ;
}
因为printf里忘了用lld而WA,纠结了半个多小时才意识到问题所在←又犯了这种蠢错误
POJ3468 A Simple Problem with Integers的更多相关文章
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
- [POJ3468] A Simple Problem with Integers (Treap)
题目链接:http://poj.org/problem?id=3468 这题是线段树的题,拿来学习treap. 不旋转的treap. #include <cstdio> #include ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- POJ-3468 A Simple Problem with Integers Splay Tree区间练习
题目链接:http://poj.org/problem?id=3468 以前用线段树做过,现在用Splay Tree A了,向HH.kuangbin.cxlove大牛学习了各种Splay各种操作,,, ...
随机推荐
- 转:openwrt中luci学习笔记
原文地址:openwrt中luci学习笔记 最近在学习OpenWrt,需要在OpenWrt的WEB界面增加内容,本文将讲述修改OpenWrt的过程和其中遇到的问题. 一.WEB界面开发 ...
- ASP.NET Web API Help Pages using Swagger
Understanding the various methods of an API can be a challenge for a developer when building a consu ...
- 让时间处理简单化 【第三方扩展类库org.apache.commons.lang.time】
JAVA的时间日期处理一直是一个比较复杂的问题,大多数程序员都不能很轻松的来处理这些问题.首先Java中关于时间的类,从 JDK 1.1 开始,Date的作用很有限,相应的功能已由Calendar与D ...
- 高性能JavaScript 加载和执行
前言 本章主要讲述如何加载脚本使得用户能有良好的用户体验,而核心内容就是JavaScript的异步加载.之前写过一篇不得不说的JavaScript异步加载,相似的内容就不多加描述,讲些不同的东西,主要 ...
- C#基础之IEnumerable
1.IEnumerable的作用 在使用Linq查询数据时经常以IEnumerable<T>来作为数据查询返回对象,在使用foreach进行遍历时需要该对象实现IEnumerable接口, ...
- Bootstrap系列 -- 8. 代码显示
一. Bootstrap中的代码块 代码块一般在博客中使用的较多,比较博客园中提供的贴代码. 在Bootstrap中提供了三种形式的代码显示 1. 使用<code></code> ...
- xml序列化及反序列化.net对象
序列化一个类通常添加[XmlRoot("根节点名字")] 找到要序列化的内容 对要序列化的类添加 [Serializable]属性用于序列化 对于要序列化的字段添加 [XmlEl ...
- [AJAX系列]XMLHttpRequest请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- canvas缓动
通过不断地将与目标的距离和系数相乘来让物体实现远快近缓的运动. 如图所示可以做出缓动效果,具体代码如下 var canvas = document.getElementById("canva ...
- 在Winform中播放视频等【DotNet,C#】
在项目中遇到过这样的问题,就是如何在Winform中播放视频.当时考察了几种方式,第一种是直接使用Windows Media Player组件,这种最简单:第二种是利用DirectX直接在窗体或者控件 ...