首页 > 代码库 > hdu1556-Color the ball
hdu1556-Color the ball
题目链接 http://bak.vjudge.net/problem/HDU-1556
解题思路
直接线段树。
代码
#include<stdio.h>#include<string.h>#define MAX_SIZE 100010//#define LOCALstruct node { int left, right; int count;}tree[MAX_SIZE*4];int now;int n;void build(int l, int r, int root){ tree[root].left = l; tree[root].right = r; tree[root].count = 0; if(l == r) return; int mid = (l + r) / 2; build(l, mid, root*2); build(mid+1, r, root*2+1);}void update(int l, int r, int root){ if(tree[root].left == l && r == tree[root].right) { tree[root].count++; return ; } if(tree[root].count != 0) { tree[2*root].count += tree[root].count; tree[2*root+1].count += tree[root].count; tree[root].count = 0; } int mid = (tree[root].left + tree[root].right) / 2; if(r <= mid) update(l, r, root*2); else if(mid < l) update(l, r, root*2+1); else { update(l, mid, root*2); update(mid+1, r, root*2+1); }}void cal(int root){ if(tree[root].left == tree[root].right) { if(now != n) printf("%d ", tree[root].count); else printf("%d\n", tree[root].count); now++; return ; } if(tree[root].count != 0) { tree[2*root].count += tree[root].count; tree[2*root+1].count += tree[root].count; tree[root].count = 0; } cal(root*2); cal(root*2+1);}int main(){ #ifdef LOCAL freopen("ans.txt", "w", stdout); #endif scanf("%d", &n); while(n != 0) { build(1, n, 1); int x, y; now = 1; for(int i=0; i<n; i++) { scanf("%d%d", &x, &y); update(x, y, 1); } cal(1); scanf("%d", &n); } return 0;}
hdu1556-Color the ball
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。