树状数组模板题 P1904

Description

给定一个长度为n的整数数组A[1]、A[2]、…、A[n](-10^9


Input

第一行包含两个整数n和m,表示数组有n个元素,m表示有m个操作;接下来的一行包含n个整数,第i个整数表示A[i];再接下来的m行,每行表示一个操作。


Output

按输入顺序输出操作2的结果。


Hint

反正要用long long


Solution

嗯,这个题,告诉我们,位运算要打括号。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define maxn 100005
#define lowbit(x) (x&(-x))
using namespace std;
long long c[maxn];
long long n,m,z,u,z2,ans1,ans2,ans;
void doadd(long long x,long long y){
    while(x<=n){
        c[x]=c[x]+y;
        x=x+lowbit(x);
    }
}
long long sets(long long x){
    long long s=0;
    while(x>0){
        s+=c[x];
        x-=lowbit(x);
    }
    return s;
}
int main(){
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&z);
        doadd(i,z);
    }
    for(int j=1;j<=m;j++){
        scanf("%lld",&u);
        if(u==1){
            scanf("%lld%lld",&z2,&z);
            doadd(z2,z);
        }
        else{
            scanf("%lld%lld",&z2,&z);
            ans1=sets(z);
            ans2=sets(z2-1);
            ans=ans1-ans2;
            printf("%lld\n",ans);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/virtual-north-Illya/p/10045147.html

时间: 2024-08-29 01:24:15

树状数组模板题 P1904的相关文章

POJ 3928 Ping pong 树状数组模板题

开始用瓜神说的方法撸了一发线段树,早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #includ

树状数组模板题(特强浓雾

#include<bits/stdc++.h> using namespace std; int a,b; int c[100005]; void add(int x,int y)//树状数组初始化 { while(x<=a+b) c[x]+=y,x+=x&-x;//露馅了噗哈哈哈哈哈哈蛤哈哈哈哈哈哈 } int ask(int x)//询问 { int ans=0; while(x) ans+=c[x],x-=x&-x; return ans; } int main()

hdu 1541 Stars 树状数组模板题

#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int  maxn=15010; const int maxlen=32010; int tree[4*maxn]; int lowbit(int n) { return (n&-n); } int getsum(int i) { int sum=0; while(i>0) { sum+=tree

Light bulbs (树状数组模板题)

There are N light bulbs indexed from 00 to N−1. Initially, all of them are off. A FLIP operation switches the state of a contiguous subset of bulbs. FLIP(L, R)means to flip all bulbs x such that L≤x≤R. So for example, FLIP(3, 5) means to flip bulbs 3

HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)

题目链接 ac代码(注意字符读入前需要注意回车的影响) #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<c

二维树状数组模板(区间修改+区间查询)

二维树状数组模板(区间修改+区间查询) 例题:JOIOI上帝造题的七分钟 一共两种操作: \(L\ x_1\ y_1\ x_2\ y_2\ d\):把\((x_1,y_1)\),\((x_2,y_2)\)这个矩形内所有元素加\(d\). \(k\ x_1\ y_1\ x_2\ y_2\):查询\((x_1,y_1)\),\((x_2,y_2)\)这个矩形内所有元素的和. 代码如下: #include<bits/stdc++.h> #define RG register #define IL i

hdu 3584 二进制0,1反转 三维树状数组 及三维树状数组模板

先贴自己类比二维树状数组写的三维树状数组模板: 开始的时候循环体内j=y,k=z,没写,以为自己思路错了,,,hehe..... 更高维的树状数组以此类比 const int MAXN = 100+10; int c[MAXN][MAXN][MAXN];int X,Y,Z; int N; inline int lowbit(int x){return x&(-x);} void update(int x, int y, int z, int v) { int i=x,j=y,k=z; while

poj 1195 二维树状数组 及二维树状数组模板

http://poj.org/problem?id=1195 求矩阵和的时候,下标弄错WA了一次... 求矩形(x1,y1) (x2,y2)的sum |sum=sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1) 二维树状数组讲解:http://blog.csdn.net/u011026968/article/details/38532117 二维树状数组模板: /*========================================

HDU1166 敌兵布阵 树状数组水题

中文题目,很简单的题目,区间求和,当然对于线段树来说也很水,为了练习一下树状数组,多做做水题吧,加深理解,并且打好基础,我算是被没打好基础给吓坏了,宁可多花几个小时 刷刷水题扎实点, 很裸的题目 操作也很裸,了解树状数组的肯定能做 #include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string&g