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. [JAVA] 面向对象小小总结

    面向对象概述 符合人类思维习惯的编程思想 , 生活中存在着不同形态的事物 , 这些事物存在着不同的联系 , 在程序中使用对象来映射现实中事物 , 使用对象关系来描述事物之间的联系, 这种思想就是面向对 ...

  2. CSS小记录

    1.图片铺满 background: rgba(12, 100, 129, 1) url('https://images.cnblogs.com/cnblogs_com/yukarin/1639008 ...

  3. 【pattern】设计模式(2) - 模版方法模式

    前言 一晃一年又过了,还是一样的渣. 一晃2周又过去了,还是没有坚持写博客. 本来前2天说填一下SQL注入攻击的坑,结果看下去发现还是ojdbc.jar中的代码,看不懂啊.这坑暂时填不动,强迫在元旦最 ...

  4. Linux的语系的修改

    1. 显示目前所支持的语系 [root@test ~]# echo $LANG 2. 修改语系成为英文语系 字体集目录:"/etc/sysconfig/i18n",编辑该文件,将字 ...

  5. 【H5适配 笔记1】rem适配

    设备像素比(dpr)= 物理像素(手机渲染像素.分辨率)/设备独立像素(手机所显示元素的大小) 视口(viewport) 布局视口(获取浏览器布局视口高度,包括内边距,但不包括垂直滚动条.边框和外边距 ...

  6. 小白的java学习之路 “ 类和对象”

    一.※ 万物皆对象 二.对象的两个特征: 属性:对象具有的各种特征 方法:对象执行的操作 对象:用来描述客观事物的一个实体,由一组属性和方法构成 三.封装: 对象同时具有属性和方法两项特性 对象的属性 ...

  7. laravel中redis数据库的简单使用

    1.简介 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s . 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Set ...

  8. appium知识点

    1 appium元素获取技巧 # 就是页面滑动 driver.swipe(x1, y1, x1, y2, t) # 拿到所有跟元素有关的标签,其实是个列表 driver.find_elements_b ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 Give Candies 题解

    ACM-ICPC 2018 焦作赛区网络预赛 Give Candies n个糖果分给n个小朋友 从1到n个小朋友依次给,每次随机给个数,至少一个,知道没有糖果为止. 问糖果的分布情况方案数. 输出方案 ...

  10. ACM-ICPC 2018 徐州赛区网络预赛 Ryuji doesn't want to study

    简单数学变换+线段树 简单数据结构签到题不解释 本来应该贴板子的,鉴于最近写代码太少了,而且由于要用两个线段树,平时板子都是一个的.以及板子在队友那.就当熟悉写代码,自己写了一下. #include ...