离散化板子

离散化板子

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> PII;

const int N = 3e5 + 10;

int a[N], sum[N];
vector<int> alls;
vector<PII> add, query;
map<int, int> mp;
int n, m, x, c, l, r;

signed main(){
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; ++i){
        scanf("%d%d", &x, &c);
        alls.push_back(x);
        add.push_back({x, c});
    }
    for(int i = 0; i < m; ++i){
        scanf("%d%d", &l, &r);
        alls.push_back(l); alls.push_back(r);
        query.push_back({l, r});
    }
    sort(alls.begin(), alls.end());
    alls.erase(unique(alls.begin(), alls.end()), alls.end());
    
    for(int i = 0; i < alls.size(); ++i){
        mp[alls[i]] = i + 1;
    }
    for(auto item: add){
        int x = item.first;
        a[mp[x]] += item.second;
    }
    for(int i = 1; i <= alls.size(); ++i) sum[i] = sum[i-1] + a[i];
    for(auto item: query){
        l = item.first, r = item.second;
        printf("%d\n", sum[mp[r]] - sum[mp[l]-1]);
    }
    
    return 0;
}
posted @ 2025-04-05 19:10  awei040519  阅读(26)  评论(0)    收藏  举报