您的位置:首页 > 其它

Color the ball - HDU 1556 - 线段树 区间更新单点查询

2017-05-28 11:22 537 查看

Color the ball - HDU 1556 - 线段树 区间更新单点查询

  国际惯例中文题目不解释,思路直接裸线段树,Lazy思想入门题。

  Lazy传送门:延迟更新详解

AC代码:

//
//  main.cpp
//  L
//
//  Created by LucienShui on 2017/5/28.
//  Copyright © 2017年 LucienShui. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define memset(a,b) memset(a,b,sizeof(a))
#define il inline
#define ll long long
#define ull unsigned long long

using namespace std;

#define ls (u<<1)
#define rs (u<<1|1)
#define lson u<<1,l,mid
#define rson u<<1|1,mid+1,r

const int maxn = 100000+7;
int node[maxn<<2];

void pushdown(int u) {
node[ls] += node[u];
node[rs] += node[u];
node[u]=0;
}

void update(int u, int l, int r, int b, int e) {
if(b <= l && r <= e) {
node[u]++;
return ;
}
if(node[u]!=0) pushdown(u);
int mid = (l+r)>>1;
if(e<=mid) update(lson,b,e);
else if(b>mid) update(rson,b,e);
else {
update(lson,b,e);
update(rson,b,e);
}
}

int query(int u, int l, int r, int x) {
if(l==r&&l==x) {
return node[u];
}
if(node[u]!=0) pushdown(u);
int mid = (l+r)>>1;
if(x<=mid) return query(lson,x);
else return query(rson,x);
}

int main ()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int n;
while(scanf("%d",&n),n) {
memset(node,0);
for(int i=0,a,b ; i<n ; i++) {
scanf("%d%d",&a,&b);
update(1,1,n,a,b);
}
for(int i=1 ; i<n ; i++) {
printf("%d ",query(1,1,n,i));
}
printf("%d\n",query(1,1,n,n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息