Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
WA了N回了..TT..哪位高手能帮看看啊..感激不尽..#include <stdio.h> #include <iostream> using namespace std; const int N = 100600; long n, m; long num[N]; long long sum[N]; long len; struct SegTree { long long sum; long increment; long left, right; SegTree * Lchild, * Rchild; void Build(long, long); void Add(long, long, long); long long Query(long, long); }Tree[2 * N], * Root = Tree; void SegTree::Build(long l, long r) { left = l; right = r; if(l + 1 == r) { Lchild = &Tree[++len]; Rchild = &Tree[++len]; Lchild -> Build(l, l); Rchild -> Build(r, r); return; } if(l == r) { Lchild = Rchild = NULL; return; } int mid = (l + r) / 2; Lchild = &Tree[++len]; Rchild = &Tree[++len]; Lchild -> Build(l, mid); Rchild -> Build(mid + 1, r); } long long SegTree::Query(long s, long t) { if(increment) { if(Lchild) { Lchild -> increment = increment; Lchild -> sum += (Lchild -> right - Lchild -> left + 1) * increment; } if(Rchild) { Rchild -> increment = increment; Rchild -> sum += (Rchild -> right - Rchild -> left + 1) * increment; } increment = 0; } if(left == s && right == t) return sum; int mid = (left + right) / 2; if(t <= mid) return Lchild -> Query(s, t); if(s >= mid + 1) return Rchild -> Query(s, t); return Lchild -> Query(s, mid) + Rchild -> Query(mid + 1, t); } void SegTree::Add(long s, long t, long k) { if(left == s && right == t) { increment = k; sum += (t - s + 1) * k; return; } long mid = (left + right) / 2; if(t <= mid) { Lchild -> Add(s, t, k); sum = Lchild -> sum + Rchild -> sum; return; } if(s >= mid + 1) { Rchild -> Add(s, t ,k); sum = Lchild -> sum + Rchild -> sum; return; } Lchild -> Add(s, mid, k); Rchild -> Add(mid + 1, t, k); sum = Lchild -> sum + Rchild -> sum; } void read() { //freopen("p3468.in", "r", stdin); scanf("%ld %ld", &n, &m); for(int i = 1; i <= n; i++) { scanf("%ld", &num[i]); sum[i] = sum[i - 1] + num[i]; } } int main() { read(); Root -> Build(1, n); long s, t, k; while(m) switch(getchar()) { case 'Q' : m--; scanf("%ld %ld", &s, &t); printf("%I64d\n", Root -> Query(s, t) + sum[t] - sum[s - 1]); break; case 'C' : m--; scanf("%ld %ld %ld", &s, &t, &k); Root -> Add(s, t, k); break; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator