#define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 constint maxn = 55555; int sum[maxn << 2]; voidPushUP(int rt){ sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } voidbuild(int l, int r, int rt){ if (l == r) { scanf("%d", &sum[rt]); return ; } int m = (l + r) >> 1; build(lson); build(rson); PushUP(rt); } voidupdate(int p, int add, int l, int r, int rt){ if (l == r) { sum[rt] += add; return ; } int m = (l + r) >> 1; if (p <= m) update(p , add , lson); elseupdate(p , add , rson); PushUP(rt); } intquery(int L, int R, int l, int r, int rt){ if (L <= l && r <= R) { return sum[rt]; } int m = (l + r) >> 1; int ret = 0; if (L <= m) ret += query(L , R , lson); if (R > m) ret += query(L , R , rson); return ret; } intmain(){ int T , n; scanf("%d", &T); for (int cas = 1 ; cas <= T ; cas ++) { printf("Case %d:\n", cas); scanf("%d", &n); build(1 , n , 1); char op[10]; while (scanf("%s", op)) { if (op[0] == 'E') break; int a , b; scanf("%d%d", &a, &b); if (op[0] == 'Q') printf("%d\n", query(a , b , 1 , n , 1)); elseif (op[0] == 'S') update(a , -b , 1 , n , 1); elseupdate(a , b , 1 , n , 1); } } return0; }
#define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 constint maxn = 222222; int MAX[maxn << 2]; voidPushUP(int rt){ MAX[rt] = max(MAX[rt << 1] , MAX[rt << 1 | 1]); } voidbuild(int l, int r, int rt){ if (l == r) { scanf("%d", &MAX[rt]); return ; } int m = (l + r) >> 1; build(lson); build(rson); PushUP(rt); } voidupdate(int p, int sc, int l, int r, int rt){ if (l == r) { MAX[rt] = sc; return ; } int m = (l + r) >> 1; if (p <= m) update(p , sc , lson); elseupdate(p , sc , rson); PushUP(rt); } intquery(int L, int R, int l, int r, int rt){ if (L <= l && r <= R) { return MAX[rt]; } int m = (l + r) >> 1; int ret = 0; if (L <= m) ret = max(ret , query(L , R , lson)); if (R > m) ret = max(ret , query(L , R , rson)); return ret; } intmain(){ int n , m; while (~scanf("%d%d", &n, &m)) { build(1 , n , 1); while (m --) { char op[2]; int a , b; scanf("%s%d%d", op, &a, &b); if (op[0] == 'Q') printf("%d\n", query(a , b , 1 , n , 1)); elseupdate(a , b , 1 , n , 1); } } return0; }
#define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 constint maxn = 5555; int sum[maxn << 2]; voidPushUP(int rt){ sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } voidbuild(int l, int r, int rt){ sum[rt] = 0; if (l == r) return ; int m = (l + r) >> 1; build(lson); build(rson); } voidupdate(int p, int l, int r, int rt){ if (l == r) { sum[rt] ++; return ; } int m = (l + r) >> 1; if (p <= m) update(p , lson); elseupdate(p , rson); PushUP(rt); } intquery(int L, int R, int l, int r, int rt){ if (L <= l && r <= R) { return sum[rt]; } int m = (l + r) >> 1; int ret = 0; if (L <= m) ret += query(L , R , lson); if (R > m) ret += query(L , R , rson); return ret; } int x[maxn]; intmain(){ int n; while (~scanf("%d", &n)) { build(0 , n - 1 , 1); int sum = 0; for (int i = 0 ; i < n ; i ++) { scanf("%d", &x[i]); sum += query(x[i] , n - 1 , 0 , n - 1 , 1); update(x[i] , 0 , n - 1 , 1); } int ret = sum; for (int i = 0 ; i < n ; i ++) { sum += n - x[i] - x[i] - 1; ret = min(ret , sum); } printf("%d\n", ret); } return0; }
#define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 constint maxn = 111111; int h , w , n; int col[maxn << 2]; int sum[maxn << 2]; voidPushUp(int rt){ sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } voidPushDown(int rt, int m){ if (col[rt]) { col[rt << 1] = col[rt << 1 | 1] = col[rt]; sum[rt << 1] = (m - (m >> 1)) * col[rt]; sum[rt << 1 | 1] = (m >> 1) * col[rt]; col[rt] = 0; } } voidbuild(int l, int r, int rt){ col[rt] = 0; sum[rt] = 1; if (l == r) return ; int m = (l + r) >> 1; build(lson); build(rson); PushUp(rt); } voidupdate(int L, int R, int c, int l, int r, int rt){ if (L <= l && r <= R) { col[rt] = c; sum[rt] = c * (r - l + 1); return ; } PushDown(rt , r - l + 1); int m = (l + r) >> 1; if (L <= m) update(L , R , c , lson); if (R > m) update(L , R , c , rson); PushUp(rt); } intmain(){ int T , n , m; scanf("%d", &T); for (int cas = 1 ; cas <= T ; cas ++) { scanf("%d%d", &n, &m); build(1 , n , 1); while (m --) { int a , b , c; scanf("%d%d%d", &a, &b, &c); update(a , b , c , 1 , n , 1); } printf("Case %d: The total value of the hook is %d.\n", cas , sum[1]); } return0; }
#include<cstdio> #include<cstring> #include<algorithm> usingnamespace std; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1
constint maxn = 11111; bool hash[maxn]; int li[maxn] , ri[maxn]; int X[maxn * 3]; int col[maxn << 4]; int cnt;
voidPushDown(int rt){ if (col[rt] != -1) { col[rt << 1] = col[rt << 1 | 1] = col[rt]; col[rt] = -1; } } voidupdate(int L, int R, int c, int l, int r, int rt){ if (L <= l && r <= R) { col[rt] = c; return ; } PushDown(rt); int m = (l + r) >> 1; if (L <= m) update(L , R , c , lson); if (m < R) update(L , R , c , rson); } voidquery(int l, int r, int rt){ if (col[rt] != -1) { if (!hash[col[rt]]) cnt ++; hash[ col[rt] ] = true; return ; } if (l == r) return ; int m = (l + r) >> 1; query(lson); query(rson); } intBin(int key, int n, int X[]){ int l = 0 , r = n - 1; while (l <= r) { int m = (l + r) >> 1; if (X[m] == key) return m; if (X[m] < key) l = m + 1; else r = m - 1; } return-1; } intmain(){ int T , n; scanf("%d", &T); while (T --) { scanf("%d", &n); int nn = 0; for (int i = 0 ; i < n ; i ++) { scanf("%d%d", &li[i] , &ri[i]); X[nn++] = li[i]; X[nn++] = ri[i]; } sort(X , X + nn); int m = 1; for (int i = 1 ; i < nn; i ++) { if (X[i] != X[i - 1]) X[m ++] = X[i]; } for (int i = m - 1 ; i > 0 ; i --) { if (X[i] != X[i - 1] + 1) X[m ++] = X[i] + 1; } sort(X , X + m); memset(col , -1 , sizeof(col)); for (int i = 0 ; i < n ; i ++) { int l = Bin(li[i] , m , X); int r = Bin(ri[i] , m , X); update(l , r , i , 0 , m , 1); } cnt = 0; memset(hash , false , sizeof(hash)); query(0 , m , 1); printf("%d\n", cnt); } return0; }
#include<cstdio> #include<cstring> #include<cctype> #include<algorithm> usingnamespace std; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1
constint maxn = 2222; int cnt[maxn << 2]; double sum[maxn << 2]; double X[maxn]; structSeg { double h , l , r; int s; Seg() {} Seg(double a, double b, double c, int d) : l(a) , r(b) , h(c) , s(d) {} booloperator < (const Seg &cmp) const { return h < cmp.h; } } ss[maxn]; voidPushUp(int rt, int l, int r){ if (cnt[rt]) sum[rt] = X[r + 1] - X[l]; elseif (l == r) sum[rt] = 0; else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } voidupdate(int L, int R, int c, int l, int r, int rt){ if (L <= l && r <= R) { cnt[rt] += c; PushUp(rt , l , r); return ; } int m = (l + r) >> 1; if (L <= m) update(L , R , c , lson); if (m < R) update(L , R , c , rson); PushUp(rt , l , r); } intBin(double key, int n, double X[]){ int l = 0 , r = n - 1; while (l <= r) { int m = (l + r) >> 1; if (X[m] == key) return m; if (X[m] < key) l = m + 1; else r = m - 1; } return-1; } intmain(){ int n , cas = 1; while (~scanf("%d", &n) && n) { int m = 0; while (n --) { double a , b , c , d; scanf("%lf%lf%lf%lf", &a, &b, &c, &d); X[m] = a; ss[m++] = Seg(a , c , b , 1); X[m] = c; ss[m++] = Seg(a , c , d , -1); } sort(X , X + m); sort(ss , ss + m); int k = 1; for (int i = 1 ; i < m ; i ++) { if (X[i] != X[i - 1]) X[k++] = X[i]; } memset(cnt , 0 , sizeof(cnt)); memset(sum , 0 , sizeof(sum)); double ret = 0; for (int i = 0 ; i < m - 1 ; i ++) { int l = Bin(ss[i].l , k , X); int r = Bin(ss[i].r , k , X) - 1; if (l <= r) update(l , r , ss[i].s , 0 , k - 1, 1); ret += sum[1] * (ss[i + 1].h - ss[i].h); } printf("Test case #%d\nTotal explored area: %.2lf\n\n", cas++ , ret); } return0; }