首页 > 其他分享 >P2068 统计和

P2068 统计和

时间:2022-10-29 16:22:05浏览次数:76  
标签:P2068 return int tree mid change ull 统计

水题,线段树板子(单点修改和区间和)

 

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;

const int N = 1e5 + 7;

ull a[N], tree[N * 4];

void change_tree(int l, int r, int i, int p, int k){
  if(l == r){
    tree[i] += k;
    return;
  }
  int mid = (l + r) >> 1;
  if(mid >= p) change_tree(l, mid, i * 2, p, k);
  else change_tree(mid + 1, r, i * 2 + 1, p, k);
  tree[i] = tree[i * 2] + tree[i * 2 + 1];
}

ull query_tree(int l, int r, int i, int x, int y){
  if(x <= l && y >= r)return tree[i];
  ull ans = 0;
  int mid = (l + r) >> 1;
  if(mid >= x) ans += query_tree(l, mid, i * 2, x, y);
  if(mid + 1 <= y) ans += query_tree(mid + 1, r, i * 2 + 1, x, y);
  return ans;
}

int main(){
  int n, q;
  scanf("%d%d", &n, &q);
  for(int i = 0;i < q;i ++){
  getchar();
  char ch;
  scanf("%c", &ch);
  int x, y;
  scanf("%d%d", &x, &y);
  if(ch == 'x')
    change_tree(1, n, 1, x, y);
  else
    printf("%lld\n", query_tree(1, n, 1, x, y));
  }
  return 0;
}

标签:P2068,return,int,tree,mid,change,ull,统计
From: https://www.cnblogs.com/loser--QAQ/p/16838978.html

相关文章