using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4");
WillReturn.Add("1 3 5 7");
WillReturn.Add("1 2 3 4");
WillReturn.Add("4");
WillReturn.Add("1 1");
WillReturn.Add("2 6");
WillReturn.Add("0 10");
WillReturn.Add("2 2");
//1
//5
//10
//0
}
else if (InputPattern == "Input2") {
WillReturn.Add("7");
WillReturn.Add("-10 -5 -3 -1 0 1 4");
WillReturn.Add("2 5 6 5 2 1 7");
WillReturn.Add("8");
WillReturn.Add("-7 7");
WillReturn.Add("-1 5");
WillReturn.Add("-10 -4");
WillReturn.Add("-8 10");
WillReturn.Add("-5 0");
WillReturn.Add("-10 5");
WillReturn.Add("-8 7");
WillReturn.Add("-8 -3");
//26
//15
//7
//26
//18
//28
//26
//11
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct QueryInfoDef
{
internal long RangeSta;
internal long RangeEnd;
}
static List<QueryInfoDef> mQueryInfoList = new List<QueryInfoDef>();
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();
long[] XArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long[] PArr = InputList[2].Split(' ').Select(pX => long.Parse(pX)).ToArray();
foreach (string EachStr in InputList.Skip(4)) {
SplitAct(EachStr);
QueryInfoDef WillAdd;
WillAdd.RangeSta = wkArr[0];
WillAdd.RangeEnd = wkArr[1];
mQueryInfoList.Add(WillAdd);
}
var PosSet = new HashSet<long>();
PosSet.UnionWith(XArr);
foreach (QueryInfoDef EachQueryInfo in mQueryInfoList) {
PosSet.Add(EachQueryInfo.RangeSta);
PosSet.Add(EachQueryInfo.RangeEnd);
}
// 座圧後の値[座圧前の値]なDict
Dictionary<long, long> ZaatuDict = DeriveZaatuDict(PosSet);
var InsFenwick_Tree = new Fenwick_Tree(ZaatuDict.Count);
for (long I = 0; I <= XArr.GetUpperBound(0); I++) {
long NewX = ZaatuDict[XArr[I]];
InsFenwick_Tree.Add(NewX, PArr[I]);
}
var sb = new System.Text.StringBuilder();
foreach (QueryInfoDef EachQueryInfo in mQueryInfoList) {
long RangeSta = ZaatuDict[EachQueryInfo.RangeSta];
long RangeEnd = ZaatuDict[EachQueryInfo.RangeEnd];
long Answer = InsFenwick_Tree.GetSum(RangeSta, RangeEnd);
sb.Append(Answer);
sb.AppendLine();
}
Console.Write(sb.ToString());
}
//////////////////////////////////////////////////////////////////////////
// 列挙を引数として、座標圧縮し、座圧後の値[座圧前の値]なDictを返す
//////////////////////////////////////////////////////////////////////////
static Dictionary<long, long> DeriveZaatuDict(IEnumerable<long> pEnum)
{
var ZaatuDict = new Dictionary<long, long>();
var ValSet = new HashSet<long>(pEnum);
long No = 0;
foreach (long EachVal in ValSet.OrderBy(pX => pX)) {
ZaatuDict[EachVal] = No;
No++;
}
return ZaatuDict;
}
}
// フェニック木
#region Fenwick_Tree
internal class Fenwick_Tree
{
private long[] mBitArr;
private long mExternalArrUB;
// ノードのIndexの列挙を返す
internal IEnumerable<long> GetNodeIndEnum()
{
for (long I = 0; I <= mExternalArrUB; I++) {
yield return I;
}
}
// 木のノードのUBを返す
internal long GetUB()
{
return mExternalArrUB;
}
// コンストラクタ(外部配列のUBのみ指定)
internal Fenwick_Tree(long pExternalArrUB)
{
mExternalArrUB = pExternalArrUB;
// フェニック木の外部配列は0オリジンで、
// フェニック木の内部配列は1オリジンなため、2を足す
mBitArr = new long[pExternalArrUB + 2];
}
// コンストラクタ(初期化用の配列指定)
internal Fenwick_Tree(long[] pArr)
: this(pArr.GetUpperBound(0))
{
for (long I = 0; I <= pArr.GetUpperBound(0); I++) {
this.Add(I, pArr[I]);
}
}
// コンストラクタ(初期化用のList指定)
internal Fenwick_Tree(List<long> pList)
: this(pList.Count - 1)
{
for (int I = 0; I <= pList.Count - 1; I++) {
this.Add(I, pList[I]);
}
}
// [pSta,pEnd] のSumを返す
internal long GetSum(long pSta, long pEnd)
{
return GetSum(pEnd) - GetSum(pSta - 1);
}
// [0,pEnd] のSumを返す
internal long GetSum(long pEnd)
{
pEnd++; // 1オリジンに変更
long Sum = 0;
while (pEnd >= 1) {
Sum += mBitArr[pEnd];
pEnd -= pEnd & -pEnd;
}
return Sum;
}
// [I] に Xを加算
internal void Add(long pI, long pX)
{
pI++; // 1オリジンに変更
while (pI <= mBitArr.GetUpperBound(0)) {
mBitArr[pI] += pX;
pI += pI & -pI;
}
}
}
#endregion