洛谷P5057简单题
P5057 [CQOI2006] 简单题
思路
每次操作,直接区间加\(1\),最后求结果的时候对\(2\)取余就好了
这个题就是区间修改 + 单点查询
可以用树状数组或者线段数维护
代码
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define lowbit(x) x & -x
const int maxn = 5e5 + 5;
const int inf = 0x7f7f7f7f;
struct custom_hash
{
static uint64_t splitmix64(uint64_t x)
{
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
return x;
}
size_t operator () (uint64_t x) const
{
static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count(); // 时间戳
return splitmix64(x + FIXED_RANDOM);
}
};
int n = 0, m = 0;
int fenwick1[maxn], fenwick2[maxn];
void modify(int pos, int x)
{
int v = pos * x;
while(pos <= n)
{
fenwick1[pos] += x;
fenwick2[pos] += v;
pos += lowbit(pos);
}
}
int query(int pos, int t[])
{
int res = 0;
while(pos)
{
res += t[pos];
pos -= lowbit(pos);
}
return res;
}
void range_modify(int l, int r, int v)
{
modify(l, v);
modify(r + 1, -v);
}
int range_query(int l, int r)
{
int pre = (r + 1) * query(r, fenwick1) - l * query(l - 1, fenwick1);
int pos = query(r, fenwick2) - query(l - 1, fenwick2);
return pre - pos;
}
int single_query(int pos)
{
return query(pos, fenwick1);
}
void solve()
{
std::cin >> n >> m;
int op = 0, L = 0, R = 0;
for (int i = 1; i <= m; i++)
{
std::cin >> op;
if (op == 1)
{
std::cin >> L >> R;
range_modify(L, R, 1);
}
else
{
std::cin >> L;
int ans = single_query(L);
std::cout << ans % 2 << endl;
}
}
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr); std::cout.tie(nullptr);
//freopen("out.txt", "w", stdout);
int t = 1;
//std::cin >> t;
while(t--)
{
solve();
}
return 0;
}