当前位置: 代码迷 >> 综合 >> LOJ-#6281. 数列分块入门 5
  详细解决方案

LOJ-#6281. 数列分块入门 5

热度:7   发布时间:2023-11-20 05:57:31.0

题目链接:https://ajax.loj.ac/problem/6281

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 5e4 + 5;
int blo, opt, l, r, c, a[maxn], flag[maxn], pos[maxn], n, sum[maxn];
void sovle_sqrt(int x)
{if(flag[x]) return;flag[x] = 1;sum[x] = 0;for (int i = (x-1)*blo+1; i <= x*blo; i++){a[i] = sqrt(a[i]);sum[x] += a[i];if(a[i]>1) flag[x] = 0;}
}
int add(int l, int r)
{for (int i = l; i <= min(r, pos[l]*blo); i++){sum[pos[l]]-= a[i];a[i] = sqrt(a[i]);sum[pos[l]] += a[i];}if(pos[l] != pos[r]){for (int i = (pos[r]-1)*blo+1; i <= r; i++){sum[pos[r]] -= a[i];a[i] = sqrt(a[i]);sum[pos[r]] += a[i];}}for (int i = pos[l]+1; i <= pos[r]-1; i++){sovle_sqrt(i);}
}
int query(int l, int r)
{int ans = 0;for (int i = l;i <= min(pos[l]*blo, r); i++){ans += a[i];}if(pos[l] != pos[r]){for (int i = (pos[r]-1)*blo+1; i<= r; i++){ans += a[i];}}for (int i = pos[l]+1; i <= pos[r]-1; i++)ans += sum[i];return ans;
}
int main()
{scanf("%d", &n);blo = sqrt(n);for (int i = 1; i <= n; i++) scanf("%d", &a[i]),pos[i] = (i-1)/blo +1, sum[pos[i]] += a[i];for(int i = 1; i <= n; i++){scanf("%d%d%d%d", &opt, &l, &r, &c);if(opt == 0) add(l, r);else printf("%d\n",query(l, r));}
}