A Simple Problem with Integers

【题目链接】A Simple Problem with Integers

【题目类型】线段树 成段增减+区间求和

&题解:

线段树 成段增减+区间求和 模板题 这种题真的应该理解并且可以流畅的独立码出来了

【时间复杂度】\(O(nlogn)\)

&代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define PU(x) cout<<#x<<endl;
#define PI(A) cout<<(A)<<endl;
#define DG(x) cout<<#x<<"="<<(x)<<endl;
#define DGG(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
#define DGGG(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
#define PIar(a,n) rep(i,0,n-1)cout<<a[i]<<" ";PU()
#define PIarr(a,n,m) rep(aa,0,n-1){rep(bb,0,m-1)cout<<a[aa][bb]<<" ";PU()}
typedef pair<int, int> pii;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
const double EPS = 1e-9 ;
/* //////////////////////// C o d i n g S p a c e //////////////////////// */ #define lsn b,m,rt<<1
#define rsn m+1,e,rt<<1|1 const int maxn = (int)1e5 + 9 ;
int n,q,a[maxn];
ll seg[maxn<<2],si[maxn<<2];
void puup(int rt) {
seg[rt]=seg[rt<<1]+seg[rt<<1|1];
}
void pudo(int len,int rt) {
if(si[rt]) {
si[rt<<1]+=si[rt];
si[rt<<1|1]+=si[rt];
seg[rt<<1]+=si[rt]*(len-len/2);
seg[rt<<1|1]+=si[rt]*(len/2);
si[rt]=0;
}
}
void build(int b,int e,int rt) {
if(b==e) {
seg[rt]=a[b];
return ;
}
int m=b+e>>1;
build(lsn);
build(rsn);
puup(rt);
}
ll query(int l,int r,int b,int e,int rt) {
if (l<=b&&e<=r) {
return seg[rt];
}
pudo(e-b+1,rt);
int m=b+e>>1;
ll ans=0;
//小于等于m的全在左儿子 大于m的全在右儿子
//如果需要求的区间[l,r]最左边的那个(也就是l) 小于等于m 那么就一定要往左儿子走
//反之 如果区间[l,r]最右边的那个(也就是r) 大于m 那么就一定要走向右儿子
if (l<=m)
ans+=query(l,r,lsn);
if (m<r)
ans+=query(l,r,rsn);
return ans;
}
void upda(int l,int r,ll xx,int b,int e,int rt) {
if (l<=b&&e<=r) {
si[rt]+=xx;
seg[rt]+=(e-b+1)*xx;
return;
}
pudo(e-b+1,rt);
int m=b+e>>1;
if (l<=m)
upda(l,r,xx,lsn);
if (m<r)
upda(l,r,xx,rsn);
puup(rt);
}
void Solve() {
scanf("%d%d",&n,&q);
rep(i,1,n) scanf("%d",&a[i]);
build(1,n,1);
rep(o,1,q) {
char op[9];
int x,y,z;
scanf("%s",op);
if (op[0]=='Q') {
scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y,1,n,1));
}
else {
scanf("%d%d%d",&x,&y,&z);
upda(x,y,z,1,n,1);
}
}
}
int main() {
//iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// int T;cin>>T;while(T--)
Solve();
return 0;
}

POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

  2. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  3. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

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

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

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

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

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

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

随机推荐

  1. Core MIDI and Friends

    http://www.slideshare.net/invalidname/core-midi-and-friends               31 of 31     Core MIDI and ...

  2. LINQ To SQL

    议程 1.LINQ To SQL概述 2.LINQ To SQL对象模型 3.LINQ To SQL查询 用到的数据库 SQL Server 2005,数据库名为Test. 两张表,分别为Studen ...

  3. Deactivate .NET refector

    8.5版本用注册机注册时手快成Standed版本,搞错......,能否Deactivated,发现要联网..... 接下来查找.net reflector 在位置%UserProfile%\AppD ...

  4. WWDC 2014 发布会 Keynote 视频下载 3.6G 1080P地址

    我费尽九牛二虎之力,终于可以下载这个1080P高清的视频了,话说今天凌晨我是看的图文直播,现在终于有视频了,大家会不会很激动啊,好废话不多说,我把下载地址发给大家! 百度云:http://pan.ba ...

  5. WPF学习笔记——依赖属性(Dependency Property)

    1.什么是依赖属性 依赖属性是一种可以自己没有值,并且通过Binding从数据源获得值(依赖在别人身上)的属性,拥有依赖属性的对象被称为"依赖对象". 依赖项属性通过调用 Regi ...

  6. LintCode Subarray Sum

    For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...

  7. js按钮浮动随手指方向移动而移动

    window.document.getElementById("moveDIV").addEventListener("touchmove", function ...

  8. HDU 5446 中国剩余定理+lucas

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  9. Codeforces Round #325 (Div. 2) D bfs

    D. Phillip and Trains time limit per test 1 second memory limit per test 256 megabytes input standar ...

  10. iOS开发~CocoaPods使用详细说明

    一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...