GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国

GSS4 - Can you answer these queries IV

题目链接:https://www.luogu.org/problemnew/show/SP2713

线段树经典题目,然而被我用分块A了.

对于区间开根号,\(1e18\)最多会被开\(6\)次就会成为\(1\),成为\(1\)后,再开根号也是\(1\),0开根号也是0,线段树(分块)维护区间所有的数是否全部小于等于1,如果不是,就暴力更新,如果是,那就不要更新这个区间.

时间复杂度\(O(\sqrt n * n)\)

分块CODE:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define ll long long
const int maxN = 100000 + 7;
ll a[maxN];
bool is_sqrt[maxN];
int L[maxN],R[maxN],belong[maxN];
ll sum[maxN];
int num[maxN]; inline ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
} ll Query(int l,int r) {
int b_l = belong[l],b_r = belong[r];
ll ans = 0;
if(b_l == b_r) {
for(int i = l;i <= r;++ i)
ans += a[i];
return ans;
}
for(int i = b_l + 1;i < b_r;++ i) ans += sum[i];
for(int i = l;i <= R[b_l];++ i) ans += a[i];
for(int i = L[b_r];i <= r;++ i) ans += a[i];
return ans;
} void Inter_sqrt(int l,int r) {
int b_l = belong[l],b_r = belong[r];
if(b_l == b_r) {
if(is_sqrt[b_l]) return;
for(int i = l;i <= r;++ i) {
if(a[i] == 1) continue;
sum[b_l] -=a[i];
a[i] = sqrt(a[i]);
sum[b_l] += a[i];
if(a[i] == 1) num[b_l] ++;
}
if(num[b_l] == R[b_l] - L[b_l] + 1) is_sqrt[b_l] = true;
return;
}
for(int i = b_l + 1;i < b_r;++ i) {
if(is_sqrt[i]) continue;
for(int j = L[i];j <= R[i];++ j) {
if(a[j] == 1) continue;
sum[i] -=a[j];
a[j] = sqrt(a[j]);
sum[i] += a[j];
if(a[j] == 1) num[i] ++;
}
if(num[i] == R[i] - L[i] + 1) is_sqrt[i] = true;
}
for(int i = l;i <= R[b_l];++ i) {
if(a[i] == 1) continue;
sum[b_l] -=a[i];
a[i] = sqrt(a[i]);
sum[b_l] += a[i];
if(a[i] == 1) num[b_l] ++;
}
if(num[b_l] == R[b_l] - L[b_l] + 1) is_sqrt[b_l] = true; for(int i = L[b_r];i <= r;++ i) {
if(a[i] == 1) continue;
sum[b_r] -=a[i];
a[i] = sqrt(a[i]);
sum[b_r] += a[i];
if(a[i] == 1) num[b_r] ++;
}
if(num[b_r] == R[b_r] - L[b_r] + 1) is_sqrt[b_r] = true;
} int main() {
int tot = 0;
int n;
while(scanf("%d",&n) == 1) {
printf("Case #%d:\n",++ tot);
memset(num,0,sizeof(num));
memset(L,0,sizeof(L));
memset(R,0,sizeof(R));
memset(sum,0,sizeof(sum));
memset(is_sqrt,0,sizeof(is_sqrt));
int q = sqrt(n);
for(int i = 1;i <= n;++ i)
a[i] = read();
for(int i = 1;i <= n;++ i){
belong[i] = i / q + 1;
sum[belong[i]] += a[i];
if(a[i] == 1) num[belong[i]] ++;
}
for(int i = 1;i <= n;++ i) R[belong[i]] = i;
for(int i = n;i >= 1;-- i) L[belong[i]] = i;
int m = read();
int opt,l,r;
while(m --) {
opt = read();l = read();r = read();
if(l > r) std::swap(l,r);
if(opt) printf("%lld\n",Query(l,r));
else Inter_sqrt(l,r);
}
}
return 0;
}

luogu4145上帝造题的七分钟2 / 花神游历各国

用分块很难水过,我们用线段树维护区间最大值即可.

线段树CODE:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#define max(a,b) a > b ? a : b
#define ll long long
const ll maxN = 100000 + 7;
using namespace std; struct Node{
ll l,r;
ll sum;
ll maxx;
}tree[maxN << 2];
ll a[maxN]; void swap(ll &a,ll &b) {
ll k = b;
b = a;
a = k;
} inline ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
} void updata(ll now) {
tree[now].sum = tree[now << 1].sum + tree[now << 1 | 1].sum;
tree[now].maxx = max(tree[now << 1 | 1].maxx,tree[now << 1].maxx);
} void build(ll l,ll r,ll now) {
tree[now].l = l;
tree[now].r = r;
if(l == r) {
tree[now].sum = a[l];
tree[now].maxx = a[l];
return ;
}
ll mid = (l + r) >> 1;
build(l,mid,now << 1);
build(mid + 1,r,now << 1 | 1);
updata(now);
} ll Query(ll l,ll r,ll now) {
if(tree[now].l >= l && tree[now].r <= r) return tree[now].sum;
ll mid = (tree[now].l + tree[now].r) >> 1;
ll sum = 0;
if(l <= mid) sum += Query(l,r,now << 1);
if(r > mid) sum += Query(l,r,now << 1 | 1);
return sum;
} void work(ll now) {
if(tree[now].l == tree[now].r) {
ll L = tree[now].l;
a[L] = sqrt(a[L]);
tree[now].sum = a[L];
tree[now].maxx = a[L];
return;
}
if(tree[now << 1].maxx > 1) work(now << 1);
if(tree[now << 1 | 1].maxx > 1) work(now << 1 | 1);
updata(now);
return ;
} void Inter_sqrt(ll l,ll r,ll now) {
if(tree[now].l >= l && tree[now].r <= r) {
if( tree[now].maxx > 1 ) work(now);
return ;
}
ll mid = (tree[now].l + tree[now].r) >> 1;
if(l <= mid) Inter_sqrt(l,r,now << 1);
if(r > mid) Inter_sqrt(l,r,now << 1 | 1);
updata(now);
return;
} int main() {
ll n = read();
for(ll i = 1;i <= n;++ i)
a[i] = read();
build(1,n,1);
ll m = read();
ll opt,l,r;
while(m --) {
opt = read();l = read();r = read();
if(l > r)swap(l,r);
if(opt == 1) printf("%lld\n",Query(l,r,1));
else Inter_sqrt(l,r,1);
}
return 0;
} /*
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
*/

GSS4 - Can you answer these queries IV || luogu4145上帝造题的七分钟2 / 花神游历各国 (线段树)的更多相关文章

  1. 线段树 SP2713 GSS4 - Can you answer these queries IV暨 【洛谷P4145】 上帝造题的七分钟2 / 花神游历各国

    SP2713 GSS4 - Can you answer these queries IV 「题意」: n 个数,每个数在\(10^{18}\) 范围内. 现在有「两种」操作 0 x y把区间\([x ...

  2. 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)

    题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...

  3. 上帝造题的七分钟2/花神游历各国/GSS4 线段树维护区间开方 By cellur925

    题目传送门 或者 另一个传送门 询问区间和都好说.但是开方?? 其实是这样的,一个数(1e9)以内连续开方6次就会变成1,于是我们就可在开方操作上进行暴力修改.暴力修改的意思其实也就是找到叶子节点进行 ...

  4. 【luogu4145】上帝造题的七分钟2 / 花神游历各国--区间开根-线段树

    题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...

  5. luogu4145 上帝造题的七分钟2 (线段树)

    题意:给一个数列,维护两个操作,区间开根号.询问区间和 注意到1e12开根号六次后就变成1,而且根号1等于1 也就是说,就算我们用单点修改,只要跳过1,那么修改的次数最多也就是6n 那么维护一个区间最 ...

  6. 题解【luogu4145 上帝造题的七分钟2(花神游历各国)】

    题目大意: 一个序列,支持区间开方与求和操作. 算法:线段树实现开方修改与区间求和 分析: 显然,这道题的求和操作可以用线段树来维护 但是如何来实现区间开方呢 大家有没有这样的经历:玩计算器的时候,把 ...

  7. GSS4 - Can you answer these queries IV(线段树懒操作)

    GSS4 - Can you answer these queries IV(线段树懒操作) 标签: 线段树 题目链接 Description recursion有一个正整数序列a[n].现在recu ...

  8. SP2713 GSS4 - Can you answer these queries IV(线段树)

    传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...

  9. SPOJ GSS4 Can you answer these queries IV

    Time Limit: 500MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

随机推荐

  1. 远程kafka通信实例,各种bug解决----虚拟机+本地电脑

    为了实现远程kafka通信,我可谓是呕心沥血.期间各种bug各种调,太煎熬了 (T.T) 介绍: 我用一台虚拟机作为远程消息的发送方,用本地电脑主机作为消息的接收方 虚拟机:安装java,kafka, ...

  2. Java程序员都应该去使用一下这款强大的国产工具类库

    这不是标题党,今天给大家推荐一个很棒的国产工具类库:Hutool.可能有很多朋友已经知道这个类库了,甚至在已经在使用了,如果你还没有使用过,那不妨去尝试一下,我们项目组目前也在用这个.这篇文章来简单介 ...

  3. day02 多态

  4. B. Mancala (Codeforces Round #478 (Div. 2))

    #include <bits/stdc++.h> using namespace std; ; typedef long long ll; ll a[maxn]; ll b[maxn]; ...

  5. js实现打印正三角

    代码: <html> <head> <title>function</title> </head> <body> <scr ...

  6. library not found for -lXXXXX 编译问题的解决方法

    喜欢交朋友的加:微信号 dwjluck2013 编译和真机调试的时候都正常 在打包的时候遇到这个问题 解决方案:pod install 重新下载就解决了  分享给遇到同类问题的小伙伴

  7. 修正 FreeBSD 字体锯齿问题

    如果你给 FreeBSD 安装完图形界面,一登录就被满屏幕不论中英全是锯齿且残缺不堪入目的文字吓了一跳,那一定是安装了文泉驿字体.先不必急着卸载文泉驿,只需简单修改相关配置即可恢复正常显示.这是因为文 ...

  8. linux系统任务调度命令crontab

    循环重复的执行计划任务.有计划性的执行任务,像这种任务,在linux系统中就有cron命令来完成. linux系统下的任务调度分为两类:系统任务调度和用户任务调度. /etc/crontab文件就是系 ...

  9. P1042 乒乓球

    题目背景 国际乒联现在主席沙拉拉自从上任以来就立志于推行一系列改革,以推动乒乓球运动在全球的普及.其中11分制改革引起了很大的争议,有一部分球员因为无法适应新规则只能选择退役.华华就是其中一位,他退役 ...

  10. GetDC(),ReleaseDC()

    用GetDC()得到的DC, 必须调用ReleaseDC()用CreateDC()创建的DC, 必须调用DeleteDC() 两者是不能混淆的.一种典型的错误代码如下:CDC* pDC = GetDC ...