https://www.bnuoj.com/v3/problem_show.php?pid=49100
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=1000005; int num[N*3]; void build() { memset(num,0,sizeof(num)); } struct node{ int ls,rs,val; }e[N]; int PushUp(int k){ if(num[k]){ if(!num[k<<1]) num[k<<1]=num[k];//只有值不存在时候更新 if(!num[k<<1|1])num[k<<1|1]=num[k]; num[k]=0; } } void Update(int l,int r,int L,int R,int k,int val) { if(num[k]) return;//存在值,直接返回 if(l<=L && r>=R){ num[k]=val; return; } int mid=(L+R)>>1; if(l<=mid) Update(l,r,L,mid,k<<1,val); if(r>mid) Update(l,r,mid+1,R,k<<1|1,val); if(num[k<<1]==num[k<<1|1]) num[k]=num[k<<1]; PushUp(k);//返回时候更新数值 } void Query(int l,int r,int k) { if(l==r){ printf("%d\n",num[k]); return; } PushUp(k); int mid=(l+r)>>1; Query(l,mid,k<<1); Query(mid+1,r,k<<1|1); } int main() { int m,n,i,j; int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); build(); int l,r,k; for(i=1;i<=m;i++) scanf("%d%d%d",&e[i].ls,&e[i].rs,&e[i].val); //从后往前倒这更新,很巧妙,更新过就不用在更新,节省时间 for(i=m;i>0;i--) Update(e[i].ls,e[i].rs,1,n,1,e[i].val); Query(1,n,1); } return 0; }
#pragma warning(disable:4996) #include <iostream> #include <functional> #include <algorithm> #include <cstring> #include <vector> #include <string> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <deque> #include <set> #include <map> using namespace std; typedef long long ll; #define INF 0x33ffffff #define eps 1e-8 const ll mod = 1000000007; const int maxn = 1e6 + 5; const double PI = acos(-1.0); struct no { int le; int ri; int p; }node[maxn]; int n, m; int fa[maxn], col[maxn]; stack<int>st; int getfa(int x) { while (fa[x] != x) { st.push(x); x = fa[x]; } while (!st.empty()) { fa[st.top()] = x; st.pop(); } return x; } void uni(int x, int y) { x = getfa(x), y = getfa(y); if (x == y) return; if (x < y) { fa[x] = y; } else { fa[y] = x; } } void solve() { int i, j; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) fa[i] = i; for (i = 1; i <= m; i++) { scanf("%d%d%d", &node[i].le, &node[i].ri, &node[i].p); } memset(col, 0, sizeof(col)); for (i = m; i >= 1; i--) { int le = node[i].le; int ri = node[i].ri; int p = node[i].p; if (col[le]) { le = getfa(le) + 1; } while (le <= ri) { col[le] = p; if (le > 1 && col[le - 1]) { uni(le, le - 1); } if (le < n&&col[le + 1]) { uni(le, le + 1); } le = getfa(le) + 1; } } for (i = 1; i <= n; i++) { printf("%d\n", col[i]); } } int main() { int t; scanf("%d", &t); while (t--) { solve(); } return 0; }