P2184 貪婪大陸c++
線段樹,差分?
在所修改的區間的開頭位置+1,表示從這個位置開始日後開始埋一種地雷,在結尾位置+1,表示在這個位置有一種地雷被埋完
查詢的時候咱們就只須要查詢git
因此查詢時輸出前者減後者的差就能夠了。
線段樹維護單點修改+區間查詢ui
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n, m; int d[N]; class tree { public: int len, sta, end; } t[N]; #define lson rt << 1 #define rson rt << 1 | 1 template<class T>inline void read(T &x) { x = 0; int f = 0; char ch = getchar(); while (!isdigit(ch)) f |= (ch == '-'), ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); x = f ? -x : x; return; } void build(int l, int r, int rt) { t[rt].len = r - l + 1; if (l == r) return; int m = (l + r) >> 1; build(l, m, lson), build(m + 1, r, rson); } void update_sta(int L, int c, int l = 1, int r = n, int rt = 1) { if (l == r) { t[rt].sta += c; return ; } int m = (l + r) >> 1; if (L <= m) update_sta(L, c, l, m, lson); else update_sta(L, c, m + 1, r, rson); t[rt].sta = t[lson].sta + t[rson].sta; } void update_end(int L, int c, int l = 1, int r = n, int rt = 1) { if (l == r) { t[rt].end += c; return ; } int m = (l + r) >> 1; if (L <= m) update_end(L, c, l, m, lson); else update_end(L, c, m + 1, r, rson); t[rt].end = t[lson].end + t[rson].end; } int query_sta(int L, int R, int l = 1, int r = n, int rt = 1) { if (L <= l && r <= R) return t[rt].sta; int m = (l + r) >> 1, ans = 0; if (L <= m) ans += query_sta(L, R, l, m, lson); if (R > m) ans += query_sta(L, R, m + 1, r, rson); return ans; } int query_end(int L, int R, int l = 1, int r = n, int rt = 1) { if (L <= l && r <= R) return t[rt].end; int m = (l + r) >> 1, ans = 0; if (L <= m) ans += query_end(L, R, l, m, lson); if (R > m) ans += query_end(L, R, m + 1, r, rson); return ans; } int main() { read(n), read(m); build(1, n, 1); for (int i = 1, x, y, opt; i <= m; ++i) { read(opt) , read(x), read(y); if (opt == 1) update_sta(x, 1), update_end(y, 1); else printf("%d\n", query_sta(1, y) - query_end(1, x - 1)); } }