zoukankan      html  css  js  c++  java
  • HDU 2642 Stars (二维树状数组)

    Stars

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
    Total Submission(s): 465    Accepted Submission(s): 200


    Problem Description
    Yifenfei is a romantic guy and he likes to count the stars in the sky.
    To make the problem easier,we considerate the sky is a two-dimension plane.Sometimes the star will be bright and sometimes the star will be dim.At first,there is no bright star in the sky,then some information will be given as "B x y" where 'B' represent bright and x represent the X coordinate and y represent the Y coordinate means the star at (x,y) is bright,And the 'D' in "D x y" mean the star at(x,y) is dim.When get a query as "Q X1 X2 Y1 Y2",you should tell Yifenfei how many bright stars there are in the region correspond X1,X2,Y1,Y2.

    There is only one case.
     
    Input
    The first line contain a M(M <= 100000), then M line followed.
    each line start with a operational character.
    if the character is B or D,then two integer X,Y (0 <=X,Y<= 1000)followed.
    if the character is Q then four integer X1,X2,Y1,Y2(0 <=X1,X2,Y1,Y2<= 1000) followed.
     
    Output
    For each query,output the number of bright stars in one line.
     
    Sample Input
    5
    B 581 145
    B 581 145
    Q 0 600 0 200
    D 581 145
    Q 0 600 0 200
     
    Sample Output
    1
    0
     
     
    开始时,没深刻理解c[]时管理了许多的a[]的,误把c[]当成了a[]。
     
    单点增减(且要非负),区域求和
     
    错误代码:
    View Code
    #include <iostream>
    #include <algorithm>
    using namespace std;

    int n = 1001;
    int c[1010][1010];
    //int vis[1010][1010];

    int LowBit(int x)
    {
    return x&(-x);
    }

    void Update(int x, int y, int sc)
    {
    for (int i = x; i <= n; i += LowBit(i))
    {
    for (int j = y; j <= n; j += LowBit(j))
    {
    if (sc == 1)//这里错了。
    {
    c[i][j] = 1;//c[][]可以取[0,...],而a[][]才是只有0和1
    }
    else
    {
    c[i][j] = 0;
    }
    //c[i][j] += sc;
    }
    }
    }

    int Sum(int x, int y)
    {
    int sum = 0;
    for (int i = x; i > 0; i -= LowBit(i))
    {
    for (int j = y; j > 0; j -= LowBit(j))
    {
    sum += c[i][j];
    }
    }
    return sum;
    }

    int main()
    {
    int m;
    char str[5];
    cin >> m;
    while (m--)
    {
    scanf("%s", str);
    if (str[0] == 'Q')
    {
    int x1,x2, y1, y2;
    cin >> x1 >> x2 >> y1 >> y2;
    if (x1 > x2)
    {
    swap(x1, x2);
    }
    if (y1 > y2)
    {
    swap(y1, y2);
    }

    cout << Sum(x2+1, y2+1) - Sum(x1, y2+1) - Sum(x2+1, y1) + Sum(x1, y1) << endl;
    }
    else
    {
    int x, y;
    cin >> x >> y;
    if (str[0] == 'B')
    {
    /*if (vis[x+1][y+1] == 0)
    {
    Update(x+1, y+1, 1);
    vis[x+1][y+1] = 1;
    }
    */
    Update(x+1, y+1, 1);
    }
    else
    {
    /*if (vis[x+1][y+1] == 1)
    {
    Update(x+1, y+1, -1);
    vis[x+1][y+1] = 0;
    }
    */
    Update(x+1, y+1, 0);
    }
    }
    }
    return 0;
    }

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

    int n = 1001;
    int c[1010][1010];
    int dim[1010][1010];//记录黑白默认为黑

    int LowBit(int x)
    {
     return x&(-x);
    }

    void Update(int x, int y, int sc)//单点增减
    {
     for (int i = x; i <= n; i += LowBit(i))
     {
      for (int j = y; j <= n; j += LowBit(j))
      {

       c[i][j] += sc;
      }
     }
    }

    int Sum(int x, int y)//区域求和。
    {
     int sum = 0;
     for (int i = x; i > 0; i -= LowBit(i))
     {
      for (int j = y; j > 0; j -= LowBit(j))
      {
       sum += c[i][j];
      }
     }
     return sum;
    }

    int main()
    {
     int m;
     char str[5];
     cin >> m;
     while (m--)
     {
      scanf("%s", str);
      if (str[0] == 'Q')
      {
       int x1,x2, y1, y2;
       cin >> x1 >> x2 >> y1 >> y2;
       if (x1 > x2)//这里小心。
       {
        swap(x1, x2);
       }
       if (y1 > y2)
       {
        swap(y1, y2);
       }

       cout << Sum(x2+1, y2+1) - Sum(x1, y2+1) - Sum(x2+1, y1) + Sum(x1, y1) << endl;
      }
      else
      {
       int x, y;
       cin >> x >> y;
       if (str[0] == 'B')//注意,因为同一位置最多只能有1颗星星,所以亮的时候不能再加1,
       {
        if (dim[x+1][y+1] == 0)//只有黑的才可以变亮
        {
         Update(x+1, y+1, 1);
         dim[x+1][y+1] = 1;
        }
       }
       else
       {
        if (dim[x+1][y+1] == 1)//只有亮的才可以变黑
        {
         Update(x+1, y+1, -1);
         dim[x+1][y+1] = 0;
        }
       }
      }
     }
     return 0;
    }

  • 相关阅读:
    VS批处理命令使用
    python实现域账号登陆
    Sql Server 优化技巧
    Windows 2012 R2 安装net4.6.1
    Resharper报“Possible multiple enumeration of IEnumerable”
    京东模拟点击
    使用常规方法爬取猫眼电影
    关于断点调试
    看网络开发实战书笔记
    scrapy的request的meta参数是什么意思?
  • 原文地址:https://www.cnblogs.com/qiufeihai/p/2389332.html
Copyright © 2011-2022 走看看