Codeforces Round #442 (Div. 2) F. Ann and Books 莫队,离散化

F. Ann and Books
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In Ann’s favorite book shop are as many as n books on math and economics. Books are numbered from 1 to n. Each of them contains non-negative number of problems.

Today there is a sale: any subsegment of a segment from l to r can be bought at a fixed price.

Ann decided that she wants to buy such non-empty subsegment that the sale operates on it and the number of math problems is greater than the number of economics problems exactly by k. Note that k may be positive, negative or zero.

Unfortunately, Ann is not sure on which segment the sale operates, but she has q assumptions. For each of them she wants to know the number of options to buy a subsegment satisfying the condition (because the time she spends on choosing depends on that).

Currently Ann is too busy solving other problems, she asks you for help. For each her assumption determine the number of subsegments of the given segment such that the number of math problems is greaten than the number of economics problems on that subsegment exactly by k.

Input
The first line contains two integers n and k (1 ≤ n ≤ 100 000,  - 109 ≤ k ≤ 109) — the number of books and the needed difference between the number of math problems and the number of economics problems.

The second line contains n integers t1, t2, …, tn (1 ≤ ti ≤ 2), where ti is 1 if the i-th book is on math or 2 if the i-th is on economics.

The third line contains n integers a1, a2, …, an (0 ≤ ai ≤ 109), where ai is the number of problems in the i-th book.

The fourth line contains a single integer q (1 ≤ q ≤ 100 000) — the number of assumptions.

Each of the next q lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n) describing the i-th Ann’s assumption.

Output
Print q lines, in the i-th of them print the number of subsegments for the i-th Ann’s assumption.

Examples
input
4 1
1 1 1 2
1 1 1 1
4
1 2
1 3
1 4
3 4
output
2
3
4
1
input
4 0
1 2 1 2
0 0 0 0
1
1 4
output
10

题意:问区间l,r之间有多少对区间和为k,
做法:莫队,在处理区间里有多少个数的时候不能用map,map会超时,可以用先用map离散化,我试过unorder_map但是超时了,后来手写了hash_map,也超时了,后来发现是在hashmap里计算hash值得时候用了太多时间,然后预处理了hash值,卡过去了,hashmap室友常数的,如果预处理hash值得话大概是4-5,如果不预处理的话,估计更大。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+100;
const int B = 333;
struct node{
    int l,r,id;
    bool operator<(const node & p)const{
        if(l/B == p.l/B) return r < p.r;
        return l<p.l;
    }
};
node pt[N];
ll ans[N];
ll now = 0;
ll num[N];
int n,q,k;
set<ll> s;
map<ll,int> mp;
int pos[3][N];
ll sum[N*3];
void Move(int x,int op){
    //cout <<"!!" << x << ' ' << op << endl;
    if(op==1){
        //mp[num[x]]++;
        //now += mp[num[x]-k];
        sum[pos[0][x]]++;
        now += sum[pos[2][x]];
        if(k == 0) now --;
    }
    else if(op == 2){
        //now -= mp[num[x]-k];
        //mp[num[x]] --;
        now -= sum[pos[2][x]];
        sum[pos[0][x]]--;
        if(k == 0) now ++;
    }
    else if(op == 3){
        //mp[num[x-1]]++;
        //now += mp[num[x-1]+k];
        sum[pos[0][x-1]]++;
        now += sum[pos[1][x-1]];
        if(k == 0) now --;
    }
    else{
        //now -= mp[num[x-1]+k];
        //mp[num[x-1]] --;
        now -= sum[pos[1][x-1]];
        sum[pos[0][x-1]] --;
        if(k == 0) now ++;
    }
}

int main(){
    scanf("%d %d",&n,&k);
    for(int i = 1;i <= n;i ++) scanf("%lld",&num[i]);
    for(int i = 1;i <= n;i ++){
        if(num[i] == 2) num[i] = -1;
        int now;
        scanf("%d",&now);
        num[i] *= now;
    }
    for(int i = 0;i <= n;i ++) {
        num[i] = num[i-1]+num[i];
        s.insert(num[i]);
        s.insert(num[i]+k);
        s.insert(num[i]-k);
    }
    int tot = 0;
    for(auto it:s){
        mp[it] = tot++;
    }
    for(int i = 0;i <= n;i ++){
        pos[0][i] = mp[num[i]];
        pos[1][i] = mp[num[i]+k];
        pos[2][i] = mp[num[i]-k];
    }
    scanf("%d",&q);
    for(int i =1;i <= q;i ++){
        scanf("%d %d",&pt[i].l,&pt[i].r);
        pt[i].id = i;
    }
    sort(pt+1,pt+1+q);
    int L = 1,R = 1;
    sum[pos[0][0]]++;
    sum[pos[0][1]]++;
    if(num[1] == k) now ++;
    for(int i = 1;i <= q;i ++){
        while(R < pt[i].r) Move(++R,1);
        while(R > pt[i].r) Move(R--,2);
        while(L > pt[i].l) Move(--L,3);
        while(L < pt[i].l) Move(L++,4);
        ans[pt[i].id] = now;
    }
    for(int i = 1;i <= q;i ++){
        printf("%lld\n",ans[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zstu_zy/article/details/78812218
今日推荐