#include <stdio.h>
struct Elem
{  long ind,cena;
  Elem *sled;
  Elem (long ind1,long cena1, Elem *s=NULL)
    {ind=ind1;
     cena=cena1;
     sled=s;
     }
     };
  Elem    *prvi=NULL,*obidji=NULL;
   
  void brisi(Elem* p)
  {Elem* pom;
   pom=p->sled;
   p->sled=pom->sled;
   delete(pom);
       
   }    
         
  void ubaci(long j,long c)
    {Elem *novi,*pom;
       novi=new Elem(j,c);
       if (prvi==NULL)prvi=novi;
        else{ 
              pom=prvi;
              prvi=novi;
              prvi->sled=pom;
              
              pom=prvi;
              while(pom->sled!=NULL)
               {while (pom->sled->ind <= novi->ind)brisi(pom);
                pom=pom->sled;
                }
              
                       
            }
    
    } 
   
   int main()
  { FILE *in,*out;
    in=fopen("pijaca.in","r");
    out=fopen("pijaca.out","w");
    long i,j,k,b,l,r,n,q,o,c,a[50001],x1,x2,pp,ss,cc,ppn;
   fscanf(in,"%ld",&n);
   for(i=1;i<=n;i++)fscanf(in,"%ld",&a[i]);
   fscanf(in,"%ld",&q);
   for(i=1;i<=q;i++)
      { fscanf(in,"%ld",&o);
       if (o==1){
                 b=0;
                 fscanf(in,"%ld%ld%ld%ld",&l,&r,&x1,&x2);
                  if (prvi==NULL)for(j=l;j<=r;j++)b+=( x1<=a[j] && x2>=a[j]);
                     else{  obidji=prvi;pp=l-1;
                            while(obidji!=NULL)
                              {cc=obidji->cena;
                               ss=obidji->ind;
                               ppn=ss;
                               ss-=pp;
                               pp=ppn+1;
                               if (pp-1>r)
                                        {ss-=pp-r-1;
                                        if(cc>=x1  &&  cc <=x2)b+=ss;
                                        break;
                                        }
                               else{         
                               if(cc>=x1  &&  cc <=x2)b+=ss;
                                 obidji=obidji->sled;
                               }
                               }
                             if (pp<=r)for(j=pp;j<=r;j++)b+=(x1<=a[j] && x2>=a[j]);
                          
                          }
                          
                  fprintf(out,"%ld\n",b);        
                 }
        else   {fscanf(in,"%ld%ld",&j,&c);
                 ubaci(j,c);         
                 }     
                    
        }
        fclose(in);
        fclose(out);
   
   }
