zoukankan      html  css  js  c++  java
  • 1266

    1266 - Points in Rectangle
    Time Limit: 2 second(s) Memory Limit: 32 MB

    As the name says, this problem is about finding the number of points in a rectangle whose sides are parallel to axis. All the points and rectangles consist of 2D Cartesian co-ordinates. A point that lies in the boundary of a rectangle is considered inside.

    Input

    Input starts with an integer T (≤ 10), denoting the number of test cases.

    Each case starts with a line containing an integer q (1 ≤ q ≤ 30000) denoting the number of queries. Each query is either one of the following:

    1)      0 x y, meaning that you have got a new point whose co-ordinate is (x, y). But the restriction is that, if a point (x, y) is already listed, then this query has no effect.

    2)      1 x1 y1 x2 y2 meaning that you are given a rectangle whose lower left co-ordinate is (x1, y1) and upper-right corner is (x2, y2); your task is to find the number of points, given so far, that lie inside this rectangle. You can assume that (x1 < x2, y1 < y2).

    You can assume that the values of the co-ordinates lie between 0 and 1000 (inclusive).

    Output

    For each case, print the case number in a line first. Then for each query type (2), you have to answer the number of points that lie inside that rectangle. Print each of the results in separated lines.

    Sample Input

    Output for Sample Input

    1

    9

    0 1 1

    0 2 6

    1 1 1 6 6

    1 2 2 5 5

    0 5 5

    1 0 0 6 5

    0 3 3

    0 2 6

    1 2 1 10 10

    Case 1:

    2

    0

    2

    3

    Note

    Dataset is huge, use faster I/O methods.


    PROBLEM SETTER: JANE ALAM JAN
    思路:二维树状数组;
    模板题:
     1 #include<stdio.h>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<stdlib.h>
     5 #include<queue>
     6 #include<string.h>
     7 using namespace std;
     8  int bit[1005][1005];
     9 bool  flag[1005][1005];
    10 int lowbit(int x)
    11 {
    12         return x&(-x);
    13 }
    14 void add(int x1,int y1)
    15 {
    16         int i,j;
    17         for(i = x1; i <= 1001; i+=lowbit(i))
    18                 for(j = y1; j <= 1001; j+=lowbit(j))
    19                 {
    20                         bit[i][j]++;
    21                 }
    22 }
    23 int ask(int x1,int y1)
    24 {
    25         int i,j;
    26         int sum = 0;
    27         for(i = x1; i > 0; i-=lowbit(i))
    28                 for(j = y1; j > 0; j-=lowbit(j))
    29                 {
    30                         sum+=bit[i][j];
    31                 }
    32         return sum;
    33 }
    34 int main(void)
    35 {
    36         int T;
    37         scanf("%d",&T);
    38         int __ca = 0,q;
    39         while(T--)
    40         {
    41                 __ca++;
    42                 memset(bit,0,sizeof(bit));
    43                 memset(flag,0,sizeof(flag));
    44                 scanf("%d",&q);
    45                 printf("Case %d:
    ",__ca);
    46                 int val ;
    47                 int x,y,x1,y1;
    48                 while(q--)
    49                 {
    50                         scanf("%d",&val);
    51                         if(!val)
    52                         {
    53                                 scanf("%d %d",&x,&y);
    54                                 x+=1;
    55                                 y+=1;
    56                                 if(!flag[x][y])
    57                                 {
    58                                         add(x,y);
    59                                         flag[x][y]=true;
    60                                 }
    61                         }
    62                         else
    63                         {
    64                                 scanf("%d %d %d %d",&x,&y,&x1,&y1);
    65                                 x++;y++;x1++;y1++;
    66                                 int sum = ask(x1,y1);
    67                                 sum += ask(x-1,y-1);
    68                                 sum -= ask(x-1,y1);
    69                                 sum -= ask(x1,y-1);
    70                                 printf("%d
    ",sum);
    71                         }
    72                 }
    73         }
    74         return 0;
    75 }
    76  

    复杂度:n*log(n)^2;

    油!油!you@
  • 相关阅读:
    三种省市级联下拉列表的写法
    三种省市级联下拉列表的写法
    SQL经典试题(mysql)
    60行代码俄罗斯方块
    ibatis xml中配置信息详解
    60行代码俄罗斯方块
    xinetd
    csh and tcsh
    xinetd restart
    bash sh
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5890545.html
Copyright © 2011-2022 走看看