Color the ball
Total Submission(s): 8131 Accepted Submission(s): 4163
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,输入结束。
当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
1 1 1 3 2 1
Author
8600
Source
#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[N];
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[n]);
}
return 0;
}
二维区间更新
Matrix
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 52 Accepted Submission(s) : 24
Problem Description
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].
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].
Input
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.
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.
Output
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.
There is a blank line between every two continuous test cases.
Sample Input
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
Sample Output
1 0 0 1
Source
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[N][N];
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;
}