algorithm - How to increment all values in an array interval by a given amount -


suppose have array of length l. given n intervals(i,j) , have increment values between a[i] , a[j].which data structure suitable given operations?
intervals known beforehand.

break intervals start , end indexes: s_i,e_i i-th interval starts including s_i , ends excluding e_i

sort s_i-s array s sort e_i-s array e

set increment 0 start linear scan of input , add increment everyone, in each loop if next s_i current index increment increment if next e_i index decement increment

inc=0 s=<priorityqueue of interval startindexes> e=<priorityqueue of interval endindexes> for(i=0;i<n;i++){   if( inc == 0 ){     // skip adding zeros     i=min(s.peek(),e.peek())   }   while( s.peek() == ) {     s.pop();     inc++;   }   while( e.peek() == ) {     e.pop();     inc--;   }   a[i]+=inc; } 

complexity(without skipping nonincremented elements): o(n+m*log(m)) // m number of intervals if n>>m it's o(n)

complexity when skipping elements: o( min( n , \sum length(i_i) ) ), length(i_i)=e_i-s_i


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -