HDU 4027 Can you answer these queries(线段树 + 观察 )
2024-09-28 10:58:02
这题主要考察观察能力。
2^63最多只需要开7次根号就会变成1,当数字变成1之后就不需要再对其进行操作。
对于含有大于1数字的区间,向下更新。
对于数字全为1的区间,直接返回。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath> #define LL long long int
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define lc rt << 1
#define rc rt << 1 | 1 using namespace std; const int MAXN = ; LL sum[MAXN << ];
bool one[MAXN << ]; void PushUp( int rt )
{
sum[rt] = sum[lc] + sum[rc];
one[rt] = one[lc] && one[rc];
return;
} void build( int l, int r, int rt )
{
if ( l == r )
{
scanf( "%I64d", &sum[rt] );
if ( sum[rt] <= ) one[rt] = true;
else one[rt] = false;
return;
}
int m = ( l + r ) >> ;
build(lson);
build(rson);
PushUp( rt );
return;
} void Update( int L, int R, int l, int r, int rt )
{
if ( one[rt] ) return;
if ( l == r )
{
sum[rt] = (LL)sqrt( (double)sum[rt] );
//之前这里忘了更新one,一直TLE
if ( sum[rt] <= ) one[rt] = true;
else one[rt] = false;
return;
}
int m = ( l + r ) >> ;
if ( L <= m ) Update( L, R, lson );
if ( R > m ) Update( L, R, rson );
PushUp( rt );
return;
} LL Query( int L, int R, int l, int r, int rt )
{
//这句不要,之前想错了
//if ( one[rt] ) return (LL)r - l + 1;
if ( L <= l && r <= R ) return sum[rt]; int m = ( l + r ) >> ;
LL res = ;
if ( L <= m ) res += Query( L, R, lson );
if ( R > m ) res += Query( L, R, rson );
return res;
} int N; int main()
{
int cas = ;
while ( scanf( "%d", &N ) == )
{
build( , N, );
int Q;
scanf( "%d", &Q );
printf( "Case #%d:\n", ++cas );
while ( Q-- )
{
int op, a, b;
scanf( "%d%d%d", &op, &a, &b );
if ( b < a ) swap(a, b);
if ( op )
{
printf("%I64d\n", Query( a, b, , N, ) );
}
else
{
Update( a, b, , N, );
}
}
puts("");
}
return ;
}
最新文章
- 《Javascript高级程序设计》:创建对象
- css before&;after 特殊用途
- 阻塞队列LinkedBlockingQueue和并发队列ConcurrentLinkedQueue
- Windows 托盘区域显示图标
- java的getClass()函数
- python安装psycopg2
- IOS Remote Notification
- event和window.event
- SmartCoder每日站立会议04
- Akka(37): Http:客户端操作模式
- NLog日志管理工具(转)
- 找不到BufferedImage这个Class的解决方法
- Could not find property &#39;outputFile
- Android 资源文件命名与使用
- vue省市区三级联动
- python数据结构与算法第十五天【二叉树】
- Vijos P1459 车展 (treap 任意区间中位数)
- istringstream、ostringstream、stringstream 类介绍 和 stringstream类 clear函数的真正用途
- 远程桌面连接MySQL遇到的问题及解决方法总结
- python3获取主机名、主机IP