BZOJ3155: Preprefix sum

题解:

写过树状数组搞区间修改和区间求和的就可以秒出吧。。。

代码:

  1 #include<cstdio>
  2
  3 #include<cstdlib>
  4
  5 #include<cmath>
  6
  7 #include<cstring>
  8
  9 #include<algorithm>
 10
 11 #include<iostream>
 12
 13 #include<vector>
 14
 15 #include<map>
 16
 17 #include<set>
 18
 19 #include<queue>
 20
 21 #include<string>
 22
 23 #define inf 1000000000
 24
 25 #define maxn 200000+5
 26
 27 #define maxm 20000000+5
 28
 29 #define eps 1e-10
 30
 31 #define ll long long
 32
 33 #define pa pair<int,int>
 34
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42
 43 #define mod 1000000007
 44
 45 using namespace std;
 46
 47 inline int read()
 48
 49 {
 50
 51     int x=0,f=1;char ch=getchar();
 52
 53     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
 54
 55     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
 56
 57     return x*f;
 58
 59 }
 60 int n,m;
 61 ll a[maxn],s[maxn][2];
 62 inline void add(int k,int x,ll y)
 63 {
 64     for(;x<=n;x+=x&(-x))s[x][k]+=y;
 65 }
 66 inline ll sum(int k,int x)
 67 {
 68     ll t=0;
 69     for(;x;x-=x&(-x))t+=s[x][k];
 70     return t;
 71 }
 72
 73 int main()
 74
 75 {
 76
 77     freopen("input.txt","r",stdin);
 78
 79     freopen("output.txt","w",stdout);
 80
 81     n=read();m=read();
 82     for1(i,n){ll x=a[i]=read();add(0,i,x);add(1,i,i*x);}
 83     char ch[10];
 84     while(m--)
 85     {
 86         scanf("%s",ch);
 87         if(ch[0]==‘M‘)
 88         {
 89             ll x=read(),y=read();
 90             add(0,x,y-a[x]);add(1,x,x*(y-a[x]));
 91             a[x]=y;
 92         }else
 93         {
 94             ll x=read();
 95             printf("%lld\n",(x+1)*sum(0,x)-sum(1,x));
 96         }
 97     }
 98
 99     return 0;
100
101 }  

题目:http://ch.ezoj.tk/contest/Katharon%20%EF%BC%831/Preprefix%20Sum

感觉bz上的题面怎么不一样。。。233

时间: 2024-08-30 01:53:19

BZOJ3155: Preprefix sum的相关文章

[bzoj3155]Preprefix sum(树状数组)

3155: Preprefix sum Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 1183  Solved: 546[Submit][Status][Discuss] Description Input 第一行给出两个整数N,M.分别表示序列长度和操作个数 接下来一行有N个数,即给定的序列a1,a2,....an 接下来M行,每行对应一个操作,格式见题目描述 Output 对于每个询问操作,输出一行,表示所询问的SSi的值. Sample In

3155: Preprefix sum

3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这题有树状数组! 代码: 线段树620ms 1 /* 2 一个数修改影响后面的数,使后面的数都增加或者减少多少,所以线段树维护区间和,区间修改 3 */ 4 #include<bits/stdc++.h> 5 using namespace std; 6 typedef long long LL;

差分+树状数组【p4868】Preprefix sum

Description 前缀和(prefix sum)\(S_i=\sum_{k=1}^i a_i\). 前前缀和(preprefix sum) 则把\(S_i\)作为原序列再进行前缀和.记再次求得前缀和第i个是\(SS_i\) 给一个长度n的序列\(a_1, a_2, \cdots, a_n\)有两种操作: Modify i x:把\(a_i\)改成\(x\): Query i:查询\(SS_i\) Input 第一行给出两个整数N,M.分别表示序列长度和操作个数 接下来一行有N个数,即给定的

BZOJ 3155 Preprefix sum

两个BIT. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 100050 using namespace std; long long n,m,x,y,t[maxn][3],a[maxn]; char s[12]; long long lowbit(long long x) {return (x&(-x));} void m

BZOJ3155Preprefix sum

3155: Preprefix sum Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 1288  Solved: 588 Description Input 第一行给出两个整数N,M.分别表示序列长度和操作个数 接下来一行有N个数,即给定的序列a1,a2,....an 接下来M行,每行对应一个操作,格式见题目描述 Output 对于每个询问操作,输出一行,表示所询问的SSi的值. Sample Input 5 3 1 2 3 4 5 Query 5

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734

树状数组(下)

树状数组(下) 目录 树状数组(下) 应用 逆序对 康托展开 逆康托展开 RMQ问题树状数组解法 查询第k小 习题 Preprefix sum 在树状数组(上)中我提到了树状数组的基本操作与变式,现在来看看它的实际应用和一些题目. 应用 逆序对 设\(a\)为一个有\(n\)个数字的有序集(\(n>1\)),其中所有数字各不相同. 如果存在正整数\(i\),\(j\)使得\(1\leqslant i<j\leqslant n\)且\(a[i]>a[j]\), 则有序对\((a[i],a[

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T