g | x | w | all
Bytes Lang Time Link
139Ruby160316T221332ZValue In
113Retina160319T232201Zdaavko
217Python 3160316T144500ZErwan
174JavaScript ES6160316T200707Zedc65

Ruby, 139 bytes

Felt nostalgic and decided I wanted to golf down my first ever answer on this site with everything I've learned since then. I wasn't even using stabby lambdas or basic idioms like map in place of for loops for that answer!

->x,y{s=' ';puts s+?_*x[0]+s
y.zip(x,x[1..]){|i,n,m|puts"|#{s*n}|
"*~-i+?|+s*[n,m||=0].min+(m<1??_:m>n ??|:s)+?_*~-(m-n).abs+(m<n ??|:'')}}

Attempt This Online!

Ruby 191

First time golfing, also it's my first day with Ruby, so it's probably not the most elegant thing in the world, but it'll do?

def f(x)
a=x[0]+[0]
puts" #{'_'*a[0]} "
for i in 0..x[1].length-1
n,m=a[i,2].sort
puts"|#{' '*a[i]}|\n"*(x[1][i]-1)+'|'+' '*n+(a[i+1]<1?'_':m>a[i]?'|':' ')+'_'*(m-n-1)+(n<a[i]?'|':'')
end
end

Attempt This Online!

Retina, 169 150 113 bytes

Byte count assumes ISO 8859-1 encoding.

\d+
$*
{+r`1(1*¶[^|]*(1+))
$1¶|$2|
}` (¶.*) 1+
$1
1
_
(\|_+)\|(?=¶\1_(_+))
$1|$2
T`w` `(\|_+)_?(?=_*\|.*¶\1)
^¶
 

The code contains a trailing space on a trailing newline.

Input format:

Height (separated by spaces)
Width (also separated by spaces)

For example:

1 2 3 1 2 1 1 1 2 1
2 14 6 8 4 18 2 10 4 2

Try it online!

Python 3, 230 223 222 217 bytes

def f(a):b=a[0]+[0];m=' _';print('\n'.join([' '+'_'*b[0]+' ']+['|'+' '*min(b[l],b[l+1])+m[b[l+1]<1]*(b[l]>b[l+1])+m[k]*(b[l]-b[l+1]-1)+'|'+m[k]*(b[l+1]-b[l]-1)for l,i in enumerate(zip(*a))for k in[0]*(i[1]-1)+[1]]))

Thanks to @StewieGriffin @KevinLau for their help

Results

>>> f([[2, 14, 6, 8, 4, 18, 2, 10, 4, 2],[1, 2, 3, 1, 2, 1, 1, 1, 2, 1]])
 __ 
|  |___________
|              |
|       _______|
|      | 
|      | 
|      |_
|     ___|
|    |             
|    |_____________
|   _______________|
|  |_______
|     _____|
|    |
|   _|
|__|

JavaScript (ES6) 174

The only critical part is the horizontal row joining 2 parts of different widths, with the vertical bar on right side that can be in the middle or at the right end.

(p,h,q=-1,R=(n,s=' ')=>s.repeat(n))=>[...p,0].map((x,i)=>(x>q?p=x:(p=q,q=x),(~q?`
|`+R(q+(x<p)-!x)+R(x>q,'|'):' ')+R(p+~q+!x,'_')+R(x<p,'|')+R(h[i]-1,`
|${R(q=x)}|`))).join``

TEST

f=(p,h,q=-1,R=(n,s=' ')=>s.repeat(n))=>[...p,0].map((x,i)=>(x>q?p=x:(p=q,q=x),(~q?`
|`+R(q+(x<p)-!x)+R(x>q,'|'):' ')+R(p+~q+!x,'_')+R(x<p,'|')+R(h[i]-1,`
|${R(q=x)}|`))).join``

// Less golfed

F=(p,h, q=-1, 
   R=(n,s=' ')=>s.repeat(n)
  )=>
  [...p, 0].map((x,i)=> (
    x>q? p=x : (p=q,q=x),
    (q>=0?`\n|`+R(q+(x<p)-!x)+R(x>q,'|'):' ')
    + R(p+~q+!x,'_') + R(x<p,'|')
    + R(h[i]-1,`\n|${R(q=x)}|`)
  )).join``

console.log=x=>O.textContent+=x+'\n'

;[  
  [[2],[1]],
  [[4],[2]],
  [[2, 6, 2, 4],[2, 2, 1, 3]],
  [[2, 14, 6, 8, 4, 18, 2, 10, 4, 2],[1, 2, 3, 1, 2, 1, 1, 1, 2, 1]]
].forEach(t=>{
  var w=t[0],h=t[1]
  console.log('['+w+'] ['+h+']\n'+f(w,h)+'\n')
})
<pre id=O></pre>