Circle 树状数组

Description

You are given n points and two circles. The radius of the circle will be dynamical. Your task is to find how many points are under both circles at each time.

A point is under a circle iff the point is strictly inside the circle or on the border of the circle.

Input Description

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases.
For each case, the first line contains two integers n, m.(1 <= n, m <= 100000) Means the number of points and the number of queries.
Next n lines each contains two integer x, y(0 <= x, y <= 80000), describe a point.
Next line contains four integers x1, y1, x2, y2 (0 <= x1, y1, x2, y2 <= 80000), describe the center of two circles.
Next m lines each line contains two integers R1, R2, describe the radius of two circles.(1 <= R1, R2 <= 100000)

Output Description

For each query, output the number of points under both circles.

Sample Input

1
4 4
0 0
0 1
1 0
1 1
0 0 2 2
1 1
1 10
10 1
10 10

Sample Output

0
3
0
4碰见很多这样的题目了,哎,以为是几何题,题解出来了才发现是树状数组,多做做,多总结

 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdio>
 5 #include<string>
 6 #include<queue>
 7 #include<cmath>
 8 #include<vector>
 9
10 using namespace std;
11
12 #define mnx 104000
13 #define ll long long
14 #define inf 0x3f3f3f3f
15 #define lson l, m, rt << 1
16 #define rson m+1, r, rt << 1 | 1
17
18 const int N = mnx;
19 struct point{
20     int x, y;
21     point( int x = 0, int y = 0 ) : x(x), y(y) {}
22     point operator - ( const point &b ) const {
23         return point( x - b.x, y - b.y );
24     }
25     int length(){
26         ll len = (ll)x * x + (ll)y * y;
27         ll pt = sqrt( len );
28         if( pt * pt < len ) pt++;
29         return (int)pt;
30     }
31     bool operator < ( const point & b ) const{
32         return x < b.x;
33     }
34 }p[mnx], A, B;
35 struct rad{
36     int r1, r2, id;
37     bool operator < ( const rad & b ) const{
38         return r1 < b.r1;
39     }
40 }query[mnx];
41 int bit[mnx];
42 int sum( int x ){
43     int ret = 0;
44     while( x > 0 ){
45         ret += bit[x]; x -= x & -x;
46     }
47     return ret;
48 }
49 void add( int i, int x ){
50     while( i <= N ){
51         bit[i] += x;
52         i += i & -i;
53     }
54 }
55 int ans[mnx];
56 int main(){
57     int cas;
58     scanf( "%d", &cas );
59     while( cas-- ){
60         memset( bit, 0, sizeof(bit) );
61         int n, m;
62         scanf( "%d %d", &n, &m );
63         for( int i = 0; i < n; i++ ){
64             scanf( "%d %d", &p[i].x, &p[i].y );
65         }
66         scanf( "%d %d %d %d", &A.x, &A.y, &B.x, &B.y );
67         for( int i = 0; i < n; i++ ){
68             int dis1 = ( p[i] - A ).length();
69             int dis2 = ( p[i] - B ).length();
70             p[i] = point( dis1, dis2 );
71         }
72         sort( p, p + n );
73         for( int i = 0; i < m; i++ ){
74             scanf( "%d %d", &query[i].r1, &query[i].r2 );
75             query[i].id = i;
76         }
77         sort( query, query + m );
78         int j = 0;
79         for( int i = 0; i < m; i++ ){
80             while( j < n && p[j].x <= query[i].r1 ){
81                 add( p[j].y, 1 );
82                 j++;
83             }
84             ans[query[i].id] = sum( query[i].r2 );
85         }
86         for( int i = 0; i < m; i++ ){
87             printf( "%d\n", ans[i] );
88         }
89     }
90     return 0;
91 }


Circle 树状数组

时间: 2024-10-26 20:01:42

Circle 树状数组的相关文章

HDOJ 5338 ZZX and Permutations 线段树+树状数组

[题意]: 给一个排列加上表示循环的括号,问如何让1到n的对应的字典序最大. 从1开始贪心每个数字可以往三个地方走,右边第一个,跳转到左边的某一个,和自己构成循环 对于走到右边第一个的情况,只要判断右边的那个有没有被占据就可以了,如果可以和右边的互换,那么需要在线段树中将右边的数置为0 跳转到左边的某一个,一个数如果跳转到左边的某一个则说明左边的那个是括号开头这个数是括号结尾,用一个线段树查询区间里的最大值,由于括号间是不能重叠的,所以需要用树状数组二分一下左边界.如果这个数是要跳转到左边的某个

Why Did the Cow Cross the Road III(树状数组)

Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during

URAL 1521 War Games 2 树状数组解决约瑟夫环,输出离队顺序

1521. War Games 2 Time limit: 1.0 second Memory limit: 64 MB Background During the latest war games (this story is fully described in the problem "War games") the Minister of Defense of the Soviet Federation comrade Ivanov had a good chance to m

poj 2886 Who Gets the Most Candies? (树状数组+二分+反素数)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 11597   Accepted: 3616 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise

洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)

题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morn

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

(POJ 3067) Japan (慢慢熟悉的树状数组)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29295   Accepted: 7902 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas

【二维树状数组】See you~

https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A x y n1 :给格点(x,y)的值加n1 D x y n1: 给格点(x,y)的值减n1,如果现在格点的值不够n1,把格点置0 M x1 y1 x2 y2:(x1,y1)移动给(x2,y2)n1个 S x1 y1 x2 y2 查询子矩阵的和 [思路] 当然是二维树状数组 但是一定要注意:lowbi

Vijos P1066 弱弱的战壕【多解,线段树,暴力,树状数组】

弱弱的战壕 描述 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了-_-b. mx在他的基地附近建立了n个战壕,每个战壕都是一个独立的作战单位,射程可以达到无限(“mx不赢定了?!?”永恒[email protected][email protected]). 但是,战壕有一个弱点,就是只能攻击它的左下方,说白了就是横纵坐标都不大于它的点(mx:“我的战壕为什么这么菜”ToT).这样,永恒就可以从别的地方进攻摧毁战壕,从而消灭mx的部队. 战壕都有一个保护范围,同它的攻击