bzoj3155Preprefix sum
题意:
询问一个数组前缀和数组的前缀和,支持单点修改。
题解:
SSi=sigma(i,1,n)(n-i+1)*ai=(n+1)*Si-sigma(i,1,n)i*ai。然后就只要用树状数组维护ai和i*ai的前缀和就行了。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define maxn 100010
#define lb(x) x&-x
#define ll long long
using namespace std; ll v[maxn][],sm[maxn][]; int n,m; char s[];
inline int read(){
char ch=getchar(); int f=,x=;
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();} while(ch>=''&&ch<='')x=x*+ch-'',ch=getchar();
return f*x;
}
void update(int x,ll val,bool b){while(x<=n)sm[x][b]+=val,x+=lb(x);}
ll query(int x,bool b){ll q=; while(x)q+=sm[x][b],x-=lb(x); return q;}
int main(){
n=read(); m=read();
inc(i,,n)v[i][]=(ll)read(),v[i][]=(ll)i*v[i][],update(i,v[i][],),update(i,v[i][],);
inc(i,,m){
scanf("%s",s);
if(s[]=='Q'){int a=read(); printf("%lld\n",query(a,)*(a+)-query(a,));}
if(s[]=='M'){
int a=read(); ll b=(ll)read();
update(a,b-v[a][],); v[a][]=b; update(a,b*a-v[a][],); v[a][]=b*a;
}
}
return ;
}
20160630
bzoj3155Preprefix sum的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
随机推荐
- 2019-02-08 Python学习之Scrapy的简单了解
今天遇到的问题和昨天差不多,一个Scrapy装了好久,anaconda卸了又装,pycharm卸了又装,环境变量配置一堆,依赖包下载一堆.查了一堆资料总算是搞好了. Scripy: 先放个框架结构图( ...
- Github删除分支下所有提交记录
[本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 有时候,我们提交 ...
- JavaWeb网上图书商城完整项目--day02-27.查询所有分类功能之Servlet和Service层
我们在上面实现了数据库层的代码,现在我们来实现业务层和Servlet层的代码:业务层的代码如下: package com.weiyuan.goods.category.service; import ...
- redis基础一
2.修改redis.conf的配置文件有两个地方 a.将daemonize设置成true支持后台启动 b.将redis的数据库文件保存到 下面的目录 3.启动redis服务器 4.操作redis ,给 ...
- 【SpringMVC】
前言
- python高级-闭包-装饰器
闭包内容: 匿名函数:能够完成简单的功能,传递这个函数的引用,只有功能 普通函数:能够完成复杂的功能,传递这个函数的引用,只有功能 闭包:能够完成较为复杂的功能,传递这个闭包中的函数以及数据,因此传递 ...
- Java 反射简介
本文部分内容参考博客.点击链接可以查看原文. 1. 反射的概念 反射是指在运行时将类的属性.构造函数和方法等元素动态地映射成一个个对象.通过这些对象我们可以动态地生成对象实例,调用类的方法和更改类的属 ...
- python解析json文件信息到csv中
json格式多种多样,本代码着重看函数部分 import json, csv, os import pandas as pd josns_root = 'jsons' csvs_root = 'csv ...
- 补充:回答网友的问题,如何不用路径,而直接将CImage画到DC中,之后DC一起显示.
补充:回答网友的问题,如何不用路径,而直接将CImage画到DC中,之后DC一起显示.注释掉 pDC->BeginPath(); // 打开路径层 pDC->Rectangle(0,0,p ...
- 一个比CBitmap更优秀的类 -- CImage类
Visual C++的CBitmap类的功能是比较弱的,它只能显示出在资源中的图标.位图.光标以及图元文件的内容,而不像VB中的Image控件可以显示出绝大多数的外部图像文件(BMP.GIF.JPEG ...