POJ 3468 A Simple Problem with Integers
来源:互联网 发布:乐视员工获刑4年 知乎 编辑:程序博客网 时间:2024/06/06 03:00
线段树区间更新求和
#include <map>#include <set>#include <list>#include <cmath>#include<cctype>#include <ctime>#include <deque>#include <stack>#include <queue>#include <cstdio>#include <string>#include <vector>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define LL long long#define PI 3.1415926535897932626using namespace std;int gcd(int a, int b){return a % b == 0 ? b : gcd(b, a % b);}#define MAXN 100003struct node{ int l,r; LL sum; LL mark;//延时标记}tree[MAXN*4];LL src[MAXN];void buildtree(int id,int l ,int r){ tree[id].l=l;tree[id].r=r; tree[id].mark=0; if (l==r) { tree[id].sum=src[l]; return ; } int m=(l+r)/2; buildtree(id*2,l,m); buildtree(id*2+1,m+1,r); tree[id].sum=tree[id*2].sum+tree[id*2+1].sum;}void update(int id ,int l ,int r,LL val){ if (tree[id].l==l && tree[id].r==r) { tree[id].mark+=val; return ; } tree[id].sum+=(r-l+1)*val; int m=(tree[id].l+tree[id].r)/2; if (l>m) update(id*2+1,l,r,val); else if (r<=m) update(id*2,l,r,val); else { update(id*2,l,m,val); update(id*2+1,m+1,r,val); }}LL query(int id ,int l ,int r){ if (tree[id].l==l && tree[id].r==r) { return tree[id].sum+tree[id].mark*(r-l+1);//这里不更新这个节点的sum,延时标记也不变 } if (tree[id].mark!=0) { tree[id*2].mark+=tree[id].mark; tree[id*2+1].mark+=tree[id].mark; tree[id].sum+=tree[id].mark*(tree[id].r-tree[id].l+1); tree[id].mark=0;//清空延时标记, } int m=(tree[id].l+tree[id].r)/2; if (l>m) return query(id*2+1,l,r); else if (r<=m) return query(id*2,l,r); else return query(id*2,l,m)+query(id*2+1,m+1,r);}int main(){ int N,M; scanf("%d%d",&N,&M); for (int i=1;i<=N;i++) scanf("%lld",&src[i]); buildtree(1,1,N); char input[3];int a,b;LL c; for (int i=0;i<M;i++) { scanf("%s",input); if (input[0]=='Q') { scanf("%d%d%*c",&a,&b); //printf("%d %d\n",a,b); printf("%lld\n",query(1,a,b)); } else { scanf("%d%d%lld%*c",&a,&b,&c); update(1,a,b,c); } } return 0;}
0 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
- Dijkstra算法求最短路径
- 砝码称重_DP
- How to run cpp-tests on WIN32
- 简单概率DP——hdu4405
- 关于全局变量和局部变量的一个小陷阱
- POJ 3468 A Simple Problem with Integers
- HTML中<form>表单利用 post 提交与利用 get 提交时的区别
- NEFU 630 数论扩展欧几里得算法的应用
- umlの实现图
- Cocos2d-x3.0 解压zip
- java开发之json工具类
- S3C6410中断控制,基于OK6410A裸机按键中断程序设计
- 如何升级esxi的版本
- Codeforces 416C Booking System