LightOJ 1112 Curious Robin Hood (单点更新+区间求和)

http://lightoj.com/volume_showproblem.php?problem=1112

题目大意:

1 i        将第i个数值输出,并将第i个值清0

2 i v     将第i个数值加v

3 i j      输出从i到j的数值和

简单的单点更新+区间求和,代码很简单的模板

但此题有一个神坑的地方当操作为1是输出第i个数值不是直接输出,而是通过查找输出(太坑了。。。)

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 100010
#define Lson root<<1, L, tree[root].Mid()
#define Rson root<<1|1, tree[root].Mid() + 1, R

using namespace std;

struct Tree
{
    int L, R;
    long long sum, e;
    bool op;
    int Mid()
    {
        return (R + L) / 2;
    }
} tree[N * 4];

long long al[N];

void Build(int root, int L, int R)
{
    tree[root].L = L, tree[root].R = R;
    tree[root].op = false;
    if(L == R)
    {
        tree[root].sum = al[L];
        return ;
    }

    Build(Lson);
    Build(Rson);

    tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}

void Insert(int root, int k, long long e)
{
    tree[root].sum += e;
    if(tree[root].L == tree[root].R)
        return ;
    if(k <= tree[root].Mid())
        Insert(root<<1, k, e);
    else if(k > tree[root].Mid())
        Insert(root<<1|1, k, e);
}

long long Query(int root, int L, int R)
{
    if(tree[root].L == L && tree[root].R == R)
        return tree[root].sum;
    if(R <= tree[root].Mid())
        return Query(root<<1, L, R);
    else if(L > tree[root].Mid())
        return Query(root<<1|1, L, R);
    else
        return Query(Lson) + Query(Rson);
}

int main()
{
    int m, n, a, p, b, i, t, x = 0;
    long long e;
    char s[10];
    scanf("%d", &t);
    while(t--)
    {
        x++;
        scanf("%d%d", &m, &n);
        for(i = 1 ; i <= m ; i++)
            scanf("%lld", &al[i]);
        Build(1, 1, m);
        printf("Case %d:\n", x);
        while(n--)
        {
            scanf("%d", &p);
            if(p == 1)
            {
                scanf("%d", &a);
                printf("%d\n", Query(1, a + 1, a + 1));//注意! ! ! 坑来了!!!
                Insert(1, a + 1, -Query(1, a + 1, a + 1));//此处一样
            }
            else if(p == 2)
            {
                scanf("%d%d", &a, &b);
                Insert(1, a + 1, b);
            }
            else if(p == 3)
            {
                scanf("%d%d", &a, &b);
                printf("%lld\n", Query(1, a + 1, b + 1));
            }
        }
    }
    return 0;
}

时间: 2024-07-30 15:28:24

LightOJ 1112 Curious Robin Hood (单点更新+区间求和)的相关文章

Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.

Lightoj 1112 - Curious Robin Hood 【单点修改 + 单点、 区间查询】【树状数组 水题】

1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick.

LightOj 1112 Curious Robin Hood(线段树||树状数组)

Curious Robin Hood Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.

Light oj 1112 - Curious Robin Hood【单点更新】

1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another tri

hdu 1166 敌兵布阵(线段树之 单点更新+区间求和)

敌兵布阵                                                                             Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵

poj 1195 单点更新 区间求和

Mobile phones Time Limit: 5000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Suppose that the fourth generation mobile phone base stations in the Tampere area operate a

HYSBZ 1036 树链剖分(单点更新区间求和求最大值)

http://www.lydsy.com/JudgeOnline/problem.php?id=1036 Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问从点u到点v的路径上的节点的权值和 注意:从点u到点v的路径上的节点包括u和v本身 Input 输入

树状数组 模板 单点更新 区间求和

(来自luogu)原题目 lowbit(x)=2^k次幂,k为x末尾0的数量.大家可以模拟试试lowbit (-x)=(~x)+1,把x取反+1 void update(int x,int k)表示a[x]+=k(单点更新) int sum(int x)表示求1-x区间和 求x-y区间和只需要sum(y)-sum(x-1)即可 #include <iostream> #include <string> #include <cstring> #define lowbit(

poj 2828 Buy Tickets 【线段树】【逆序插入 + 单点更新 + 区间求和】

Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16067   Accepted: 8017 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue- The Lunar New Year wa