zoukankan      html  css  js  c++  java
  • 二维树状数组--hdu1892

    二维树状数组--hdu1892

    题意

    二维书架上原本有1本书

    T组样例,四种操作,search,add,del,move

    书架上的书小于del的值时,取完该格子的书即可

    查询操作用二维的容斥推

    由于格子取值可以等于0,故下标都+1

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <cmath>
    #include <sstream>
    #include <algorithm>
    #include <set>
    #include <map>
    #include <vector>
    #include <queue>
    #include <iomanip>
    #include <stack>
    
    using namespace std;
    
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const int N = 1e3 + 5;
    const int MOD = 1e9 + 9;
    const double pi = 3.1415926;
    
    #define lson l, m, rt << 1
    #define rson m + 1, r, rt << 1 | 1
    #define F(i, l, r) for(int i = l;i <= (r);++i)
    #define RF(i, l, r) for(int i = l;i >= (r);--i)
    
    int sum[N << 1][N << 1], p[N][N];
    int lowbit(int x) {return x & (-x);}
    void change(int x, int y, int num)
    {
        for(int i = x;i <= N;i += lowbit(i))
            for(int j = y;j <= N;j += lowbit(j))
            sum[i][j] += num;
    }
    
    int query(int x, int y)
    {
        int ret = 0;
        for(int i = x;i;i -= lowbit(i))
            for(int j = y;j;j -= lowbit(j))
            ret += sum[i][j];
        return ret;
    }
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        int T;cin >> T;
        for(int cas = 1;cas <= T;++cas)
        {
            cout << "Case " << cas << ":" << endl;
            for(int i = 1;i < N;++i)
                for(int j = 1;j < N;++j)
                {sum[i][j] = lowbit(i) * lowbit(j);p[i][j] = 1;}
            int Q;cin >> Q;
            while(Q--)
            {
                string s;int a, b, c, d, e;
                cin >> s;
                if(s == "A")
                {
                    cin >> a >> b >> c;
                    a++;b++;p[a][b] += c;
                    change(a, b, c);
                }
                else if(s == "S")
                {
                    cin >> a >> b >> c >> d;
                    a++;b++;c++;d++;
                    if(a > c) swap(a, c);
                    if(b > d) swap(b, d);
                    cout << query(c, d) - query(a - 1, d) - query(c, b - 1) + query(a - 1, b - 1) << endl;
                }
                else if(s == "D")
                {
                    cin >> a >> b >> c;
                    a++;b++;c = min(c, p[a][b]);p[a][b] -= c;
                    change(a, b, -c);
                }
                else
                {
                    cin >> a >> b >> c >> d >> e;
                    a++;b++;c++;d++;
                    e = min(e, p[a][b]);p[a][b] -= e;p[c][d] += e;
                    change(a, b, -e);change(c, d, e);
                }
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    支付宝小程序InputItem清除icon不显示
    win11 激活 wi7 win11 魔兽争霸切换 后无法 回到游戏界面 处理办法
    HJ10 字符个数统计
    iOS 15系统导航栏适配
    HJ4 字符串分隔
    HJ7 取近似值
    [iOS]隐藏导航栏3种方式
    HJ3 明明的随机数
    HJ8 合并表记录
    HJ5 进制转换
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/10859108.html
Copyright © 2011-2022 走看看