3211: 花神游历各国
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 4065 Solved: 1485
[Submit][Status][Discuss]
Description
Input
Output
每次x=1时,每行一个整数,表示这次旅行的开心度
Sample Input
4
1 100 5 5
5
1 1 2
2 1 2
1 1 2
2 2 3
1 1 4
1 100 5 5
5
1 1 2
2 1 2
1 1 2
2 2 3
1 1 4
Sample Output
101
11
11
11
11
HINT
对于100%的数据, n ≤ 100000,m≤200000 ,data[i]非负且小于10^9
Source
观察到每一个数开方几次以后就成了1,所以我们可以每次暴力单点修改
用一个并查集维护不是1的序列就可以做完了
PS.蒟蒻一开始用vector结果常数太大GG
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int INF = 2147483647;
const int maxn = 100100;
vector<int> q;
int n,m,fa[maxn];
LL sum[maxn],a[maxn];
inline LL getint()
{
LL ret = 0,f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
ret = ret * 10 + c - '0',c = getchar();
return ret * f;
}
inline int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main()
{
n = getint();
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= n; i++)
{
a[i] = getint();
if (a[i] <= 1 && a[i - 1] <= 1)
fa[find(i - 1)] = find(i);
}
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j += j & -j)
sum[j] += a[i];
m = getint();
a[n + 1] = a[0] = 20020130;
for (int i = 1; i <= m; i++)
{
int d = getint(),l = getint(),r = getint();
if (d == 1)
{
LL ans = 0;
for (int j = r; j; j -= j & -j) ans += sum[j];
for (int j = l - 1; j; j -= j & -j) ans -= sum[j];
printf("%lld\n",ans);
}
else
{
for (int j = l; j <= r;)
{
for (int k = j; k <= n; k += k & -k)
sum[k] -= a[j];
a[j] = sqrt(a[j]);
for (int k = j; k <= n; k += k & -k)
sum[k] += a[j];
if (a[j] <= 1)
{
if (a[j - 1] <= 1)
fa[find(j - 1)] = find(j);
if (a[j + 1] <= 1)
fa[find(j)] = find(j + 1);
}
j = find(j) + 1;
}
}
}
return 0;
}