专栏文章
题解:P12194 [NOISG 2025 Prelim] Snacks
P12194题解参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mipd7ywd
- 此快照首次捕获于
- 2025/12/03 10:05 3 个月前
- 此快照最后确认于
- 2025/12/03 10:05 3 个月前
题目大意
有一个长度为 的序列。
有 次查询,每次查询将输入 ,你需要将值在 内的所有数字都变为 。
解法
我们可以使用 STL。
在这里,我们采用
map 来记录每个值有多少数。于是,对于每次查询,我们将范围内的元素删除,再将 和对应的数量加入
map。时间复杂度
很明显,每次修改不会增加数的总数,所以
map 最多只会修改 次,时间复杂度为 ,可以通过本题代码
CPP#include <bits/stdc++.h>
#define int long long
const int N = 1e5 + 5;
const int Mod = 1e9 + 7;
using namespace std;
int n, q;
map<int, int> mp;
signed main()
{
int sum = 0;
cin >> n >> q;
while (n--)
{
int x;
cin >> x;
mp[x]++;
sum += x;
}
cout << sum << endl;
while (q--)
{
int l, r, x;
cin >> l >> r >> x;
auto it = mp.lower_bound(l);
int cnt = 0;
while (it != mp.end() && it->first <= r)
{
cnt += it->second;
sum += (x - it->first) * it->second;
it = mp.erase(it);
}
mp[x] += cnt;
cout << sum << endl;
}
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...