2月份线段树树状数组专题代码
2月份刷了树状数组和线段树专题。
代码贴在博客上,方便查看。
Solved | Problem ID | Title | Ratio(Accepted / Submitted) |
1001 | Rotate | 28.57%(4/14) | |
1002 | Cube | 100.00%(2/2) | |
1003 | 敌兵布阵 | 25.00%(2/8) | |
1004 | See you~ | 22.73%(5/22) | |
1005 | Stars | 14.29%(2/14) | |
1006 | Find the nondecreasing subsequences | 20.00%(2/10) | |
1007 | Stars | 50.00%(2/4) | |
1008 | Sort it | 100.00%(2/2) | |
1009 | Counting Sequences | 14.29%(4/28) | |
1010 | Ping pong | 21.74%(5/23) | |
1011 | The more, The Better | 100.00%(2/2) | |
1012 | Color the Ball | 66.67%(2/3) | |
1013 | 覆盖的面积 | 62.50%(5/8) | |
1014 | I Hate It | 66.67%(2/3) | |
1015 | Just a Hook | 100.00%(3/3) | |
1016 | Counting Offspring | 9.68%(3/31) | |
1017 | Increasing Speed Limits | 25.00%(1/4) | |
1018 | Minimum Inversion Number | 50.00%(2/4) | |
1019 | Frosh Week | 14.29%(2/14) | |
1020 | Atlantis |
1019
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #include <iostream> #include <fstream> using namespace std; __int64 value[1000010]; __int64 dx[1000010]; __int64 yy[1000010]; int lowbit( int x) { return x&-x; } __int64 sum(int x) { __int64 sum = 0; while( x > 0 ) { sum += dx[x]; x -= lowbit(x); } return sum; } void update( int i, int num, int N) { while( i <= N ) { dx[i] += num; i += lowbit(i); } } int main( ) { int N; while ( scanf("%d", &N) != EOF ) { memset(dx, 0, sizeof(dx)); for( int i = 1; i <= N; i++) { scanf("%I64d",&value[i]); yy[i] = value[i]; } sort(yy + 1, yy + N + 1); for( int i = 1; i <= N; i++) { value[i] = lower_bound(yy + 1, yy + N + 1, value[i]) - yy; } __int64 maxn = 0, temp; for( int i = N; i > 0; i--) { temp = sum(value[i]); update(value[i], 1, N + 1); maxn += temp; } printf("%I64d\n",maxn); } return 0; }
1018
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #include <iostream> #include <fstream> using namespace std; int value[11100]; int dx[11100]; int lowbit( int x) { return x&-x; } int sum(int x) { int sum = 0; while( x > 0 ) { sum += dx[x]; x -= lowbit(x); } return sum; } void update( int i, int num, int N) { while( i <= N ) { dx[i] += num; i += lowbit(i); } } int main( ) { int N; while ( scanf("%d", &N) != EOF ) { memset(dx, 0, sizeof(dx)); for( int i = 1; i <= N; i++) { scanf("%d",&value[i]); value[i]++; } int maxn = 0, temp; for( int i = N; i > 0; i--) { int temp = sum(value[i]); update(value[i], 1, N + 1); maxn += temp; } int ans = maxn; for( int i = 1; i <= N; i++) { temp = maxn - value[i] + 1 + N - value[i]; maxn = temp; if( temp < ans ) ans = temp; } printf("%d\n",ans); } return 0; }
1020
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #include <iostream> #include <fstream> using namespace std; struct node { int l, r, lazy; double sum; }T[100000]; struct Line { double y1, y2; double x; int inout; }L[1000000]; bool cmp(Line A, Line B) { return A.x < B.x; } double yy[10000]; int lnum, ynum; void build( int l, int r, int root) { if( l > r ) return; int mid = (l + r) / 2; T[root].l = l, T[root].r = r; T[root].sum = 0; T[root].lazy = 0; if( l + 1 == r ) return; build(l, mid, root * 2); build(mid, r, root * 2 + 1); } void update( int root ) { if( T[root].lazy > 0 ) { T[root].sum = yy[T[root].r] - yy[T[root].l]; } else if( T[root].l + 1 == T[root].r ) { T[root].sum =0; } else { T[root].sum = T[root * 2].sum + T[root * 2 + 1].sum; } } void insert( int a, int b, int root, int flag) { if( T[root].l >= T[root].r ) return; int mid = (T[root].l + T[root].r) / 2; if( T[root].l == a && T[root].r == b) { T[root].lazy += flag; update(root); return; } if( mid >= b ) insert(a, b, root * 2 , flag); else if( mid <= a ) insert(a, b, root * 2 + 1, flag); else { insert(a, mid, root * 2 , flag); insert(mid, b, root * 2 + 1, flag); } update(root); } void input( int N ) { double x1, x2, y1, y2; for( int i = 1; i <= N; i++) { scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); L[lnum].x = x1, L[lnum].y1 = y1, L[lnum].y2 = y2, L[lnum].inout = 1; L[lnum + 1].x = x2, L[lnum + 1].y1 = y1, L[lnum + 1].y2 = y2, L[lnum + 1].inout = -1; yy[ynum] = y1; yy[ynum + 1] = y2; ynum += 2, lnum += 2; } sort(yy, yy + ynum); sort(L, L + lnum, cmp); ynum = unique(yy, yy + ynum) - yy; } void solve(int ans) { int i, y1, y2; double sum = 0; build(0, ynum, 1); for( int i = 0; i < lnum-1; i++) { int y1 = lower_bound(yy, yy + ynum, L[i].y1) - yy; int y2 = lower_bound(yy, yy + ynum, L[i].y2) - yy; insert(y1, y2, 1, L[i].inout); sum += T[1].sum * (L[i+1].x - L[i].x); } printf("Test case #%d\n",ans); printf("Total explored area: %.2lf\n", sum); puts(""); } int main( ) { int N, ans = 0; while ( scanf("%d", &N), N) { lnum = ynum = 0; memset(T, 0, sizeof(T)); input( N ); ans++; solve(ans); } return 0; }
1016
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> using namespace std; vector<int>mp[100010]; int dx[150020]; int value[150020]; int st[1000000]; int visit[150020]; int top; int lowbit( int x) { return x&-x; } int sum( int x ) { int sum = 0; while( x > 0) { sum += dx[x]; x -= lowbit(x); } return sum; } void add( int x, int num) { while( x < 100010) { dx[x] += num; x += lowbit(x); } } /* 递归式的爆栈 void solve( int p, int N) { value[p] = sum(p-1); for( vector <int>::iterator iter = mp[p].begin( ); iter != mp[p].end( ); iter++) { add(*iter, 1, N); solve(*iter, N); } value[p] = sum(p-1) - value[p]; } */ //非递归,模拟栈 void solve( int p ) { int flag = 0; top = 0; st[top++] = p; while( top ) { int t = st[top - 1]; if( visit[t] == 0) { value[t] = sum(t-1); visit[t] = 1; } flag = 0; for( vector <int>::iterator iter = mp[t].begin( ); iter != mp[t].end( ); iter++) { if( visit[*iter] == 0 ) { st[top++] = *iter; flag = 1; break; } } if( flag ) continue; if( visit[t] == 1 ) { value[t] = sum(t - 1) - value[t]; add(t, 1); top--; } } } int main( ) { int N, p, a, b; while ( scanf("%d%d", &N, &p), N + p ) { top = 0; memset(dx, 0, sizeof(dx)); memset(value, 0, sizeof(value)); memset(visit, 0, sizeof(visit)); memset(st, 0, sizeof(st)); for( int i = 1; i <= 100000; i++) mp[i].clear( ); for( int i = 1; i < N; i++) { scanf("%d%d", &a, &b); mp[a].push_back(b); mp[b].push_back(a); } solve( p ); for( int i = 1; i < N; i++) printf("%d ", value[i]); printf("%d\n", value[N]); } return 0; }
1015
View Code
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { int l, r, flag; }T[1000000]; void build( int l, int r, int root) { int mid = (l + r) / 2; T[root].l = l, T[root].r = r,T[root].flag = 1; if( l == r ) return; build(l, mid, root * 2); build(mid + 1, r, root * 2 + 1); } void update( int a, int b, int c, int root) { int mid = (T[root].l + T[root].r) / 2; if( T[root].l == a && T[root].r == b ) { T[root].flag = c; return; } if( T[root].flag > 0 ) { T[root * 2].flag = T[root * 2 + 1].flag = T[root].flag; } T[root].flag = -1; if( mid >= b ) update(a, b, c, root * 2); else if( mid < a ) update(a, b, c, root * 2 + 1); else { update(a, mid, c, root * 2); update(mid + 1, b, c, root * 2 + 1); } } int query( int l, int r, int root ) { int mid = (l + r ) / 2; if( T[root].flag > 0 ) return (T[root].r - T[root].l + 1) * T[root].flag; else if( T[root].flag == 0 ) return 0; else { return query(l, mid, root * 2) + query(mid + 1, r, root * 2 + 1); } } int main( ) { int N, M, Op, a, b, c, ans = 0; scanf("%d", &N); while(N--) { scanf("%d%d", &M, &Op); build(1, M, 1); ans++; for( int i = 1; i <= Op; i++) { scanf("%d%d%d", &a, &b, &c); update(a, b, c, 1); } printf("Case %d: The total value of the hook is %d.\n", ans, query(1, M, 1)); } return 0; }
1014
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #include <iostream> #include <fstream> using namespace std; int stu[200010]; int maxn = 0; struct node { int l, r; int maxn; }T[1000000]; void build( int l, int r, int root) { int mid = (l + r) / 2; T[root].l = l, T[root].r = r; T[root].maxn = 0; if( l == r ) { T[root].maxn = stu[l]; return; } build(l, mid, root * 2); build(mid + 1, r, root * 2 + 1); T[root].maxn = max(T[root * 2].maxn, T[root * 2 + 1].maxn); } void update( int num, int value, int root ) { int mid = (T[root].l + T[root].r) / 2; if( T[root].l == num && T[root].r == num) { T[root].maxn = value; return; } if( mid >= num ) update(num, value, root * 2); else update(num, value, root * 2 + 1); T[root].maxn = max(T[root * 2].maxn, T[root * 2 + 1].maxn); } void query( int l, int r, int root) { int mid = (T[root].l + T[root].r) / 2; if ( T[root].l == l && T[root].r == r ) { if( T[root].maxn > maxn ) maxn = T[root].maxn; return; } else if(mid >= r ) { query(l, r, root * 2); } else if( mid < l ) { query(l, r, root * 2 + 1); } else { query(l,mid, root * 2); query(mid + 1, r, root * 2 + 1); } } int main( ) { int N, M, a, b; char str[100]; while ( scanf("%d%d", &N, &M) != EOF ) { for( int i = 1; i <= N; i++) scanf("%d", &stu[i]); build(1, N, 1); for( int i = 1; i <= M; i++) { scanf("%s%d%d", str, &a, &b); maxn = 0; if( str[0] == 'Q' ) { query(a, b, 1); printf("%d\n", maxn); } else { update(a, b, 1); } } } return 0; }
1013
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #include <iostream> #include <fstream> using namespace std; struct node { int l, r; int cover; //0表示在此区间有多种覆盖,有覆盖一次的,2次的 }T[100000]; struct Line { double y1, y2; double x; int inout; }L[1000000]; bool cmp(Line A, Line B) { return A.x < B.x; } int linenum, mnum; double yy[100000]; double length = 0; void build( int l, int r, int root) { int mid = (l + r) / 2; T[root].l = l, T[root].r = r; T[root].cover = 0; if( l + 1 == r ) return; build(l, mid, root * 2); build(mid, r, root * 2 + 1); } void insert( int l, int r, int root, int flag) { int mid = (T[root].l + T[root].r) / 2; if( T[root].l >= l && T[root].r <= r ) { T[root]. cover += flag; return; } if( l >= mid ) { insert(l, r, root * 2 + 1, flag); } else if( r <= mid ) { insert(l, r, root * 2, flag); } else { insert(l, mid, root * 2, flag); insert(mid, r, root * 2 + 1, flag); } } void query( int l, int r, int root) { int mid = (l + r) / 2; if ( T[root].cover >= 2 ) { length += yy[T[root].r] - yy[T[root].l]; } else if ( T[root].l + 1 == T[root].r ) { return; } else { T[root * 2].cover += T[root].cover; T[root * 2 + 1].cover += T[root].cover; query(l,mid, root * 2); query(mid, r, root * 2 + 1); T[root * 2].cover -= T[root].cover; T[root * 2 + 1].cover -= T[root].cover; } } void input(int M) { double x1, x2, y1, y2; for( int i = 0; i < M; i++) { scanf("%lf%lf%lf%lf",&x1, &y1, &x2, &y2); L[linenum].x = x1, L[linenum].y1 = y1; L[linenum].y2 = y2, L[linenum].inout = 1; L[linenum + 1].x = x2, L[linenum + 1].y1 = y1; L[linenum + 1].y2 = y2, L[linenum + 1].inout = -1; yy[mnum] = y1; yy[mnum + 1] = y2; linenum += 2; mnum += 2; } sort(yy, yy + mnum); sort(L, L + linenum, cmp); mnum = unique(yy, yy + mnum) - yy; } void solve( ) { int y1, y2, i; double sum = 0; build(0, mnum, 1); for( int i = 0; i < linenum-1; i++) { y1 = lower_bound(yy, yy + mnum, L[i].y1) - yy; y2 = lower_bound(yy, yy + mnum, L[i].y2) - yy; length = 0; // printf("%.2lf %.2lf %.2lf\n%d %d\n", L[i].x, L[i].y1, L[i].y2, y1, y2); insert(y1, y2, 1, L[i].inout); query(1, mnum, 1); sum += (L[i + 1].x - L[i].x) * length; // printf("%.2lf\n", sum); } printf("%.2lf\n", sum); } int main( ) { int N, M; scanf("%d", &N); while ( N-- ) { linenum = mnum = 0; scanf("%d", &M); input(M); solve( ); } return 0; }
1010
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #include <iostream> #include <fstream> using namespace std; int value[20010]; int dx[20010]; int dt[20010]; int yy[20010]; int _left[20010]; int _right[20010]; int lowbit( int x) { return x&-x; } int sum( int x, int flag) { int sum = 0; while( x > 0 ) { if ( flag == 0 ) sum += dx[x]; else sum += dt[x]; x -= lowbit(x); } return sum; } void add( int i, int x, int flag) { while( i < 20005 ) { if ( flag == 0 ) dx[i] += x; else dt[i] += x; i += lowbit(i); } } int main( ) { int N, M, maxn; __int64 total; scanf("%d", &N); while ( N-- ) { total = 0; scanf("%d", &M); memset(dx, 0, sizeof(dx)); memset(dt, 0, sizeof(dt)); memset(_left, 0, sizeof(_left)); memset(_right, 0, sizeof(_right)); memset(yy, 0, sizeof(yy)); for( int i = 1; i <= M; i++) { scanf("%d", &value[i]); yy[i] = value[i]; } sort(yy + 1, yy + M + 1); int cnt = unique(yy + 1, yy + M + 1) - yy - 1; //左边比它小的。 for( int i = 1; i <= M; i++) { int pos = lower_bound(yy + 1 , yy + cnt + 1, value[i]) - yy; _left[i] = sum(pos - 1, 0); add(pos, 1, 0); } //右边比它大的 for( int i = M; i >= 1; i--) { int pos = lower_bound(yy + 1 , yy + cnt + 1, value[i]) - yy; _right[i] = sum(M + 1, 1) - sum(pos-1, 1); //printf("%d %d\n", maxn + 1, value[i]); add(pos, 1, 1); } for( int i = 1; i <= M; i++) { total += _left[i] * _right[i];//左小右大 total += (i - _left[i] - 1) * ( M - i - _right[i]);//左大右小 } printf("%I64d\n", total); /* for( int i = 1; i <= M; i++) printf("%d ", _left[i]); puts("\n"); for( int i = 1; i <= M; i++) printf("%d ", _right[i]); */ } return 0; }
1009
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #define MAXN 100005 using namespace std; int dx[100010]; int value[100010]; int yy[100010]; int dt[100010]; int lowbit( int x ) { return x&-x; } void add( int i, int x) { while( i < MAXN ) { dx[i] += x; if ( dx[i] >= 9901 ) dx[i] %= 9901; i += lowbit(i); } } int sum( int x) { int sum = 0; while( x > 0) { sum += dx[x]; if ( sum >= 9901 ) sum %= 9901; x -= lowbit(x); } return sum; } //找最小的大于等于它的。。。。 int low_find(int l, int r, int val) { int ans = 1; while(l <= r) { int mid = (l + r) / 2; if ( yy[mid] >= val) { r = mid - 1; ans = mid; } else l = mid + 1; } return ans; } //找最大的但小于等于它的。。。 int up_find(int l, int r, int val) { int ans = r; while( l <= r) { int mid = (l + r) / 2; if ( yy[mid] <= val) { l = mid + 1; ans = mid; } else r = mid - 1; } return ans; } int main( ) { int N, M; while ( scanf("%d%d", &N, &M) != EOF ) { memset(dx, 0, sizeof(dx)); for( int i = 1; i <= N; i++) { scanf("%d", &value[i]); yy[i] = value[i]; } sort(yy + 1, yy + N + 1); int maxn = 0, num; num = unique(yy + 1, yy + N + 1)- yy - 1; for( int i = 1; i <= N; i++) { int st = lower_bound(yy + 1, yy + num + 1, value[i]) - yy; int left = value[i] - M; int right = value[i] + M; // printf("num = %d %d %d %d\n", num, value[i], left, right); int leftid = low_find(1, num, left); int rightid = up_find(1, num, right); // printf("%d %d %d %d\n", leftid, rightid, left, right); // printf("%d %d\n", yy[leftid], yy[rightid]); int temp = (sum(rightid) - sum(leftid - 1)) % 9901 ; if ( temp < 0 ) temp += 9901; // printf("temp = %d\n", temp); add(st, temp + 1); maxn += temp; if ( maxn >= 9901 ) maxn %= 9901; } printf("%d\n", maxn % 9901); } return 0; } /* 3 3 1 2 3 3 1 2 1 2 5 */
1006
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #define MAXN 100010 using namespace std; int dt[101000]; int dx[100010]; int yy[100010]; int lowbit( int x) { return x&-x; } void add( int x, int num) { while( x < MAXN ) { dx[x] += num; if ( dx[x] > 1000000007) dx[x] %= 1000000007; x += lowbit(x); } } int sum( int x) { int sum = 0; while( x > 0 ) { sum += dx[x]; if ( sum > 1000000007) sum %= 1000000007; x -= lowbit(x); } return sum; } int main( ) { int N; while ( scanf("%d", &N) != EOF ) { memset(dx, 0, sizeof(dx)); memset(yy, 0, sizeof(yy)); for( int i = 1; i <= N; i++) { scanf("%d", &dt[i]); yy[i] = dt[i]; } sort(yy + 1, yy + N + 1); for( int i = 1; i <= N; i++) { dt[i] = lower_bound(yy + 1 , yy + N + 1, dt[i]) - yy; } int maxn = 0, temp = 1; add(1,1); for( int i = 1; i <= N; i++) { temp = sum(dt[i]) % 1000000007; add(dt[i] , temp); maxn += temp; if ( maxn >= 1000000007 ) maxn %= 1000000007; } if ( maxn >= 1000000007 ) maxn %= 1000000007; printf("%d\n", maxn % 1000000007) ; } return 0; }
1008
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> using namespace std; int dx[1200]; int dt[1200]; int lowbit( int x) { return x&-x; } void add( int i) { while( i < 1002 ) { dx[i] += 1; i += lowbit(i); } } int sum( int i) { int sum = 0; while( i > 0) { sum += dx[i]; i -= lowbit(i); } return sum; } int main( ) { int N; while ( scanf("%d", &N) != EOF ) { memset(dx, 0, sizeof(dx)); for( int i = 1; i <= N; i++) { scanf("%d", &dt[i]); } int maxn = 0, temp; for( int i = N; i >= 1; i-- ) { temp = sum(dt[i]); add(dt[i] + 1); maxn += temp; } printf("%d\n", maxn); } return 0; }
1007
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #define MAXN 1005 using namespace std; bool hash[1010][1010]; int dx[1010][1010]; int lowbit( int x ) { return x&-x; } void update( int x, int y, int num) { for( int i = x; i < MAXN; i += lowbit(i)) for( int j = y; j < MAXN; j += lowbit(j)) dx[i][j] += num; } int sum( int x, int y) { int sum = 0; for( int i = x; i > 0; i -= lowbit(i)) for( int j = y; j > 0; j -= lowbit(j)) sum += dx[i][j]; return sum; } int getvalue( int x1, int y1, int x2, int y2) { int S1 = sum(x1 - 1, y1 - 1); int S2 = sum(x2, y2); int S3 = sum(x1-1, y2); int S4 = sum(x2, y1 - 1); return S2 - S3 - S4 + S1; } int main( ) { int N, x1, y1, x2, y2; char str[1000]; scanf("%d", &N); memset(hash, 0, sizeof(hash)); memset(dx, 0, sizeof(dx)); for( int i = 1; i <= N; i++) { scanf("%s", str); if( str[0] == 'B' ) { scanf("%d%d", &x1,&y1); x1++, y1++; if ( !hash[x1][y1] ) update(x1, y1, 1); hash[x1][y1] = 1; } else if ( str[0] == 'D' ) { scanf("%d%d", &x1,&y1); x1++, y1++; if ( hash[x1][y1] ) update(x1, y1, -1); hash[x1][y1] = 0; } else { scanf("%d%d%d%d",&x1,&x2, &y1,&y2); x1++, y1++, x2++, y2++; if(x1 > x2) swap(x1,x2); if(y1 > y2) swap(y1,y2); printf("%d\n", getvalue(x1, y1, x2, y2)); } } return 0; }
1005
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> using namespace std; int num[100000]; int data[100000]; int dx[100000]; int MAXN = 33000; int lowbit( int x) { return x&-x; } void add( int i, int x) { while ( i < MAXN ) { dx[i] += x; i += lowbit(i); } } int sum( int N) { int sum = 0; while( N > 0) { sum += dx[N]; N -= lowbit(N); } return sum; } int main( ) { int N, x, y; while ( scanf("%d", &N) != EOF ) { memset(dx, 0, sizeof(dx)); memset(data,0, sizeof(data)); memset(num, 0, sizeof(num)); for( int i = 1; i <= N; i++) { scanf("%d%d", &x, &y); x++, y++; num[i] = sum(x); add(x, 1); } for( int i = 1; i <= N; i++) { data[num[i]]++; } for( int i = 0; i < N; i++) { printf("%d\n", data[i]); } } return 0; }
1012
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> using namespace std; struct node { int l, r, color; //0,black, 1, white, -1,表示该区间多种颜色 }T[100000]; int color[100000]; int yy[5000]; void build( int l, int r, int root ) { if ( l >= r ) return; int mid = (l + r) / 2; T[root].l = l, T[root].r = r; T[root].color = 0; if ( l + 1 == r ) return; build(l, mid, root * 2); build(mid, r, root * 2 + 1); } void insert( int a, int b, int root, int color) { int mid = (T[root].l + T[root].r) / 2; if ( a >= b ) return; if ( T[root].l >= a && T[root].r <= b && T[root].color == color ) return; if ( T[root].l >= a && T[root].r <= b ) { T[root].color = color; return; } if ( T[root].color != -1 ) T[root * 2].color = T[root * 2 + 1].color = T[root].color; T[root].color = -1; if ( mid >= b ) insert(a, b, root *2 , color); else if( mid <= a ) insert(a, b, root * 2 + 1, color); else { insert(a, mid, root * 2, color); insert(mid , b, root * 2 + 1, color); } } void query( int l, int r, int root) { int mid = (l + r ) / 2; if(l >= r ) return; if ( T[root].color != -1) { for( int i = T[root].l; i < T[root].r; i++) color[i] = T[root].color; return; } else if ( T[root].l + 1 == T[root].r ) { return; } query(l, mid, root * 2); query(mid, r, root * 2 + 1); } void solve(int N ) { int s = 0,e = 0, end,start; for(int i=0; i < N; i++) { if( color[i] != 1 ) continue; start = yy[i]; while( color[i] == 1 ) i++; if( i > N ) break; end = yy[i]; if( end - start > e - s ) { e = end; s = start; } } if( s == e ) printf("Oh, my god\n"); else printf("%d %d\n",s,e-1); } int main( ) { int N, a[2100], b[2100], num; char str[4000]; while ( scanf("%d", &N) != EOF ) { num = 0; memset(color, 0, sizeof(color)); memset(yy, 0, sizeof(yy)); for( int i = 1;i <= N; i++) { scanf("%d%d%s",&a[i], &b[i], &str[i]); b[i]++; yy[num++] = a[i]; yy[num++] = b[i]; } sort(yy, yy + num); num = unique(yy, yy + num) - yy; yy[num] = yy[num-1]; build(0, num, 1); int flag; for( int i = 1; i <= N; i++) { int x = lower_bound( yy, yy + num, a[i]) - yy; int y = lower_bound( yy, yy + num, b[i]) - yy; if ( str[i] == 'w') flag = 1; else flag = 0; insert(x, y, 1, flag); query(0, num , 1); } query(0, num, 1); solve(num); } return 0; }
1004
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #define MAXN 1005 using namespace std; int dx[1005][1005]; int dt[1005][1005]; int lowbit( int x) { return x&-x; } void update( int x, int y, int num) { for( int i = x; i < MAXN; i += lowbit(i)) for( int j = y; j < MAXN; j += lowbit(j)) dx[i][j] += num; } int sum( int x, int y) { int sum = 0; for( int i = x; i > 0; i -= lowbit(i)) for( int j = y; j > 0; j -= lowbit(j)) sum += dx[i][j]; return sum; } int main( ) { int N, M, x1, x2, y1, y2, t, x3, y3, ans = 0; scanf("%d", &N); char str[1000]; while( N-- ) { scanf("%d", &M); ans++; memset(dx, 0, sizeof(dx)); memset(dt, 0, sizeof(dt)); for( int i = 1; i <= 1002; i++) for( int j = 1; j <= 1002; j++) { dx[i][j] = lowbit(i) * lowbit(j); dt[i][j] = 1; } printf("Case %d:\n", ans); for( int i = 1; i <= M; i++) { scanf("%s", str); if( str[0] == 'S') { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); x1++, y1++, x2++, y2++; if(x1 > x2) swap(x1,x2); if(y1 > y2) swap(y1,y2); int S1 = sum(x1 - 1,y2); int S2 = sum(x2,y1- 1); int S3 = sum(x1 - 1,y1 - 1); int S4 = sum(x2, y2); int S = S4 - S1 - S2 + S3; printf("%d\n", S); } else if( str[0] == 'A') { scanf("%d%d%d", &x1, &y1, &t); x1++, y1++; update(x1, y1, t); dt[x1][y1] += t; } else if ( str[0] == 'D') { scanf("%d%d%d", &x1, &y1, &t); x1++, y1++; if( dt[x1][y1] < t ) { update(x1, y1, -dt[x1][y1]); dt[x1][y1] = 0; } else { update(x1, y1, -t); dt[x1][y1] -= t; } } else if( str[0] == 'M' ) { scanf("%d%d%d%d%d",&x1, &y1, &x3, &y3, &t); x1++, y1++, x3++, y3++; if( dt[x1][y1] < t ) { update(x1, y1, -dt[x1][y1]); update(x3, y3, dt[x1][y1]); dt[x3][y3] += dt[x1][y1]; dt[x1][y1] = 0; } else { update(x1, y1, -t); update(x3, y3, t); dt[x3][y3] += t; dt[x1][y1] -= t; } } } } return 0; }
1003
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> #define N 60005 using namespace std; int dx[60000]; int lowbit( int x ) { return x&-x; } void add( int i, int x) { while( i < N ) { dx[i] += x; i += lowbit(i); } } int sum( int x ) { int sum = 0; while( x > 0) { sum += dx[x]; x -= lowbit( x ); } return sum; } int main( ) { int M, a, b, t, tm, ans = 0; scanf("%d", &M); char str[100]; while ( M-- ) { memset(dx, 0, sizeof(dx)); scanf("%d", &tm); ans++; for( int i = 1; i <= tm; i++) { scanf("%d", &t); add(i, t); } printf("Case %d:\n", ans); while( 1 ) { scanf("%s",str); if ( str[0] == 'E' ) break; scanf("%d%d",&a, &b); if( str[0] == 'A') add(a, b); else if( str[0] == 'S' ) add(a, -b); else if ( str[0] == 'Q' ) printf("%d\n", sum(b) - sum(a-1)); } } return 0; }
1002
View Code
#include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <map> #include <set> #include <time.h> using namespace std; #define MAXN 104 int dx[110][110][110]; int lowbit( int x) { return x&-x; } void update( int x, int y, int z, int num) { for( int i = x; i < MAXN; i += lowbit( i ) ) for( int j = y; j < MAXN; j += lowbit( j ) ) for( int k = z; k < MAXN; k += lowbit(k)) dx[i][j][k] += num; } int sum( int x, int y, int z) { int sum = 0; for( int i = x; i > 0; i -= lowbit( i ) ) for( int j = y; j > 0; j -= lowbit( j ) ) for( int k = z; k > 0; k -= lowbit(k)) sum += dx[i][j][k]; return sum; } int main( ) { int N, M, flag; while ( scanf("%d%d", &N, &M) != EOF ) { int x1, y1, z1, x2, y2, z2; memset(dx, 0, sizeof(dx)); for( int i = 1; i <= M; i++) { scanf("%d", &flag); if ( flag ) { scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2,&y2, &z2); update(x2+1, y2+1, z2+1, 1); update(x1, y2+1, z2+1, 1); update(x2+1, y1, z2+1, 1); update(x2+1, y2+1, z1, 1); update(x1, y1, z2+1, 1); update(x2+1, y1, z1, 1); update(x1, y2+1, z1, 1); update(x1, y1, z1, 1); } else { scanf("%d%d%d", &x1, &y1, &z1); flag = sum(x1, y1, z1); printf("%d\n", flag&1); } } } return 0; }
1001
View Code
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXN 10010 #define nt long long nt dx[10010], dt[3000010]; int lowbit( int x) { return x&-x; } void add( int x, int N) { while( x < N ) { dx[x] += 1; x += lowbit( x ); } } int sum( int x ) { int sum = 0; while( x >= 1 ) { sum += dx[x]; x -= lowbit( x ); } return sum; } int main( ) { int N, M, a, b; nt xsum; char str[1000]; while ( scanf("%d", &N) != EOF ) { xsum = 0; memset(dx, 0, sizeof(dx)); memset(dt, 0, sizeof(dt)); for( int i = 0; i < N; i++) { scanf("%d", &dt[i]); add(dt[i] + 1, MAXN); xsum += sum(dt[i]); } scanf("%d", &M); for( int i = 1; i <= M; i++) { scanf("%s", str); if ( str[0] == 'R') { scanf("%d%d", &a, &b); int temp = dt[a]; for( int j = a + 1; j <= b; j++) { dt[j - 1] = dt[j]; if( dt[j] > temp ) xsum--; else if ( dt[j] < temp ) xsum++; } dt[b] = temp; } else { printf("%I64d\n", xsum); } } } return 0; }
16.67%(2/12) |
posted on 2012-07-14 11:39 more think, more gains 阅读(179) 评论(0) 编辑 收藏 举报