首页 > 代码库 > [线段树]HDU5091 Beam Cannon
[线段树]HDU5091 Beam Cannon
题意:给n, w, h (1 < = N < = 10000, 1 < = W < = 40000, 1 < = H < = 40000)
w*h是可以射到的范围
然后给n个点的坐标x, y (-20000 < = x,y < = 20000)
问 射一次 能射到几个点.
很裸的扫描线线段树
这么水的题不知为何在现场没有写啊??????
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <climits> 5 #include <cctype> 6 #include <cmath> 7 #include <string> 8 #include <sstream> 9 #include <iostream> 10 #include <algorithm> 11 #include <iomanip> 12 using namespace std; 13 #include <queue> 14 #include <stack> 15 #include <vector> 16 #include <deque> 17 #include <set> 18 #include <map> 19 typedef long long LL; 20 typedef long double LD; 21 #define pi acos(-1.0) 22 #define lson l, m, rt<<1 23 #define rson m+1, r, rt<<1|1 24 typedef pair<int, int> PI; 25 typedef pair<int, PI> PP; 26 #ifdef _WIN32 27 #define LLD "%I64d" 28 #else 29 #define LLD "%lld" 30 #endif 31 //#pragma comment(linker, "/STACK:1024000000,1024000000") 32 //LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;} 33 //inline int read(){char ch=‘ ‘;int ans=0;while(ch<‘0‘ || ch>‘9‘)ch=getchar();while(ch<=‘9‘ && ch>=‘0‘){ans=ans*10+ch-‘0‘;ch=getchar();}return ans;} 34 //inline void print(LL x){printf(LLD, x);puts("");} 35 //inline void read(double &x){char c = getchar();while(c < ‘0‘) c = getchar();x = c - ‘0‘; c = getchar();while(c >= ‘0‘){x = x * 10 + (c - ‘0‘); c = getchar();}} 36 37 const int maxn = 100005; 38 int X[maxn<<3]; 39 struct Seg 40 { 41 int h , l , r, s; 42 Seg() {} 43 Seg(int a,int b,int c,int d) : l(a) , r(b) , h(c) , s(d) {} 44 bool operator < (const Seg &cmp) const 45 { 46 return h < cmp.h; 47 } 48 } ss[maxn<<3]; 49 int len[maxn<<3],cnt[maxn<<3]; 50 void PushUp(int rt) 51 { 52 len[rt]=max(len[rt<<1], len[rt<<1|1]); 53 } 54 void PushDown(int rt) 55 { 56 if(cnt[rt]) 57 { 58 cnt[rt<<1]+=cnt[rt]; 59 cnt[rt<<1|1]+=cnt[rt]; 60 len[rt<<1]+=cnt[rt]; 61 len[rt<<1|1]+=cnt[rt]; 62 cnt[rt]=0; 63 } 64 } 65 void Update(int L, int R, int c, int l , int r, int rt) 66 { 67 if(L<=l && r<=R) 68 { 69 cnt[rt]+=c; 70 len[rt]+=c; 71 return ; 72 } 73 // PushDown(rt); 74 int m=(l+r)>>1; 75 if(L<=m) 76 Update(L, R, c, lson); 77 if(m<R) 78 Update(L, R, c, rson); 79 // PushUp(rt); 80 len[rt]=max(len[rt<<1], len[rt<<1|1])+cnt[rt]; 81 } 82 int main() 83 { 84 #ifndef ONLINE_JUDGE 85 freopen("in.txt", "r", stdin); 86 freopen("out.txt", "w", stdout); 87 #endif 88 int n; 89 while(~scanf("%d", &n)) 90 { 91 if(n<0) 92 break; 93 int w, h; 94 scanf("%d%d", &w, &h); 95 int m=0; 96 while(n--) 97 { 98 int a, b; 99 scanf("%d%d", &a, &b);100 X[m]=a;101 ss[m++]=Seg(a, a+w, b, 1);102 X[m]=a+w;103 ss[m++]=Seg(a, a+w, b+h, -1);104 }105 sort(X, X+m);106 sort(ss, ss+m);107 int k=unique(X, X+m)-X;108 int ans=0;109 memset(len, 0, sizeof(len));110 memset(cnt, 0, sizeof(cnt));111 for(int i=0;i<m;i++)112 {113 int l=lower_bound(X, X+k, ss[i].l)-X;114 int r=lower_bound(X, X+k, ss[i].r)-X;115 Update(l, r, ss[i].s, 0, k-1, 1);116 ans=max(ans, len[1]);117 }118 printf("%d\n", ans);119 }120 return 0;121 }
[线段树]HDU5091 Beam Cannon
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。