A Simple Problem with Integers POJ - 3468 区间更新,区间查询板子
Posted smallhester
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A Simple Problem with Integers POJ - 3468 区间更新,区间查询板子相关的知识,希望对你有一定的参考价值。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<math.h> #include<vector> #include<stack> #include<string> #include<stdio.h> using namespace std; typedef long long LL; const int MAXN = 1e5 + 10; LL a[MAXN],st[MAXN << 2]; LL add[MAXN << 2]; void pushup(int o) st[o] = st[o << 1] + st[o << 1 | 1]; void build(int o,int l,int r) if (l == r) st[o] = a[l]; else int m = (l + r) >> 1; build(o << 1, l, m); build(o << 1 | 1, m + 1, r); pushup(o); void pushdown(int o,int l,int r) if(add[o]) add[o << 1] += add[o]; add[o << 1 | 1] += add[o]; int m = (l + r) >> 1; st[o << 1] += add[o] * (m - l + 1); st[o << 1 | 1] += add[o] * (r - m); add[o] = 0; LL query(int o,int l,int r,int ql,int qr) if(ql <= l && r <= qr) return st[o]; pushdown(o,l,r); int m = (l + r) >> 1; LL ans = 0; if(ql <= m) ans += query(o << 1, l, m, ql, qr); if(qr >= m + 1) ans += query(o << 1 | 1, m + 1, r, ql, qr); return ans; void update(int o,int l,int r,int ql,int qr,LL addv) if(ql <= l && r <= qr) add[o] += addv; st[o] += addv * (r - l + 1); return; pushdown(o,l,r); int m = (l + r) >> 1; if(ql <= m) update(o << 1,l,m,ql,qr,addv); if(qr >= m + 1) update(o << 1 | 1,m + 1,r,ql,qr,addv); pushup(o); int main() int n,q; scanf("%d %d",&n,&q); for (int i = 1; i <= n ; i++) scanf("%lld",&a[i]); build(1,1,n); char s[10]; int x,y; LL add; while(q--) scanf("%s",s); if(s[0] == ‘Q‘) scanf("%d %d",&x,&y); LL ans = query(1,1,n,x,y); printf("%lld\n",ans); else scanf("%d %d %lld",&x,&y,&add); update(1,1,n,x,y,add);
以上是关于A Simple Problem with Integers POJ - 3468 区间更新,区间查询板子的主要内容,如果未能解决你的问题,请参考以下文章
POJ3468A Simple Problem with Integers
线段树专题 POJ3468 A Simple Problem with Integers
A Simple Problem with Integers
A Simple Problem with Integers