AcWing - 101 - 最高的牛(差分)

题目链接
  既然当且仅当两头牛中间的牛身高都比它们矮时,两头牛方可看到对方。那么我们可以假设这两头可以互相看到的牛身高相等,而他们中间的牛最少要比他们两个的身高少\(1\),所以我们每次
让他们之间的所有数减1就行了,但是如果是直接修改的话,显然复杂度会到\(n^2\)级别,但是因为询问只有一次,所以说不需要我们动态修改,这时候用差分就能大大的减少时间复杂度。

//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("==================================================\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  9901;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e4+10;
map<P, bool> mark;
int cow[maxn];
int main(void) {
    int n, p, h, m;
    scanf("%d%d%d%d", &n, &p, &h, &m);
    for (int i = 0, a, b; i<m; ++i) {
        scanf("%d%d", &a, &b);
        if (a>b) swap(a, b);
        if (mark[P(a,b)]) continue;
        mark[P(a,b)] = true; //用map检查是否重复操作
        cow[a+1] += -1; cow[b] += 1;
    }
    for (int i = 1; i<=n; ++i) {
        cow[i] += cow[i-1];
        printf("%d\n", cow[i]+h);
    }
    mark.clear();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shuitiangong/p/12545103.html