HDU1166线段树点更新

Problem Description
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
 
Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
 
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
 
Sample Input
 
  
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
 
Sample Output
 
  
Case 1: 6 33 59

两种写法

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <vector>
#include <string.h>
#include <string>
#include <queue>
#include <set>
using namespace std;
//ios_base::sync_with_stdio(false);
//#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const int N = 5e5+10;
char str[10];
struct Node {
    int l,r;
    ll sum;
    int mid() {
        return (l+r)>>1;
    }
}tree[N<<2];
void PushUp(int rt) //回溯时把左右孩子的值带上来
{
    tree[rt].sum = tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void build(int l, int r, int rt)
{
    tree[rt].l = l;
    tree[rt].r = r;
    if(l == r) {
        scanf("%I64d",&tree[rt].sum);//小技巧,输入叶子的值
        return;
    }
    int m = tree[rt].mid();
    build(lson);
    build(rson);
    PushUp(rt);
}
void update(int pos,int rt,int val)
{
    if(tree[rt].l == pos && tree[rt].r == pos) {
        tree[rt].sum+=val;
        return;
    }
    int m = tree[rt].mid();
    if(pos <= m)
        update(pos,rt<<1,val);
    else
        update(pos,rt<<1|1,val);

    PushUp(rt);
}
ll query(int l, int r, int rt)
{
    if(l == tree[rt].l&&r == tree[rt].r) return tree[rt].sum;
    //PushDown(rt,tree[rt].r-tree[rt].l+1);
    int m = tree[rt].mid();
    ll res = 0;
    if(r <= m) res+=query(l, r, rt<<1);
    else if(l > m) res+=query(l, r, rt <<1|1);
    else {
        res+=query(lson);
        res+=query(rson);
    }
    return res;
}

int main()
{
    int T,n;
    int Cas = 0;
    scanf("%d",&T);
    while(T--)
    {
        int a, b;
        scanf("%d",&n);
        build(1,n,1);
        printf("Case %d:\n", ++Cas);
        while(scanf("%s",str))
        {
            if(str[0] == 'E') break;
            else if(str[0] == 'Q') {
                scanf("%d%d",&a,&b);
                query(a,b,1);
                printf("%I64d\n",query(a,b,1));
            }
            else if(str[0] == 'A') {
                scanf("%d%d",&a,&b);
                update(a,1,b);
            }
            else if(str[0] == 'S') {
                scanf("%d%d",&a,&b);
                update(a,1,-b);
            }

        }
    }

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn = 1<<20;

struct Node {
    int value;
    int left, right;
}node[maxn*2];
int father[maxn*2];

void BuildTree(int i, int left, int right)
{
    node[i].left = left;
    node[i].right = right;
    node[i].value = 0;
    if(left == right) {

        father[left] = i;
        return;
    }
    BuildTree(i<<1, left, (right+left)/2);
    BuildTree((i<<1)+1, (right+left)/2+1, right);
}

void UpdateTree(int ri, int x)
{
    if(ri == 0) return;
    node[ri].value += x;
    UpdateTree(ri/2, x);
}
int sum = 0;
void Query(int i, int l, int r)
{
    if(node[i].left == l && node[i].right == r) {
        sum += node[i].value;
        return;
    }
    i = i << 1;
    if(l <= node[i].right) {
        if(r <= node[i].right)
            Query(i, l, r);
        else
            Query(i, l, node[i].right);
    }
    i+=1;
    if (r >= node[i].left) {
        if(l >= node[i].left)
            Query(i, l, r);
      else
            Query(i, node[i].left, r);
    }
}
int main()
{
    //freopen("in.txt", "r", stdin);
    int T;
    scanf("%d", &T);
    int cas = 0;
    while(T--)
    {
        int n;
        scanf("%d", &n);
        BuildTree(1, 1, n);
        for(int i = 1; i <=n; i++)
        {
            int a;
            scanf("%d", &a);
            UpdateTree(father[i], a);
        }
        char op[10];
        int x, y;
        printf("Case %d:\n", ++cas);
        while(scanf("%s", op))
        {
            if(op[0] == 'E') break;
            else if(op[0] == 'A') {
                scanf("%d%d", &x, &y);
                UpdateTree(father[x], y);
            }
            else if(op[0] == 'S') {
                scanf("%d%d", &x, &y);
                UpdateTree(father[x], -y);
            }
            else if(op[0] == 'Q') {
                scanf("%d%d", &x, &y);
                sum = 0;
                Query(1, x, y);
                printf("%d\n", sum);
            }
        }
    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/qihang_qihang/article/details/80738211
今日推荐