Curious Robin Hood【线段树单点更新】

此博客介绍了一个基于RobinHood劫富济贫概念的数据结构问题,涉及到数组操作包括清空元素、增加元素值及求区间和。通过构建线段树解决多次更新和查询的问题。

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.
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 ith integer 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
1
5 6
3 2 1 4 5
1 4
2 3 4
3 0 3
1 2
3 0 4
1 1
Sample Output
Case 1:
5
14
1
13
2

题意:
输入T组测试数据:
N个数据,Q个操作;
1,i 将编号为i的数变成0,并且输出被改变的点的数值;
2,i,v 将编号为i的数加v;
3,i,j 输出i~j之间的总和;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define maxn 100001
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int sum[maxn << 2];

void pushUp(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1|1];
}

void buildTree(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d", &sum[rt]);
    }
    else
    {
        int m;
        m=(l + r)>>1;
        buildTree(lson);
        buildTree(rson);
        pushUp(rt);
    }
}

void update(int p,int add,int l,int r,int rt)
{
    if(l == r)
    {
        sum[rt]+=add;
    }
    else
    {
        int m = (l + r) >> 1;
        if(p <= m)
            update(p, add, lson);
        else
            update(p, add, rson);
        pushUp(rt);
    }
}

int Query(int L, int R, int l, int r, int rt)
{

    if (L <= l && r <= R)
    {
        return sum[rt];
    }
    int m = (l + r) >> 1;
    int ans = 0;
    if (L <= m)
        ans += Query(L, R, lson);
    if (R >m)
        ans += Query(L, R, rson);
    return ans;
}


void delet(int p, int l, int r, int rt)
{
    if(l == r)
    {
        printf("%d\n",sum[rt]);
        sum[rt] = 0;
    }
    else
    {
        int m=(l + r)>>1;
        if(p <= m)
            delet(p,lson);
        else
            delet(p,rson);
        pushUp(rt);
    }
}




int main()
{
    int t;
    scanf("%d", &t);
    for(int cases = 1; cases <= t; cases++ )
    {
        cout << "Case " << cases << ":" << endl;
        int n,q;
        scanf("%d%d", &n, &q);
        buildTree(1, n, 1);

        for(int i = 0; i < q; i++)
        {
            int k;
            scanf("%d",&k);
            if(k== 1)
            {
                int a;
                scanf("%d", &a);
                delet(a+1, 1, n, 1);
            }
            if(k==2)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                update(a+1,b,1,n,1);

            }
            if(k==3)
            {
                int a, b;
                scanf("%d%d",&a,&b);
                printf("%d\n",Query(a+1,b+1,1,n,1));
            }
        }
    }
    return 0;
}
在 C++ 程序中,`freopen` 是一个用于**重定向标准输入/输出流**的函数,通常用于调试或测试时从文件中读取输入或将输出写入文件,而不是从控制台读取或打印到控制台。 --- ### ✅ 在你的程序中加入 `freopen` 的方法如下: ```cpp #include <iostream> #include <queue> #include <unordered_map> using namespace std; // 使用 freopen 时需要包含的头文件 #include <cstdio> int minStepsTo1(int n) { if (n == 1) return 0; queue<pair<int, int>> q; unordered_map<int, bool> visited; q.push({n, 0}); visited[n] = true; while (!q.empty()) { auto [current, steps] = q.front(); q.pop(); if (current % 2 == 0) { int next = current / 2; if (next == 1) return steps + 1; if (!visited[next]) { visited[next] = true; q.push({next, steps + 1}); } } if (current % 3 == 0) { int next = (2 * current) / 3; if (next == 1) return steps + 1; if (!visited[next]) { visited[next] = true; q.push({next, steps + 1}); } } if (current % 5 == 0) { int next = (4 * current) / 5; if (next == 1) return steps + 1; if (!visited[next]) { visited[next] = true; q.push({next, steps + 1}); } } } return -1; } int main() { // 加入 freopen,重定向输入输出 freopen("input.txt", "r", stdin); // 从 input.txt 读取输入 freopen("output.txt", "w", stdout); // 输出写入 output.txt int T; cin >> T; for (int i = 0; i < T; ++i) { int n; cin >> n; cout << minStepsTo1(n) << endl; } return 0; } ``` --- ### 🔍 `freopen` 详解: - `freopen("input.txt", "r", stdin);`: - 将标准输入(即 `cin`)从控制台重定向到文件 `input.txt`。 - `"r"` 表示以只读方式打开文件。 - `freopen("output.txt", "w", stdout);`: - 将标准输出(即 `cout`)从控制台重定向到文件 `output.txt`。 - `"w"` 表示以写入方式打开文件(若文件不存在则创建,若存在则清空)。 --- ### 📌 注意事项: - 确保 `input.txt` 文件存在,否则程序可能崩溃或行为不可预测。 - `freopen` 只在当前运行有效,程序结束后标准输入输出会恢复默认。 - 在竞赛中,使用 `freopen` 时要小心,有些平台(如在线评测系统)可能不允许文件操作。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值