Title Description
There are n(1 \leq n \leq 10^5)n(1 ≤ n ≤ 105) children. They will give m(1 \leq m \leq 10^5)m(1 ≤ m ≤ 105) gifts for the new year.
Each time, 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) will be given, indicating that a gift kk will be given to children in the range [l, r][l,r].
After all the gifts are given, for each child, answer what is the most frequent gift among the gifts he receives. If there are more than one, output the one with the smallest number; if not, output - 1 − 1.
I / O format
Input format:
The first line has two integers n, mn,m, meaning as above.
Next mm lines, three numbers l,r,kl,r,k in each line, meaning as above.
Output format:
There are nn lines in total, one number for each line, indicating the answer.
Example of input and output
6 3 1 5 1 2 3 2 3 4 2
1 1 2 1 1 -1
It's all routine problems
Build weight line segment tree according to the serial number of xiaopenyou
For each question, please give a difference
Mark the tree and record the position of the maximum value and the maximum value
After emmm, we should consider how to write the line tree. It feels that DFS is not only small in memory, 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; }