标签:des style blog http color java os io strong
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9502 Accepted Submission(s):
4872
#include<stdio.h> #include<string.h> int hash[100002]; int main() { int n,i,x,y,sum; while(scanf("%d",&n),n) { memset(hash,0,sizeof(hash)); for(i=1;i<=n;i++) { scanf("%d%d",&x,&y); hash[x]++; hash[y+1]--; } printf("%d",hash[1]); sum=hash[1]; for(i=2;i<=n;i++) { sum+=hash[i]; printf(" %d",sum); } printf("\n"); } return 0; }
线段树区间跟新。
#include<iostream> #include<cstring> #include<string> #include<cstdio> #include<algorithm> #define Lson left,mid,n<<1 #define Rson mid+1,right,n<<1|1 const int MAX=100001; const int Max=100000<<2; int s[Max]; int m; using namespace std; typedef struct Node { int left; int right; int value; }; Node node[Max]; void build_tree(int left,int right,int n) { node[n].left=left; node[n].right=right; node[n].value=0; if(node[n].left==node[n].right) return ; int mid=(node[n].left+node[n].right)>>1;//位运算相当于除以2 build_tree(Lson); build_tree(Rson); } void query(int left,int right,int n)//查询 { if(node[n].left>=left&&node[n].right<=right)//找到要涂颜色区间,这里很重要,表示用区间后再慢慢回归到子节点上 { node[n].value+=1; return; } int mid=(node[n].left+node[n].right)>>1; if(right<=mid) query(left,right,n<<1); else if(left>mid) query(left,right,n<<1|1);//相当于2*n+1 else { query(Lson); query(Rson); } } void sum(int n) { if(node[n].left==node[n].right) { s[m]=node[n].value; m+=1; return; } node[n<<1].value+=node[n].value; node[n<<1|1].value+=node[n].value; sum(n<<1); sum(n<<1|1); } int main() { int n,i,j,a,b; while(scanf("%d",&n)&&n) { build_tree(1,n,1); for(i=0;i<n;i++) { scanf("%d%d",&a,&b); query(a,b,1); } m=0; sum(1); for(i=0;i<m;i++) { if(i==m-1) printf("%d\n",s[i]); else printf("%d ",s[i]); } } return 0; }
Color the ball(hdu1556)(hash)或(线段树,区间更新)
标签:des style blog http color java os io strong
原文地址:http://www.cnblogs.com/yuyixingkong/p/3930310.html