zoukankan      html  css  js  c++  java
  • Poj1195&tyvj1474二维线段树

    二维树状数组模板题,熟悉下二维树状数组。

    tyvj1474

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #include <set>
    #include <bitset>
    #include <queue>
    #include <stack>
    #include <string>
    #include <iostream>
    #include <cmath>
    #include <climits>
    
    using namespace std;
    const int MAX=1200;
    int c[MAX][MAX];
    int n;
    int LowBit(int t)
    {
        return t&(-t);
    }
    int Sum(int endx,int endy)
    {
        int sum=0;
        int temp=endy;
        while(endx>0)
        {
            endy=temp;
            while (endy>0)
            {
                sum+=c[endx][endy];
                endy-=LowBit(endy);
            }
    
            endx-=LowBit(endx);
        }
        return sum;
    }
    void add(int addx,int addy,int num)
    {
        int temp=addy;
        while (addx <=n)
        {
            addy=temp;
            while(addy<=n)
            {
                c[addx][addy]+=num;
                addy+=LowBit(addy);
            }
            addx+=LowBit(addx);
        }
    }
    int GetSum(int l,int b,int r,int t)
    {
        return Sum(r,t)-Sum(r,b-1)-Sum(l-1,t)+Sum(l-1,b-1);
    }
    
    int main()
    {
        int gg;
        int x;
        int a1,a2,a3,a4;
        scanf("%d",&n);
        while(scanf("%d",&x)!=EOF&&x!=3){
            if(x==1){
                scanf("%d%d%d",&a1,&a2,&a3);
                add(a1+1,a2+1,a3);
            }
            else{
                scanf("%d%d%d%d",&a1,&a2,&a3,&a4);
                cout<<GetSum(a1+1,a2+1,a3+1,a4+1)<<endl;
            }
        }
        return 0;
    }

    poj1195

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #include <set>
    #include <bitset>
    #include <queue>
    #include <stack>
    #include <string>
    #include <iostream>
    #include <cmath>
    #include <climits>
    
    using namespace std;
    const int MAX=1200;
    int c[MAX][MAX];
    int n;
    int LowBit(int t)
    {
        return t&(-t);
    }
    int Sum(int endx,int endy)
    {
        int sum=0;
        int temp=endy;
        while(endx>0)
        {
            endy=temp;
            while (endy>0)
            {
                sum+=c[endx][endy];
                endy-=LowBit(endy);
            }
    
            endx-=LowBit(endx);
        }
        return sum;
    }
    void add(int addx,int addy,int num)
    {
        int temp=addy;
        while (addx <=n)
        {
            addy=temp;
            while(addy<=n)
            {
                c[addx][addy]+=num;
                addy+=LowBit(addy);
            }
            addx+=LowBit(addx);
        }
    }
    int GetSum(int l,int b,int r,int t)
    {
        return Sum(r,t)-Sum(r,b-1)-Sum(l-1,t)+Sum(l-1,b-1);
    }
    
    int main()
    {
        int gg;
        int x;
        int a1,a2,a3,a4;
        scanf("%d %d",&gg,&n);
        while(scanf("%d",&x)!=EOF&&x!=3){
            if(x==1){
                scanf("%d%d%d",&a1,&a2,&a3);
                add(a1+1,a2+1,a3);
            }
            else{
                scanf("%d%d%d%d",&a1,&a2,&a3,&a4);
                cout<<GetSum(a1+1,a2+1,a3+1,a4+1)<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    VScode 关闭回车后自动格式化代码
    『转载』专利申请
    『转载』 免费公用DNS服务及三大运营商DNS大全 含IPV4和IPV6
    正则表达式和元字符
    随机背景图
    秒表
    数组相关的函数
    对象的结构语法
    数组的结构语法
    展开合并运算符
  • 原文地址:https://www.cnblogs.com/yigexigua/p/4162649.html
Copyright © 2011-2022 走看看