码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1556

时间:2015-04-04 22:27:25      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <cstring>
#include <iostream>
#include <algorithm>

#define REP(i,N) for (int i = 0;i < (N);i++)
#define REP_1(i,N) for (int i = 1;i < (N);i++)
#define REP_2(i,be,en) for (int i = (be);i < (en);i++)
#define DWN(i,N) for (int i = (N);i >= 0;i--)
#define DWN_1(i,N) for (int i = (N);i >= 1;i--)
#define DWN_2(i,en,be) for (int i = (en);i >= (be);i--)
#define FR(N) freopen((N),"r",stdin)
#define FW(N) freopen((N),"w",stdout)
#define MAXN 100010
#define GETS(ch) fgets((ch),MAXN,stdin)
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;

typedef long long LL;

typedef map<int,LL> MINT;
typedef map<char,int> MCH;
typedef map<string,int> MSTR;

typedef vector<int> VINT;
typedef set<LL> SINT;
typedef pair<int,int> PINT;

LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>=‘0‘ && x<=‘9‘)){if(x==‘-‘)f=-1;x=getchar();}
    while(x>=‘0‘ && x<=‘9‘) ret=ret*10+x-‘0‘, x=getchar();
    return ret*f;
}

int a[MAXN];

int main() {
	int N;
	//FR("1.txt");
	while (cin >> N,N) {
		memset(a,0,sizeof(a));
		REP(i,N) {
			int x,y;
			cin >> x >> y;
			a[x]++,a[y + 1]--;
		}
		REP_1(i,N + 1) {
			a[i] += a[i - 1];
		}
		REP_1(i,N) {
			cout << a[i] << " ";
		}
		cout << a[N] << endl;
	}
}

 

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <cstring>
#include <iostream>
#include <algorithm>

#define REP(i,N) for (int i = 0;i < (N);i++)
#define REP_1(i,N) for (int i = 1;i < (N);i++)
#define REP_2(i,be,en) for (int i = (be);i < (en);i++)
#define DWN(i,N) for (int i = (N);i >= 0;i--)
#define DWN_1(i,N) for (int i = (N);i >= 1;i--)
#define DWN_2(i,en,be) for (int i = (en);i >= (be);i--)
#define FR(N) freopen((N),"r",stdin)
#define FW(N) freopen((N),"w",stdout)
#define MAXN 100010
#define GETS(ch) fgets((ch),MAXN,stdin)
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;

typedef long long LL;

typedef map<int,LL> MINT;
typedef map<char,int> MCH;
typedef map<string,int> MSTR;

typedef vector<int> VINT;
typedef set<LL> SINT;
typedef pair<int,int> PINT;

LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>=‘0‘ && x<=‘9‘)){if(x==‘-‘)f=-1;x=getchar();}
    while(x>=‘0‘ && x<=‘9‘) ret=ret*10+x-‘0‘, x=getchar();
    return ret*f;
}

int N;
int c[MAXN];

int lowbit(int x){
	return x & (-x);
}

void Updata(int x,int value) {
	while (x <= N) {
		c[x] += value;
		x += lowbit(x);
	}
}

int Query(int x) {
	int sum = 0;
	while (x > 0) {
		sum += c[x];
		x -= lowbit(x);
	}
	return sum;
}

int main() {
	//FR("1.txt");
	while (cin >> N,N) {
		memset(c,0,sizeof(c));
		REP(i,N) {
			int x,y;
			cin >> x >> y;
			Updata(x,1);
			Updata(y + 1,-1);
		}
		REP_1(i,N) {
			cout << Query(i) << " ";
		}
		cout << Query(N) << endl;
	}
}

 

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <complex>
#include <cstring>
#include <iostream>
#include <algorithm>

#define REP(i,N) for (int i = 0;i < (N);i++)
#define REP_1(i,N) for (int i = 1;i < (N);i++)
#define REP_2(i,be,en) for (int i = (be);i < (en);i++)
#define DWN(i,N) for (int i = (N);i >= 0;i--)
#define DWN_1(i,N) for (int i = (N);i >= 1;i--)
#define DWN_2(i,en,be) for (int i = (en);i >= (be);i--)
#define FR(N) freopen((N),"r",stdin)
#define FW(N) freopen((N),"w",stdout)
#define MAXN 100010
#define GETS(ch) fgets((ch),MAXN,stdin)
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;

typedef long long LL;

typedef map<int,LL> MINT;
typedef map<char,int> MCH;
typedef map<string,int> MSTR;

typedef vector<int> VINT;
typedef set<LL> SINT;
typedef pair<int,int> PINT;

LL read(){
    LL ret=0,f=1;
    char x=getchar();
    while(!(x>=‘0‘ && x<=‘9‘)){if(x==‘-‘)f=-1;x=getchar();}
    while(x>=‘0‘ && x<=‘9‘) ret=ret*10+x-‘0‘, x=getchar();
    return ret*f;
}

int Array[MAXN << 2];
int Add[MAXN << 2];

void PushDown(int node,int num) {
	if (Add[node]) {
		Add[node << 1] += Add[node];
		Add[node << 1 | 1] += Add[node];
		Array[node << 1] += Add[node] * (num - (num >> 1));
		Array[node << 1 | 1] += Add[node] * (num >> 1);
		Add[node] = 0;
	}
}

void Bulid(int node,int left,int right) {
	Array[node] = 0;
	Add[node] = 0;
	if (left == right) {
		return;
	}
	int mid = (right + left) >> 1;
	Bulid(node << 1,left,mid);
	Bulid(node << 1 | 1,mid + 1,right);
}

void Updata(int node,int left,int right,int L,int R) {
	if (L <= left && right <= R) {
		Array[node] += right - left + 1;
		Add[node]++;
		return;
	}
	PushDown(node,right - left + 1);
	int mid = (right + left) >> 1;
	if (L <= mid) Updata(node << 1,left,mid,L,R);
	if (R > mid) Updata(node << 1 | 1,mid + 1,right,L,R);
	Array[node] = Array[node << 1] + Array[node << 1 | 1];
}

int Query(int node,int left,int right,int pos) {
	if (left == right) {
		return Array[node];
	}
	PushDown(node,right - left + 1);
	int mid = (left + right) >> 1;
	if (pos <= mid) return Query(node << 1 , left , mid, pos);
	else return Query(node << 1 | 1 , mid + 1,right,pos);
}

int main() {
	int N;
	//FR("1.txt");
	while (cin >> N,N) {
        Bulid(1,1,N);
		REP(i,N) {
			int x1,x2;
			cin >> x1 >> x2;
			Updata(1,1,N,x1,x2);
		}
		REP_1(i,N) {
			cout << Query(1,1,N,i) << " ";
		}
		cout << Query(1,1,N,N) << endl;
	}
}

  

HDU 1556

标签:

原文地址:http://www.cnblogs.com/xiaoshanshan/p/4359232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!