// ROOT : DRAGON3012009 : WA in Real Life
#include <bits/stdc++.h>
#define FOR(i,l,r) for(int i = l ; i <= r ; i ++)
#define FORD(i,r,l) for(int i = r ; i >= l ; i --)
#define REP(i, a ) for(int i = 0 ; i < a ; i ++ )
#define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end());
#define ll long long
#define el "\n"
#define fi first
#define se second
#define _ROOT_ int main()
#define M 1000000007
#define MAXN 200001
#define BLOCK 600
#define Bit(i) (1LL << i )
#define INF (1ll<<30)
#define NAME "file"
#define debug(a) cout << #a << " = " << a << endl;
using namespace std;

ll n, m, q ;
ll a[MAXN] ;
ll p[MAXN ] ;
ll belong[MAXN ] ;
ll blockSum[MAXN ] ;
ll belongP[MAXN ] ;
ll res[MAXN ] ;
ll lienquan[BLOCK + 1 ][BLOCK + 1 ] ;
ll lazy[MAXN ] ;
vector<ll> adj[MAXN ] ;
struct Data {
    ll t, l, r, x ;
} que[MAXN ] ;

void buildBlock(ll curBlock ) {
    FOR(j, belong[1], belong[n]) {
        ll st = j * BLOCK, fin = (j + 1) * BLOCK - 1;
        ll L = curBlock * BLOCK, R = (curBlock + 1 ) * BLOCK - 1 ;
        L = max(L, 1LL ) ;
        R = min(n, R ) ;
        FOR(k, L, R ) if(st <= p[k] && p[k] <= fin ) lienquan[curBlock][j] ++ ;
    }
    FOR(j , 1, belong[n]) lienquan[curBlock][j] += lienquan[curBlock][j - 1] ;
}

void update(ll l, ll r, ll x ) {
    if(belong[l] == belong[r]) {
        FOR(i, l, r ) {
            a[p[i]] += x ;
            blockSum[p[i] / BLOCK] += x ;
        }
    } else {
        for(ll i = l ; belong[i] == belong[l] ; i ++ ) {
            a[p[i]] += x ;
            blockSum[p[i] / BLOCK] += x ;
        }
        for(ll i = r ; belong[i] == belong[r] ; i -- ) {
            a[p[i]] += x ;
            blockSum[p[i] / BLOCK] += x ;

        }
        FOR(i, belong[l] + 1, belong[r] - 1 ) lazy[i] += x ;
    }
}

ll get(ll l , ll r ) {
    ll ans = 0 ;
    if(belong[l] == belong[r]) {
        FOR(i , l , r ) {
        ans += a[i] + lazy[belongP[i] ] ;
        }
    }else {
        for(ll i = l ; belong[i] == belong[l] ; i ++ ) {
                   ans += a[i] + lazy[belongP[i] ] ;
        }
        for(ll i = r ; belong[i] == belong[r] ; i -- ) {
               ans += a[i] + lazy[belongP[i] ] ;
        }
        FOR(i, belong[l] + 1, belong[r] - 1 ) {
        ans += blockSum[i] ;
        }
        FOR(i , belong[1] , belong[n]) {
      if(lienquan[i][belong[r] - 1 ] >= lienquan[i][belong[l]] )  ans += (lienquan[i][ belong[r] - 1 ] - lienquan[i][ belong[l] ] ) * lazy[i] ;
        }
    }
    return ans ;
}

void undo_prefix_block(ll curBlock ) {
    FORD(j , belong[n] , 1 ) lienquan[curBlock][j] -= lienquan[curBlock][j - 1 ] ;
}

void rebuild_prefix_block(ll curBlock ) {
    FOR(j , 1 , belong[n]) lienquan[curBlock][j] += lienquan[curBlock][j - 1] ;
}

void manual_fix_block(ll curBlock ) {
    ll L = curBlock * BLOCK , R = (curBlock + 1 ) * BLOCK - 1 ;
    L = max(1LL , L ) ;
    R = min(R , n ) ;
    FOR(i , L , R ) {
    a[p[i]] += lazy[curBlock ] ;
    blockSum[p[i] / BLOCK] += lazy[curBlock ] ;
    }
    lazy[curBlock] = 0 ;
}

void addBlock(ll curBlock , ll pos , ll value ) {
    lienquan[curBlock][p[pos] / BLOCK] += value ;
}

void swap_perm(ll l , ll r ) {
    if(belong[l] == belong[r]) {
    swap(p[l] , p[r]) ;
    return ;
    }
    manual_fix_block(belong[l]) ;
    manual_fix_block(belong[r]) ;

    undo_prefix_block(belong[l] ) ;
    undo_prefix_block(belong[r] ) ;

    addBlock(belong[l] , l , -1 ) ;
    addBlock(belong[r] , r , -1 ) ;

    belongP[p[l]] = r / BLOCK ;
    belongP[p[r]] = l / BLOCK ;

    swap(p[l] , p[r]) ;

    addBlock(belong[l] , l , 1 ) ;
    addBlock(belong[r] , r , 1 ) ;

    rebuild_prefix_block(belong[l]) ;
    rebuild_prefix_block(belong[r]) ;

}

void dfs(ll u ) {
    if(que[u].t == 2 ) {
        res[u] = get(que[u].l , que[u].r ) ;
    }
    for(ll v : adj[u]) {
        if(que[v].t == 1 ) {
            update(que[v].l, que[v].r, que[v].x ) ;
        } else if(que[v].t == 3 ) {
            swap_perm(que[v].l, que[v].r ) ;
        }
        dfs(v ) ;
        if(que[v].t == 1 ) {
            update(que[v].l, que[v].r, - que[v].x ) ;
        } else if(que[v].t == 3 ) {
            swap_perm(que[v].l, que[v].r ) ;
        }
    }
}

void init() {
    cin >> n >> q ;
    FOR(i, 1, n ) cin >> a[i] ;
    FOR(i, 1, n ) cin >> p[i] ;
    FOR(i , 1 , n ) blockSum[i / BLOCK ] += a[i];  
    FOR(i, 1, n ) belong[i] = i / BLOCK ;
    FOR(i, 1, n ) belongP[p[i]] = i / BLOCK ;
    ll last = -1 ;
    FOR(i, 1, n ) {
        if(last != belong[i]) {
            last = belong[i] ;
            buildBlock(last ) ;
        }
    }
    FOR(i , 1 , q ) {
    cin >> que[i].t >> que[i].l ;
    if(que[i].t == 4 ) {
        adj[que[i].l - 1 ].push_back(i) ;
        continue ;
    }else adj[i - 1].push_back(i) ;
    cin >> que[i].r ;
    if(que[i].t ==  1 ) cin >> que[i].x ;
    }
}

void solve() {
    dfs(0 ) ;
    FOR(i, 1, q ) if(que[i].t == 2 ) cout << res[i] << el ;
}


_ROOT_ {
//    freopen(NAME".inp", "r", stdin);
//    freopen(NAME".out", "w", stdout) ;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1; // cin >> t ;
    while(t--) {
        init();
        solve();
    }
    return (0&0);
}
