线段树完全版【代码集合
可能有些题要重写,先放这么多
单点更新
1 #include <stdio.h> 2 #define maxn 200000 3 #include <algorithm> 4 using namespace std; 5 int qr, ql, v, x, l, n, ans; 6 int tree[maxn]; 7 void build(int o, int l,int r) { 8 if (l == r) { 9 scanf("%d",&tree[o]); 10 return ; 11 } 12 int mid = (l + r) >> 1; 13 build(o*2, l, mid); 14 build(o*2+1, mid+1, r); 15 tree[o] = tree[o*2] + tree[o*2+1]; 16 } 17 void update(int o, int l, int r) 18 { 19 if (l == r) 20 {tree[o] += v;return;} 21 int mid = (l + r) >> 1; 22 if(x > mid)update(o*2+1, mid+1, r); 23 else update(o*2, l, mid); 24 tree[o] = tree[o*2] + tree[o*2+1]; 25 } 26 void query(int o, int l, int r) 27 { 28 if(l >= ql && qr >= r) 29 { 30 ans += tree[o];return ; 31 } 32 int mid = (l + r) >> 1; 33 if(mid >= ql)query(o*2, l, mid); 34 if(mid < qr)query(o*2+1, mid+1, r); 35 } 36 int main() 37 { 38 int T; 39 scanf("%d", &T); 40 for (int cas = 1 ; cas <= T ; cas ++) 41 { 42 printf("Case %d:\n",cas); 43 scanf("%d",&n); 44 build(1, 1, n); 45 char s[10]; 46 while(scanf("%s",s)) 47 { 48 if(s[0] == 'E')break; 49 if(s[0] == 'A') 50 { 51 scanf("%d %d", &x, &v); 52 update(1, 1, n); 53 } 54 if(s[0] == 'S') 55 { 56 scanf("%d %d", &x, &v); 57 v = -v;update(1, 1, n); 58 } 59 if(s[0] == 'Q') 60 { 61 scanf("%d %d", &ql, &qr); 62 ans = 0; 63 query(1, 1, n); 64 printf("%d\n", ans); 65 } 66 } 67 } 68 }
1 #include <stdio.h> 2 #include <algorithm> 3 #define maxn 222222 4 using namespace std; 5 int n, m, tree[maxn<<2]; 6 void build(int o, int l ,int r) 7 { 8 if(l == r){ 9 scanf("%d", &tree[o]); 10 return ; 11 } 12 int mid = (l + r) >> 1; 13 build(o<<1, l, mid); 14 build(o<<1|1, mid+1, r); 15 tree[o] = max(tree[o<<1], tree[o<<1|1]); 16 } 17 void update(int x, int v, int o, int l, int r) 18 { 19 if(l == r) 20 { 21 tree[o] = v; 22 return; 23 } 24 int mid = (l + r) >> 1; 25 if(x <= mid)update(x, v, o * 2, l, mid); 26 else update(x, v, o*2+1, mid+1, r); 27 tree[o] = max(tree[o<<1], tree[o<<1|1]); 28 } 29 int query(int ql, int qr, int o, int l, int r) 30 { 31 if (ql <= l&& r <= qr) 32 { 33 return tree[o]; 34 } 35 int ans = 0; 36 int mid = (l + r) >> 1; 37 if(ql <= mid)ans = max(ans, query(ql, qr, o<<1, l, mid)); 38 if(qr > mid)ans = max(ans, query(ql, qr, o<<1|1, mid+1, r));//ans = max(ans, query(ql, qr, o*2, l, mid)); 39 return ans; 40 } 41 int main() 42 { 43 while(~scanf("%d %d", &n, &m)) 44 { 45 build(1, 1, n); 46 while(m--) 47 { 48 char s[20]; 49 int a, b; 50 //ans = 0; 51 scanf("%s %d %d", s, &a, &b); 52 if(s[0] == 'Q') 53 printf("%d\n", query(a, b, 1, 1, n)); 54 else update(a, b, 1, 1, n); 55 } 56 } 57 return 0; 58 }
3.hdu1394 Minimum Inversion Number
1 #include <stdio.h> 2 #define maxn 6000 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 int an, x[maxn<<2], sum, n, tree[maxn << 2]; 7 void update(int X, int o, int l, int r) 8 { 9 if(l == r) 10 { 11 tree[o]++; 12 return; 13 } 14 int mid = (l + r) >> 1; 15 if (X > mid)update(X, o<<1|1, mid+1, r); 16 else update(X, o<<1, l, mid); 17 tree[o] = tree[o<<1] + tree[o<<1|1]; 18 } 19 int query(int ql, int qr, int o, int l, int r) 20 { 21 if(ql <= l&&r <= qr) 22 { 23 return tree[o]; 24 } 25 int mid = (l + r) >> 1; 26 int ans = 0; 27 if(ql <= mid)ans += query(ql, qr, o<<1, l, mid); 28 if(qr > mid)ans += query(ql, qr, o<<1|1, mid+1, r); 29 return ans; 30 } 31 int main() 32 { 33 while(~scanf("%d", &n)) 34 { 35 an = 2000000000; sum = 0; 36 memset(tree, 0, sizeof(tree)); 37 for (int i = 1; i <= n; i++) 38 { 39 scanf("%d", &x[i]); 40 x[i]++; 41 sum += query(x[i]+1, n, 1, 1, n); 42 update(x[i], 1, 1, n); 43 } 44 for (int i = n; i >= 1; i--) 45 { 46 sum = sum + (x[i]-1) - (n-x[i]); 47 // da n - (x[i]) 48 // xiao x[i]-1 49 // sum = sum + x[i] - 1 - n + x[i] ; 50 an = min (an, sum); 51 } 52 printf("%d\n", an); 53 } 54 }
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #define maxn 200010 5 using namespace std; 6 int tree[maxn<<2], h, w, n, sum; 7 void build(int o, int l, int r) 8 { 9 tree[o] = w; 10 if(l == r)return; 11 int mid = (l + r) >> 1; 12 build(o<<1, l, mid); 13 build(o<<1|1, mid+1, r); 14 } 15 int query(int a, int o, int l, int r) 16 { 17 if (l == r) 18 { 19 tree[o] -= a; 20 return l; 21 } 22 int mid = (l + r) >> 1; 23 if (a <= tree[o<<1])sum = query(a, o<<1, l, mid); 24 else sum = query(a, o<<1|1, mid+1, r); 25 tree[o] = max(tree[o<<1], tree[o<<1|1]); 26 return sum; 27 } 28 int main() 29 { 30 while (~scanf("%d %d %d", &h, &w, &n)) 31 { 32 if (h > n)h = n; 33 build(1, 1, h); 34 while (n--) 35 { 36 int a; 37 scanf("%d", &a); 38 if(tree[1] < a)printf("-1\n"); 39 else printf("%d\n", query(a, 1, 1, h)); 40 } 41 } 42 }
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #define maxn 200010 5 using namespace std; 6 int tree[maxn<<2], h, w, n, sum; 7 int p[maxn], v[maxn], ans[maxn]; 8 void build(int o, int l, int r) 9 { 10 if(l == r){tree[o] = 1;return;} 11 int mid = (l + r) >> 1; 12 build(o<<1, l, mid); 13 build(o<<1|1, mid+1, r); 14 tree[o] = tree[o<<1] + tree[o<<1|1]; 15 } 16 void query(int x, int v, int o, int l, int r) 17 { 18 if (l == r) 19 { 20 ans[l] = v; 21 tree[o] = 0; 22 return; 23 } 24 int mid = (l + r) >> 1; 25 if (x <= tree[o<<1])query(x, v, o<<1, l, mid); 26 else query(x - tree[o<<1], v, o<<1|1, mid+1, r); 27 tree[o] = tree[o<<1] + tree[o<<1|1]; 28 } 29 int main() 30 { 31 while (scanf("%d", &n)!=EOF) 32 { 33 build(1, 1, n); 34 for(int i = 1; i <= n; i++) 35 scanf("%d %d", &p[i], &v[i]); 36 for(int i = n; i > 0; i--) 37 query(p[i]+1, v[i], 1, 1, n); 38 for(int i = 1; i <= n; i++) 39 printf("%d ", ans[i]); 40 printf("\n"); 41 } 42 return 0; 43 }
6.poj2886 Who Gets the Most Candies?
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 500010 4 #include <algorithm> 5 using namespace std; 6 int tree[maxn<<2], maxx, ans[maxn]; 7 int pos, mod, id, x[maxn], n, k; 8 char name[maxn][20]; 9 void build(int o, int l, int r) 10 { 11 if (l == r) 12 { 13 tree[o] = 1; 14 return ; 15 } 16 int mid = (l + r) >> 1; 17 build(o<<1, l, mid); 18 build(o<<1|1, mid+1, r); 19 tree[o] = tree[o<<1] + tree[o<<1|1]; 20 } 21 int update(int k, int o, int l, int r) 22 { 23 tree[o]--; 24 if(l == r){return l;} 25 int mid = (l + r) >> 1; 26 if (k <= tree[o<<1]) 27 return update(k, o<<1, l, mid); 28 else return update(k - tree[o<<1], o<<1|1, mid+1, r); 29 } 30 /*int update(int p, int o, int l,int r) 31 { 32 tree[o]--; 33 if(l==r) 34 return r; 35 int m=(l+r)>>1; 36 if(p<=tree[o<<1]) 37 return update(k,o<<1,l,m); 38 return update(k-tree[o<<1],o<<1|1, m+1,r); 39 }*/ 40 void getans() 41 { 42 memset(ans, 0, sizeof(ans)); 43 for (int i = 1; i <= n; i++) 44 { 45 ans[i]++; 46 for (int j = 2 * i; j <= n; j+=i) 47 ans[j]++; 48 } 49 int maxx = ans[1]; 50 id = 1; 51 for (int i = 2; i <= n; i++) 52 { 53 if(ans[i] > maxx) 54 { 55 maxx = ans[i]; 56 id = i; 57 } 58 } 59 } 60 int main() 61 { 62 while(~scanf("%d %d", &n, &k)) 63 { 64 getans(); 65 build(1, 1, n); 66 int N = n; 67 for (int i = 1; i <= n; i++) 68 { 69 scanf("%s %d", name[i], &x[i]); 70 } 71 pos = 0, n = id, x[0] = 0, mod = tree[1]; 72 while(n--) 73 { 74 if(x[pos] > 0) 75 k = ((k - 1 + x[pos] - 1)%mod + mod)%mod + 1; 76 else 77 k = ((k + x[pos] - 1)%mod + mod)%mod + 1; 78 pos = update(k, 1, 1, N); 79 mod = tree[1]; 80 } 81 printf("%s %d\n", name[pos], ans[id]); 82 } 83 }
成段更新
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 using namespace std; 5 6 #define lson l,m,rt<<1 7 #define rson m+1,r,rt<<1|1 8 const int maxn=100002; 9 int col[maxn<<2];//用来标记每个节点,为0则表示没有标记,否则为标记,以颜色的价值作为标记。 10 int sum[maxn<<2];//求和 11 12 void PushUp(int rt)//向上更新和 13 { 14 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; 15 } 16 17 void PushDown(int rt,int m)//对某一个区间进行改变,如果被标记了,在查询的时候就得把改变传给子节点,因为查询的并不一定是当前区间 18 {//m为区间长度 19 if(col[rt])//已经标记过,该区间被改变过 20 { 21 col[rt<<1]=col[rt<<1|1]=col[rt];//标记子节点 22 sum[rt<<1]=(m-(m>>1))*col[rt];//更新左儿子的和 23 sum[rt<<1|1]=(m>>1)*col[rt];//更新右儿子的和 24 col[rt]=0; 25 } 26 } 27 28 void build(int l,int r,int rt) 29 { 30 col[rt]=0;//初始化为所有节点均未标记 31 sum[rt]=1;//初始值为1 32 if(l==r) 33 return; 34 int m=(l+r)>>1; 35 build(lson); 36 build(rson); 37 } 38 39 void update(int L,int R,int c,int l,int r,int rt) 40 { 41 if(L<=l&&r<=R) 42 { 43 col[rt]=c; 44 sum[rt]=c*(r-l+1);//更新代表某个区间的节点和,该节点不一定是叶子节点 45 return ; 46 } 47 PushDown(rt,r-l+1);//向下传递 48 int m=(l+r)>>1; 49 if(L<=m) 50 update(L,R,c,lson);//更新左儿子 51 if(R>m) 52 update(L,R,c,rson);//更新右儿子 53 PushUp(rt);//向上传递更新和 54 } 55 int main() 56 { 57 int k;cin>>k; 58 int n,c,add,a,b; 59 for(int i=1;i<=k;i++) 60 { 61 scanf("%d",&n); 62 build(1,n,1); 63 scanf("%d",&c); 64 for(int j=1;j<=c;j++) 65 { 66 scanf("%d%d%d",&a,&b,&add); 67 update(a,b,add,1,n,1); 68 } 69 printf("Case %d: The total value of the hook is %d.\n",i,sum[1]); 70 } 71 return 0; 72 }
2.poj3468 A Simple Problem with Integers
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 100010 4 #include <algorithm> 5 using namespace std; 6 long long lazy[maxn<<2], tree[maxn<<2], N, Q; 7 void build (long long o, long long l,long long r) 8 { 9 lazy[o] = 0; 10 if(l == r){scanf("%I64d", &tree[o]);return;} 11 int mid = (l+r)>>1; 12 build(o<<1, l, mid); 13 build(o<<1|1, mid+1, r); 14 tree[o] = tree[o<<1] + tree[o<<1|1]; 15 } 16 void pushdown(long long o, long long x) 17 { 18 if (lazy[o]) 19 { 20 lazy[o<<1] += lazy[o]; 21 lazy[o<<1|1] += lazy[o]; 22 tree[o<<1] += (x-(x>>1))*lazy[o]; 23 tree[o<<1|1] += (x>>1)*lazy[o]; 24 lazy[o] = 0; 25 } 26 } 27 void update(long long ql, long long qr, long long v, long long o, long long l, long long r) 28 { 29 if(ql <= l&&r <= qr) 30 { 31 lazy[o] += v; 32 tree[o] += (long long )v * (r - l + 1); 33 return; 34 } 35 pushdown(o, r - l + 1); 36 int mid = (l+r)>>1; 37 if(ql <= mid)update(ql, qr, v, o<<1, l, mid); 38 if(qr > mid)update(ql, qr, v, o<<1|1, mid+1, r); 39 tree[o] = tree[o<<1] + tree[o<<1|1]; 40 } 41 long long query(long long ql, long long qr, long long o, long long l, long long r) 42 { 43 if(ql <= l&&r <= qr) 44 { 45 return tree[o]; 46 } 47 pushdown(o, r - l + 1); 48 long long mid = (l+r)>>1; 49 long long ans = 0; 50 if(ql <= mid)ans += query(ql, qr, o<<1, l, mid); 51 if(qr > mid)ans += query(ql, qr, o<<1|1, mid+1, r); 52 return ans; 53 } 54 int main() 55 { 56 scanf("%I64d%I64d", &N, &Q); 57 build(1, 1, N); 58 while (Q--) 59 { 60 char s[50]; 61 long long a, b, c; 62 scanf("%s", s); 63 if (s[0] == 'Q') 64 { 65 scanf("%I64d %I64d", &a, &b); 66 printf("%lld\n", query(a,b,1,1,N)); 67 } 68 else { 69 scanf("%I64d%I64d%I64d", &a, &b, &c); 70 update(a, b, c, 1, 1, N); 71 } 72 } 73 return 0; 74 }
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 100010 4 #include <algorithm> 5 using namespace std; 6 int lazy[maxn<<2], vis[maxn<<2], n, x[maxn], y[maxn], X[maxn]; 7 int tot, cnt, m; 8 void pushdown(int o) 9 { 10 if(lazy[o] != -1) 11 { 12 lazy[o<<1] = lazy[o<<1|1] = lazy[o]; 13 lazy[o] = -1; 14 } 15 } 16 void update(int ql, int qr, int v, int o, int l, int r) 17 { 18 if(ql <= l&&r <= qr) 19 { 20 lazy[o] = v; 21 return; 22 } 23 pushdown(o); 24 int mid = (l + r)>>1; 25 if(ql <= mid)update(ql, qr, v, o<<1, l, mid); 26 if(qr > mid)update(ql, qr, v, o<<1|1, mid+1, r); 27 } 28 void query(int o, int l, int r) 29 { 30 if(lazy[o] != -1) 31 {if(!vis[lazy[o]])cnt++; 32 vis[lazy[o]] = 1;return;} 33 int mid = (l + r) >> 1; 34 if(l == r)return; 35 query(o<<1, l, mid); 36 query(o<<1|1, mid+1, r); 37 } 38 int erfen(int key, int m, int *a) 39 { 40 int l = 1, r = m; 41 while(l <= r) 42 { 43 int mid = (l + r) >> 1; 44 if(a[mid] == key) return mid; 45 if(a[mid] < key)l = mid+1; 46 else r = mid - 1; 47 } 48 return -1; 49 } 50 int main() 51 { 52 int T; 53 scanf("%d", &T); 54 tot = 0; 55 while(T--) 56 { 57 tot = 0; 58 m = 0; 59 scanf("%d", &n); 60 for (int i = 1; i <= n; i++) 61 { 62 scanf("%d %d", &x[i], &y[i]); 63 X[++tot] = x[i]; 64 X[++tot] = y[i]; 65 } 66 memset(lazy, -1, sizeof(lazy)); 67 sort(X+1, X+1+tot); 68 for(int i = 1; i <= tot; i++) 69 if(X[i] != X[i-1]) 70 X[++m] = X[i]; 71 for(int i = m; i >= 1; i--) 72 { 73 if(X[i] != 1 + X[i-1])X[++m] = X[i-1] + 1; 74 } 75 sort(X+1, X+1+m); 76 for (int i = 1; i <= n; i++) 77 { 78 int l = erfen(x[i], m, X); 79 int r = erfen(y[i], m, X); 80 update(l, r, i, 1, 1, m); 81 } 82 cnt = 0; 83 memset(vis, 0, sizeof(vis)); 84 query(1, 1, m); 85 printf("%d\n", cnt); 86 } 87 }
1 #include <stdio.h> 2 #include <algorithm> 3 #define maxn 150000 4 using namespace std; 5 int xorv[maxn<<2], setv[maxn<<2], L, R, val[maxn+10]; 6 char kase, kl, kr; 7 void xorit(int o) 8 { 9 if (setv[o] != -1)setv[o] ^= 1; 10 else xorv[o]^=1; 11 } 12 void pushdown(int o) 13 { 14 int lc = o*2, rc = o * 2 + 1; 15 if (setv[o] != -1) 16 { 17 setv[lc] = setv[rc] = setv[o]; 18 xorv[lc] = xorv[rc] = 0; 19 setv[o] = -1; 20 } 21 if (xorv[o]) 22 { 23 xorit(lc);xorit(rc);xorv[o]=0; 24 } 25 } 26 void update(int o, int l, int r) 27 { 28 //int lc = o *2, rc = o *2 + 1; 29 if (L <= l && R >= r) 30 { 31 if (kase == 'U')setv[o] = 1,xorv[o] = 0; 32 else if (kase == 'D')setv[o] = xorv[o] = 0; 33 else if (kase == 'C'||kase == 'S')xorit(o); 34 } 35 else { 36 pushdown(o); 37 int mid = (r + l)/2; 38 if (L <= mid)update(o * 2, l, mid); 39 else if (kase == 'I'||kase=='C')setv[o*2] = xorv[o*2] = 0; 40 if (R > mid)update(o *2 +1, mid + 1, r); 41 else if (kase == 'I'||kase=='C')setv[o*2+1] = xorv[o*2+1] = 0; 42 } 43 } 44 void suckit(int o, int l, int r) 45 { 46 if (setv[o] == 1) 47 { 48 for (int i = l; i <= r; i++) 49 val[i] = 1; 50 return ; 51 } 52 else if (!setv[o] || l == r)return ; 53 int mid = (l + r)/2; 54 pushdown(o); 55 suckit(o*2, l, mid); 56 suckit(o*2+1, mid +1, r); 57 } 58 void print() 59 { 60 int l = -1,r, find =0; 61 for (int i = 0; i <= maxn;i++) 62 { 63 if (val[i]){if(l==-1)l=i;r=i;} 64 else if(~l){ 65 if(find)putchar(' ');find = 1; 66 printf("%c%d,%d%c",l&1 ? '(' : '[',l >> 1,(r+1)>>1,r&1 ? ')' : ']'); 67 l = -1; 68 } 69 } 70 if (!find)printf("empty set"); 71 else puts(""); 72 } 73 int main() { 74 while (~scanf("%c %c%d,%d%c\n", &kase, &kl, &L, &R, &kr)) { 75 L <<= 1; if (kl == '(') L++; 76 R <<= 1; if (kr == ')') R--; 77 if (L > R) { if (kase == 'I' || kase == 'C') xorv[1] = setv[1] = 0; } 78 else update(1, 0, maxn); 79 } 80 suckit(1, 0, maxn); 81 print(); 82 return 0; 83 }
5.poj1436 Horizontally Visible Segments
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 int n, y1, y2, x, T, nn; 6 bool map[8010][8010]; 7 struct node{ 8 int l, r, x; 9 }a[8010<<1]; 10 int colo[8010<<3]; 11 int cmp(node a, node b) 12 { 13 return a.x < b.x; 14 } 15 void pushdown(int o) 16 { 17 if (colo[o]) 18 { 19 colo[o<<1] = colo[o<<1|1] = colo[o]; 20 colo[o] = 0; 21 } 22 } 23 void update(int ql, int qr, int x, int o, int l, int r) 24 { 25 if(ql <=l &&r <= qr) 26 { 27 colo[o] = x;return ; 28 } 29 pushdown(o); 30 int mid = (l + r) >>1; 31 if(ql <= mid)update(ql, qr, x, o<<1, l, mid); 32 if(qr > mid)update(ql, qr, x, o<<1|1, mid+1, r); 33 } 34 void query(int ql, int qr, int x, int o, int l, int r) 35 { 36 if(colo[o]) 37 { 38 map[x][colo[o]] = 1; 39 return ; 40 } 41 if (l == r)return ; 42 int mid = (l + r) >> 1; 43 if(ql <= mid)query(ql, qr, x, o<<1, l ,mid); 44 if(qr > mid)query(ql, qr, x, o<<1|1, mid+1, r); 45 } 46 int main() 47 { 48 scanf("%d", &T); 49 while(T--) 50 { 51 scanf("%d", &n); 52 for(int i = 1; i <= n; i++) 53 { 54 scanf("%d %d %d", &y1, &y2, &x); 55 y1<<=1;y2<<=1; 56 a[i].l = y1;a[i].r = y2;a[i].x = x; 57 } 58 sort(a+1, a+1+n, cmp); 59 memset(map, 0, sizeof(map)); 60 memset(colo, 0, sizeof(colo)); 61 for(int i = 1; i <= n; i++) 62 { 63 query(a[i].l, a[i].r, i, 1, 0, 8001<<1); 64 update(a[i].l, a[i].r, i, 1, 0, 8001<<1); 65 } 66 int ans = 0; 67 for (int i = 1; i<= n; i++) 68 for (int j = 1; j <= n; j++) 69 if(map[i][j]) 70 for(int k = 1; k <= n; k++) 71 { 72 if(map[i][k]&&map[j][k]) 73 ans++; 74 } 75 printf("%d\n", ans); 76 } 77 }
1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #define MAXD 10010 5 const double PI = acos(-1.0); 6 int N, M, a[MAXD], A[MAXD], degree[MAXD], rd[4 * MAXD]; 7 struct point 8 { 9 double x, y; 10 }dp[4 * MAXD]; 11 double getrad(int x) 12 { 13 return x * PI / 180; 14 } 15 void build(int cur, int x, int y) 16 { 17 int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1; 18 rd[cur] = 0; 19 dp[cur].x = 0, dp[cur].y = A[y] - A[x - 1]; 20 if(x == y) 21 return ; 22 build(ls, x, mid); 23 build(rs, mid + 1, y); 24 } 25 void init() 26 { 27 int i; 28 A[0] = 0; 29 for(i = 1; i <= N; i ++) 30 { 31 scanf("%d", &a[i]); 32 A[i] = A[i - 1] + a[i]; 33 degree[i] = 0; 34 } 35 build(1, 1, N); 36 } 37 void update(int cur) 38 { 39 int ls = cur << 1, rs = (cur << 1) | 1; 40 dp[cur].x = dp[ls].x + dp[rs].x, dp[cur].y = dp[ls].y + dp[rs].y; 41 } 42 void Rotate(double &dx, double &dy, double rad) 43 { 44 double x = dx, y = dy; 45 dx = x * cos(rad) - y * sin(rad); 46 dy = x * sin(rad) + y * cos(rad); 47 } 48 void pushdown(int cur) 49 { 50 int ls = cur << 1, rs = (cur << 1) | 1; 51 if(rd[cur]) 52 { 53 double rad = getrad(rd[cur]); 54 rd[ls] += rd[cur], rd[rs] += rd[cur]; 55 Rotate(dp[ls].x, dp[ls].y, rad); 56 Rotate(dp[rs].x, dp[rs].y, rad); 57 rd[cur] = 0; 58 } 59 } 60 void refresh(int cur, int x, int y, int k, int delta) 61 { 62 int mid = (x + y) >> 1, ls = cur << 1, rs = (cur << 1) | 1; 63 if(x == y) 64 { 65 double rad = getrad(delta); 66 Rotate(dp[cur].x, dp[cur].y, rad); 67 return ; 68 } 69 pushdown(cur); 70 if(mid + 1 < k) 71 refresh(rs, mid + 1, y, k, delta); 72 else 73 { 74 double rad = getrad(delta); 75 if(k <= mid) 76 refresh(ls, x, mid, k, delta); 77 Rotate(dp[rs].x, dp[rs].y, rad); 78 rd[rs] += delta; 79 } 80 update(cur); 81 } 82 void solve() 83 { 84 int i, j, k, d, delta; 85 for(i = 0; i < M; i ++) 86 { 87 scanf("%d%d", &k, &d); 88 ++ k, d = d - 180; 89 delta = d - degree[k]; 90 degree[k] = d; 91 refresh(1, 1, N, k, delta); 92 printf("%.2f %.2f\n", dp[1].x, dp[1].y); 93 } 94 } 95 int main() 96 { 97 int t = 0; 98 while(scanf("%d%d", &N, &M) == 2) 99 { 100 init(); 101 if(t ++) 102 printf("\n"); 103 solve(); 104 } 105 return 0; 106 }
区间合并
1.poj3667 Hotel 【重写预定
1 #include <cstdio> 2 #include <cstring> 3 #include <cctype> 4 #include <algorithm> 5 using namespace std; 6 #define lson l , m , rt << 1 7 #define rson m + 1 , r , rt << 1 | 1 8 const int maxn = 55555; 9 int lsum[maxn<<2] , rsum[maxn<<2] , msum[maxn<<2]; 10 int cover[maxn<<2]; 11 void PushDown(int rt,int m) { 12 if (cover[rt] != -1) { 13 cover[rt<<1] = cover[rt<<1|1] = cover[rt]; 14 msum[rt<<1] = lsum[rt<<1] = rsum[rt<<1] = cover[rt] ? 0 : m - (m >> 1); 15 msum[rt<<1|1] = lsum[rt<<1|1] = rsum[rt<<1|1] = cover[rt] ? 0 : (m >> 1); 16 cover[rt] = -1; 17 } 18 } 19 void PushUp(int rt,int m) { 20 lsum[rt] = lsum[rt<<1]; 21 rsum[rt] = rsum[rt<<1|1]; 22 if (lsum[rt] == m - (m >> 1)) lsum[rt] += lsum[rt<<1|1]; 23 if (rsum[rt] == (m >> 1)) rsum[rt] += rsum[rt<<1]; 24 msum[rt] = max(lsum[rt<<1|1] + rsum[rt<<1] , max(msum[rt<<1] , msum[rt<<1|1])); 25 } 26 void build(int l,int r,int rt) { 27 msum[rt] = lsum[rt] = rsum[rt] = r - l + 1; 28 cover[rt] = -1; 29 if (l == r) return ; 30 int m = (l + r) >> 1; 31 build(lson); 32 build(rson); 33 } 34 void update(int L,int R,int c,int l,int r,int rt) { 35 if (L <= l && r <= R) { 36 msum[rt] = lsum[rt] = rsum[rt] = c ? 0 : r - l + 1; 37 cover[rt] = c; 38 return ; 39 } 40 PushDown(rt , r - l + 1); 41 int m = (l + r) >> 1; 42 if (L <= m) update(L , R , c , lson); 43 if (m < R) update(L , R , c , rson); 44 PushUp(rt , r - l + 1); 45 } 46 int query(int w,int l,int r,int rt) { 47 if (l == r) return l; 48 PushDown(rt , r - l + 1); 49 int m = (l + r) >> 1; 50 if (msum[rt<<1] >= w) return query(w , lson); 51 else if (rsum[rt<<1] + lsum[rt<<1|1] >= w) return m - rsum[rt<<1] + 1; 52 return query(w , rson); 53 } 54 int main() { 55 int n , m; 56 scanf("%d%d",&n,&m); 57 build(1 , n , 1); 58 while (m --) { 59 int op , a , b; 60 scanf("%d",&op); 61 if (op == 1) { 62 scanf("%d",&a); 63 if (msum[1] < a) puts("0"); 64 else { 65 int p = query(a , 1 , n , 1); 66 printf("%d\n",p); 67 update(p , p + a - 1 , 1 , 1 , n , 1); 68 } 69 } else { 70 scanf("%d%d",&a,&b); 71 update(a , a + b - 1 , 0 , 1 , n , 1); 72 } 73 } 74 return 0; 75 }
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 100010 4 #include <algorithm> 5 using namespace std; 6 int lsum[maxn<<2], rsum[maxn<<2], ssum[maxn<<2], num[maxn]; 7 int T, n, q, a, b; 8 void pushup(int o, int m, int l, int r) 9 { 10 int mid = (l + r) >> 1; 11 lsum[o] = lsum[o<<1]; 12 rsum[o] = rsum[o<<1|1]; 13 ssum[o] = max(ssum[o<<1], ssum[o<<1|1]); 14 if(num[mid] < num[mid+1]) 15 { 16 if(lsum[o] == m-(m>>1))lsum[o]+=lsum[o<<1|1]; 17 if(rsum[o] == (m>>1))rsum[o]+=rsum[o<<1]; 18 ssum[o] = max(lsum[o<<1|1]+rsum[o<<1], ssum[o]); 19 } 20 } 21 22 void build(int o, int l, int r) 23 { 24 //lsum[o] = rsum[o] = ssum[o] = r - l + 1; 25 if(l == r) 26 { 27 scanf("%d", &num[l]); 28 lsum[o] = rsum[o] = ssum[o] = 1; 29 return ; 30 } 31 int mid = (l + r) >> 1; 32 build(o<<1, l, mid); 33 build(o<<1|1, mid+1, r); 34 pushup(o,r-l+1,l, r); 35 } 36 void update(int p, int v, int o, int l, int r) 37 { 38 if(l == r) 39 { 40 num[l] = v; 41 return; 42 } 43 int mid = (l + r) >> 1; 44 if(p <= mid)update(p, v, o<<1, l, mid); 45 else if(p > mid)update(p, v, o<<1|1, mid+1, r); 46 pushup(o, r-l+1, l, r); 47 } 48 int query(int ql, int qr, int o, int l, int r) 49 { 50 if(ql <= l&& r <= qr) 51 return ssum[o]; 52 int mid = (l + r) >> 1; 53 int ans = 0; 54 if(ql <= mid)ans = max(ans,query(ql, qr, o<<1, l, mid)); 55 if(qr > mid)ans = max(ans, query(ql, qr, o<<1|1, mid+1, r)); 56 if(num[mid] < num[mid+1]) 57 ans = max(ans, min(mid-ql+1, rsum[o<<1]) + min(qr-mid, lsum[o<<1|1])); 58 return ans; 59 } 60 int main() 61 { 62 char s[5]; 63 scanf("%d", &T); 64 while (T--) 65 { 66 scanf("%d %d", &n, &q); 67 build(1, 1, n); 68 //for (int i = 1; i <= n; i++)scanf("%d", &num[i]); 69 while(q--) 70 { 71 scanf("%s %d %d", s, &a, &b); 72 if(s[0] == 'Q') 73 printf("%d\n", query(a+1,b+1,1,1,n)); 74 else update(a+1,b,1,1,n); 75 } 76 } 77 }
3.hdu3397 Sequence operation 【重写预定
1 #include <stdio.h> 2 #define maxn 100010 3 #include <algorithm> 4 using namespace std; 5 struct node{ 6 int sum, v0, v1, left0, left1; 7 int right0, right1; 8 }tree[maxn<<2]; 9 int lazy[maxn<<2], cover[maxn<<2]; 10 int n; 11 void pushup(int mid, int o, int l, int r) 12 { 13 tree[o].sum=tree[o<<1].sum+tree[o<<1|1].sum; 14 tree[o].left0=tree[o<<1].left0; 15 tree[o].right0=tree[o<<1|1].right0; 16 tree[o].left1=tree[o<<1].left1; 17 tree[o].right1=tree[o<<1|1].right1; 18 if(tree[o].left0==mid-l+1) 19 tree[o].left0+=tree[o<<1|1].left0; 20 if(tree[o].left1==mid-l+1) 21 tree[o].left1+=tree[o<<1|1].left1; 22 if(tree[o].right0==r-mid) 23 tree[o].right0+=tree[o<<1].right0; 24 if(tree[o].right1==r-mid) 25 tree[o].right1+=tree[o<<1].right1; 26 tree[o].v0=max(tree[o<<1].v0, tree[o<<1|1].v0); 27 tree[o].v0=max(tree[o].v0,tree[o<<1].right0+tree[o<<1|1].left0); 28 tree[o].v1=max(tree[o<<1].v1, tree[o<<1|1].v1); 29 tree[o].v1=max(tree[o].v1,tree[o<<1].right1+tree[o<<1|1].left1); 30 } 31 void fxor(int o, int l, int r) 32 { 33 if(cover[o]!=-1) 34 { 35 cover[o]^=1; 36 tree[o].left1=tree[o].right1=tree[o].v1=tree[o].sum=(r-l+1)*cover[o]; 37 tree[o].left0=tree[o].right0=tree[o].v0=(r-l+1)*(cover[o]^1); 38 } 39 else { 40 lazy[o]^=1; 41 tree[o].sum=r-l+1 - tree[o].sum; 42 swap(tree[o].left0, tree[o].left1); 43 swap(tree[o].right0, tree[o].right1); 44 swap(tree[o].v0, tree[o].v1); 45 } 46 } 47 void pushdown(int mid, int o, int l, int r) 48 { 49 if(cover[o] != -1) 50 { 51 cover[o<<1]=cover[o<<1|1]=cover[o]; 52 lazy[o<<1]=lazy[o<<1|1]=0; 53 tree[o<<1].left1=tree[o<<1].right1=tree[o<<1].v1=tree[o<<1].sum=(mid-l+1)*cover[o]; 54 tree[o<<1|1].left1=tree[o<<1|1].right1=tree[o<<1|1].v1=tree[o<<1|1].sum=(r-mid)*cover[o]; 55 tree[o<<1].left0=tree[o<<1].right0=tree[o<<1].v0=(mid-l+1)*(cover[o]^1); 56 tree[o<<1|1].left0=tree[o<<1|1].right0=tree[o<<1|1].v0=(r-mid)*(cover[o]^1); 57 cover[o]=-1; 58 } 59 if(lazy[o]) 60 { 61 fxor(o<<1, l, mid); 62 fxor(o<<1|1, mid+1, r); 63 lazy[o] = 0; 64 } 65 } 66 void build(int o, int l, int r) 67 { 68 cover[o] = -1; 69 lazy[o] = 0; 70 if(l == r) 71 { 72 scanf("%d", &tree[o].sum); 73 tree[o].v1=tree[o].left1=tree[o].right1=tree[o].sum; 74 tree[o].v0=tree[o].left0=tree[o].right0=tree[o].sum^1; 75 } 76 else {int mid = (l + r)>>1; 77 build(o<<1, l, mid); 78 build(o<<1|1, mid+1, r); 79 pushup(mid, o, l, r); 80 } 81 } 82 void update(int ql, int qr, int v, int o, int l, int r) 83 { 84 if(ql <= l&& r <= qr) 85 { 86 cover[o] = v; 87 lazy[o]=0; 88 tree[o].left1=tree[o].right1=tree[o].sum=tree[o].v1=(r-l+1)*v; 89 tree[o].left0=tree[o].right0=tree[o].v0=(r-l+1)*(v^1); 90 } 91 else { 92 int mid = (l + r)>>1; 93 pushdown(mid, o, l, r); 94 if(ql <= mid)update(ql, qr, v, o<<1, l, mid); 95 if(qr > mid)update(ql, qr, v, o<<1|1, mid+1, r); 96 pushup(mid, o, l, r); 97 } 98 } 99 void change(int ql, int qr, int o, int l, int r) 100 { 101 if(ql <= l&&r <= qr) 102 fxor(o, l, r); 103 else 104 { 105 int mid = (l + r)>>1; 106 pushdown(mid, o, l, r); 107 if(ql <= mid)change(ql, qr, o<<1, l, mid); 108 if(qr > mid)change(ql, qr, o<<1|1, mid+1, r); 109 pushup(mid, o, l, r); 110 } 111 } 112 int sum(int ql, int qr, int o, int l, int r) 113 { 114 if(ql <= l&&r <= qr) 115 return tree[o].sum; 116 int mid = (l + r)>>1; 117 int ans = 0; 118 pushdown(mid, o, l, r); 119 if(ql <= mid)ans += sum(ql, qr, o<<1, l, mid); 120 if(qr > mid)ans += sum(ql, qr, o<<1|1, mid+1, r); 121 return ans; 122 } 123 int query(int ql, int qr, int o, int l, int r) 124 { 125 if(ql <= l&& r <= qr) 126 return tree[o].v1; 127 int temp, ans, mid = (l + r) >>1; 128 ans = 0; 129 pushdown(mid, o, l, r); 130 if(ql <= mid)ans =max (ans, query(ql, qr, o<<1, l, mid)); 131 if(qr > mid)ans =max (ans, query(ql, qr, o<<1|1, mid+1, r)); 132 temp=min(qr-mid,tree[o<<1|1].left1) + min(mid-ql+1, tree[o<<1].right1); 133 return max(ans, temp); 134 } 135 int main() 136 { 137 int T, n, m, a, b; 138 int s; 139 scanf("%d", &T); 140 while (T--) 141 { 142 scanf("%d %d", &n, &m); 143 build(1, 1, n); 144 while(m--) 145 { 146 scanf("%d %d %d", &s, &a, &b); 147 a++;b++; 148 if(s == 0||s == 1) 149 update(a, b, s, 1, 1, n); 150 else if(s == 2) 151 change(a, b, 1, 1, n); 152 else if(s == 3) 153 printf("%d\n", sum(a, b, 1, 1, n)); 154 else 155 printf("%d\n", query(a, b, 1, 1, n)); 156 } 157 } 158 return 0; 159 }
1 #include <stdio.h> 2 #include <string.h> 3 #define maxn 50010 4 #include <algorithm> 5 using namespace std; 6 struct node { 7 int left, right, sum; 8 int lazy, cover, num; 9 int st, end; 10 }tree[maxn<<2]; 11 int n, q; 12 void pushup(int mid, int o, int l, int r) 13 { 14 tree[o].left = tree[o<<1].left; 15 tree[o].right = tree[o<<1|1].right; 16 tree[o].sum = max(tree[o<<1].sum, tree[o<<1|1].sum); 17 if(tree[o].left == (mid - l + 1))tree[o].left += tree[o<<1|1].left; 18 if(tree[o].right == (r-mid))tree[o].right += tree[o<<1].right; 19 tree[o].sum = max(tree[o].sum, tree[o<<1].right + tree[o<<1|1].left); 20 } 21 void pushdown(int mid, int o, int l, int r) 22 { 23 if(tree[o].lazy != -1) 24 { 25 tree[o<<1].lazy = tree[o<<1|1].lazy = tree[o].lazy; 26 tree[o<<1].sum=tree[o<<1].left=tree[o<<1].right=(mid-l+1)*tree[o].lazy; 27 tree[o<<1|1].sum=tree[o<<1|1].left=tree[o<<1|1].right=(r-mid)*tree[o].lazy; 28 tree[o<<1].st = tree[o<<1|1].st = tree[o].st; 29 tree[o<<1].end = tree[o<<1|1].end = tree[o].end; 30 tree[o].lazy = -1; 31 } 32 } 33 int New(int x, int o, int l, int r) 34 { 35 if(l == r)return l; 36 int mid = (l + r)>>1; 37 pushdown(mid, o, l, r); 38 if(tree[o<<1].sum >= x) 39 return New(x, o<<1, l, mid); 40 else if(tree[o<<1|1].left + tree[o<<1].right >= x) 41 return mid - tree[o<<1].right + 1; 42 else return New(x, o<<1|1, mid+1, r); 43 } 44 void update(int ql, int qr, int v, int o, int l, int r) 45 { 46 if(ql <= l&& r <= qr) 47 { 48 tree[o].lazy = v; 49 tree[o].sum=tree[o].left=tree[o].right=v*(r-l+1); 50 if(v)tree[o].st = tree[o].end = -1; 51 else { 52 tree[o].st = ql; 53 tree[o].end = qr; 54 } 55 } 56 else { 57 int mid = (l + r) >>1; 58 pushdown(mid, o, l, r); 59 if(ql <= mid)update(ql, qr, v, o<<1, l, mid); 60 if(qr > mid)update(ql, qr, v, o<<1|1, mid+1, r); 61 pushup(mid, o, l, r); 62 } 63 } 64 void countdown(int o) 65 { 66 if(tree[o].cover) 67 { 68 tree[o<<1].num = tree[o<<1|1].num = 0; 69 tree[o<<1].cover = tree[o<<1|1].cover = 1; 70 tree[o].cover = 0; 71 } 72 } 73 void count(int x, int v, int o, int l, int r) 74 { 75 if(l == r) 76 { 77 tree[o].num = v; 78 } 79 else { 80 countdown(o); 81 int mid = (l + r) >>1 ; 82 if(x<=mid)count(x, v, o<<1, l, mid); 83 else count(x, v, o<<1|1, mid+1, r); 84 tree[o].num = tree[o<<1].num + tree[o<<1|1].num; 85 } 86 } 87 void build(int o, int l, int r) 88 { 89 update(1, n, 1, 1, 1, n); 90 tree[1].cover = 1; 91 tree[1].num = 0; 92 } 93 int free(int x, int o, int l, int r) 94 { 95 if(l == r) 96 return o; 97 int mid = (l + r) >> 1; 98 pushdown(mid, o, l, r); 99 if(x <= mid)return free(x, o<<1, l, mid); 100 else return free(x, o<<1|1, mid+1, r); 101 } 102 int get(int x, int o, int l, int r) 103 { 104 if (l == r) 105 return l; 106 else 107 { 108 int mid = (l + r) >>1 ; 109 countdown(o); 110 if(tree[o<<1].num >= x) return get(x, o<<1, l, mid); 111 else return get(x-tree[o<<1].num, o<<1|1, mid+1, r); 112 } 113 } 114 int main() 115 { 116 int x, ans; 117 char s[10]; 118 while (~scanf("%d %d", &n, &q)) 119 { 120 build(1, 1, n); 121 while (q--) 122 { 123 scanf("%s", s); 124 if(!strcmp(s,"Reset")) 125 { 126 build(1, 1, n); 127 printf("Reset Now\n"); 128 continue; 129 } 130 scanf("%d", &x); 131 if(!strcmp(s,"New")) 132 { 133 if (tree[1].sum >= x) 134 { 135 ans = New(x, 1, 1, n); 136 printf("New at %d\n", ans); 137 count(ans, 1, 1, 1, n); 138 update(ans, x+ans-1, 0, 1, 1, n); 139 continue; 140 } 141 else 142 printf("Reject New\n"); 143 } 144 else if(!strcmp(s, "Free")) 145 { 146 ans = free(x, 1, 1, n); 147 if(tree[ans].st < 0)printf("Reject Free\n"); 148 else { 149 printf("Free from %d to %d\n", tree[ans].st, tree[ans].end); 150 count(tree[ans].st, 0, 1, 1, n); 151 update(tree[ans].st, tree[ans].end, 1, 1, 1, n); 152 } 153 } 154 else { 155 if(x > tree[1].num)printf("Reject Get\n"); 156 else 157 printf("Get at %d\n", get(x, 1, 1, n)); 158 } 159 } 160 printf("\n"); 161 } 162 }
1 #include <stdio.h> 2 #include <stack> 3 #include <algorithm> 4 using namespace std; 5 stack <int> q; 6 int n, m, x; 7 struct node{ 8 int lsum, rsum, sum; 9 }tree[50010<<2]; 10 char s[10]; 11 void build(int o, int l, int r) 12 { 13 tree[o].sum = tree[o].lsum = tree[o].rsum = r - l + 1; 14 if(l == r)return ; 15 int mid = (l + r) >> 1; 16 build(o<<1, l ,mid); 17 build(o<<1|1, mid+1, r); 18 } 19 void pushup(int o, int l, int r) 20 { 21 int mid = (l + r) >> 1; 22 tree[o].lsum = tree[o<<1].lsum; 23 tree[o].rsum = tree[o<<1|1].rsum; 24 if(tree[o].lsum == mid-l+1)tree[o].lsum += tree[o<<1|1].lsum; 25 if(tree[o].rsum == r-mid)tree[o].rsum += tree[o<<1].rsum; 26 tree[o].sum = max(tree[o<<1].sum, tree[o<<1|1].sum); 27 tree[o].sum = max(tree[o<<1].rsum+tree[o<<1|1].lsum, tree[o].sum); 28 } 29 void update(int x, int v, int o, int l ,int r) 30 { 31 if(l == r) 32 { 33 tree[o].sum = tree[o].lsum = tree[o].rsum = v; 34 return ; 35 } 36 int mid = (l + r) >> 1; 37 if(x <= mid)update(x, v, o<<1, l, mid); 38 else update(x, v, o<<1|1, mid+1, r); 39 pushup(o, l, r); 40 } 41 int query(int x, int o, int l, int r) 42 { 43 if(l == r) 44 return tree[o].lsum; 45 int mid = (l + r)>>1; 46 if(x <= mid) 47 { 48 if(tree[o<<1].lsum == mid-l+1) 49 return tree[o<<1].lsum + tree[o<<1|1].lsum; 50 else if(l+tree[o<<1].lsum > x) 51 return tree[o<<1].lsum; 52 else if(x > mid - tree[o<<1].rsum) 53 return tree[o<<1].rsum + tree[o<<1|1].lsum; 54 else return query(x, o<<1, l, mid); 55 } 56 else { 57 if(tree[o<<1|1].rsum == r - mid) 58 return tree[o<<1].rsum + tree[o<<1|1].rsum; 59 else if(mid + tree[o<<1|1].lsum+1 > x) 60 return tree[o<<1].rsum + tree[o<<1|1].lsum; 61 else if(x > r - tree[o<<1|1].rsum) 62 return tree[o<<1|1].rsum; 63 else return query(x, o<<1|1, mid+1, r); 64 65 } 66 67 } 68 int main() 69 { 70 while (~scanf("%d %d", &n, &m)) 71 { 72 build(1, 1, n); 73 for(;!q.empty();q.pop()); 74 while (m--) 75 { 76 scanf("%s", s); 77 if (s[0] == 'D') 78 { 79 scanf("%d", &x); 80 update(x, 0, 1, 1, n); 81 q.push(x); 82 } 83 if(s[0] == 'Q') 84 { 85 scanf("%d", &x); 86 printf("%d\n", query(x, 1, 1, n)); 87 } 88 if(s[0] == 'R') 89 { 90 if(!q.empty()) 91 { 92 int y = q.top();q.pop(); 93 update(y, 1, 1, 1, n); 94 } 95 } 96 } 97 } 98 }
扫描线暂时搁置