题目有了一些变化:查询区间的总和;将区间内的值都修改为指定值。
因此可以对之前的代码进行修改,当修改的时候,修改所有被影响到的节点。但是这样做会TLE,题目中给出了提示,修改的时候,如果搜到了符合条件的区间,本应该继续向下修改,但是我们不往下搜了,用一个lazytag来标记这个节点,等到以后要用它的子节点的时候,再用lazytag更新左右孩子节点,这样就会节省时间。
TLE代码:
#include <stdio.h>
int ST[2097152];//最多2^21-1个节点,注意不是2*N-1个节点,[0]不用
int N, A, B;
int Query(){
int l, r, sum;
scanf("%d%d", &l, &r);
l = l + A - 1;
r = r + A - 1;
sum = 0;
while(1){
if(l == r){
sum += ST[l];
break;
}
if(l&1){//若l是奇数
sum += ST[l];
l++;
}
if((r&1) == 0){//若r是偶数
sum += ST[r];
r--;
}
l >>= 1;
r >>= 1;
}
return sum;
}
void Update(){
int l, r, v, i;
scanf("%d%d%d", &l, &r, &v);
l = l + A - 1;
r = r + A - 1;
for(i = l; i <= r; i++){
ST[i] = v;
}
l >>= 1;
r >>= 1;
while(l){
for(i = l; i <= r; i++){
ST[i] = ST[l << 1] + ST[(l << 1) + 1];
}
l >>= 1;
r >>= 1;
}
}
int main(){
int Q, i, ans, a;
scanf("%d", &N);
for(A = 1; A < N; A <<= 1);
for(i = A; i < A+N; i++){
scanf("%d", &ST[i]);
}
for(i = A+N, B = A << 1; i < B; i++){
ST[i] = 0;
}
//build segment tree
for(i = A-1; i; i--){
ST[i] = ST[i << 1] + ST[(i << 1) + 1];
}
scanf("%d", &Q);
while(Q--){
scanf("%d", &a);
if(a == 0){
printf("%d\n", Query());
}else{
Update();
}
}
return 0;
}
AC代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 100000
typedef struct NODE{
struct NODE *left, *right;
int value, lazytag;
}Node;
Node* Creat(int i, int j){
Node *p = (Node*)malloc(sizeof(Node));
if(i == j){
p -> left = NULL;
p -> right = NULL;
scanf("%d", & p -> value);
}else{
p -> left = Creat(i, (i+j)/2);
p -> right = Creat((i+j)/2+1, j);
p -> value = p -> left -> value + p -> right -> value;
}
p -> lazytag = -1;
return p;
}
//深度搜索并更新
void Adjust(Node *p, int l, int r, int i, int j, int v){//搜索节点,节点区间,修改区间,修改值
if(l == i && r == j){
p -> lazytag = v;
p -> value = (r - l + 1) * v;
return;
}
int mid = (l+r)/2, temp;
if(p -> lazytag != -1){//检查并处理lazytag
p -> left -> lazytag = p -> lazytag;
p -> left -> value = p -> lazytag * (mid - l + 1);
p -> right -> lazytag = p -> lazytag;
p -> right -> value = p -> lazytag * (r - mid);
p -> lazytag = -1;
}
if(j <= mid){
Adjust(p -> left, l, mid, i, j, v);
}else if(i > mid){
Adjust(p -> right, mid+1, r, i, j, v);
}else{
Adjust(p -> left, l, mid, i, mid, v);
Adjust(p -> right, mid+1, r, mid+1, j, v);
}
p -> value = p -> left -> value + p -> right -> value;
}
int Query(Node* p, int l, int r, int i, int j){//查询的节点,节点代表的区间,查询的区间
if(i == l && j == r){
return p -> value;
}
int mid = (l + r)/2;
if(p -> lazytag != -1){//检查并处理lazytag
p -> left -> lazytag = p -> lazytag;
p -> left -> value = p -> lazytag * (mid - l + 1);
p -> right -> lazytag = p -> lazytag;
p -> right -> value = p -> lazytag * (r - mid);
p -> lazytag = -1;
}
if(j <= mid){
return Query(p -> left, l, mid, i, j);
}
if(i >= mid + 1){
return Query(p -> right, mid+1, r, i, j);
}
return Query(p -> left, l, mid, i, mid) + Query(p -> right, mid+1, r, mid + 1, j);
}
int main(){
int n, i, Q, a, b, c, d;
Node *root;
scanf("%d", &n);
root = Creat(1, n);
scanf("%d", &Q);
while(Q--){
scanf("%d%d%d", &a, &b, &c);
if(a == 0){
printf("%d\n", Query(root, 1, n, b, c));
}else{
scanf("%d", &d);
Adjust(root, 1, n, b, c, d);
}
}
return 0;
}