Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8272 Accepted Submission(s): 4239
Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
Author
8600
普通方法毫无意外的超时:
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;
}
线段树:2014-10-27 23:07:28更新 671ms
#include <stdio.h>
#include <string.h>
#define maxn 100002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
struct Node {
int val, lazy;
} T[maxn << 2];
int n, num;
void pushDown(int rt) {
T[rt<<1].lazy += T[rt].lazy;
T[rt<<1|1].lazy += T[rt].lazy;
T[rt<<1].val += T[rt].lazy;
T[rt<<1|1].val += T[rt].lazy;
T[rt].lazy = 0;
}
void build(int l, int r, int rt) {
T[rt].lazy = T[rt].val = 0;
if(l == r) return;
int mid = (l + r) >> 1;
build(lson);
build(rson);
}
void update(int L, int R, int l, int r, int rt) {
if(L == l && R == r) {
++T[rt].val;
++T[rt].lazy;
return;
}
if(T[rt].lazy) pushDown(rt);
int mid = (l + r) >> 1;
if(R <= mid) update(L, R, lson);
else if(L > mid) update(L, R, rson);
else {
update(L, mid, lson);
update(mid + 1, R, rson);
}
}
void query(int l, int r, int rt) {
if(l == r) {
printf("%d", T[rt].val);
putchar(++num == n ? '\n' : ' ');
return;
}
int mid = (l + r) >> 1;
if(T[rt].lazy) pushDown(rt);
query(lson);
query(rson);
}
int main() {
// freopen("stdin.txt", "r", stdin);
int a, b, i;
while(scanf("%d", &n), n) {
build(1, n, 1);
for(i = 0; i < n; ++i) {
scanf("%d%d", &a, &b);
update(a, b, 1, n, 1);
}
num = 0;
query(1, n, 1);
}
return 0;
}
11020236 |
2014-07-13 21:31:02 |
Accepted |
1556 |
984MS |
1772K |
1045 B |
G++ |
#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;
}
树状数组:(模式二:区间修改,单点查询,方向与模式一正好相反)
11025667 |
2014-07-14 16:04:19 |
Accepted |
1556 |
703MS |
596K |
677 B |
G++ |
#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;
}
另外在网上看到了一个巧妙的方法:这个方法主要是对自增自减区间进行标记,这样能减少大量遍历自增的时间。
10951265 |
2014-07-06 16:35:21 |
Accepted |
1556 |
593MS |
596K |
430 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);
++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;
}