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. EasyUI笔记(一)Base基础

    总结: 1)每个UI都是通过属性.事件和方法运作的 2)用JS加载时只需传入一个参数(用大括号{}包围),包含若干个键值对,之间用逗号隔开: 3)事件也是写在JS加载时的方法中,也是键值对形式,值是匿 ...

  2. Java中的isEmpty方法、null以及""的区别

    本文转自:https://blog.csdn.net/peng86788/article/details/80885814 这是一个比较容易混淆的概念,为了弄清楚这个问题,最好的方法当然是写程序来验证 ...

  3. ASP.NET MVC 方法View返回的两种方式

    1.参数为字符串类型 例如我们在地址栏输入http://localhost:56431/Test/Index,会查找TestController类下的Index方法并执行,如下图 当我们返回字符串类型 ...

  4. docker 安装 jenkins touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

    拉取镜像 docker pull jenkins/jenkins:lts 官方下载 运行容器 docker run -it -v /home/jenkins:/var/jenkins_home -p ...

  5. springboot中返回值json中null转换空字符串

    在实际项目中,我们难免会遇到一些无值.当我们转JSON时,不希望这些null出现,比如我们期望所有的null在转JSON时都变成“”“”这种空字符串,那怎么做呢? Jackson中对null的处理 @ ...

  6. 常用sql 2018.08.31

    concat()函数 concat(str1, str2,...)功能:将多个字符串连接成一个字符串 返回结果为连接参数产生的字符串,如果有任何一个参数为null,则返回值为null. 如:CONCA ...

  7. 分析器错误消息: Reference.svcmap:未能加载文件

    -------------- 缺少了:System.Web.Razor.dll

  8. 【你不知道的javaScript 上卷 笔记3】javaScript中的声明提升表现

    console.log( a ); var a = 2; 执行输出undefined a = 2; var a; console.log( a ); 执行输出2 说明:javaScript 运行时在编 ...

  9. 多项式乘法逆元 - NTT

    递归求解即可 #include <bits/stdc++.h> using namespace std; #define int long long namespace NTT { #de ...

  10. [APIO2010] 回文串 - 回文自动机

    经典题吧 我觉得我要换个板子,这结构体板子真TM不顺手 #include <bits/stdc++.h> using namespace std; const int N = 2e6 + ...