http://cogs.pro:8080/cogs/problem/problem.php?pid=1682
线段树单点修改,单点查询
#include <bits/stdc++.h> using namespace std; const int N = 1e7 + 10; #define gc getchar() #define lson jd << 1 #define rson jd << 1| 1 int W[N << 2]; bool O[1010]; int js; inline int read(){ int x = 0, f = 1; char c = gc; while(c < ‘0‘ || c > ‘9‘) {if(c == ‘-‘) f = -1; c = gc;} while(c >= ‘0‘ && c <= ‘9‘) x = x * 10 + c - ‘0‘, c = gc; return x * f; } void Sec_G(int l, int r, int jd, int x, int y, int g){ if(x <= l && r <= y) {W[jd] = g; return ;} if(W[jd]) {W[lson] = W[jd], W[rson] = W[jd], W[jd] = 0; } int mid = (l + r) >> 1; if(x <= mid) Sec_G(l, mid, lson, x, y, g); if(y > mid) Sec_G(mid + 1, r, rson, x, y, g); } void dfs_tree(int l, int r, int jd){ if(l == r) {O[W[jd]] = 1; return ;} if(W[jd]) {W[lson] = W[jd]; W[rson] = W[jd];} int mid = (l + r) >> 1; dfs_tree(l, mid, lson); dfs_tree(mid + 1, r, rson); } int main() { freopen("ha14d.in", "r", stdin); freopen("ha14d.out", "w", stdout); int n = read(), T = read(); while(T --){ js ++; int x = read(), y = read(); Sec_G(1, n, 1, x, y, js); } dfs_tree(1, n, 1); int answer(0); for(int i = 1; i <= 1001; i ++) if(O[i]) answer ++; cout << answer; return 0; }