Color the ballTime
limit:9000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 11387 Accepted Submission (s): 5680
Problem descriptionn a row of balloons, numbered from left to right to 1,2,3....N. Each time a given 2 integers a B (a <= B), Lele will be riding his "little Pigeon" brand electric car from balloon a start to balloon B to each balloon painted once color. But n times after Lele has forgotten the first balloon has been painted several times, can you help him to figure out how many times each balloon has been painted color?
Input each test instance first behaves as an integer N, (n <= 100000). The next N rows, each line consists of 2 integers a B (1 <= a <= b <= N).
When n = 0, the input ends.
Output one row for each test instance, including n integers, and the number of numbers representing the number of times the balloon was painted in total.
Sample Input
31 12 23 331 11 21 30
Sample Output
1 1 13) 2 1
Author8600
Sourcehdu 2006-12 Programming Contest
Recommendll | We have carefully selected several similar problems for you:1542 1698 1541 3397 3333
Idea: Test instructions is easy to understand just use the general method will time out, so need to use some time-saving algorithm, tree array and line segment tree exactly match this condition
Tree-like array
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <string.h> #include <stdlib.h>using namespace Std;int a[100010];int n;int lowbit ( int x) {return x& (-X);} void build (int x,int y) {while (x<=n) {a[x] + = y; x + = Lowbit (x); }}int sum (int x)//sum {int s=0; while (x>0) {s=s+a[x]; X=x-lowbit (x); } return s;} int main () {while (scanf ("%d", &n)!=eof) {if (n = = 0) {break; } int x, y; memset (A,0,sizeof (a)); for (int i=1;i<=n;i++) {scanf ("%d%d", &x,&y); Build (x,1); Build (Y+1,-1); } for (int i=1;i<=n;i++) {if (i = = N) {printf ("%d\n", sum (i)); } else {printf ("%d", sum (i)); }}}} return 0;}
Segment Tree:
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include < String.h> #include <stdlib.h>using namespace std;const int maxn = 100010;struct node{int l; int R; int LZ; int ans;} Q[maxn<<4];int n;void pushdown (int rt,int lr) {if (Q[rt].lz) {Q[rt<<1].ans + = (lr-(lr>>1)) * Q[rt].lz; Q[rt<<1|1].ans + = (lr>>1) *q[rt].lz; Q[rt<<1].lz + = Q[rt].lz; Q[rt<<1|1].lz + = Q[rt].lz; Q[rt].lz = 0; }}void Build (int l,int R,int rt) {q[rt].l = l; Q[RT].R = R; Q[rt].ans = 0; Q[rt].lz = 0; if (L = = r) {return; } int mid = (l+r) >>1; Build (l,mid,rt<<1); Build (mid+1,r,rt<<1|1); Q[rt].ans = Q[rt<<1].ans + Q[rt<<1|1].ans;} void Updata (int ll,int rr,int k,int l,int r,int RT) {if (Ll>r | | rr<l) {return; } if (Ll<=l && rr>=r) {Q[rt].ans = Q[rt].ans + (r-l+1) *k; Q[rt].lz + = k; return; } pushdown (rt,r-l+1); int mid = (l+r) >>1; if (mid>=ll) {updata (ll,rr,k,l,mid,rt<<1); } if (Rr>mid) {updata (ll,rr,k,mid+1,r,rt<<1|1); } Q[rt].ans = Q[rt<<1].ans + Q[rt<<1|1].ans;} void Qurry (int l,int r,int RT) {if (L = = r) {if (L = = N) {printf ("%d\n", Q[rt].ans); } else {printf ("%d", Q[rt].ans); } return; } pushdown (rt,r-l+1); int mid = (l+r) >>1; Qurry (l,mid,rt<<1); Qurry (mid+1,r,rt<<1|1);} int main () {while (scanf ("%d", &n)!=eof) {if (n = = 0) {break; } build (1,n,1); int x, y; for (int i=0; i<n; i++) {scanf ("%d%d", &x,&y); Updata (x,y,1,1,n,1); } qurry (1,n,1); } return 0;}
HDU 1566 Color The ball (tree array or segment tree)