当前位置:网站首页>2022 Hangzhou Electric Power Multi-School Session 6 1008.Shinobu Loves Segment Tree Regular Questions
2022 Hangzhou Electric Power Multi-School Session 6 1008.Shinobu Loves Segment Tree Regular Questions
2022-08-05 10:17:00 【HeartFireY】
题目分析
The time complexity of solving this problem O ( T log n ) O(T\log n) O(Tlogn),标程 O ( T log 2 n ) O(T \log^2 n) O(Tlog2n)
Find each node in order b u i l d build buildThe regularity of the resulting addition sequence:(若干个 1 1 1)+(same number 2 , 3 , 4... 2,3,4... 2,3,4...)+(several identical numbers),Then find the plus 2 , 3 , 4 , . . . 2,3,4,... 2,3,4,...The number of times is related to the number of layer nodes.即为加 2 ⌊ l o g 2 x ⌋ 2^{ {\lfloor log_2{x} \rfloor}} 2⌊log2x⌋次 2 , 3 , 4 , … 2,3,4,\dots 2,3,4,….
This rule can be passed 1. 1. 1.Analyze the segment tree b u i l d build build性质 2. 2. 2.暴力打表(牛逼) 发现
Then we can write the formula:Suppose the addition sequence length is s i z siz siz,加 1 1 1的个数为 c n t 1 cnt_1 cnt1,那么该节点 r t rt rtThe sum of can be expressed as :
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)The number of digits to add 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,Then follow the path up the tree(Actually corresponds to the binary digit of the number)向上走,Then subtract the value corresponding to the current node.
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,Ask for the first level s i z siz sizIt is also necessary to make special judgments to avoid overflow.同时对 n = 1 n = 1 n=1的 4 4 4A special case is also required,避免溢出.
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;
}
边栏推荐
- Bias lock/light lock/heavy lock lock is healthier. How is locking and unlocking accomplished?
- [Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
- Offensive World-PWN-new_easypwn
- ffmpeg drawtext add text watermark
- 告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
- 第七章,activiti个人任务分配,动态指定和监听器指定任务委派人「建议收藏」
- 创建一个 Dapp,为什么要选择波卡?
- 第四章:activiti流程中,变量的传递和获取流程变量 ,设置和获取多个流程变量,设置和获取局部流程变量「建议收藏」
- 还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
- 哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
猜你喜欢
Our Web3 Entrepreneurship Project, Yellow
2022 Huashu Cup Mathematical Modeling Question A Optimization Design Ideas for Ring Oscillators Code Sharing
Custom filters and interceptors implement ThreadLocal thread closure
three.js调试工具dat.gui使用
Jenkins使用手册(2) —— 软件配置
High-quality DeFi application building guide to help developers enjoy DeFi Summer
Introduction to SD NAND Flash!
Egg framework usage (2)
我们的Web3创业项目,黄了
Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
随机推荐
产品太多了,如何实现一次登录多产品互通?
Imitation SBUS fixed with serial data conversion
SMB + SMB2: Accessing shares return an error after prolonged idle period
STM32+ULN2003驱动28BYJ4步进电机(根据圈数正转、反转)
Getting started with Polkadot parachain development, this article is enough
uniapp 连接ibeacon
Pytorch Deep Learning Quick Start Tutorial -- Mound Tutorial Notes (3)
一个栈的输入序列为1 2 3 4 5 的出站顺序的理解
E-sports, convenience, efficiency, security, key words for OriginOS functions
告白数字化转型时代:麦聪软件以最简单的方式让企业把数据用起来
Keil升级到AC6后,到底有哪些变化?
leetcode: 529. 扫雷游戏
How can project cost control help project success?
这份阿里强推的并发编程知识点笔记,将是你拿大厂offer的突破口
【AGC】增长服务1-远程配置示例
C语言的高级用法
three.js调试工具dat.gui使用
阿里顶级架构师多年总结的JVM宝典,哪里不会查哪里!
静态链接和动态链接
dotnet OpenXML 解析 PPT 图表 面积图入门