Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11566 Accepted Submission(s): 5784
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
1 1 1 3 2 1
#include<stdio.h> int ans; int ball[100000+10]; struct record { int left,right,sum; }num[1000000+10]; void build(int left,int right,int node)//建立 { int mid; num[node].left=left; num[node].right=right; num[node].sum=0; if(left==right)//到单个点结束 { return ; } mid=(left+right)>>1; build(left,mid,node*2); build(mid+1,right,node*2+1); } void update(int left,int right,int node) { int mid; if(num[node].left==left&&num[node].right==right) { num[node].sum++;//该区间每个气球都自增一 return ; } if(num[node].left==num[node].right)//搜索到单个点 return ; mid=(num[node].left+num[node].right)>>1; if(mid>=right) update(left,right,node*2); else { if(mid<left) update(left,right,node*2+1); else { update(left,mid,node*2); update(mid+1,right,node*2+1); } } } void query(int pos,int node) { int mid; ans+=num[node].sum; if(num[node].left==num[node].right)//到当前点 { return ; } mid=(num[node].left+num[node].right)>>1; if(pos>mid) { query(pos,node*2+1); } else { query(pos,node*2); } } int main() { int n,i,j; int x,y; while(scanf("%d",&n)&&(n!=0)) { build(1,n,1); for(i=1;i<=n;i++) { scanf("%d%d",&x,&y); update(x,y,1); } for(i=1;i<=n;i++) { ans=0; query(i,1); if(i>1) printf(" "); printf("%d",ans); } printf("\n"); } return 0; }
#include<cstdio> #include<cstring> #define MAX 100000+10 using namespace std; int sum[MAX<<2]; int add[MAX<<2]; //int v;//区间增加值 void PushUp(int o) { sum[o] = sum[o<<1] + sum[o<<1|1]; } void PushDown(int o, int m) { if(add[o]) { add[o<<1] += add[o]; add[o<<1|1] += add[o]; sum[o<<1] += add[o] * (m-(m>>1)); sum[o<<1|1] += add[o] * (m>>1); add[o] = 0; } } void update(int o, int l, int r, int L, int R) { if(L <= l && R >= r) { add[o] += 1; sum[o] += 1 * (r-l+1); return ; } PushDown(o, r-l+1); int mid = (r+l) >> 1; if(L <= mid) update(o<<1, l, mid, L, R); if(R >mid) update(o<<1|1, mid+1, r, L, R); PushUp(o); } int query(int o, int l, int r, int L, int R) { if(L <= l && R >= r) { return sum[o]; } PushDown(o, r-l+1); int mid = (r+l) >> 1; int res = 0; if(L <= mid) res += query(o<<1, l, mid, L, R); if(R >mid) res += query(o<<1|1, mid+1, r, L, R); return res; } int main() { int n, i; int a, b;//处理区间 while(scanf("%d", &n),n) { memset(sum, 0, sizeof(sum)); memset(add, 0, sizeof(add)); for(i = 0;i < n; i++) { scanf("%d%d", &a, &b); update(1, 1, n, a, b); } for(i = 1;i <= n; i++) { if(i > 1) printf(" "); printf("%d",query(1, 1, n, i, i)); } printf("\n"); } return 0; }
#include<cstdio> #include<cstring> #define MAX 100000+10 using namespace std; int sum[MAX<<2]; int add[MAX<<2]; //int v;//区间增加值 void PushUp(int o) { sum[o] = sum[o<<1] + sum[o<<1|1]; } void PushDown(int o, int m) { if(add[o]) { add[o<<1] += add[o]; add[o<<1|1] += add[o]; sum[o<<1] += add[o] * (m-(m>>1)); sum[o<<1|1] += add[o] * (m>>1); add[o] = 0; } } void build(int o,int l,int r) { add[o] = sum[o] = 0; if(l == r) return ; int mid = (r+l) >> 1; build(o<<1, l, mid); build(o<<1|1, mid+1, r); PushUp(o); } void update(int o, int l, int r, int L, int R) { if(L <= l && R >= r) { add[o] += 1; sum[o] += 1 * (r-l+1); return ; } PushDown(o, r-l+1); int mid = (r+l) >> 1; if(L <= mid) update(o<<1, l, mid, L, R); if(R >mid) update(o<<1|1, mid+1, r, L, R); PushUp(o); } int query(int o, int l, int r, int L, int R) { if(L <= l && R >= r) { return sum[o]; } PushDown(o, r-l+1); int mid = (r+l) >> 1; int res = 0; if(L <= mid) res += query(o<<1, l, mid, L, R); if(R >mid) res += query(o<<1|1, mid+1, r, L, R); return res; } int main() { int n, i; int a, b;//处理区间 while(scanf("%d", &n),n) { build(1, 1, n); for(i = 0;i < n; i++) { scanf("%d%d", &a, &b); update(1, 1, n, a, b); } for(i = 1;i <= n; i++) { if(i > 1) printf(" "); printf("%d",query(1, 1, n, i, i)); } printf("\n"); } return 0; }
#include<stdio.h> #include<string.h> #define MAX 100000+10 int c[MAX],n; int lowbit(int x) { return x&(-x); } int sum(int x) { int s=0; while(x<=n) { s+=c[x]; x+=lowbit(x); } return s; } void update(int x,int d) { while(x>0) { c[x]+=d; x-=lowbit(x); } } int main() { int i; int a,b; while(scanf("%d",&n),n) { memset(c,0,sizeof(c)); for(i=0;i<n;i++) { scanf("%d%d",&a,&b); update(b,1);//1到b全部加一 update(a-1,-1);//1到b-1全部减一 } for(i=1;i<=n;i++) { if(i>1) printf(" "); printf("%d",sum(i)); } printf("\n"); } return 0; }
#include "stdio.h" int main() { int j,n,i,a,m,b; while(scanf("%d",&n),n) { int c[100001]={0}; j=n; m=0; while(j--) { scanf("%d %d",&a,&b); c[a]++; c[b+1]--; } for(i=1;i<n;i++) { m+=c[i]; printf("%d ",m); } printf("%d\n",m+c[n]); } return 0; }