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. Nginx反向代理配置可跨域

    由于业务需要,同一项目中的前端代码放在静态环境中,而后端代码放在tomcat中,但此时问题却出现了:前端使用ajax请求后端获取数据时出现如下报错 XMLHttpRequest cannot load ...

  2. WebView返回时设置Title

    private TextView mWebTitle; private com.tencent.smtt.sdk.WebView mX5Web; ......... if (mX5Web.canGoB ...

  3. Array.splice()理解记忆

    var arr = [0,1,2,3,4,5,6,7,8,9]; arr.splice(0,0,"添加项1"); //arr => ["添加项",0,1, ...

  4. Linux下select&poll&epoll的实现原理(一)

    最近简单看了一把 linux-3.10.25 kernel中select/poll/epoll这个几个IO事件检测API的实现.此处做一些记录.其基本的原理是相同的,流程如下 先依次调用fd对应的st ...

  5. /etc/default/grub 部分配置选项设置

    GRUB_HIDDEN_TIMEOUT=0 此配置将影响菜单显示.若设置此选项,将在此时间内隐藏菜单而显示引导画面. 菜单将会被隐藏,除非在此行开头加上一个 # 符号.(# GRUB_HIDDEN_T ...

  6. MySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 的解决办法和原因

    这两天下载了MySQL5.7.11进行安装,发现到了初次使用输入密码的时候,不管怎样都进不去,即使按照网上说的在mysqld 下面添加skip-grant-tables也是不行,后来研究了两天,终于找 ...

  7. EF Core CodeFirst实践 ( 使用MS SqlServer)

    这里使用 MS SQLSERVER ,网上大多使用 SQLite 先来一个CodeFirst 新建项目 这里我们选择  ASP.NET Core Web Application (.NET Core) ...

  8. touch srceen

    /etc/udev/rules.d touchrules reset

  9. UNITY更新到5后设置的动画无法播放了怎么办

    点击对应的animator,将 apply root motion  这个选项的勾去掉就可以了,纠结了很久最后在UNITY官方论坛找到的答案

  10. 用shebang编写一个ssh自动登陆脚本

    单例模式是软件开发中非常普遍的一种模式.它的主要作用是确保系统中,始终只存在一个类的实例对象. 这样做的好处有两点: 1.对于需要频繁使用的对象,在每次使用时,如果都需要重新创建,并且这些对象的内容都 ...