https://www.acwing.com/problem/content/804/

#include <iostream>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = ;
int n, m;
int a[N], s[N]; //a是数字 s是前缀和
vector<int> alls; //存离散化之前的坐标 存的所有要离散化的值
vector<PII> add, query;//add是插入,query是求
int find(int x) { //求一下x这个值离散化之后的结果 找到第一个大于等于x的位置
int l = , r = alls.size() - ;
while (l < r) {
int mid = l + r >> ;
if (alls[mid] >= x) r = mid;
else l = mid + ;
}
return r + ; //从1开始 方便求前缀和
}
int main() {
cin >> n >> m;
for (int i = ; i < n; i ++ ) {
int x, c;
cin >> x >> c;
add.push_back({x, c}); //在下标位x 的位置加上c
alls.push_back(x); //把待离散化的坐标x存起来
}
for (int i = ; i < m; i ++ ) {
int l, r;
cin >> l >> r;
query.push_back({l, r}); //存操作的区间
alls.push_back(l); //把左右区间全部存到待离散化的数组里
alls.push_back(r);
} //此时已经把所有需要用到的下标放到了all数组里
// 去重 把all数组去重 //可能又重复的元素
sort(alls.begin(), alls.end());
alls.erase(unique(alls.begin(),alls.end()), alls.end()); //unique是将数组中重复的元素去重,并且返回去重之后数组的尾端点 ,再用erase删除到原来尾端点之间的数字
// 处理插入
for (auto item : add) {
int x = find(item.first); //先求离散化之后的结果
a[x] += item.second; //在离散化之后的坐标上加上要加的数
}
// 预处理前缀和
for (int i = ; i <= alls.size(); i ++ ) s[i] = s[i - ] + a[i];
// 处理询问
for (auto item : query) {
int l = find(item.first), r = find(item.second);
cout << s[r] - s[l - ] << endl;
}
return ;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = ;
int n, m;
int a[N], s[N]; //a是数字 s是前缀和
vector<int> alls; //存离散化之前的坐标 存的所有要离散化的值
vector<PII> add, query;//add是插入,query是求
int main() {
cin >> n >> m;
for (int i = ; i < n; i ++ ) {
int x, c;
cin >> x >> c;
add.push_back({x, c}); //在下标位x 的位置加上c
alls.push_back(x); //把待离散化的坐标x存起来
}
for (int i = ; i < m; i ++ ) {
int l, r;
cin >> l >> r;
query.push_back({l, r}); //存操作的区间
alls.push_back(l); //把左右区间全部存到待离散化的数组里
alls.push_back(r);
} //此时已经把所有需要用到的下标放到了all数组里
// 去重 把all数组去重 //可能又重复的元素
sort(alls.begin(), alls.end());
alls.erase(unique(alls.begin(),alls.end()), alls.end()); //unique是将数组中重复的元素去重,并且返回去重之后数组的尾端点 ,再用erase删除到原来尾端点之间的数字
// 处理插入
for (auto item : add) {
auto x = lower_bound(alls.begin(),alls.end(),item.first)- alls.begin() +; //先求离散化之后的结果
a[x] += item.second; //在离散化之后的坐标上加上要加的数
}
// 预处理前缀和
for (int i = ; i <= alls.size(); i ++ ) s[i] = s[i - ] + a[i];
// 处理询问
for (auto item : query) {
auto l = lower_bound(alls.begin(),alls.end(),item.first)- alls.begin() +;
auto r = lower_bound(alls.begin(),alls.end(),item.second)- alls.begin() +;
cout << s[r] - s[l - ] << endl;
}
return ;
}

AcWing 802. 区间和 离散化的更多相关文章

  1. AcWing 802. 区间和

    (https://www.acwing.com/problem/content/804/) 假定有一个无限长的数轴,数轴上每个坐标上的数都是0. 现在,我们首先进行 n 次操作,每次操作将某一位置x上 ...

  2. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  3. POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

  4. hiho一下21周 线段树的区间修改 离散化

    离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho ...

  5. POJ 2528 Mayor's posters(线段树/区间更新 离散化)

    题目链接: 传送门 Mayor's posters Time Limit: 1000MS     Memory Limit: 65536K Description The citizens of By ...

  6. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  7. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 ...

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  9. POJ2528 Mayor's posters(线段树&区间更新+离散化)题解

    题意:给一个区间,表示这个区间贴了一张海报,后贴的会覆盖前面的,问最后能看到几张海报. 思路: 之前就不会离散化,先讲一下离散化:这里离散化的原理是:先把每个端点值都放到一个数组中并除重+排序,我们就 ...

随机推荐

  1. Tensor的合并与分割

    先来看一下有哪些接口用来进行张量的合并与分割: tf.concat用来进行张量的拼接,tf.stack用来进行张量的堆叠,tf.split用来进行张量的分割,tf.unstack是tf.split的一 ...

  2. Java(三)String类

    一.String类初始化方法 1.初始化一个空字符串 String str=new String();//这里调用了String的无参构造方法 2.初始化一个有值的字符串 String str1=&q ...

  3. python3练习100题——039

    原题链接:http://www.runoob.com/python/python-exercise-example39.html 题目:有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数 ...

  4. python3练习100题——038

    原题链接:http://www.runoob.com/python/python-exercise-example38.html 题目:求一个3*3矩阵主对角线元素之和. 程序分析:利用双重for循环 ...

  5. markdwon编辑公式入门

    上标与下标   上标和下标分别使用^ 与_ ,例如\(x_i^2\)表示的是:.   默认情况下,上.下标符号仅仅对下一个组起作用.一个组即单个字符或者使用{..} 包裹起来的内容.如果使用\(10^ ...

  6. TensorFlow入门(常量变量及其基本运算)

    1.tensorflow常量变量的定义 测试代码如下: # encoding:utf-8 # OpenCV tensorflow # 类比 语法 api 原理 # 基础数据类型 运算符 流程 字典 数 ...

  7. RabbitMQ+PHP教程

    RabbitMQ+PHP 教程一(Hello World) RabbitMQ+PHP 教程二(Work Queues) RabbitMQ+PHP 教程三(Publish/Subscribe) Rabb ...

  8. eclipse中tomcat正常启动,但浏览器访问不了tomcat首页之tomcat配置问题

    症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能访问. 关闭eclipse里面的tomc ...

  9. 浅谈radis

    1.概述 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API 从2010年3月15日起,Redis的开发工作由VM ...

  10. navicat连接mysql查询结果中文都是?号(C#)

    记录解决方法,方便以后查看.  有几个地方需要注意: 1.连接mysql数据库的字符串最后加上Charset=utf8; 2.mysql中character_set_XX设置都为utf8,使用show ...