码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu U37449 Lycanthropy

时间:2018-11-01 19:55:07      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:names   printf   val   ant   using   algo   int   show   差分   

题目

差分

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");
}

Luogu U37449 Lycanthropy

标签:names   printf   val   ant   using   algo   int   show   差分   

原文地址:https://www.cnblogs.com/pushinl/p/9891182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!