传送门:CodeForces-1111C
题目描述
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base.
Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of 2. Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following:
- if the current length is at least 2, divide the base into 2 equal halves and destroy them separately, or
- burn the current base. If it contains no avenger in it, it takes A amount of power, otherwise it takes his $B⋅n_a⋅l$ amount of power, where $n_a$ is the number of avengers and $l$ is the length of the current base.
Output the minimum power needed by Thanos to destroy the avengers’ base.
输入描述
The first line contains four integers $n$, $k$, $A$ and $B$ $(1≤n≤30, 1≤k≤10^5, 1≤A,B≤10^4)$, where $2^n$ is the length of the base, $k$ is the number of avengers and $A$ and $B$ are the constants explained in the question.
The second line contains $k$ integers $a_1,a_2,a_3,…,a_k$ $(1≤a_i≤2^n)$, where $a_i$ represents the position of avenger in the base.
输出描述
Output one integer — the minimum power needed to destroy the avengers base.
思路分析
很容易想到线段树模拟,但是空间太大了,所以要用到动态开点,只开需要的空间
样例输入
2 2 1 2
1 3
样例输出
6
AC代码
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define vi vector<int>
#define lowbit(x) x&(-x)
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e7 + 10;
struct node {
int lson, rson;
ll sum;
} tree[N];
int n, k, tot;
ll A, B;
void update(int &x, int l, int r, int pos) {
if (!x)x = ++tot;
tree[x].sum++;
if (l == r)return ;
int mid = l + r >> 1;
if (pos <= mid)
update(tree[x].lson, l, mid, pos);
else
update(tree[x].rson, mid + 1, r, pos);
}
ll query(int x, int l, int r) {
if (!x)return A;
if (l == r)return B * tree[x].sum;
int mid = l + r >> 1;
ll ans = B * (r - l + 1) * tree[x].sum;
ans = min(ans, query(tree[x].lson, l, mid) + query(tree[x].rson, mid + 1, r));
return ans;
}
int main() {
IOS;
#ifdef xiaofan
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
int ro = 0;
cin >> n >> k >> A >> B;
int cnt = 1 << n;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
update(ro, 1, cnt, x);
}
cout << query(ro, 1, cnt) << endl;
return 0;
}