AcWing - 101 - the highest cattle (differential)

Topic Link
  Now if and only if the middle two cows when they are shorter than the height of the cattle, two cows before they see each other. So we can assume that this is equal to two can see each other cattle height, and among them two of their cattle a minimum height less than \ (1 \) , so every time we
make all the numbers between them on the line minus 1 but if it is modified directly, it is clear that the complexity will go to \ (n ^ 2 \) level, but because only ask once, so that we do not need dynamic modification, this time with a difference can greatly reduce the time complexity.

//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;
}

Guess you like

Origin www.cnblogs.com/shuitiangong/p/12545103.html