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各种操作,,, ...
随机推荐
- 精通jQuery选择器
虽然jQuery上手简单,相比于其他库学习起来较为简单,但是要全面掌握,却不轻松.因为它涉及到网页开发的方方面面,提供的方法和内部变化有上千种之多.初学者常常感到,入门很方便,提高很困难.本文的目标是 ...
- ${pageContext.request.contextPath}无效
发现在Tomcat7.0.58,在jsp页面使用${pageContext.request.contextPath}获取不到项目名称,网上找了很多答案试了都无效: 把Tomcat版本换成Tomcat7 ...
- DTCMS插件的制作实例电子资源管理(一)插件目录结构
总目录 插件目录结构(一) Admin后台页面编写(二) 前台模板页编写(三) URL重写(四) 本实例旨在以一个实际的项目中的例子来介绍如何在dtcms中制作插件,本系列文章非入门教程,部分 ...
- get_post
各种http的请求协议: http://ymiter.iteye.com/blog/1922464 HTTP请求报文和HTTP响应报文 http://www.cnblogs.com/biyeymyhj ...
- Excel导入导出(篇二)
<body> <h3>一.Excel导入</h3> <h5>.模板下载:<a href="UpFiles/TemplateFiles/学 ...
- RHCE认证考试教材
前段时间考RHCE7,顺便给大家分享下RHCE6.7的中文教材!毕竟此书是官方的培训教材,还是值得看看!RHEL6.7承前启后的,给个赞! 下载:http://pan.baidu.com/s/1nu9 ...
- 59-chown 简明笔记
改变文件的所有者或与文件相关联的组 chown [options] owner file-list chown [options] owner: group file-list chown [opti ...
- 4-pwd 打印当前工作目录
pwd print name of current/working directory 打印当前工作目录 [语法]: pwd [选项] [参数] [功能介绍] pwd命令以绝对路径的方式显示用户当前工 ...
- ubuntu完全卸载nginx
删除nginx连带配置文件 sudo apt-get purge nginx # Removes everything. 卸载不再需要的nginx依赖程序 sudo apt-get autoremov ...
- mybatis和ibatis控制台打印sql语句方法
#将ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句 log4j.rootLogger=debug,stdout,logfile### 把日志信息输出到控制 ...