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. He keeps nsacks where he keeps this money. The sacks are numbered from 0 to n-1.

Now each time he can he can do one of the three tasks.

1)                  Give all the money of the ith sack to the poor, leaving the sack empty.

2)                  Add new amount (given in input) in the ith sack.

3)                  Find the total amount of money from ith sack to jth sack.

Since he is not a programmer, he seeks your help.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers in the range [0, 1000]. The ithinteger
denotes the initial amount of money in the ith sack (0 ≤ i < n).

Each of the next q lines contains a task in one of the following form:

1 i        Give all the money of the ith (0 ≤ i < n) sack to the poor.

2 i v     Add money v (1 ≤ v ≤ 1000) to the ith (0 ≤ i < n) sack.

3 i j      Find the total amount of money from ith sack to jth sack (0 ≤ i ≤ j < n).

Output

For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from ith to jth sack.

Sample Input

Output for Sample Input


1

5 6

3 2 1 4 5

1 4

2 3 4

3 0 3

1 2

3 0 4

1 1


Case 1:

5

14

1

13

2

Notes

Dataset is huge, use faster I/O methods.

题意:给你N个数和M次查询,查询分三种

一,1 i   表示查询i处的值,并把 i 处的值变为0。

二,2 i v 表示将 i 处 值增加 v。

三,3 x y 查询 区间[x, y]的值。

水题,直接用树状数组就可以了,没必要用线段树。

AC代码:注意下标是从0开始到N-1的

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 100000+10
#define MAXM 60000+10
#define INF 1000000
#define eps 1e-8
using namespace std;
int N, M;
int C[MAXN<<1];
int k = 1;
int lowbit(int x)
{
    return x & (-x);
}
int sum(int x)
{
    int s = 0;
    while(x > 0)
    {
        s += C[x];
        x -= lowbit(x);
    }
    return s;
}
void update(int x, int d)
{
    while(x <= N)
    {
        C[x] += d;
        x += lowbit(x);
    }
}
void solve()
{
    int x, y, d, op;
    printf("Case %d:\n", k++);
    while(M--)
    {
        scanf("%d", &op);
        if(op == 1)
        {
            scanf("%d", &x);
            x++;
            printf("%d\n", sum(x) - sum(x-1));
            int t = sum(x) - sum(x-1);
            update(x, -t);
        }
        else if(op == 2)
        {
            scanf("%d%d", &x, &d);
            x++;
            update(x, d);
        }
        else
        {
            scanf("%d%d", &x, &y);
            x++, y++;
            printf("%d\n", sum(y) - sum(x-1));
        }
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &N, &M);
        memset(C, 0, sizeof(C));
        int a;
        for(int i = 1; i <= N; i++)
        {
            scanf("%d", &a);
            update(i, a);
        }
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-05 12:28:05

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(线段树||树状数组)

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.

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> #incl

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

【poj1901-求区间第k大值(带修改)】树状数组套主席树

901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 7025  Solved: 2925[Submit][Status][Discuss] Description 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[j]中第k小的数是多少(1≤k≤j-i+1),并且,你可以改变一些a[i]

HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)

题目链接 ac代码(注意字符读入前需要注意回车的影响) #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cmath> #include<cstdlib> #include<c

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

【算法系列学习】线段树vs树状数组 单点修改,区间查询 [kuangbin带你飞]专题七 线段树 A - 敌兵布阵

https://vjudge.net/contest/66989#problem/A 单点修改,区间查询 方法一:线段树 http://www.cnblogs.com/kuangbin/archive/2011/08/15/2139834.html 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<cmath> 6 #

A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4032    Accepted Submission(s): 1255 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with