1. 程式人生 > >ZOJ - 1610 Count the Colors(線段樹區間更新)

ZOJ - 1610 Count the Colors(線段樹區間更新)

線段樹 pan 遍歷 cst include stdin cstring syn bit

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; }

ZOJ - 1610 Count the Colors(線段樹區間更新)