zoukankan      html  css  js  c++  java
  • LightOJ 1266

    题目链接


    题意:给一个平面,有两个操作

    • 0 x y 在(x,y)处放置一个点
    • 1 x1 y1 x2 y2 查询左下角为(x1,y1),右上角为(x2,y2)的矩形区域包含多少个点(包括边界)

    使用二维树状数组,矩形面积相减得到目标区域的和
    二维数组模板题

    "math.h" 中包含了对变量 x1,y1 的定义

    题意中的位置是0~1000


    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <vector>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <stack>
    #include <string>
    //#include <math.h>
    #include <bitset>
    #include <ctype.h>
    using namespace std;
    typedef pair<int,int> P;
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    //const double PI = acos(-1.0);
    const double eps = 1e-9;
    const int N = 1200 + 5;
    const int mod = 1e9 + 7;
    int t, kase = 0;
    int c[N][N];
    int n;
    void init()
    {
        memset(c, 0, sizeof(c));
    }
    inline int lowbit(int i)
    {
        return i&(-i);
    }
    void add(int x, int y, int val)
    {
        for(int i = x; i <= n; i += lowbit(i))
        {
            for(int j = y; j <= n; j += lowbit(j))
            {
                c[i][j] += val;
            }
        }
    }
    int sum(int x, int y)
    {
        int ans = 0;
        for(int i = x; i > 0; i -= lowbit(i))
            for(int j = y; j > 0; j -= lowbit(j))
                ans += c[i][j];
        return ans;
    }
    int query(int x1, int y1, int x2, int y2)
    {
        return sum(x2,y2) - sum(x2, y1-1) - sum(x1-1, y2) + sum(x1-1, y1-1);
    }
    int x1,x2,y1,y2,q,type;
    int main()
    {
        n = 1001;
        scanf("%d", &t);
        while(t--)
        {
            memset(c, 0, sizeof(c));
            printf("Case %d:
    ", ++kase);
            scanf("%d", &q);
            while(q--)
            {
                scanf("%d", &type);
                if(type == 0)
                {
                    scanf("%d%d", &x1,&y1);
                    x1++,y1++;
                    if(!query(x1,y1,x1,y1))
                        add(x1,y1,1);
                }
                else
                {
                    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
                    x1++,y1++,x2++,y2++;
                    printf("%d
    ", query(x1,y1,x2,y2));
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Batch
    Batch
    《mysql必知必会》学习_第17章
    指针实现时间复杂度为O(n*logN)的排序算法(归并排序算法)
    《自控力》读书笔记
    Leetcode 49. Group Anagrams (242.Valid Anagram)
    Leetcode43. Multiply Strings
    Leetcode 233. Number of Digit One
    Python 格式化输出占位符替换
    Python字符串拼接
  • 原文地址:https://www.cnblogs.com/Alruddy/p/7471108.html
Copyright © 2011-2022 走看看