• 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;
    }
    
  • 相关阅读:
    洛谷7月月赛 B 题解
    undone
    树剖学习
    关于两周后noip---小计划
    线段树技巧练习--GSS1
    链式前向星存图及注意要点
    错题集合
    树上差分问题
    2020暑假学习内容整理及后续计划
    安利大佬博客
  • 原文地址:https://www.cnblogs.com/tuchen/p/13910402.html
Copyright © 2020-2023  润新知