#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
const int N = 130005;
int n;
long long a[N] , b[N];
pair <long long,int> p[N];
int timer = 1;
int root[N];
long long t[N * 30];
int son1[N * 30] , son2[N * 30];
long long sum1[N * 30] , sum2[N * 30] , sum[N * 30];
void build(int v,int s,int e){
if(s == e)return ;
int m = (s + e) / 2;
if(son1[v] == 0){
son1[v] = ++timer;
son2[v] = ++timer;
}
build(son1[v] , s , m);
build(son2[v] , m+1 , e);
}
int update(int v,int s,int e,int pos){
int vv = ++timer;
if(s == e){
t[vv] = 1;
sum1[vv] = a[s];
sum2[vv] = b[s];
sum[vv] = (a[s] + b[s]) / 2;
return vv;
}
int m = (s + e) / 2;
if(pos <= m){
son2[vv] = son2[v];
son1[vv] = update(son1[v] , s , m , pos);
}
else{
son1[vv] = son1[v];
son2[vv] = update(son2[v] , m+1 , e , pos);
}
t[vv] = t[son1[vv]] + t[son2[vv]];
sum[vv] = sum[son1[vv]] + sum[son2[vv]];
sum1[vv] = sum1[son1[vv]] + sum1[son2[vv]];
sum2[vv] = sum2[son1[vv]] + sum2[son2[vv]];
return vv;
}
long long get(int v,int s,int e,int l,int r,int id){
if(l > r)return 0;
if(s == l && e == r){
if(id == 0)return t[v];
if(id == 1)return sum1[v];
if(id == 2)return sum[v];
if(id == 3)return sum2[v];
}
int m = (s + e) / 2;
return get(son1[v] , s , m , l , min(r , m) , id) + get(son2[v] , m+1 , e , max(m+1 , l) , r , id);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
a[i] *= 2;
}
for(int i=1;i<=n;i++){
scanf("%lld",&b[i]);
b[i] *= 2;
}
for(int i=1;i<=n;i++){
p[i] = make_pair(b[i] - a[i] , i);
}
sort(p + 1 , p + n + 1);
build(1 , 1 , n);
root[0] = 1;
for(int i=1;i<=n;i++){
root[i] = update(root[i - 1] , 1 , n , p[i].second);
}
int q;
scanf("%d",&q);
while(q--){
int l,r;
scanf("%d%d",&l,&r);
int x = (r - l + 1) / 3;
int ina = 1 , inb = n , ind1;
long long answ = 0;
while(ina <= inb){
int mid = (ina + inb) / 2;
long long T = get(root[mid] , 1 , n , l , r , 0);
if(T >= x){
ind1 = mid;
inb = mid - 1;
}
else{
ina = mid + 1;
}
}
answ += get(root[ind1] , 1 , n , l , r , 1);
int ind2;
ina = 1;inb = n;
while(ina <= inb){
int mid = (ina + inb) / 2;
long long T = get(root[mid] , 1 , n , l , r , 0);
if(T >= 2 * x){
ind2 = mid;
inb = mid - 1;
}
else{
ina = mid + 1;
}
}
answ += get(root[ind2] , 1 , n , l , r , 2) - get(root[ind1] , 1 , n , l , r , 2);
answ += get(root[n] , 1 , n , l , r , 3) - get(root[ind2] , 1 , n , l , r , 3);
printf("%lld",answ / 2);
if(answ % 2)printf(".5");
printf("\n");
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHNldD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTiA9IDEzMDAwNTsKCmludCBuOwpsb25nIGxvbmcgYVtOXSAsIGJbTl07CnBhaXIgPGxvbmcgbG9uZyxpbnQ+IHBbTl07CgppbnQgdGltZXIgPSAxOwppbnQgcm9vdFtOXTsKCmxvbmcgbG9uZyB0W04gKiAzMF07CmludCBzb24xW04gKiAzMF0gLCBzb24yW04gKiAzMF07CmxvbmcgbG9uZyBzdW0xW04gKiAzMF0gLCBzdW0yW04gKiAzMF0gLCBzdW1bTiAqIDMwXTsKCnZvaWQgYnVpbGQoaW50IHYsaW50IHMsaW50IGUpewogICAgaWYocyA9PSBlKXJldHVybiA7CiAgICBpbnQgbSA9IChzICsgZSkgLyAyOwogICAgaWYoc29uMVt2XSA9PSAwKXsKICAgICAgICBzb24xW3ZdID0gKyt0aW1lcjsKICAgICAgICBzb24yW3ZdID0gKyt0aW1lcjsKICAgIH0KICAgIGJ1aWxkKHNvbjFbdl0gLCBzICwgbSk7CiAgICBidWlsZChzb24yW3ZdICwgbSsxICwgZSk7Cn0KCmludCB1cGRhdGUoaW50IHYsaW50IHMsaW50IGUsaW50IHBvcyl7CiAgICBpbnQgdnYgPSArK3RpbWVyOwogICAgaWYocyA9PSBlKXsKICAgICAgICB0W3Z2XSA9IDE7CiAgICAgICAgc3VtMVt2dl0gPSBhW3NdOwogICAgICAgIHN1bTJbdnZdID0gYltzXTsKICAgICAgICBzdW1bdnZdID0gKGFbc10gKyBiW3NdKSAvIDI7CiAgICAgICAgcmV0dXJuIHZ2OwogICAgfQogICAgaW50IG0gPSAocyArIGUpIC8gMjsKICAgIGlmKHBvcyA8PSBtKXsKICAgICAgICBzb24yW3Z2XSA9IHNvbjJbdl07CiAgICAgICAgc29uMVt2dl0gPSB1cGRhdGUoc29uMVt2XSAsIHMgLCBtICwgcG9zKTsgCiAgICB9CiAgICBlbHNlewogICAgICAgIHNvbjFbdnZdID0gc29uMVt2XTsKICAgICAgICBzb24yW3Z2XSA9IHVwZGF0ZShzb24yW3ZdICwgbSsxICwgZSAsIHBvcyk7IAogICAgfQogICAgdFt2dl0gPSB0W3NvbjFbdnZdXSArIHRbc29uMlt2dl1dOwogICAgc3VtW3Z2XSA9IHN1bVtzb24xW3Z2XV0gKyBzdW1bc29uMlt2dl1dOwogICAgc3VtMVt2dl0gPSBzdW0xW3NvbjFbdnZdXSArIHN1bTFbc29uMlt2dl1dOwogICAgc3VtMlt2dl0gPSBzdW0yW3NvbjFbdnZdXSArIHN1bTJbc29uMlt2dl1dOwogICAgcmV0dXJuIHZ2Owp9Cgpsb25nIGxvbmcgZ2V0KGludCB2LGludCBzLGludCBlLGludCBsLGludCByLGludCBpZCl7CiAgICBpZihsID4gcilyZXR1cm4gMDsKICAgIGlmKHMgPT0gbCAmJiBlID09IHIpewogICAgICAgIGlmKGlkID09IDApcmV0dXJuIHRbdl07CiAgICAgICAgaWYoaWQgPT0gMSlyZXR1cm4gc3VtMVt2XTsKICAgICAgICBpZihpZCA9PSAyKXJldHVybiBzdW1bdl07CiAgICAgICAgaWYoaWQgPT0gMylyZXR1cm4gc3VtMlt2XTsKICAgIH0KICAgIGludCBtID0gKHMgKyBlKSAvIDI7CiAgICByZXR1cm4gZ2V0KHNvbjFbdl0gLCBzICwgbSAsIGwgLCBtaW4ociAsIG0pICwgaWQpICsgZ2V0KHNvbjJbdl0gLCBtKzEgLCBlICwgbWF4KG0rMSAsIGwpICwgciAsIGlkKTsKfQoKaW50IG1haW4oKXsKICAgIHNjYW5mKCIlZCIsJm4pOwogICAgZm9yKGludCBpPTE7aTw9bjtpKyspewogICAgICAgIHNjYW5mKCIlbGxkIiwmYVtpXSk7CiAgICAgICAgYVtpXSAqPSAyOwogICAgfQogICAgZm9yKGludCBpPTE7aTw9bjtpKyspewogICAgICAgIHNjYW5mKCIlbGxkIiwmYltpXSk7CiAgICAgICAgYltpXSAqPSAyOwogICAgfQogICAgZm9yKGludCBpPTE7aTw9bjtpKyspewogICAgICAgIHBbaV0gPSBtYWtlX3BhaXIoYltpXSAtIGFbaV0gLCBpKTsKICAgIH0KICAgIHNvcnQocCArIDEgLCBwICsgbiArIDEpOwogICAgYnVpbGQoMSAsIDEgLCBuKTsKICAgIHJvb3RbMF0gPSAxOwogICAgZm9yKGludCBpPTE7aTw9bjtpKyspewogICAgICAgIHJvb3RbaV0gPSB1cGRhdGUocm9vdFtpIC0gMV0gLCAxICwgbiAsIHBbaV0uc2Vjb25kKTsKICAgIH0KICAgIGludCBxOwogICAgc2NhbmYoIiVkIiwmcSk7CiAgICB3aGlsZShxLS0pewogICAgICAgIGludCBsLHI7CiAgICAgICAgc2NhbmYoIiVkJWQiLCZsLCZyKTsKICAgICAgICBpbnQgeCA9IChyIC0gbCArIDEpIC8gMzsKICAgICAgICBpbnQgaW5hID0gMSAsIGluYiA9IG4gLCBpbmQxOwogICAgICAgIGxvbmcgbG9uZyBhbnN3ID0gMDsKICAgICAgICB3aGlsZShpbmEgPD0gaW5iKXsKICAgICAgICAgICAgaW50IG1pZCA9IChpbmEgKyBpbmIpIC8gMjsKICAgICAgICAgICAgbG9uZyBsb25nIFQgPSBnZXQocm9vdFttaWRdICwgMSAsIG4gLCBsICwgciAsIDApOwogICAgICAgICAgICBpZihUID49IHgpewogICAgICAgICAgICAgICAgaW5kMSA9IG1pZDsKICAgICAgICAgICAgICAgIGluYiA9IG1pZCAtIDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgIGluYSA9IG1pZCArIDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgYW5zdyArPSBnZXQocm9vdFtpbmQxXSAsIDEgLCBuICwgbCAsIHIgLCAxKTsKICAgICAgICBpbnQgaW5kMjsKICAgICAgICBpbmEgPSAxO2luYiA9IG47CiAgICAgICAgd2hpbGUoaW5hIDw9IGluYil7CiAgICAgICAgICAgIGludCBtaWQgPSAoaW5hICsgaW5iKSAvIDI7CiAgICAgICAgICAgIGxvbmcgbG9uZyBUID0gZ2V0KHJvb3RbbWlkXSAsIDEgLCBuICwgbCAsIHIgLCAwKTsKICAgICAgICAgICAgaWYoVCA+PSAyICogeCl7CiAgICAgICAgICAgICAgICBpbmQyID0gbWlkOwogICAgICAgICAgICAgICAgaW5iID0gbWlkIC0gMTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgaW5hID0gbWlkICsgMTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBhbnN3ICs9IGdldChyb290W2luZDJdICwgMSAsIG4gLCBsICwgciAsIDIpIC0gZ2V0KHJvb3RbaW5kMV0gLCAxICwgbiAsIGwgLCByICwgMik7CiAgICAgICAgYW5zdyArPSBnZXQocm9vdFtuXSAsIDEgLCBuICwgbCAsIHIgLCAzKSAtIGdldChyb290W2luZDJdICwgMSAsIG4gLCBsICwgciAsIDMpOwogICAgICAgIHByaW50ZigiJWxsZCIsYW5zdyAvIDIpOwogICAgICAgIGlmKGFuc3cgJSAyKXByaW50ZigiLjUiKTsKICAgICAgICBwcmludGYoIlxuIik7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==