标签:hdu1556
3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
1 1 1 3 2 1
10951071 | 2014-07-06 16:14:52 | Time Limit Exceeded | 1556 | 3000MS | 584K | 400 B | G++ | 长木 |
#include <stdio.h> #include <string.h> #define maxn 100000 + 2 int arr[maxn]; int main() { int n, a, b; while(scanf("%d", &n), n){ memset(arr, 0, sizeof(arr)); for(int i = 0; i < n; ++i){ scanf("%d%d", &a, &b); while(a <= b) ++arr[a++]; } for(int i = 1; i <= n; ++i) if(i == n) printf("%d\n", arr[i]); else printf("%d ", arr[i]); } return 0; }
#include <stdio.h> #include <string.h> #define maxn 100002 #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 int tree[maxn << 2], n; void pushDown(int rt) { tree[rt << 1] += tree[rt]; tree[rt << 1 | 1] += tree[rt]; tree[rt] = 0; } void update(int left, int right, int l, int r, int rt) { if(left == l && right == r){ ++tree[rt]; return; } int mid = (l + r) >> 1; if(right <= mid) update(left, right, lson); else if(left > mid) update(left, right, rson); else{ update(left, mid, lson); update(mid + 1, right, rson); } } void query(int l, int r, int rt) { if(l == r){ if(l != n) printf("%d ", tree[rt]); else printf("%d\n", tree[rt]); return; } if(tree[rt]) pushDown(rt); int mid = (l + r) >> 1; query(lson); query(rson); } int main() { int a, b, i; while(scanf("%d", &n), n){ memset(tree, 0, sizeof(tree)); for(i = 0; i < n; ++i){ scanf("%d%d", &a, &b); update(a, b, 1, n, 1); } query(1, n, 1); } return 0; }
#include <stdio.h> #include <string.h> #define maxn 100002 int tree[maxn], n; int lowBit(int x){ return x & (-x); } void update(int pos, int val) { while(pos > 0){ tree[pos] += val; pos -= lowBit(pos); } } int getSum(int pos) { int sum = 0; while(pos <= n){ sum += tree[pos]; pos += lowBit(pos); } return sum; } int main() { int a, b, i; while(scanf("%d", &n), n){ memset(tree, 0, sizeof(tree)); for(i = 0; i < n; ++i){ scanf("%d%d", &a, &b); update(a - 1, -1); update(b, 1); } for(i = 1; i <= n; ++i) if(i != n) printf("%d ", getSum(i)); else printf("%d\n", getSum(i)); } return 0; }
#include <stdio.h> #include <string.h> #define maxn 100000 + 2 int arr[maxn]; int main() { int n, a, b; while(scanf("%d", &n), n){ memset(arr, 0, sizeof(arr)); for(int i = 0; i < n; ++i){ scanf("%d%d", &a, &b); ++arr[a]; --arr[b + 1]; } for(int i = 1, ans = 0; i <= n; ++i){ ans += arr[i]; if(i != n) printf("%d ", ans); else printf("%d\n", ans); } } return 0; }
HDOJ1556 Color the ball 【线段树】+【树状数组】+【标记法】,布布扣,bubuko.com
HDOJ1556 Color the ball 【线段树】+【树状数组】+【标记法】
标签:hdu1556
原文地址:http://blog.csdn.net/chang_mu/article/details/37342431