【树状数组】单点更新区间查询

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

typedef short int int16;///32767
typedef int int32;///2147483647
typedef long long int64;///9223372036854775807
const double PI=acos(-1.0);///3.141593
const long long MOD=(long long)1E9+7LL;///1000000007
int Fibonacci[100]={0,1};///
int Prime[100];///
int Caltan[100];///
template <class T> T Susake_pow(T a,T b)///pow
{T res;if(b==0) return 1;else while((b&1)==0){b>>=1;a*=a;}res=a;b>>=1;while(b!=0){a*=a;if((b&1)!=0)res*=a;b>>=1;}return res;}
template<class T> inline T gcd(T a,T b)///gcd
{if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T lcm(T a,T b)///lcm
{if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<class T> inline char *Susake_nsystem(T n)///itoa(26)
{T t=0,i;char *s,*p;s=(char *)malloc(sizeof(char)*1000);p=(char *)malloc(sizeof(char)*1000);
while(n){s[t]=n%26+64;if(s[t]==64){s[t]+=26;n-=26;}t++;n/=26;}s[t]=‘\0‘;for(i = 0; i < t; i++)p[i]=s[t-1-i];p[i]=‘\0‘;free(s);return p;}
int Susake_system(char *s)///atoi(26)
{int len=strlen(s),i,sum=0;char p[1000];for(i=0;i<len;i++)p[i]=s[len-1-i]-64;for(i=0;i<len;i++)sum+=p[i]*Susake_pow(26,i);return sum;}
int isdigit(int c);int islower(int c);int isupper(int c);
int tolower(int c);int toupper(int c);
///union_find
int fa[1];
template <class T> inline T union_find(T x)
{return fa[x] == x ? x : fa[x] = union_find(fa[x]);}
///tree array
int t_a[200001], t_c[200001];
int t_n;
template <class T> inline T lowbit(T x){return x&(-x);}
void update(int p,int x){while(p<=t_n){t_c[p]+=x;p+=lowbit(p);}}
template <class T> inline T sum(T p){T s=0;while(p>0){s+=t_c[p];p-=lowbit(p);}return s;}

int main(int argc, char *argv[])
{
    int t;
    scanf("%d", &t);
    char s[100];
    for(int z = 1; z <= t; z++)
    {
        memset(t_a, 0, sizeof(t_a));
        memset(t_c, 0, sizeof(t_c));
        scanf("%d", &t_n);
        for(int i = 1; i <= t_n; i++)
        {
            t_a[i] += 0;
            update(i, 0);
        }
        for(int i = 1; i <= t_n; i++)
        {
            int x;
            scanf("%d%*c", &x);
            t_a[i] += x;
            update(i, x);
        }
        printf("Case %d:\n", z);
        while(scanf("%s", s))
        {
            if(strcmp(s, "End") == 0) break;
            if(strcmp(s, "Add") == 0)
            {
                int j, k;
                scanf("%d%d", &j, &k);
                t_a[j] += k;
                update(j, k);
            }
            if(strcmp(s, "Sub") == 0)
            {
                int j, k;
                scanf("%d%d", &j, &k);
                t_a[j] -= k;
                update(j, -k);
            }
            if(strcmp(s, "Query") == 0)
            {
                int j, k;
                scanf("%d%d", &j, &k);
                printf("%d\n", sum(k) - sum(j - 1));
            }
        }
        getchar();
    }
    return 0;
}
时间: 2024-08-26 15:07:01

【树状数组】单点更新区间查询的相关文章

TOJ 2725 See you~(二维树状数组单点更新区间查询)

描述 Now I am leaving hust acm. In the past two and half years, I learned so many knowledge about Algorithm and Programming, and I met so many good friends. I want to say sorry to Mr, Yin, I must leave now ~~>.<~~. I am very sorry, we could not advanc

【2018年全国多校算法寒假训练营练习比赛(第五场)-E】情人节的电灯泡(二维树状数组单点更新+区间查询)

试题链接:https://www.nowcoder.com/acm/contest/77/E 题目描述 情人节到了,小芳和小明手牵手,打算过一个完美的情人节,但是小刚偏偏也来了,当了一个明晃晃的电灯泡,小明很尴尬,就和小刚说,我交给你个任务,你完成了我俩就带你玩,否则你就回家吧.小刚很有当单身狗的觉悟,他坚决不想让小明过好情人节,同为单身狗的你能帮帮他吗?现在有一个n×n(1 <= n <= 1000)的格子,每一个格子都有一个电灯泡,可能是亮的,也可能是灭的(1代表亮, 0代表灭),现在有两

POJ 1195-Mobile phones(二维树状数组-区间更新区间查询)

Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17661   Accepted: 8173 Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The

一维 + 二维树状数组 + 单点更新 + 区间更新 详解

树状数组详解: 假设一维数组为A[i](i=1,2,...n),则与它对应的树状数组C[i](i=1,2,...n)是这样定义的: C1 = A1 C2 = A1 + A2 C3 = A3 C4 = A1 + A2 + A3 + A4 C5 = A5 C6 = A5 + A6 ................. C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 ................ 如图可知: 为奇数的时候他是代表他本身,而为偶数的时候则是代表着自

树状数组区间更新区间查询以及gcd的logn性质

题目描述 给你一个长为n的序列a m次查询 每次查询一个区间的所有子区间的gcd的和mod1e9+7的结果 输入描述: 第一行两个数n,m之后一行n个数表示a之后m行每行两个数l,r表示查询的区间 输出描述: 对于每个询问,输出一行一个数表示答案 示例1 输入 5 7 30 60 20 20 20 1 1 1 5 2 4 3 4 3 5 2 5 2 3 输出 30 330 160 60 120 240 100 说明 [1,1]的子区间只有[1,1],其gcd为30[1,5]的子区间有:[1,1]

Stars(树状数组单点更新)

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers wa

树状数组单点更新和区间查询

这里是最基本的操作. 单操作时间复杂度O(logN),空间复杂度O(N). 1 #include <fstream> 2 #include <iostream> 3 #include <cstdio> 4 5 using namespace std; 6 7 int n,m; 8 int a[100002],tree[100002]; 9 10 void build();//建树状数组 11 int read(int pos);//求 sum[1,pos]的答案 12

树状数组区间更新区间查询

1 void ins(int k,int x,int t){ 2 for (; x<=tot; x+=x&-x) c[k][x]+=t; 3 } 4 ll getsum(int k,int x){ 5 ll t=0; for (; x; x-=x&-x) t+=c[k][x]; return t; 6 } 7 void mdy(int x,int y,int z){ 8 ins(0,x,z); ins(1,x,z*(x-1)); ins(0,y+1,-z); ins(1,y+1,-z

树状数组区间更新

在今天的文章开始之前,给大家提一个建议,由于线段树和树状数组这两个结构的分析有很多联系,因此,建议没有看前几篇文章的朋友一定需要了解一下前面的内容.链接如下: 线段树+RMQ问题第二弹 线段树第二弹(区间更新) 树状数组(Binary Indexed Tree,BIT) 上篇文章我们讨论了树状数组的基本结构以及它最擅长的两个功能:单点更新和区间求和,今天,我们来接着上一篇文章的内容继续深入研究.上篇文章我们是将树状数组和线段树进行对比讲解的,既然线段树的章节我们介绍了区间求和.区间最值.单点更新

【bzoj3779】重组病毒 LCT+树上倍增+DFS序+树状数组区间修改区间查询

题目描述 给出一棵n个节点的树,每一个节点开始有一个互不相同的颜色,初始根节点为1. 定义一次感染为:将指定的一个节点到根的链上的所有节点染成一种新的颜色,代价为这条链上不同颜色的数目. 现有m次操作,每次为一下三种之一: RELEASE x:对x执行一次感染: RECENTER x:把根节点改为x,并对原来的根节点执行一次感染: REQUEST x:询问x子树中所有节点感染代价的平均值. 输入 输入的第一行包含两个整数n和m,分别代表局域网中计算机的数量,以及操作和询问的总数.接下来n-1行,