HDU - 1556 - Color the ball ( 差分 )

题目链接:点击进入

题目

思路

每次 [ a , b ] 颜色次数+1,这不典型区间修改,最后求每个气球颜色修改次数,区间操作即对差分数组操作,对差分数组求前缀和就是原数组,因此此题差分。

代码

// Problem: Color the ball
// Contest: Virtual Judge - HDU
// URL: https://vjudge.net/problem/HDU-1556
// Memory Limit: 32 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize(3)//O3
//#pragma GCC optimize(2)//O2
#include
#include
#include
#include
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
//#define int long long
//#define double long double
//#define rep(i,x,y) for(register int i = x; i <= y;++i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=1e6+10;
const int mod=1e9+7;
const double eps=1e-9;
const int N=5e3+10;
/*--------------------------------------------*/
inline int read()
{
    int k = 0, f = 1 ;
    char c = getchar() ;
    while(!isdigit(c)){if(c == '-') f = -1 ;c = getchar() ;}
    while(isdigit(c)) k = (k << 1) + (k << 3) + c - 48 ,c = getchar() ;
    return k * f ;
}
/*--------------------------------------------*/

int n,c[maxn],dis[maxn];

int main() 
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	while(cin>>n)
	{
		if(n==0) break;
		for(int i=1;i<=n;i++)
		{
			int l,r;
			cin>>l>>r;
			dis[l]++;
			dis[r+1]--;
		}
		for(int i=1;i<=n;i++)
		{
			dis[i]=dis[i]+dis[i-1];
			if(i==n)
				cout<<dis[i]<<endl;
			else
				cout<<dis[i]<<' ';
		}
		for(int i=1;i<=n+1;i++) dis[i]=0;
	}
    return 0;
} 

你可能感兴趣的:(简单算法/思维)