Luogt21778 New Year, luogt21778
Description
There are n (1 \ leq n \ leq 10 ^ 5) n (1 ≤ n ≤ 105) Children, Chinese New Year, the m (1 \ leq m \ leq 10 ^ 5) m (1 ≤ m ≤ 105) gift will be given.
Three parameters l, r, k (1 \ leq l \ leq r \ leq n, 1 \ leq k \ leq 10 ^ 5) l, r, k (1 ≤ l ≤ r ≤ n, 1 ≤ k ≤ 105) indicates that a gift kk is sent to the children in the range [l, r] [l, r.
After all the gifts are given, each of the children will receive the most frequently-received gifts. If there are multiple, the one with the smallest output number; if not, the output is-1-1.
Input/Output Format
Input Format:
The two integers n, mn, m in the first line indicate the meaning as described above.
Next, the mm line contains three numbers: l, r, kl, r, k. The meaning is described above.
Output Format:
A total of nn rows. Each row has one number, indicating the answer.
Input and Output sample input sample #1: Copy
6 31 5 12 3 23 4 2
Output example #1: Copy
11211-1
There is no way of thinking. It's all about routines.
Create a line segment tree based on the sequence number of the small pot friends
Make a difference for each query.
Mark on the tree to record the location of the maximum and maximum values
After emmm, you should consider how to write the line segment tree. I feel that using the DFS sequence is not only memory-intensive, but also easy to write.
// luogu-judger-enable-o2#include<iostream>#include<vector>#include<cstdio>using namespace std;const int MAXN=1e6+10;struct node{ int l,r,ls,rs,mx,mxpos;}T[MAXN];vector<int>v[MAXN];int root,tot;void Build(int &k , int ll , int rr){ k=tot++; T[k].mx=0; T[k].l = ll ; T[k].r = rr; if( ll == rr ) { T[k].mxpos = ll; return ; } int mid=ll + rr >>1; Build( T[k].ls , ll , mid ); Build( T[k].rs , mid+1 , rr );}void update(int k){ if( T[ T[k].ls ].mx >= T[ T[k].rs ].mx ) T[k].mx = T[ T[k].ls ].mx , T[k].mxpos = T[ T[k].ls ].mxpos; else T[k].mx = T[ T[k].rs ].mx , T[k].mxpos = T[ T[k].rs ].mxpos;}void Add(int k, int pos ){ if( T[k].l == T[k].r ) { T[k].mx++; return ; } int mid=T[k].l + T[k].r >>1; if(pos<=mid) Add( T[k].ls , pos ); else Add( T[k].rs , pos ); update(k);}void Delet(int k, int pos ){ if( T[k].l == T[k].r ) { T[k].mx--; return ; } int mid= T[k].l + T[k].r >>1; if(pos<=mid) Delet( T[k].ls , pos ); else Delet( T[k].rs , pos ); update(k);}int main(){ #ifdef WIN32 freopen("a.in","r",stdin); #else #endif int N,M; scanf("%d%d",&N,&M); for(int i=1; i<=M ;i++ ) { int l,r,k; scanf("%d%d%d",&l,&r,&k); v[l].push_back(k); v[r+1].push_back(-k); } Build(root,1,N); for(int i=1; i<=N ;i++) { for(int j=0; j<v[i].size() ;j++ ) { // printf("*%d*",v[i][j]); if( v[i][j]>0 ) Add(root , v[i][j] ); if( v[i][j]<0 ) Delet(root , -v[i][j] ); } if( T[root].mx ) printf("%d\n",T[ root ].mxpos ); else printf("-1\n"); } return 0;}