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;
}