POJ3468__A Simple Problem with Integers (线段树)
来源:互联网 发布:c语言编程案例 编辑:程序博客网 时间:2024/06/04 19:52
本文出自blog.csdn.net/svitter
——我大C++的指针岂是尔等能够简单领悟!
题意
- 给N个节点,标号A1~An,然后有Q个操作,操作分为Q i j,查询i,j间的区间和。C i j k,i到j个数字,每个数字增加k,并且输出。
输入输出分析
给N,Q,然后跟操作。注意判断Q,C使用scanf("%s")。
测试数据:
Sample Input
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
算法数据结构分析
线段树基础题目,但是在其上添加了区间加和操作,增加了难度。 如果每个节点直接更新肯定是不行的,可以保存sum,inc(增量)来进行计算。 如果区间正好对应,那么将this.inc += inc。如果区间不是直接对应的,那么sum值增加inc×增加的个数。 之后在进行查询的同时,如果inc的值不是0,可以代入到下一层的inc中,本sum += inc×节点个数。
AC代码:
处理指针的时候出现了些问题,没有写L == R的情况溢出。还有就是r <= mid的情况也没有留意,也造成溢出问题。
最后的问题就是sum一开始忘记inc×个数,直接导致多次WA。
还有就是i从1开始,树状数组也是。
//author: svtter//#include <iostream>#include <stdio.h>#include <string.h>#include <vector>#include <map>#include <algorithm>#include <queue>#include <cmath>#define INF 0xffffff#define lln long long#ifdef ONLINE_JUDGE#define FOI(file) 0#define FOW(file) 0#else#define FOI(file) freopen(file,"r",stdin);#define FOW(file) freopen(file,"w",stdout);#endifusing namespace std;struct CNode{ int L, R; CNode *pLeft, *pRight; lln sum; lln inc; int mid() { return (L+R)/2; }};#define N 300000CNode tree[N];int c = 0;void BuildTree(CNode *root, int l, int r){ root->L = l; root->R = r; root->sum = 0; root->inc = 0; //没写 if(l == r) return; c ++; root->pLeft = tree+c; c ++; root->pRight = tree+c; if(l != r) { BuildTree(root->pLeft, l, (l+r)/2); BuildTree(root->pRight,(l+r)/2+1, r); }}void Insert(CNode *root, int i, int val){ if(root->L == root->R) { root->sum = val; return; } root->sum += val; if(i <= root->mid()) Insert(root->pLeft, i, val); else Insert(root->pRight, i, val);}void Add(CNode *root, int l, int r, lln inc){ if(root->L == l && root->R == r) { root->inc += inc; return; } root->sum += inc *(r-l+1); if(r <= root->mid()) Add(root->pLeft, l, r, inc); else if(l > root->mid()) Add(root->pRight, l, r, inc); else { Add(root->pLeft, l, root->mid(), inc); Add(root->pRight, root->mid()+1, r, inc); }}lln Query(CNode *root, int l, int r, lln inc){ if(root->L == l && root->R == r) return root->sum + (root->inc+inc) * (r-l+1); if(r <= root->mid()) return Query(root->pLeft, l, r, root->inc+inc); else if(l > root->mid()) return Query(root->pRight,l, r, root->inc+inc); else { return Query(root->pLeft, l, root->mid(), root->inc+inc) + Query(root->pRight, root->mid()+1, r, root->inc+inc); }}int main(){ FOI("input"); //FOW("output"); //write your programme here int n, q, m; int i, j, k; int t; char ch[10]; lln ans; CNode *root; scanf("%d%d", &n ,&q); root = tree; c = 0; BuildTree(root, 1, n); for(i = 1; i <=n ;i++) { scanf("%d", &t); Insert(root, i, t); } for(i = 0; i < q; i++) { scanf("%s", ch); if(ch[0] == 'Q') { scanf("%d%d", &j, &k); ans = Query(root, j, k, 0); printf("%lld\n", ans); } else { scanf("%d%d%d", &j, &k, &m); Add(root, j, k, m); } } return 0;}
0 1
- POJ3468__A Simple Problem with Integers (线段树)
- A Simple Problem with Integers(线段树)
- poj3468A Simple Problem with Integers 线段树
- POJ3468A Simple Problem with Integers线段树
- hdu4267A Simple Problem with Integers 线段树
- A Simple Problem with Integers----线段树
- 线段树_poj_3468_A Simple Problem with Integers
- 线段树 A Simple Problem with Integers
- 线段树 A Simple Problem with Integers
- poj3468 Simple Problem with Integers (线段树)
- POJ 3468 A SIMPLE PROBLEM WITH INTEGERS(线段树)
- Poj A Simple Problem with Integers(lazy线段树)
- 【hdu】A Simple Problem with Integers (线段树)
- hdu 4267 A Simple Problem with Integers(线段树)
- pku 3468 A Simple Problem with Integers(线段树)
- poj3468 A Simple Problem with Integers(spaly&&线段树)
- poj 3468 A Simple Problem with Integers ( 线段树 )
- POJ 3468 A Simple Problem with Integers (线段树)
- linux vim编辑器如何取消点亮字符串
- Qemu快照(snapshot)机制原理及关键技术理解
- 自定义LinearLayout 添加click事件
- C语言计算程序持续时间
- 数字证书原理,公钥私钥加密原理
- POJ3468__A Simple Problem with Integers (线段树)
- 查看mysql版本的四种方法
- C语言编译全过程介绍
- spark上的scala学习笔记
- 网络编程浅析(一)
- 让python代码运行的更快
- 剑指offer之寻找丑数,待字闺中之序列生成分析
- ORCFile存储格式
- .net 3.5 HttpRequest 请求的url 网址 如何实现不转义