zoukankan      html  css  js  c++  java
  • hdu 1166敌兵布阵(线段树入门题)

    》》点击进入原题测试《《

    思路:这两天在学线段树,这个题直接手敲一下线段树就行了,都没有用上懒人标记。入门题

    cin,cout会超时,记得加std::ios::sync_with_stdio(false);

    #include<string>
    #include<iostream>
    
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    
    using namespace std;
    
    
    const int maxn = 5e4 + 5;
    
    int add[maxn << 2];
    int tree[maxn << 2];
    
    void PushDown(int rt)
    {
        if (add[rt]){
            add[rt << 1] += add[rt];
            add[rt << 1 | 1] += add[rt];
            tree[rt << 1] += add[rt];
            tree[rt << 1 | 1] += add[rt];
            add[rt] = 0;
        }
    }
    void build(int l, int r, int rt)
    {
        if (l == r){
            cin >> tree[rt];
            return;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
    }
    void update(int x, int ans, int l, int r, int rt)
    {
        if (l == r){
            tree[rt] += ans;
            return;
        }
        int m = (l + r) >> 1;
        if (x <= m)update(x, ans, lson);
        else update(x, ans, rson);
        tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
    }
    int query(int L, int R, int l, int r, int rt)
    {
        if (L <= l&&r <= R){
            return tree[rt];
        }
        int m = (l + r) >> 1, ans = 0;
        if (L <= m)ans += query(L, R, lson);
        if (R > m)ans += query(L, R, rson);
        return ans;
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        int t, ca = 1;
        cin >> t;
        while (t--){
            
            int n; cin >> n;
            build(1, n, 1);
            string flag; int i, j;
            cout << "Case " << ca++ << ":" << endl;
            while (cin >> flag, flag != "End"){
                cin >> i >> j;
                if (flag == "Add"){
                    update(i, j, 1, n, 1);
                }
                else if (flag == "Sub"){
                    update(i, -1 * j, 1, n, 1);
                }
                else if (flag == "Query"){
                    cout << query(i, j, 1, n, 1) << endl;
                }
                
            }
            
        }
        return 0;
    }
  • 相关阅读:
    软件工程(2018)第一次作业
    SQA计划
    再冲刺
    第三次冲刺
    第二次冲刺
    小组第一次冲刺
    团队合作初体验
    关于git的认识与想法
    我的第一篇博客
    SQA计划和系统测试规程
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/9382353.html
Copyright © 2011-2022 走看看