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

题目链接

树状数组

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<vector>
#include<queue>
#include<string>
#include <cmath>
#include<set>
#include<map>
#define mod 999997
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define father  l , r , root
#define lowbit(x) ( x & ( - x ) )
using namespace std;
typedef long long ll;
const int maxn = 51000;
const int inf = 0x3f3f3f3f;
int n;
int num[maxn];
int arr[maxn];
void update(int x,int val){
    
    
    for(int i=x;i<=n;i+=lowbit(i)){
    
    
        arr[i]+=val;
    }
}
int getsum(int x){
    
    
    int ans=0;
    for(int i=x;i>0;i-=lowbit(i)){
    
    
        ans+=arr[i];
    }
    return ans;
}
int main( ){
    
    
    int t;
    char s[10];
    scanf("%d",&t);
    int cnt=1;
    while(t--){
    
    
        memset(arr,0,sizeof(arr));
        printf("Case %d:\n",cnt++);
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
    
    
            int val;
            scanf("%d",&val);
            update(i,val);
        }
        while(scanf("%s",s)){
    
    
            if(strcmp(s,"End")==0){
    
    
                break;
            }else if(strcmp(s,"Query")==0){
    
    
                int l,r;
                scanf("%d%d",&l,&r);
                printf("%d\n",getsum(r)-getsum(l-1));
            }else if(strcmp(s,"Sub")==0){
    
    
                int pos,val;
                scanf("%d%d",&pos,&val);
                update(pos,-val);
            }else{
    
    
                int pos,val;
                scanf("%d%d",&pos,&val);
                update(pos,val);
            }
        }
    }
}

线段树

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<vector>
#include<queue>
#include<string>
#include <cmath>
#include<set>
#define mod 999997
#define lson l, mid, root << 1
#define rson mid + 1, r, root << 1 | 1
#define father  l , r , root
#define lowbit(x) ( x & ( - x ) )
using namespace std;
typedef long long ll;
const int maxn = 5e5+5;
const int inf = 0x3f3f3f3f;

int num[maxn];
int node[maxn<<2];

void push_up(int root){
    
    
    node[root]=node[root<<1]+node[root<<1|1];
}


void build_tree(int l,int r,int root){
    
    
    if(l==r){
    
    
        node[root]=num[l];
        return ;
    }
    int mid=(l+r)>>1;
    build_tree(lson);
    build_tree(rson);
    push_up(root);
}

void change(int pos,int val,int l,int r ,int root){
    
    
    if(l==pos&&r==pos){
    
    
    	node[root]+=val;
    	return ;
	}
	
    int mid=(l+r)>>1;
    
    if(mid>=pos)change(pos,val,lson);
    else change(pos,val,rson);
    
    push_up(root);
    
}

int query(int L,int R,int l,int r ,int root){
    
    
    if(L<=l&&R>=r){
    
    
        return node[root];
    }
    int ans=0;
    int mid=(l+r)>>1;
    if(mid>=L){
    
    
    	ans+=query(L,R,lson);	
	} 
	if(mid<R){
    
    
		ans+=query(L,R,rson);
	}
	return ans;
}

int main( ){
    
    
    int t;
    scanf("%d",&t);
    char s[10];
    int cnt=1;
    while(t--){
    
    
        int n;
        scanf("%d",&n);
        printf("Case %d:\n",cnt++); 
        for(int i=1;i<=n;i++)scanf("%d",&num[i]);
        build_tree(1,n,1);
        while(1){
    
    
        	scanf("%s",s);
			if(strcmp(s,"End")==0){
    
    
				break;
			}else if(strcmp(s,"Query")==0){
    
    
				int l,r;
				scanf("%d%d",&l,&r);
				printf("%d\n",query(l,r,1,n,1));
			}else if(strcmp(s,"Sub")==0){
    
    
				int pos,val;
				scanf("%d%d",&pos,&val);
				change(pos,-val,1,n,1);
			}else{
    
    
				int pos,val;
				scanf("%d%d",&pos,&val);
				change(pos,val,1,n,1);
			}
		}
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43715171/article/details/97641507