Luogu U37449 Lycanthropy

题目

差分

CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int pls = 50001;

int pre[20000050];
long long cur;
long long tot;

void add(int l, int r, int val)
{
    pre[l + pls] += val;
    pre[r + 1 + pls] -= val;
}

int main()
{
    int n,m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
    {
        int v, x;
        scanf("%d%d", &v, &x);
        add(x - v + 1, x, -1);
        add(x + 1,x + v, 1);
        add(x - 3 * v + 1,x - 2 * v, 1);
        add(x - 2 * v + 1, x - v, -1);
        add(x + v + 1, x + 2 * v, 1);
        add(x + 2 * v + 1, x + 3 * v, -1);
    }
    for (int i = 1; i <= m + pls; ++i)
    {
        cur += pre[i];
        tot += cur;
        if (i > pls)
            printf("%lld ", tot);
    }
    printf("\n");
    return 0;
}

zky聚聚的差分:

#include<cstdio>
#include<iostream>
using namespace std;
inline int qread(){
    register int x = 0, ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch))  x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return x;
}
const int maxn = 1000010;
int mmap[maxn << 1];
int ans[maxn << 1];
int n, m;
int main(void){
    n = qread();
    m = qread();
    for(int i = 1; i <= n; ++i){
        int x = qread(), y = qread();
        y += 100000;
        mmap[y] -= 2;
        mmap[y - (x << 1)] += 2;
        mmap[y + (x << 1)] += 2;
        mmap[y - (x << 1) - x]--;
        mmap[y + (x << 1) + x]--;
    }
    for(int i = 3; i <= m + 120000; ++i){
        ans[i] = (ans[i - 1] << 1) - ans[i - 2] - mmap[i - 1];
        if(i >= 100001 && i <= 100000 + m){
            if(i == 100001) printf("%d", ans[i]);
            else            printf(" %d", ans[i]);
        }
    }
    printf("\n");
}
posted @ 2018-11-01 19:03  PushinL  阅读(117)  评论(0编辑  收藏  举报