poj 3467(线段树成段更新)
来源:互联网 发布:长江证券交易软件下载 编辑:程序博客网 时间:2024/06/12 18:13
题目链接:点击打开链接
题意: 略
分析: 可设一另设两个域sum 和d分别表示该区间的当前和与该区间的每个元素的增量,之后维护线段树即可! 再次感慨代码能力、、、、
#include <iostream>using namespace std;__int64 sum1;int a[100005];struct node{int left, right;int d;__int64 sum;}tree[1000000];void creat (int root, int left, int right){tree[root].left = left;tree[root].right = right;tree[root].sum = 0;tree[root].d = 0;if (left == right){tree[root].sum = a[left];return;} int mid = (left+right)/2; creat(2*root, left,mid);creat(2*root+1, mid+1, right);tree[root].sum = tree[2*root].sum + tree[2*root+1].sum;}void insert (int root, int left, int right, int x){ if (tree[root].left == left && tree[root].right == right){tree[root].d += x;return;} tree[root].sum += x*(right -left+1);int mid = (tree[root].left+ tree[root].right)/2; if (left > mid)insert(2*root+1,left, right, x);else if (right <= mid)insert(2*root, left, right, x);else {insert(2*root,left, mid, x);insert(2*root+1, mid+1,right,x);}}void search(int root, int left, int right){ if (tree[root].left == left && tree[root].right == right){ sum1 += (tree[root].sum + (__int64)tree[root].d*(right-left+1)); return;}if (tree[root].d){tree[root].sum +=(__int64)tree[root].d*(tree[root].right-tree[root].left+1);tree[2*root].d += tree[root].d;tree[2*root+1].d += tree[root].d;tree[root].d = 0;}int mid = (tree[root].left + tree[root].right)/2;if (left > mid)search(2*root+1, left, right);else if (right <= mid)search(2*root,left, right);else {search(2*root, left, mid);search(2*root+1, mid+1, right);}}void print(int root){if (tree[root].left == tree[root].right){printf ("%d %d %I64d\n",tree[root].left , tree[root].right , tree[root].sum);return ;}printf ("%d %d %I64d\n", tree[root].left , tree[root].right , tree[root].sum);print(2*root);print(2*root+1);}int main (){int n, m, t, i, l, r;int x;char ch; scanf ("%d%d", &n, &m);for(i = 1; i <= n; i++)scanf("%d", &a[i]);creat (1,1, n);//print(1);for (i = 0; i < m; i++){getchar();scanf("%c",&ch);if (ch == 'C'){scanf("%d%d%d", &l, &r, &x);if (l > r){t = l;l = r;r = t;}insert (1, l, r, x);}else {scanf("%d%d", &l, &r);if (l > r){t = l;l = r;r = t;}sum1 = 0;search(1, l, r);printf ("%I64d\n",sum1);}}return 0;}
- poj 3467(线段树成段更新)
- POJ - 3468 线段树成段更新
- poj 3468 线段树成段更新
- 线段树成段更新 poj 3325
- POJ -- 3468 --线段树成段更新
- poj 2528 线段树成段更新+离散化
- Count Color 线段树成段更新 hoj &poj
- POJ--线段树成段更新--树状数组做法
- 线段树成段更新——POJ 3468
- poj 3264 线段树最值更新
- poj 3468 A Simple Problem with Integers(线段树成段更新)
- poj 2991 Crane(向量旋转+线段树成段更新)
- poj 3667 hotel 线段树成段更新区间合并
- poj 2777 Count Color 线段树成段更新区间统计
- POJ 2528 Mayor posters 线段树成段更新入门题
- POJ 2528 Mayor's posters (线段树成段更新+离散化)
- POJ 3225 Help with Intervals(线段树成段更新)
- POJ 2528 Mayor's posters 线段树成段更新+离散化
- 最值得学习的JAVA技术
- C++ 虚函数表解析
- 在Java中将一个字符串倒序输出
- 如何启动/停止Oracle数据库
- hbase二级索引
- poj 3467(线段树成段更新)
- 野田佳彦
- MySQL触发器的创建与删除
- Infect PE
- hibernate、struts、Spring框架的详解
- CentOS 修改IP地址, DNS, 网关
- 向数据库中导入数据dmp,出现数据为0行的表将导入不进去
- 多线程编程(一):线程创建和退出
- 如何去掉点击链接时周围的虚线框