当前位置:网站首页>2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
2022-08-05 10:08:00 【HeartFireY】
题目分析
本题解时间复杂度 O ( T log n ) O(T\log n) O(Tlogn),标程 O ( T log 2 n ) O(T \log^2 n) O(Tlog2n)
发现每个节点的按顺序 b u i l d build build产生的加法序列的规律:(若干个 1 1 1)+(相同个数个 2 , 3 , 4... 2,3,4... 2,3,4...)+(若干个相同数字),然后发现加 2 , 3 , 4 , . . . 2,3,4,... 2,3,4,...的次数与层节点个数有关。即为加 2 ⌊ l o g 2 x ⌋ 2^{ {\lfloor log_2{x} \rfloor}} 2⌊log2x⌋次 2 , 3 , 4 , … 2,3,4,\dots 2,3,4,…。
这个规律可以通过 1. 1. 1.分析线段树 b u i l d build build性质 2. 2. 2.暴力打表(牛逼) 发现
那么我们可以写成式子:假设加法序列长度为 s i z siz siz,加 1 1 1的个数为 c n t 1 cnt_1 cnt1,那么该节点 r t rt rt的加和可以表示为:
c n t 1 + ( ( ( s i z − c n t 1 ) ( 2 ⌊ l o g 2 x ⌋ ) + 1 ) × ( ( s i z − c n t 1 ) ( 2 ⌊ l o g 2 x ⌋ ) + 2 ) 2 − 1 ) × 2 ⌊ l o g 2 x ⌋ + [ ( s i z − c n t 1 ) m o d ( 2 ⌊ l o g 2 x ⌋ ) ) ] × ( ( s i z − c n t 1 ) ( 2 ⌊ l o g 2 x ⌋ ) + 2 ) cnt_1 + (\frac{(\frac{(siz - cnt1)}{(2^{ {\lfloor log_2{x} \rfloor}})} + 1) \times (\frac{(siz - cnt1)}{(2^{ {\lfloor log_2{x} \rfloor}})} + 2)}{2} - 1) \times 2^{ {\lfloor log_2{x} \rfloor}} + [(siz - cnt_1) \mod (2^{ {\lfloor log_2{x} \rfloor}}))] \times (\frac{(siz - cnt1)}{(2^{ {\lfloor log_2{x} \rfloor}})} + 2) cnt1+(2((2⌊log2x⌋)(siz−cnt1)+1)×((2⌊log2x⌋)(siz−cnt1)+2)−1)×2⌊log2x⌋+[(siz−cnt1)mod(2⌊log2x⌋))]×((2⌊log2x⌋)(siz−cnt1)+2)加的数字个数 s i z siz siz的规律,对于当前节点 r t rt rt:
- ⌈ ( r t − 2 ⌊ l o g 2 r t ⌋ ) / 2 ⌉ m o d 2 = = 1 , s i z − = 2 ⌊ l o g 2 r t ⌋ − 2 \lceil(rt - 2^{\lfloor log_2{rt} \rfloor}) / 2 \rceil \mod2 == 1, siz -= 2^{\lfloor log_2{rt} \rfloor - 2} ⌈(rt−2⌊log2rt⌋)/2⌉mod2==1,siz−=2⌊log2rt⌋−2
- ⌈ ( r t − 2 ⌊ l o g 2 r t ⌋ ) / 2 ⌉ m o d 2 = = 0 , s i z − = 2 ⌊ l o g 2 r t ⌋ − 1 \lceil(rt - 2^{\lfloor log_2{rt} \rfloor}) / 2 \rceil \mod2 == 0, siz -= 2^{\lfloor log_2{rt} \rfloor - 1} ⌈(rt−2⌊log2rt⌋)/2⌉mod2==0,siz−=2⌊log2rt⌋−1
求 s i z siz siz时,可以令 s i z = n siz = n siz=n,然后沿着树上路径(实际上对应数字的二进制位)向上走,然后减去当前节点对应该减的值。
1 1 1的规律:可以 O ( 1 ) O(1) O(1)求:
c n t 1 = { 2 ( ⌊ l o g 2 x ⌋ − 1 ) , ( x − 2 ⌊ l o g 2 x ⌋ + 1 ) m o d 2 = 1 2 ⌊ l o g 2 x ⌋ , ( x − 2 ⌊ l o g 2 x ⌋ + 1 ) m o d 2 = 0 cnt_1 =\left\{\begin{matrix} 2^{({\lfloor log_2{x} \rfloor} - 1)},\ (x - 2^{ {\lfloor log_2{x} \rfloor}} + 1) \mod 2 =1 \\ 2^{ {\lfloor log_2{x} \rfloor}},\ (x - 2^{ {\lfloor log_2{x} \rfloor}} + 1) \mod 2 = 0 \end{matrix}\right. cnt1={ 2(⌊log2x⌋−1), (x−2⌊log2x⌋+1)mod2=12⌊log2x⌋, (x−2⌊log2x⌋+1)mod2=0注意,当 s i z < 0 siz < 0 siz<0时,特判输出 0 0 0,对第一层求 s i z siz siz时也要特判避免溢出。同时对 n = 1 n = 1 n=1的 4 4 4种情况也要特判,避免溢出。
Code
#include <bits/stdc++.h>
#pragma gcc optimize("O2")
#pragma g++ optimize("O2")
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
inline void solve(){
int n = 0, x = 0; cin >> n >> x;
if(n == 1 && x == 1){
cout << 1 << endl; return; }
else if(n == 1 && x != 1){
cout << 0 << endl; return; }
if(x == 1){
cout << (n * (n + 1) / 2) << endl; return; }
//cout << "FUCK:" << get_range(x) << endl;
//if(x > get_range(x)){ cout << 0 << endl; return; }
int ceng = log2(x), ceng_fir = (1 << (ceng));
int cnt1 = ((x - ceng_fir + 1) % 2) ? (1 << (ceng - 1)) : ceng_fir;
if((x - ceng_fir + 1) == 0) cnt1 = ceng_fir;
// cnt_1 -> 1的个数
int siz = n;
int rt = x;
while(rt != 2 && rt != 3){
// cout << rt << " BEF | ";
int ceng_now = log2(rt) + 1, ser = rt - (1 << (ceng_now - 1)) + 1;
int md = ser % 4;
if(md == 1 || md == 2) siz -= (1 << (ceng_now - 3));
if(md == 3 || md == 0) siz -= (1 << (ceng_now - 2));
rt >>= 1;
// cout << rt << " AFT " << endl;
// cout << siz << endl;
}
if(rt != 1) siz -= 1;
if(siz <= 0){
cout << 0 << endl;
return;
}
if(siz <= cnt1){
cout << siz << endl;
return;
}
int yu = (siz - cnt1) % (ceng_fir), m = (siz - cnt1) / (ceng_fir);
int ans = yu * (m + 2) + cnt1 + ceng_fir * ((m + 1) * (m + 2) / 2 - 1);
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout << fixed << setprecision(12);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}
边栏推荐
- Can MySQL use aggregate functions without GROUP BY?
- 第五章:多线程通信—wait和notify
- 攻防世界-PWN-new_easypwn
- EU | Horizon 2020 ENSEMBLE: D2.13 SOTIF Safety Concept (Part 2)
- STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
- Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
- The JVM collection that Alibaba's top architects have summarized for many years, where can't I check it!
- Our Web3 Entrepreneurship Project, Yellow
- 无题五
- 蚁剑webshell动态加密连接分析与实践
猜你喜欢

5. Deploy the web project to the cloud server

Technical dry goods | Hausdorff distance for image segmentation based on MindSpore

After Keil upgrades to AC6, what changes?
![[Strong Net Cup 2022] WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
[Strong Net Cup 2022] WP-UM

Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU

egg框架使用(一)

Advanced usage of C language

JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number

告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来

蚁剑webshell动态加密连接分析与实践
随机推荐
创建一个 Dapp,为什么要选择波卡?
第八章:activiti多用户任务分配
无题三
韦东山 数码相框 项目学习(六)tslib的移植
MySQL使用聚合函数可以不搭配GROUP BY分组吗?
Egg framework usage (2)
手写柯里化 - toString 理解
气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)
开发常用手册链接分享
基于MindSpore高效完成图像分割,实现Dice!
NowCoderTOP35-40 - continuous update ing
IDEA执行Test操作导致数据插入时出现了重复数据
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
浅析WSGI协议
语音社交软件开发——充分发挥其价值
DFINITY 基金会创始人谈熊市沉浮,DeFi 项目该何去何从
egg框架使用(一)
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
egg框架使用(二)
Introduction to SD NAND Flash!