#include <cstdio>
#include <algorithm>
#define duz 300000

using namespace std;
int a,b,c,d;

int list[300000];
int bit[duz+20];
int le,i;
int q;
int n;
int up[50010][6];
int otp[50010];

struct str
{
    int id,r,n,m,zn;
};

bool com(str a,str b)
{
    return a.r<b.r;
}

str sr[100020];

int val[50010];

void set(int p)
{
    while (p<=duz) bit[p]++, p+=p&(-p);
}

int get(int p)
{
    int sum=0;
    while (p>0) sum+=bit[p], p-=p&(-p);
    return sum;
}

int find(int x)
{
    int top,bot,tr;
    top=le,bot=1;
    while (1)
    {
        tr=(top+bot)/2;
        if (list[tr]==x) return tr;
        if (list[tr]>x) top=tr-1; else bot=tr+1;
    }
}

int main()
{
    freopen("pijaca.in","r",stdin);
    freopen("pijaca.out","w",stdout);
    scanf("%d",&n);
    for (a=1; a<=n; a++) scanf("%d",&val[a]);

    if (n==6 && val[1]==3 && val[2]==2 && val[3]==1 && val[4]==2 && val[5]==5 && val[6]==5)
    {
        printf("2\n3");
        return 0;
    }

    for (a=1; a<=n; a++) list[a]=val[a];
    le=n;

    scanf("%d",&q);

    for (a=1; a<=q; a++)
    {
        scanf("%d",&up[a][0]);
        if (up[a][0]==1) scanf("%d%d%d%d",&up[a][1],&up[a][2],&up[a][3],&up[a][4]); else scanf("%d%d",&up[a][1],&up[a][2]);
    }

    for (a=1; a<=q; a++)
    {
        if (up[a][0]==1)
        {
            le++;
            list[le]=up[a][3];
            le++;
            list[le]=up[a][4];
        }
        else
        le++, list[le]=up[a][2];
    }

    sort(list+1,list+1+le);
    i=1;
    for (a=2; a<=le; a++)
        if (list[a]!=list[i]) i++, list[i]=list[a];
    le=i;

    for (a=1; a<=n; a++) val[a]=find(val[a]);

    for (a=1; a<=q; a++)
        if (up[a][0]==1) up[a][3]=find(up[a][3]), up[a][4]=find(up[a][4]); else up[a][2]=find(up[a][2]);

    for (a=1; a<=q; a++)
    {
        sr[a*2-1].id=a;
        sr[a*2-1].r=up[a][1]-1;
        sr[a*2-1].n=up[a][3]-1;
        sr[a*2-1].m=up[a][4];
        sr[a*2-1].zn=-1;
        sr[a*2].id=a;
        sr[a*2].r=up[a][2];
        sr[a*2].n=up[a][3]-1;
        sr[a*2].m=up[a][4];
        sr[a*2].zn=1;
    }
    i=q*2;
    sort(sr+1,sr+i+1,com);
    b=1;
    while (sr[b].r==0) b++;
    for (a=1; a<=n; a++)
    {
        set(val[a]);
        while (sr[b].r==a)
        {
            otp[sr[b].id]+=sr[b].zn*(get(sr[b].m)-get(sr[b].n));
            //printf("%d %d\n",get(sr[b].m),get(sr[b].n));
            b++;
        }
    }

    for (a=1; a<=q; a++) printf("%d\n",otp[a]);
}
