AcWing101 最高的牛 (差分)

题目链接:https://www.acwing.com/problem/content/103/

假设初始身高都是(H),每次给出关系,就将([l+1,r-1])间的牛的高度减一
运用差分将区间操作转化为单点操作即可
同时,关系可能有重复,(map) 判下重即可

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int maxn = 100100;

int N,PP,H,M;
int a[maxn];

map<P, bool> exist;

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	N = read(), PP = read(), H = read(), M = read();
	a[1] = H;
	
	int u,v;
	for(int i=1;i<=M;++i){
		u = read(), v = read();
		if(u > v) swap(u, v);
		if(exist[make_pair(u, v)]) continue;
		exist[make_pair(u, v)] = true;
		if(u!=v && u+1!=v) --a[u+1], ++a[v];
	}
	
	int height = 0;
	for(int i=1;i<=N;++i){
		height += a[i];
		printf("%d
",height);
	}
	
	return 0;
}
原文地址:https://www.cnblogs.com/tuchen/p/13910402.html