离散化
离散化就是一种映射,可以将大区间较分散的点,映射到小区间密集的点。或者说原数组中被赋值,被使用过的下标,映射到一个相对密集的下标区间。相对大小没有改变,却节省了储存空间。
如果数组数据范围<=1e5,可以用前缀和。
对a[x]+=c,用s[K]-s[L-1]来求前缀和。
如果数据范围很大,则需要用到离散化。
vector <整数> alls;的操作
unique(x,y)函数可以把一段数组的重复元素放到“后边”,并返回重复元素的起始下标。
find(x,y)函数可以去掉数组在x到y区间内的全部元素。
区间和
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 300010;
int n, m;
int a[N], s[N];
vector<int> alls;
vector<PII> add, query;
int find(int x)
{
int l = 0, r = alls.size() - 1;
while (l < r)
{
int mid = l + r >> 1;
if (alls[mid] >= x) r = mid;
else l = mid + 1;
}
return r + 1;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
{
int x, c;
cin >> x >> c;
add.push_back({x, c});
alls.push_back(x);
}
for (int i = 0; i < m; i ++ )
{
int l, r;
cin >> l >> r;
query.push_back({l, r});
alls.push_back(l);
alls.push_back(r);
}
// 去重
sort(alls.begin(), alls.end());
alls.erase(unique(alls), alls.end());
// 处理插入
for (auto item : add)
{
int x = find(item.first);
a[x] += item.second;
}
// 预处理前缀和
for (int i = 1; i <= alls.size(); i ++ ) s[i] = s[i - 1] + a[i];
// 处理询问
for (auto item : query)
{
int l = find(item.first), r = find(item.second);
cout << s[r] - s[l - 1] << endl;
}
return 0;
}
A - Zero Array(转载杰巨)
You are given an array a consisting of n elements, and q queries. There are two types of queries, as follow:
“1 p v” – An update query asks to change the value at position p in array a to v.
“2” – A query asks to print the minimum number of required operations to convert array a to a zero array.
A zero array is defined as an array which all its elements are zeros. There is only one allowed operation to convert an array to a zero array. At each operation, you can choose a value x and subtract it from all non-zero elements in the array, such that no element will be negative after the operation.
Input
The first line contains an integer T (1 ≤ T ≤ 100), in which T is the number of test cases.
The first line of each test case consists of two integers n and q (1 ≤ n, q ≤ 105), in which n is the size of the array a, and q is the number of queries.
Then a line follows containing n elements a1, a2, …, an (0 ≤ ai ≤ 109), giving the array a.
Then q lines follow, each line containing a query in the format described in the problem statement. It is guaranteed that the following constraints hold for the first type of queries: 1 ≤ p ≤ n, 0 ≤ v ≤ 109.
The sum of n and q overall test cases does not exceed 106 for each.
Output
For each query of the second type, print the minimum number of required operations to convert array a to a zero array. The queries must be answered in the order given in the input.
Example
Input
1
5 5
3 2 1 5 4
1 2 3
2
1 3 2
1 4 1
2
Output
4
4
用map实现离散化映射
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
map<int,int>mp;//离散化
int a[111111];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
mp.clear(); //格式化,避免下一个测试数据出问题
int n,q;
scanf("%d%d",&n,&q);
int count=0;//用来标记有几个不同的数的(不包括0),因为题意是问把某个数变成0,所以0不考虑
for(int i=1;i<=n;i++)
{
int num;
scanf("%d",&num);
a[i]=num;
if(num&&mp[num]==0) //如果这个数第一次出现,说明输入了一个新的数,所以conut+1
{
count++;
}
mp[num]++;//用mp来储存该数出现的次数
}
while(q--)
{
int x;
scanf("%d",&x);
if(x==1)
{
int p,v;
scanf("%d%d",&p,&v); //把第p个数,变成v
int y=a[p]; //先把第p个数记下来(这个数是还没改动之前的) ,记下来是为了下面,删掉时,这个数对应出现的次数-1
if(v&&mp[v]==0) //如果新加的数,是之前没出现过的,就count+1,出现过的话,就没必要了
{
count++;
}
mp[v]++; //v这个数出现次数+1
a[p]=v; //更新p位置的数为v
mp[y]--; //这是把之前那个旧的数,删掉,
if(y&&mp[y]==0) //如果旧的那个数完全没了,说明有个种类的数完全没了,conut-1
{
count--;
}
}
else
{
printf("%d\n",count); //count就是要输出的答案,即有几个不同的数(非零)
}
}
}
return 0;
}