Codeforces Round #442 (Div. 2) F. Ann and Books (莫队分块)

http://codeforces.com/contest/877/problem/F

题意:有n = 1e5个数,有正有负,有1e5个区间询问,每次询问区间[l, r]中有多少个子区间的和为K, 所有询问K都是一样的,小于1e9.

思路:求前缀和sum[], 等价于询问sum[] 在 [L - 1, R] 中有多少对(l,r) 满足sum[r] - sum[l] = K. 所以可以分块暴力,每次加数删数都访问cnt[]数组,表示某个前缀的个数是多少,由于nsqrt(n)*log(n) 太慢,所以可以离散化优化成nsqrt(n).



这是一个莫队的模板题,莫队有套路所在,首先算法执行中的L,R指针都是前闭后闭的区间,这样莫队的过程如下:

    int L = 0, R = -1;
    for (int i = 1; i <= Q; ++i) {
      int l = seq[i].l, r = seq[i].r;
      while (R < r) add(A[++R]);
      while (R > r) del(A[R--]);
      while (L < l) del(A[L++]);
      while (L > l) add(A[--L]);
      ans[seq[i].id] = res;
    }

唯一需要修改的东西就是add和del函数,还有就是L和R的初始值,如果A[]是0-based那么L = 0, R = 0; 若为1-based那么L = 1, R = 0;  而且始终都是先移R指针再移L指针才能保持区间合法。










#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define REP(i,n) for ( int i=1; i<=int(n); i++ )  
#define MP make_pair
#define PB push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define X first
#define Y second
template inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long LL;
typedef long double LD;
const int INF = 0x3f3f3f3f;

template   
inline bool RD(T &ret) {  
        char c; int sgn;  
        if (c = getchar(), c == EOF) return 0;  
        while (c != '-' && (c<'0' || c>'9')) c = getchar();  
        sgn = (c == '-') ? -1 : 1 , ret = (c == '-') ? 0 : (c - '0');  
        while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');  
        ret *= sgn;  
        return 1;  
}  
template   
inline void PT(T x) {  
        if (x < 0) putchar('-') ,x = -x;  
        if (x > 9) PT(x / 10);  
        putchar(x % 10 + '0');
}
typedef pair pii;

const int N = 1e5 + 100;
const int BLK = 350;
LL a[N];
int t[N];
map mp;
LL cnt[N];
int top;
int nxt[N], pre[N];
pair qry[N];
LL res[N];
bool cmp(pair &a, pair &b) {
    if(a.X.X / BLK == b.X.X / BLK) {
        return a.X.Y < b.X.Y;
    } 
    return a.X.X / BLK < b.X.X / BLK;
}

int main() {
    int n, K;
    cin >> n >> K;
    for(int i = 1; i <= n; i ++) scanf("%d", &t[i]);
    mp[0] = ++ top;
    for(int i = 1; i <= n; i ++) {
        scanf("%lld", &a[i]);
        if(t[i] == 1) a[i] = a[i] + a[i - 1];
        else a[i] = a[i - 1] - a[i];
        if(mp[a[i]] == 0) mp[a[i]] = ++ top;
        if(mp[a[i] - K]) {
            pre[mp[a[i]]] = mp[a[i] - K];
            nxt[mp[a[i] - K]] = mp[a[i]];
        }
        if(mp[a[i] + K]) {
            nxt[mp[a[i]]] = mp[a[i] + K];
            pre[mp[a[i] + K]] = mp[a[i]];
        }
    } 

    for(int i = 0; i <= n; i ++) a[i] = mp[a[i]];

    int Q;
    cin >> Q;
    for(int i = 1; i <= Q; i ++) {
        scanf("%d %d", &qry[i].X.X, &qry[i].X.Y);
        qry[i].X.X --;
        qry[i].Y = i;
    }

    sort(qry + 1, qry + 1 + Q, cmp);

    LL ans = 0;
    for(int L = 0, R = -1, i = 1; i <= Q; i ++) {
        int l = qry[i].X.X, r = qry[i].X.Y;
        int now = 0;
        while(R < r) {
            now = a[++ R];
            if(pre[now]) ans += cnt[pre[now]];
            cnt[now] ++;
        }
        
        while(R > r) {
            now = a[R --];
            cnt[now] --;
            if(pre[now]) ans -= cnt[pre[now]];
        }

        while(L > l) {
            now = a[-- L];
            if(nxt[now]) ans += cnt[nxt[now]];
            cnt[now] ++;
        }

        while(L < l) {
            now = a[L ++];
            cnt[now] --;
            if(nxt[now]) ans -= cnt[nxt[now]];
        }

        res[qry[i].Y] = ans;
    }
    
    for(int i = 1; i <= Q; i ++) printf("%lld\n", res[i]);
}





你可能感兴趣的:(莫队分块)