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. P4072 [SDOI2016]征途

    斜率优化裸题 题意大概是:求 最小的 \(m^2s^2\) =\(m^2(\frac{1}{m}\sum_{i=1}^{m}(sum_i - {\frac{\sum_{i=1}^{m}sum_i}{m ...

  2. 使用hexo+github搭建免费个人博客详细教程(转载)

    https://www.cnblogs.com/liuxianan/p/build-blog-website-by-hexo-github.html 1.上传文档的hexo常用命令 2.输入hexo ...

  3. SDN-数据控制分离

    严格来说,控制面与数据面分离并不是SDN的专利.从一个chassis角度看,传统路由器其实控制面和转发面也是分离的.Route-enginee和line card分别负责控制面板和转发面.但是传统网络 ...

  4. sqli-labs less-5 --> less-6

    盲注笔记: SQL盲注攻击:只有两种回显结果,错误和正确:无法用url查询爆出数据库的信息:回显注入语句中加入判断方式,使得回显结果为true或者false: SQL盲注分类:布尔盲注,时间盲注,报错 ...

  5. H5_0011:JS动态创建html并设置CSS属性

    1,创建html文本,并设置指定css样式 r = function(e) { var t = document.createElement("div"); t.innerHTML ...

  6. vue koa2 mongodb 从零开始做个人博客(一) 登录注册功能前端部分

    0.效果演示 插入视频插不进来,就很烦.可以出门右拐去优酷看下(点我!). 1.准备工作 1.1前端框架 前端使用了基于vue.js的nuxt.js.为什么使用nuxt.js? 首先我做的是博客的项目 ...

  7. Jmeter-第三方插件安装

    1.插件下载 官方地址https://jmeter-plugins.org/install/Install/ 网盘地址链接: https://pan.baidu.com/s/1E4lnMWDGPWCN ...

  8. Wannafly Camp 2020 Day 2A 托米的字符串

    #include <bits/stdc++.h> using namespace std; const int N = 1000005; int n; char str[N]; int a ...

  9. [JSOI2013] 快乐的 JYY - 回文自动机,DFS

    #include <bits/stdc++.h> #define Sigma 30 #define MAXN 500010 #define int long long using name ...

  10. shell登录 脚本 expect

    作用 工作中,我们运行命令.脚本或程序时,这些命令.脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行. 利用expect,则可以根据程序的提示,模拟标准输入提供给程序,从而 ...