刚学的树状数组,还热乎着(就全WA了 求助
查看原帖
刚学的树状数组,还热乎着(就全WA了 求助
316827
Temperature_automata楼主2021/1/18 19:53

RT,代码如下:

#include <iostream>
#include <cstdio>

using namespace std ;

const int N = 100005 ;
int n , m ;
long long c[N] , c2[N] ;

int lowbit ( int x ) {
  return x & (-x) ;
}

void add ( int x , int way ) {
  const int t = way - 1 ;
  while(way<=n) {
    c[way] += x ;
    c2[way] += t * x ;
    way += lowbit(way) ;
  }
}

int get ( int x ) {
  int res = 0 ;
  const int t = x ;
  while(x > 0) {
    res += c[x] * t - c2[x] ;
    x-=lowbit(x) ;
  }
  return res ;
}

int main ( ) {
  ios::sync_with_stdio(false) ;
  cin >> n >> m ;
  int xx , yy ;
  for ( int i = 1 ; i <= n ; i ++ ) {
    // int x ;
    cin >> xx ;
    add ( xx - yy , i ) ; 
    yy = xx ;
  }
  while(m--) {
    int o ;
    cin >> o ;
    if(o==1) {
      int x , y , k ;
      cin >> x >> y >> k ;
      add ( k , x) ;
      add ( -k , y + 1 ) ;
    }
    else {
      int x , y ;
      cin >> x >> y ;
      cout << get(y) - get(x-1) << endl ;
    }
  }
  // system("pause") ;
}

不要在意long long,加了之后输出负数

2021/1/18 19:53
加载中...