poj 3468 A Simple Problem with Integers || 线段树
来源:互联网 发布:中文安卓编程王者荣耀 编辑:程序博客网 时间:2024/06/06 04:09
poj 3468 A Simple Problem with Integers 请戳
题意:
给 n 个数,对这组数用两种方法蹂躏:
* 一种蹂躏方法是给一段区间加上或者减去一个数;
* 一种是给出一段区间的和;
好好玩的样子。。。蹂躏起来思路:
很明显线段数蹂躏大法好,区间修改与查询。小傻子又写了一个非主流版本,虽然ac了,但是时间还是比和上次的非主流版本一样的时间的。不甘心的我从网上找了标准代码交上去,实现的时间居然和我的是一样的。。。然后看看比我用时短1秒的都是用c++交的,而我一直用g++教。。。(太傻了)ps:这次小傻子用了pushdown(),但是写的不好,人又搓(小傻子不是乱叫的)debug,然后和前面的非主流写法对拍了好久才发现bug,诶诶。
复杂度:
时间复杂度:O(n)=nlogn
空间复杂度:O(n)=8n 代码:
非主流写法1
非主流写法2
/* ***********************************************Author :IlovezilianCreated Time :2015/9/5 0:11:25File Name :seg_tree_5.cpp************************************************ *///#include <bits/stdc++.h>#include<cstdio>#include<algorithm>#include<cstring>#define INF 0x7fffffff#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;const int N = 1e5 + 10, mod = 1e9+7;typedef long long LL;LL po[N<<2], add[N<<2];void pushup(int l, int r, int rt){ po[rt] = po[rt<<1] + po[rt<<1|1] + add[rt] * (r - l + 1);}void build(int l, int r, int rt){ add[rt] = 0; if(l == r) { scanf("%I64d", po + rt); return; } int m = (l + r) >> 1; build(lson); build(rson); pushup(l, r, rt);}void update(int val, int L, int R, int l, int r, int rt){ if(L <= l && r <= R) { add[rt] += val; po[rt] += (r - l + 1) * val; return; } int m = (l + r) >> 1; if(L <= m) update(val, L, R, lson); if(R > m) update(val, L, R, rson); pushup(l, r, rt);}LL query(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) { //printf("po(%d,%d) = %I64d\n", l, r, po[rt]); return po[rt]; } int m = (l + r) >> 1; LL ret = 0; if(L <= m) ret += query(L, R, lson) + add[rt] * (min(m - max(l, L) + 1, R - max(l, L) + 1)); if(R > m) ret += query(L, R, rson) + add[rt] * (min(min(R, r) - m, min(R, r) - L + 1)); //printf("po(%d,%d) = %I64d ret = %I64d\n", l, r, po[rt], ret); return ret;}void solve(){ int n, q; while(~scanf("%d%d", &n, &q)) { build(1, n, 1); char s[3]; int a, b, c; for(int i = 0; i < q; i ++) { scanf("%s", s); if(s[0] == 'C') { scanf("%d%d%d", &a, &b, &c); update(c, a, b, 1, n, 1); } else { scanf("%d%d", &a, &b); printf("%I64d\n", query(a, b, 1, n, 1)); } } }}int main(){ //freopen("test.out","r",stdin); //freopen("ac.out","w",stdout); solve(); return 0;}
3. 主流标准写法
#include<cstdio>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long const int maxn = 111111;LL add[maxn<<2], sum[maxn<<2];void pushup(int rt){ sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void pushdown(int rt, int m){ if(add[rt]) { add[rt<<1] += add[rt]; add[rt<<1|1] += add[rt]; sum[rt<<1] += add[rt] * (m - (m >> 1)); sum[rt<<1|1] += add[rt] * (m >> 1); add[rt] = 0; }}void build(int l, int r, int rt){ add[rt] = 0; if(l == r) { scanf("%I64d", sum + rt); return; } int m = (l + r) >> 1; build(lson); build(rson); pushup(rt);}void update(int L, int R, int c, int l, int r, int rt){ if(L <= l && r <= R) { add[rt] += c; sum[rt] += c * (r - l + 1); return; } pushdown(rt, r - l + 1); int m = (l + r) >> 1; if(L <= m) update(L, R, c, lson); if(R > m) update(L, R, c, rson); pushup(rt);}LL query(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) { return sum[rt]; } pushdown(rt, r - l + 1); int m = (l + r) >> 1; LL ret = 0; if(L <= m) ret += query(L, R, lson); if(R > m) ret += query(L, R, rson); return ret;}int main(){ int N, Q; scanf("%d%d", &N, &Q); build(1, N, 1); while( Q --) { char op[2]; int a, b, c; scanf("%s", op); if(op[0] == 'Q') { scanf("%d%d", &a, &b); printf("%I64d\n", query(a, b, 1, N, 1)); } else { scanf("%d%d%d", &a, &b, &c); update(a, b, c, 1, N, 1); } } 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(线段树)
- Linux学习笔记一 ---- Linux基础知识认知以及初识Linux下C编程入门
- eclipse 不断报错:Computing Git status for repository
- 比较与整理GDI与GDIPlus抓取、合并和保存位图的方法
- Linux进程间的通信——管道
- 行人检测(Pedestrian Detection)资源整理
- poj 3468 A Simple Problem with Integers || 线段树
- windows安装配置memcache和memcached
- JS中获取元素位置以及宽高的各种属性整理
- BlockingQueue深入分析
- NetworkOnMainThreadException异常解决办法
- hannuo tower
- android support aar文件中包含jar包
- c++:sprintf应用实例
- Java构造和解析Json数据的两种方法详解一