首页 > 其他分享 >每日一题-离散化

每日一题-离散化

时间:2022-11-12 00:22:44浏览次数:41  
标签:int sum back mid 离散 add push 一题 每日

Interval Sum

vector<int> all;
vector<pair<int, int> > add, query;

const int N = 3e5 + 5; // Attention! When worst case that l, r, x all different, we need 3e5 to store though n, m are no bigger than 1e5.
int a[N], sum[N];

int find(int x) {
	int l = 0, r = all.size() - 1;
	
	while (l < r) {
		int mid = (l + r) >> 1;
		if (all[mid] >= x) {
			r = mid;
		} else {
			l = mid + 1;
		}
	}
	
	return l + 1;
}

int main() {
	IOS;
	
	int n, m;
	
	cin >> n >> m;
	
	for (int i = 0; i < n; ++i) {
		int x, c;
		cin >> x >> c;
		
		add.push_back(make_pair(x, c));
		
		all.push_back(x);
	}
	
	for (int i = 0; i < m; ++i) {
		int l, r;
		cin >> l >> r;
		
		query.push_back(make_pair(l, r));
		
		all.push_back(l);
		all.push_back(r);
	}
	sort(all.begin(), all.end());
	
	all.erase(unique(all.begin(), all.end()), all.end());
	
	for (int i = 0; i < n; ++i) {
		int x = find(add[i].first);
		a[x] += add[i].second;
	}

	for (int i = 1; i <= (int)all.size(); ++i) {
		sum[i] = sum[i - 1] + a[i];
	}
	
	for (auto i: query) {
		int l = find(i.first), r = find(i.second);
		
		cout << sum[r] - sum[l - 1] << '\n';
	}
    return 0;
}

Description

Given an infinite number axis with all points have value 0, oprate n times, each add point \(p_i\) by \(x_i\). Then m queries, each ask the interval sum of interval \([l, r]\). Interval sum is the sum of point value from \(l\) to \(r\).

Discretization

Establishing the mapping relationship between a number sequence and natural numbers is what discretization does. Usually we can sort the value array, then erase the duplicates, then we use \(Binary-Search\) to find the new index. After discretization, we can implement \(pre-sum\) algorithm to solve the problem.

标签:int,sum,back,mid,离散,add,push,一题,每日
From: https://www.cnblogs.com/whose-dream/p/16882513.html

相关文章

  • 2022-11-11 Acwing每日一题
    本系列所有题目均为Acwing课的内容,发表博客既是为了学习总结,加深自己的印象,同时也是为了以后回过头来看时,不会感叹虚度光阴罢了,因此如果出现错误,欢迎大家能够指出错误,我......
  • 2022-11-10 Acwing每日一题
    本系列所有题目均为Acwing课的内容,发表博客既是为了学习总结,加深自己的印象,同时也是为了以后回过头来看时,不会感叹虚度光阴罢了,因此如果出现错误,欢迎大家能够指出错误,我......
  • 每日一题-区间合并
    MergingIntervalssort(a.begin(),a.end());vector<pair<int,int>>res;for(inti=0;i<n;++i){intl=0,r=res.size()-1;while(l<r......
  • 每日一学 之 一起来复习 Git 的那些操作(一)
    LZ-Says:突然间闯进来,感觉,她,变成了整个世界。前言曾经,Svn俗称小王八,伴随着走过了一年又一年。某年Git的横空出世,以迅雷不及掩耳之势强势登顶。也使用了Git将近快一年......
  • [leetcode每日一题]11.10
    864. 获取所有钥匙的最短路径给定一个二维网格 ​​grid​​ ,其中:'.' 代表一个空房间'#' 代表一堵'@'小写字母代表钥匙大写字母代表锁我们从起点开始出发,一次移动是指......
  • 每日一题算法
    数字在升序数组中出现的次数描述给定一个长度为n的非降序数组和一个非负数整数k,要求统计k在数组中出现的次数解析排序数组的查找问题首先考虑二分法使用二分法......
  • 每日一题-双指针
    判断子序列intj=0,i=0; while(i<mandj<n){if(b[i]==a[j]){j++;}i++;}cout<<(j==n?"Yes":"No");description......
  • 每日更新版
    目录10.25-html1、html5有哪些新特性?2、lframe的作用及其优缺点?3、常见浏览器内核是什么?4、image标签title与alt属性的区别是什么?5、对WEB标准以及W3C的理解与认识?6......
  • 每日一题之请描述Vue组件渲染流程
    组件化是Vue,React等这些框架的一个核心思想,通过把页面拆成一个个高内聚、低耦合的组件,可以极大程度提高我们的代码复用度,同时也使得项目更加易于维护。所以,本文就来分......
  • 每日一题-区间分组
    区间分组 sort(a.begin(),a.end()); priority_queue<int,vector<int>,greater<int>>q; for(inti=0;i<n;++i){ if(q.empty()orq.top()>=a[i].fir......