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. 堆排序用JavaScript实现

    class Heap { constructor (data) { this.data = data } sort () { let iArr = this.data let n = iArr.len ...

  2. Linux 基础操作命令

    关机和注销 shutdown -h now 立刻关机 shutdown -r now 立刻重启 shutdown -h + 1分钟后关机(重启同样用法) shutdown -h : 11点钟关机(重启 ...

  3. cartographer保存地图

    手持激光,并用cartographer建图,保存的地图是.pbstream格式 ht@ht:~$ rosservice call /write_state /home/ht/Desktop/carto ...

  4. 0级搭建类008-Ubuntu Server Linux安装 (18.04.2) 公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  5. StaticFileMiddleware 解析

       说明:由于部分产品没有静态资源的管理,我突然想到能不能用现有的静态文件中间件的功能调整一下实现多组织件上传文件的隔离呢?那第一步先看懂   StaticFileMiddleware做了什么吧. ...

  6. Visual Studio Code搭建Python开发环境方法总结

    更新:目前VSCode官方Python插件已经支持代码运行与调试,无需安装Code Runner插件. 1.下载安装Python,地址 https://www.python.org/downloads ...

  7. 2020牛客寒假算法基础集训营1 F-maki和tree

    链接:https://ac.nowcoder.com/acm/contest/3002/F来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  8. Codeforce 567A - Lineland Mail

    All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its ...

  9. Wannafly Camp 2020 Day 2K 破忒头的匿名信 - AC自动机,dp

    给定字典和文章,每个单词有价值,求写文章的最小价值 标准的 AC 自动机 dp,设 \(f[i]\) 表示写 \(s[1..i]\) 的最小价值,建立AC自动机后根据 trans 边暴力转移即可 建了 ...

  10. 【转】Java Future 怎么用 才算是真正异步

    接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果.        Callable接口类似于Runnable,从名字就可以看出来了,但 ...