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 = 10000 + 10;
const int maxm = 200000 + 10;
const int mod = 998244353;
int n;
struct ND{
    int l,r;
//    ll sum,lazy;
    int col;
}tree[maxn<<2];
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!=-1){
        tree[rt<<1].col=tree[rt<<1|1].col=tree[rt].col;
        tree[rt].col=-1;
    }
}
void build(int rt,int l,int r){
    tree[rt].l=l,tree[rt].r=r;
    tree[rt].col=-1;
//    tree[rt].lazy=0;
    if(l==r){
        return;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,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)>>1;
    if(mid>=L) update(rt<<1,L,R,val);
    if(mid<R) update(rt<<1|1,L,R,val);
//    pushup(rt);
}

void query(int rt){
    if(tree[rt].l==tree[rt].r){
        if(tree[rt].col!=-1&&tree[rt].col!=pre){
            ans[tree[rt].col]++;
        }
        pre=tree[rt].col;
        return;
    }
    pushdown(rt);
    query(rt<<1);
    query(rt<<1|1);
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    int t,cas=1;
//    scanf("%d",&t);
    while(~scanf("%d",&n)){
        pre=-1;
        memset(ans,0,sizeof(ans));
        build(1,1,8000);
        while(n--){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(x<y) update(1,x+1,y,z);
        }
        query(1);
        for(int i=0;i<=8000;i++){
            if(ans[i]) printf("%d %d\n",i,ans[i]);
        }
        puts("");
    }
    return 0;
}

 

posted @ 2018-09-02 09:59  litos  阅读(175)  评论(0编辑  收藏  举报