ZOJ - 1610 Count the Colors(线段树区间更新)

时间:2023-03-09 19:44:53
ZOJ - 1610 Count the Colors(线段树区间更新)

https://cn.vjudge.net/problem/ZOJ-1610

题意

给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000。

分析

把区间看作点,即[3,4]看作点4。查询时进行前序遍历,记录上一段的颜色,不连续的就+1。注意区间的范围可达8000

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = + ;
const int maxm = + ;
const int mod = ;
int n;
struct ND{
int l,r;
// ll sum,lazy;
int col;
}tree[maxn<<];
int pre;
int ans[maxn];
//void pushup(int rt){
//
// tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
//}
void pushdown(int rt){
if(tree[rt].col!=-){
tree[rt<<].col=tree[rt<<|].col=tree[rt].col;
tree[rt].col=-;
}
}
void build(int rt,int l,int r){
tree[rt].l=l,tree[rt].r=r;
tree[rt].col=-;
// tree[rt].lazy=0;
if(l==r){
return;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
// pushup(rt);
}
void update(int rt,int L,int R,int val){
if(L<=tree[rt].l&&tree[rt].r<=R){
tree[rt].col=val;
return;
}
pushdown(rt);
int mid=(tree[rt].l+tree[rt].r)>>;
if(mid>=L) update(rt<<,L,R,val);
if(mid<R) update(rt<<|,L,R,val);
// pushup(rt);
} void query(int rt){
if(tree[rt].l==tree[rt].r){
if(tree[rt].col!=-&&tree[rt].col!=pre){
ans[tree[rt].col]++;
}
pre=tree[rt].col;
return;
}
pushdown(rt);
query(rt<<);
query(rt<<|);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t,cas=;
// scanf("%d",&t);
while(~scanf("%d",&n)){
pre=-;
memset(ans,,sizeof(ans));
build(,,);
while(n--){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(x<y) update(,x+,y,z);
}
query();
for(int i=;i<=;i++){
if(ans[i]) printf("%d %d\n",i,ans[i]);
}
puts("");
}
return ;
}