6983 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛3) [记忆化搜索]
Posted 你可以当我没名字
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6983 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛3) [记忆化搜索]相关的知识,希望对你有一定的参考价值。
Segment Tree with Pruning
*Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 544 Accepted Submission(s): 404
*
Problem Description
Chenjb is struggling with data stucture now. He is trying to solve a problem using segment tree. Chenjb is a freshman in programming contest, and he wrote down the following C/C++ code and ran ‘‘Node* root = build(1, n)’’ to build a standard segment tree on range [1,n]:
Node* build(long long l, long long r) {
Node* x = new(Node);
if (l == r) return x;
long long mid = (l + r) / 2;
x -> lchild = build(l, mid);
x -> rchild = build(mid + 1, r);
return x;
}
Chenjb submitted his code, but unfortunately, got MLE (Memory Limit Exceeded). Soon Chenjb realized that his program will new a large quantity of nodes, and he decided to reduce the number of nodes by pruning:
Node* build(long long l, long long r) {
Node* x = new(Node);
if (r - l + 1 <= k) return x;
long long mid = (l + r) / 2;
x -> lchild = build(l, mid);
x -> rchild = build(mid + 1, r);
return x;
}
You know, Chenjb is a freshman, so he will try different values of k to find the optimal one. You will be given the values of n and k, please tell him the number of nodes that will be generated by his new program.
Input
The first line contains a single integer T (1≤T≤10000), the number of test cases. For each test case:
The only line contains two integers n and k (1≤k≤n≤1018), denoting a query.
Output
For each query, print a single line containing an integer, denoting the number of segment tree nodes.
Sample Input
3
100000 1
100000 50
1000000000000000000 1
Sample Output
199999
4095
1999999999999999999
Source
2021“MINIEYE杯”中国大学生算法设计超级联赛(3)
Recommend
IceyWang | We have carefully selected several similar problems for you: 7011 7010 7009 7008 7007
代码与解释
https://acm.hdu.edu.cn/showproblem.php?pid=6983
看懂题目,会记忆化搜索就能过了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ppb pop_back
#define lbnd lower_bound
#define ubnd upper_bound
#define endl '\\n'
#define mll map<ll,ll>
#define msl map<string,ll>
#define mls map<ll, string>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x) for(auto& a : x)
#define pll pair<ll,ll>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ini(a) memset(a,0,sizeof(a))
#define ini2(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define sc ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x) x&(-x)
#define pr printf
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
(void)(cout << "L" << __LINE__ \\
<< ": " << #x << " = " << (x) << '\\n')
#define TIE \\
cin.tie(0);cout.tie(0);\\
ios::sync_with_stdio(false);
//#define long long int
//using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 100009;
const ll N = 1000000007;
mll ma;
ll n, k, cnt=0;
ll build(ll l, ll r){
if(ma.count(r-l))return ma[r-l];//存在
if(r-l+1<=k){
return 1;
}
ll mid = (l+r)/2, tmp = 1;
tmp += build(l,mid);
tmp += build(mid+1,r);
return ma[r-l] = tmp;
}
void solve(){
ma.clear();
cin>>n>>k;
cout<<build(1,n)<<"\\n";
}
int main()
{
// TIE;
// #ifndef ONLINE_JUDGE
// freopen ("input.txt","r",stdin);
// #else
// #endif
// solve();
sc{solve();}
// sc{cout<<"Case "<<Q<<":"<<endl;solve();}
}
以上是关于6983 杭电多校(2021“MINIEYE杯”中国大学生算法设计超级联赛3) [记忆化搜索]的主要内容,如果未能解决你的问题,请参考以下文章
2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题3题
2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题4题
2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题5题
2021杭电多校赛2021“MINIEYE杯”中国大学生算法设计超级联赛签到题2题