POJ 3468 A Simple Problem with Integers 线段树
来源:互联网 发布:java 0xff 编辑:程序博客网 时间:2024/06/02 11:12
线段树模版题。
贴个代码。
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <vector>#include <stack>#include <map>#include <iomanip>#define PI acos(-1.0)#define Max 100005#define inf 1<<28using namespace std;struct kdq{ int l,r; long long num,add;//add为每次更新是增加的值} tree[Max*4];int a[Max];int n,m;long long ans;void build_tree(int l,int r,int u)//建树{ tree[u].l=l,tree[u].r=r; tree[u].add=0; if(l==r) { tree[u].num=a[l]; return ; } int mid=(l+r)/2; build_tree(l,mid,u<<1); build_tree(mid+1,r,(u<<1)+1); tree[u].num=tree[u<<1].num+tree[(u<<1)+1].num;}void query(int left,int right,int u)//询问{ if(left>tree[u].r||right<tree[u].l)//位于树外 { return ; } if(left<=tree[u].l&&right>=tree[u].r)//包含这个节点 { ans+=tree[u].num; return ; } if(tree[u].add)//更新树 { tree[u<<1].num += (tree[u<<1].r-tree[u<<1].l+1)*tree[u].add; tree[u<<1].add += tree[u].add; tree[(u<<1)+1].num += (tree[(u<<1)+1].r-tree[(u<<1)+1].l+1)*tree[u].add; tree[(u<<1)+1].add += tree[u].add; tree[u].add = 0; } query(left,right,u<<1); query(left,right,(u<<1)+1); tree[u].num=tree[u<<1].num+tree[(u<<1)+1].num;}void updata(int left,int right,int u,int k)//更新{ if(left>tree[u].r||right<tree[u].l) { return ; } if(left<=tree[u].l&&right>=tree[u].r) { tree[u].num+=(tree[u].r-tree[u].l+1)*k; tree[u].add+=k; return ; } if(tree[u].add) { tree[u<<1].num += (tree[u<<1].r-tree[u<<1].l+1)*tree[u].add; tree[u<<1].add += tree[u].add; tree[(u<<1)+1].num += (tree[(u<<1)+1].r-tree[(u<<1)+1].l+1)*tree[u].add; tree[(u<<1)+1].add += tree[u].add; tree[u].add = 0; } updata(left,right,u<<1,k); updata(left,right,(u<<1)+1,k); tree[u].num=tree[u<<1].num+tree[(u<<1)+1].num;}int main(){ int i,j,k,l; while(scanf("%d%d",&n,&m)!=EOF) { char op[1]; int x,y; for(i=1; i<=n; i++) scanf("%d",&a[i]); build_tree(1,n,1); while(m--) { scanf("%s",op); if(op[0]=='Q') { ans=0; scanf("%d%d",&x,&y); query(x,y,1); printf("%I64d\n",ans); } else { scanf("%d%d%d",&x,&y,&k); updata(x,y,1,k); } } } return 0;}
- POJ 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers (线段树)
- POJ 3468 A SIMPLE PROBLEM WITH INTEGERS(线段树)
- poj 3468 A Simple Problem with Integers 基础线段树
- POJ 3468 A Simple Problem with Integers 线段树
- POJ 3468 A Simple Problem with Integers【线段树】
- 【线段树】北大 poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers 线段树!!!!
- poj 3468 A Simple Problem with Integers【线段树】
- poj 3468 A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers 线段树
- poj 3468 A Simple Problem with Integers[线段树 ]
- POJ 3468 A Simple Problem with Integers 线段树
- poj 3468 A Simple Problem with Integers(线段树区区)
- 线段树 POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers----线段树
- POJ 3468 A Simple Problem with Integers(线段树)
- poj-3468-A Simple Problem with Integers(线段树)
- html中DOM 操作(js 和 jquey)
- 报告-简单的asp.net 登陆验证配置
- 如何配置Oracle客户端以及sys系统超级管理员无法登录的问题
- ubuntu下淘宝的使用
- Decorator模式
- POJ 3468 A Simple Problem with Integers 线段树
- poj 2762 Going from u to v or from v to u?
- ubuntu 12.04 安装google输入法 输入法图标消失解决办法
- VC右键菜单中显示图片,附源码
- HDU 4366 树转化为连续序列 线段树
- Rhino学习笔记(一)---配置安装和运行第一段javascript代码
- An internal error occurred during: "Launching New_configuration".
- 二叉树两个结点的最低共同父结点
- away3d 搭建(一)