zoukankan      html  css  js  c++  java
  • 计蒜客 木桩涂涂看

    问题描述

    n 个木桩排成一排,从左到右依次编号为 1,2,3…n。每次给定 2 个整数 a,b(a≤b),蒜头君便骑上他的电动车从木桩 a 开始到木桩 b 依次给每个木桩涂一次颜色。但是 n 次以后 lele 已经忘记了第 i 个木桩已经涂过几次颜色了,你能帮他算出每个木桩被涂过几次颜色吗?


    输入格式

    第一行是一个整数 n(n≤100000)。

    接下来的 n 行,每行包括两个整数 a, b (1≤a≤b≤n)。

    输出格式

    n 个整数,第 i 个数代表第 i 个木桩总共被涂色的次数。


    样例输入

    3
    1 1
    1 2
    1 3
    1
    2
    3
    4

    样例输出

    3 2 1

    分析

    差分序列的基本应用

    代码

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #define maxn 100000
    using namespace std;
    int d[maxn + 5];
    void add(int a, int b){
    	d[a] += 1;
    	d[b + 1] -= 1;
    }
    int main(){
    	int n;
    	scanf("%d", &n);
    	for(int i = 1; i <= n; i++){
    		int a, b;
    		scanf("%d %d", &a, &b);
    		add(a, b);
    	}
    	//依次输出元素 
    	int t = d[1];
    	printf("%d ", d[1]);
    	for(int i = 2; i <= n; i++){
    		printf("%d ", d[i] + t);
    		t += d[i];
    	}
    	return 0;
    }
    
    

  • 相关阅读:
    MongoDB数据库新建数据库用户
    Grafana部署
    k8s ingress及ingress controller
    Rabbitmq如何安装插件
    RabbitMQ手册之rabbitmq-plugins
    RabbitMQ运行在Docker容器中
    K8S资源限制
    System类
    Runtime类
    StringBuffer类
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/10887485.html
Copyright © 2011-2022 走看看