题目:已知一个数列,你需要进行下面两种操作:1.将某区间每一个数数加上x;2.求出某一个数的和。

解法:树状数组+前缀和优化。数组中每位存和前一位的数的差,这样区间修改只用改两位,单点询问就是求前缀和。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; const int N=;
int n;
int c[N]; int lowbit(int x) {return x&-x;}
void ins(int x,int d)
{
for (int i=x;i<=n;i+=lowbit(i))
c[i]+=d;
}
int query(int x)
{
int h=;
for (int i=x;i>=;i-=lowbit(i))
h+=c[i];
return h;
}
int main()
{
int m,x,y,k;
scanf("%d%d",&n,&m);
y=;
for (int i=;i<=n;i++)
{
scanf("%d",&x);
ins(i,x-y);
y=x;
}
while (m--)
{
scanf("%d",&k);
if (k==)
{
scanf("%d%d%d",&x,&y,&k);
if (x>y) {int t;t=x,x=y,y=t;}
ins(x,k),ins(y+,-k);
}
else
{
scanf("%d",&x);
printf("%d\n",query(x));
}
}
return ;
}

【洛谷 p3368】模板-树状数组 2(数据结构)的更多相关文章

  1. 洛谷.3374.[模板]树状数组1(CDQ分治)

    题目链接 简易CDQ分治教程 //每个操作分解为一个有序数对(t,p),即(时间,操作位置),时间默认有序,用CDQ分治处理第二维 //对于位置相同的操作 修改优先于查询 //时间是默认有序的 所以可 ...

  2. 洛谷 P3688 - [ZJOI2017]树状数组(二维线段树+标记永久化)

    题面传送门 首先学过树状数组的应该都知道,将树状数组方向写反等价于前缀和 \(\to\) 后缀和,因此题目中伪代码的区间求和实质上是 \(sum[l-1...n]-sum[r...n]=sum[l-1 ...

  3. ●洛谷P3688 [ZJOI2017]树状数组

    题链: https://www.luogu.org/problemnew/show/P3688题解: 二维线段树. 先不看询问时l=1的特殊情况. 对于一个询问(l,r),如果要让错误的程序得到正确答 ...

  4. [模板] 树状数组 (C++ class)

    闲来无事(其实是打了两三道树状数组题),写了个树状数组模板…… /* Author: hotwords */ template<typename tp> class BinTree { p ...

  5. HDU 1166 线段树模板&树状数组模板

    HDU1166 上好的线段树模板&&树状数组模板 自己写的第一棵线段树&第一棵树状数组 莫名的兴奋 线段树: #include <cstdio> using nam ...

  6. 【洛谷 p3374】模板-树状数组 1(数据结构)

    题目:已知一个数列,你需要进行下面两种操作:1.将某一个数加上x:2.求出某区间每一个数的和. 解法:树状数组求前缀和. #include<cstdio> #include<cstd ...

  7. POJ2299逆序对模板(树状数组)

    题目:http://poj.org/problem?id=2299 只能相邻两个交换,所以交换一次只会减少一个逆序对.所以交换次数就是逆序对数. ps:原来树状数组还可以记录后边lowbit位的部分和 ...

  8. [洛谷P3384] [模板] 树链剖分

    题目传送门 显然是一道模板题. 然而索引出现了错误,狂wa不止. 感谢神犇Dr_J指正.%%%orz. 建线段树的时候,第44行. 把sum[p]=bv[pos[l]]%mod;打成了sum[p]=b ...

  9. 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】

    题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...

随机推荐

  1. 一致性哈希算法 - consistent hashing

    1 基本场景比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object 映射到 N 个 cache 上呢,你很可能会采用类似下面的通用方法计算 object 的 ...

  2. volley中网络请求

    首先使用Volley类创建 RequestQueue queue = Volley.newRequestQueue(this);  Making GET Requests final String u ...

  3. 关于float /double、string类型的hash函数/hash表实现(转)

    #include <ext/hash_map> #include <math.h> #include <stdio.h> using namespace std; ...

  4. LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium

    题目: Given a , and there exists one unique longest palindromic substring. 解题思路:1.简单思路:暴力破解法,时间复杂度O(n^ ...

  5. IIS7 php wordpress 中文url 标签tag中文URL404解决方法

    新建重写规则: <rule name="ChineseURL" stopProcessing="true"> <match url=" ...

  6. studio 快捷键

    一,基础快捷键 1.1 Ctrl+N,Navigate | Class,快速打开某个类 1.2 Ctrl+Shift+N,  Navigate | File,  快速打开某个文件 1.3 Ctrl+S ...

  7. Linux发行版大全

    基于Debian  Adamantix:基于Debian,特别关注安全.  Amber Linux:基于Debian,针对拉脱维亚用户作了一些定制.  ASLinux Desktop:西班牙语,基于D ...

  8. Ubuntu桌面版本和服务器版本之间的区别(转载)

    转载自:http://blog.csdn.net/fangaoxin/article/details/6335992 http://www.linuxidc.com/Linux/2010-11/297 ...

  9. SQL截取字段字符串的方法

    set @str='WX15-53-H-53-99-15-335-23'; select @str as '字符串' select len(@str) as '字符长度' select charind ...

  10. Java知多少(109)数据库更新

    数据库更新操作包括数据表创建.删除.以及数据表记录的增加.删除.修改等操作.如果利用数据 SQL命令实现,则利用Statement对旬的executeUpdate()方法,执行SQL的update语句 ...