hdu1556 color the ball 树状数组区间更新单点查询(附线段树做法)与二维扩展
2014-06-20 16:50
633 查看
Color the ball
Total Submission(s): 8131 Accepted Submission(s): 4163[align=left]Problem Description[/align]
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
[align=left]Input[/align]
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
[align=left]Output[/align]
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
[align=left]Sample Input[/align]
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
[align=left]Sample Output[/align]
1 1 1
3 2 1
[align=left]Author[/align]
8600
[align=left]Source[/align]
HDU 2006-12 Programming Contest
#define DeBUG #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <string> #include <set> #include <sstream> #include <map> #include <bitset> using namespace std ; #define zero {0} #define INF 0x3f3f3f3f #define EPS 1e-6 typedef long long LL; const double PI = acos(-1.0); //#pragma comment(linker, "/STACK:102400000,102400000") inline int sgn(double x){return fabs(x) < EPS ? 0 :(x < 0 ? -1 : 1);} #define N 100005 int a ; int Maxn; inline int lowbit(int x) { return x&(-x); } void Add(int x,int val) { while(x>0) { a[x]+=val; x-=lowbit(x); } } int Sum(int x) { int sum=0; while(x<=Maxn) { sum+=a[x]; x+=lowbit(x); } return sum; } int main() { #ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin); #endif int n; while(scanf("%d", &n),n) { Maxn=n; memset(a,0,sizeof(a)); int s,t; for(int i=0;i<n;i++) { scanf("%d%d", &s,&t); Add(s-1,-1); Add(t,1); } for(int i=1;i<n;i++) { printf("%d ", Sum(i)); } printf("%d\n", Sum(n)); } return 0; }
线段树解法
#define DeBUG #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <string> #include <set> #include <sstream> #include <map> #include <bitset> using namespace std ; #define zero {0} #define INF 0x3f3f3f3f #define EPS 1e-6 typedef long long LL; const double PI = acos(-1.0); //#pragma comment(linker, "/STACK:102400000,102400000") inline int sgn(double x) { return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1); } #define N 100005 struct Node { int l, r; int mid; int val; }; Node tree[N * 20]; void build(int id, int l, int r) { Node &t = tree[id]; t.l = l; t.r = r; t.val = 0; t.mid = (l + r) >> 1; if (l == r)return; build((id << 1), l, t.mid); build((id << 1) | 1, t.mid + 1, r); } void update(int id, int l, int r) { Node &t = tree[id]; if (t.l == l && t.r == r) { t.val++; return; } if (t.mid >= r)update((id << 1), l, r); else if (t.mid < l)update((id << 1) | 1, l, r); else { update((id << 1), l, t.mid); update((id << 1) | 1, t.mid + 1, r); } } int ans; void get(int id, int d) { ans += tree[id].val; if (tree[id].l == tree[id].r && tree[id].l == d) return ; if (tree[id].mid >= d) get((id << 1), d); else get((id << 1) | 1, d); } int main() { #ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin); #endif int n; int a, b; while (scanf("%d", &n), n) { // memset(tree, 0, sizeof(tree)); build(1, 1, n); for (int i = 0; i < n; i++) { scanf("%d%d", &a, &b); update(1, a, b); } for (int i = 1; i < n; i++) { ans = 0; get(1, i); printf("%d ", ans); } ans = 0; get(1, n); printf("%d\n", ans); } 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 ); } return 0; }
二维区间更新
Matrix
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 52 Accepted Submission(s) : 24
[align=left]Problem Description[/align]
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N).
We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change
it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions.
1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2).
2. Q x y (1 <= x, y <= n) querys A[x, y].
[align=left]Input[/align]
The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case.
The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2
y2", which has been described above.
[align=left]Output[/align]
For each querying output one line, which has an integer representing A[x, y].
There is a blank line between every two continuous test cases.
[align=left]Sample Input[/align]
1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1
[align=left]Sample Output[/align]
1
0
0
1
[align=left]Source[/align]
PKU
#define DeBUG #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <string> #include <set> #include <sstream> #include <map> #include <bitset> using namespace std ; #define zero {0} #define INF 0x3f3f3f3f #define EPS 1e-6 typedef long long LL; const double PI = acos(-1.0); //#pragma comment(linker, "/STACK:102400000,102400000") inline int sgn(double x) { return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1); } #define N 1005 int C ; int Sum2(int x, int y) { int sum = 0; for (int i = x; i < N; i += (i & -i)) { for (int j = y; j < N; j += (j & -j)) { sum += C[i][j]; } } return sum; } void Add2(int x, int y, int d) { for (int i = x; i > 0; i -= (i & -i)) { for (int j = y; j > 0; j -= (j & -j)) { C[i][j] += d; } } } int cnt = 1; int main() { #ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin); #endif int T; scanf("%d", &T); while (T--) { memset(C,0,sizeof(C)); if (cnt++ != 1) printf("\n"); int n, m; int x1, y1, x2, y2; char op[100]; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%s", op); if (op[0] == 'C') { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 > x2)swap(x1, x2); if (y1 > y2)swap(y1, y2); Add2(x1, y1, 1); Add2(x2 + 1, y1, -1); Add2(x1, y2 + 1, -1); Add2(x2 + 1, y2 + 1, 1); } else if (op[0] == 'Q') { scanf("%d%d", &x1, &y1); printf("%d\n", Sum2(x1 + 1, y1 + 1) %2); } } } return 0; }
相关文章推荐
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- HDU1556:Color the ball(线段树区间更新单点求值)&&树状数组解法
- HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)
- hdu1556 Color the ball【树状数组 || 线段树 || 技巧*区间修改】
- 【树状数组-区间更新】hdu1556 Color the ball
- HDU 1556 Color the ball【线段树区间更新,一次查询+数组模拟】
- HDU 1556 Color the ball(树状数组区间更新点查询)
- HDu 1556 Color the ball【线段树&&树状数组】区间更新,单点查询
- HDU1556 Color the ball【树状数组】【区间更新】
- hdoj 1556 Color the ball 【线段树 + lazy区间更新】 【树状数组】
- HDU 1556 Color the ball 很典型的更新区间查找点的题(线段树树状数组两种解法)
- hdu 1556 Color the ball (树状数组解决区间覆盖+单点查询)
- hdu1556 Color the ball(树状数组 修改区间,查找值)
- Color the ball(hdu1556)(hash)或(线段树,区间更新)
- HDU 1556 Color the ball(树状数组区间更新)
- Color the ball(hdu1556)(hash)或(线段树,区间更新)
- hdu1556 Color the ball(线段树区间更新)
- Color the ball 线段树 区间更新但点查询
- HDU1556:Color the ball(线段树区间更新)
- HDU 1556 Color the ball (树状数组区间更新)