感觉自己写的有点怪,欢迎来hack我
2024-12-16 00:00:18
发布于:上海
19阅读
0回复
0点赞
时间复杂度感觉挺悬的,感觉有点问题
#include <bits/stdc++.h>
#define x first
#define y second
#define lowbit(x) x & -x
using namespace std;
using LL = long long;
using PII = pair<LL, LL>;
const int mod = 1e9 + 7;
const int N = 2e5 + 7;
int n, q, a[N];
int tr[N];
void add(int x, int c)
{
for (int i = x; i <= n; i += lowbit(i))
tr[i] += c;
}
int sum(int x)
{
int res = 0;
for (int i = x; i > 0; i -= lowbit(i))
res += tr[i];
return res;
}
void sol()
{
cin >> n >> q;
set<int> st;
for (int i = 1; i <= n; i++)
{
int x = 0, v;
cin >> v;
while (v > 1)
{
if (v & 1)
v = v * 3 + 1;
else
v >>= 1;
x++;
}
a[i] = x;
if (x)
st.insert(i);
else
add(i, 1);
}
while (q--)
{
int op, l, r;
cin >> op >> l >> r;
if (op == 1)
{
vector<int> v;
for (auto it = st.lower_bound(l); it != st.end() && *it <= r; it++)
{
int p = *it;
a[p]--;
if (!a[p])
{
v.push_back(p);
add(p, 1);
}
}
for (int x : v)
st.erase(x);
}
else
cout << sum(r) - sum(l - 1) << "\n";
}
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T--)
sol();
return 0;
}
全部评论 3
666怎么比官方题解还快
6天前 来自 广东
0牛魔
6天前 来自 广东
0666
6天前 来自 广东
0
有帮助,赞一个