思路

线段树 区间更新 模板题 注意数据范围

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream> using namespace std;
typedef long long LL; const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6; const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7; LL t, n, q;
LL anssum; struct Node
{
LL l, r;
LL addv, sum;
}tree[maxn << 2]; void maintain(LL id)
{
if (tree[id].l >= tree[id].r)
return;
tree[id].sum = tree[id << 1].sum + tree[id << 1 | 1].sum;
} void pushdown(LL id)
{
if (tree[id].l >= tree[id].r)
return;
if (tree[id].addv)
{
LL tmp = tree[id].addv;
tree[id << 1].addv += tmp;
tree[id << 1 | 1].addv += tmp;
tree[id << 1].sum += (tree[id << 1].r - tree[id << 1].l + 1) * tmp;
tree[id << 1 | 1].sum += (tree[id << 1 | 1].r - tree[id << 1 | 1].l + 1) * tmp;
tree[id].addv = 0;
}
} void build(LL id, LL l, LL r)
{
tree[id].l = l;
tree[id].r = r;
tree[id].addv = 0;
tree[id].sum = 0;
if (l == r)
{
tree[id].sum = 0;
return;
}
LL mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
maintain(id);
} void updateAdd(LL id, LL l, LL r, LL val)
{
if (tree[id].l >= l && tree[id].r <= r)
{
tree[id].addv += val;
tree[id].sum += (tree[id].r - tree[id].l + 1) * val;
return;
}
pushdown(id);
LL mid = (tree[id].l + tree[id].r) >> 1;
if (l <= mid)
updateAdd(id << 1, l, r, val);
if (mid < r)
updateAdd(id << 1 | 1, l, r, val);
maintain(id);
} void query(LL id, LL l, LL r)
{
if (tree[id].l >= l && tree[id].r <= r)
{
anssum += tree[id].sum;
return;
}
pushdown(id);
LL mid = (tree[id].l + tree[id].r) >> 1;
if (l <= mid)
query(id << 1, l, r);
if (mid < r)
query(id << 1 | 1, l, r);
maintain(id);
} int main()
{
scanf("%lld", &t);
while (t--)
{
scanf("%lld %lld", &n, &q);
build(1, 1, n);
LL id;
LL x, y;
LL val;
while (q--)
{
scanf("%lld", &id);
if (id == 0)
{
scanf("%lld %lld %lld", &x, &y, &val);
updateAdd(1, x, y, val);
}
else if (id == 1)
{
scanf("%lld %lld", &x, &y);
anssum = 0;
query(1, x, y);
cout << anssum << endl;
}
}
}
}

SPOJ - HORRIBLE 【线段树】的更多相关文章

  1. SPOJ GSS3 线段树系列1

    SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修 ...

  2. SPOJ - GSS1 —— 线段树 (结点信息合并)

    题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...

  3. SPOJ 2713 线段树(sqrt)

    题意:       给你n个数(n <= 100000),然后两种操作,0 x y :把x-y的数全都sqrt ,1 x y:输出 x-y的和. 思路:       直接线段树更新就行了,对于当 ...

  4. SPOJ COT3 Combat on a tree(Trie树、线段树的合并)

    题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...

  5. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  6. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  7. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  8. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

  9. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

随机推荐

  1. Lucene.Net 介绍

    1 lucene简介1.1 什么是lucenepowered by 25175.netLucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desk ...

  2. FreeBSD Try to set MAKE_JOBS_UNSAFE=yes and rebuild before

    FreeBSD Try to set MAKE_JOBS_UNSAFE=yes and rebuild before,玛德,FreeBSD安装软件就是蛋疼~ mark In some ports th ...

  3. Ordering Tasks UVA - 10305 图的拓扑排序

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  4. javascript simple MVC

    <h3>javascript simple MVC</h3> <div> <select name="" id="setAnim ...

  5. OpenCV中Camshitf算法学习

    今天上午,结合OpenCV自带的camshitf例程,简单的对camshitf有了一个大致的认识和理解,现总结如下: 1:关于HSV H指hue(色相).S指saturation(饱和度).V指val ...

  6. 检测进程不存在自动重启shell脚本

    #!/bin/bash WORKDIR="/usr/local/gse/gseagent" [[ -d $WORKDIR ]] && { if ! ps aux|g ...

  7. 初窥ElasticSearch

    初窥ElasticSearch 官网上面的,不知道讲的是什么.. youtube上面有一个start with,内容是在windows以下跑这个elastic search,然后用一个fidler工具 ...

  8. 使用.NET Reflector 查看Unity引擎里面的DLL文件

    转载标注来源哦! 原地址在这里!在这里. 当你查看unity里面API的时候.是不是有时候追踪了一两步就碰到DLL文件走不下去了呢?非常是不爽吧. 这样的问题我也是常常碰到.这是人家商业引擎不想让你看 ...

  9. Linux之(node.js)服务

    1.1下载源码 你需要在下载最新的Nodejs版本, https://nodejs.org/en/download/ http://nodejs.org/dist/ 现在以node-v7.7.1.ta ...

  10. python 中NumPy和Pandas工具包中的函数使用笔记(方便自己查找)

    二.常用库 1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数据进行快速运算的标准 ...