杭电 1166 树状数组

Posted 你可以当我没名字

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了杭电 1166 树状数组相关的知识,希望对你有一定的参考价值。

杭电 1166

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define endl        '\\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)    for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define sc          ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
    (void)(cout << "L" << __LINE__ \\
    << ": " << #x << " = " << (x) << '\\n')
#define TIE \\
    cin.tie(0);cout.tie(0);\\
    ios::sync_with_stdio(false);

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1000000005;
const int N = 100;

ll n,m,cb[600000];

void init(){
	ini(cb);
}

void update(ll x,ll k){
	for(;x<=n;x+=lowbit(x))
		cb[x] += k;
}

ll ask(ll x){
	ll ans = 0;
	for(;x>0;x-=lowbit(x))
		ans+=cb[x];
	return ans;
}

void solve(){
	init();
	cin>>n;
	ll temp;
	for(int i=1;i<=n;i++){
		cin>>temp;
		update(i,temp);
	}
	char s[10];
	ll a,b;
	while(cin>>s){
		if(strcmp(s,"End")==0)break;
		cin>>a>>b;
		if(strcmp(s,"Query")==0){
			cout<<ask(b)-ask(a-1)<<endl;
		}else if(strcmp(s,"Add")==0){
			update(a,b);
		}else if(strcmp(s,"Sub")==0){
			update(a,-b);
		}
	}
}

int main()
{
//    #ifndef ONLINE_JUDGE
//    freopen ("input.txt","r",stdin);
//    #else
//    #endif
//    sc{solve();}
    sc{cout<<"Case "<<Q<<":"<<endl;solve();}
}

以上是关于杭电 1166 树状数组的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1166 敌兵布阵 数据结构-树状数组-改点查段

HDU_1166_树状数组

HDU 1166 敌兵布阵 (树状数组)

HDU1166 树状数组入门

hdu1166 敌兵布阵 树状数组/线段树

HDU 1166 敌兵布阵(线段树或树状数组)