线段树区间修改

Setv[]上来要全弄成负的。query里的sum别忘了加一。

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cmath>
  6 using namespace std;
  7 const int maxn = 100000 + 10;
  8 const int maxn3 = maxn * 3;
  9 const int INF = -1u >> 1;
 10 int A[maxn], Minv[maxn3], Maxv[maxn3], Sumv[maxn3], Setv[maxn3];
 11 int v, ql, qr, _min, _max, _sum;
 12 int n, Q;
 13 char tp;
 14 void fresh(int o, int lc, int rc){
 15     Minv[o] = min(Minv[lc], Minv[rc]);
 16     Maxv[o] = max(Maxv[lc], Maxv[rc]);
 17     Sumv[o] = Sumv[lc] + Sumv[rc];
 18     return ;
 19 }
 20 void build(int o, int L, int R){
 21     if(L == R) Minv[o] = Maxv[o] = Sumv[o] = A[L];
 22     else{
 23         int M = L + R >> 1, lc = o << 1, rc = lc | 1;
 24         build(lc, L, M); build(rc, M + 1, R); fresh(o, lc, rc);
 25     }
 26     return ;
 27 }
 28 void pushdown(int o){
 29     if(Setv[o] >= 0){
 30         int lc = o << 1, rc = lc | 1;
 31         Setv[lc] = Setv[rc] = Setv[o];
 32         Setv[o] = -1;
 33     }
 34     return ;
 35 }
 36 void maintain(int o, int L, int R){
 37     if(Setv[o] >= 0){
 38         Minv[o] = Maxv[o] = Setv[o];
 39         Sumv[o] = (R - L + 1) * Setv[o];
 40     }
 41     else if(L < R) fresh(o, o << 1, (o << 1) | 1);
 42     return ;
 43 }
 44 void update(int o, int L, int R){
 45     if(ql <= L && R <= qr) Setv[o] = v;
 46     else{
 47         int M = L + R >> 1, lc = o << 1, rc = lc | 1;
 48         pushdown(o);
 49         if(ql <= M) update(lc, L, M); else maintain(lc, L, M);
 50         if(qr > M) update(rc, M + 1, R); else maintain(rc, M + 1, R);
 51     }
 52     maintain(o, L, R); return ;
 53 }
 54 void query(int o, int L, int R){
 55     if(Setv[o] >= 0){
 56         _min = min(_min, Setv[o]);
 57         _max = max(_max, Setv[o]);
 58         _sum += (min(R, qr) - max(L, ql) + 1) * Setv[o];
 59     }
 60     else if(ql <= L && R <= qr){
 61         _min = min(_min, Minv[o]);
 62         _max = max(_max, Maxv[o]);
 63         _sum += Sumv[o];
 64     }
 65     else{
 66         int M = L + R >> 1, lc = o << 1, rc = lc | 1;
 67         if(ql <= M) query(lc, L, M);
 68         if(qr > M) query(rc, M + 1, R);
 69     }
 70     return ;
 71 }
 72 void read(int &x){
 73     x = 0; int sig = 1; char ch = getchar();
 74     while(!isdigit(ch)) { if(ch == ‘-‘) sig = -1; ch = getchar(); }
 75     while(isdigit(ch)) x = 10 * x + ch - ‘0‘, ch = getchar();
 76     x *= sig; return ;
 77 }
 78 void read(char& x){
 79     x = getchar();
 80     while(!isalpha(x)) x = getchar();
 81     return ;
 82 }
 83 void init(){
 84     memset(Setv, -1, sizeof(Setv)); // !
 85     read(n);
 86     for(int i = 1; i <= n; i ++) read(A[i]);
 87     build(1, 1, n);
 88     read(Q);
 89     return ;
 90 }
 91 void work(){
 92     while(Q--)
 93     {
 94         char tp=getchar();
 95         while(!isalpha(tp)) tp=getchar();
 96         read(ql),read(qr);
 97         if(ql>qr) swap(ql,qr);
 98         if(tp==‘Q‘)
 99         {
100             _sum=0; _min=INF; _max=-INF;
101             query(1,1,n);
102             printf("MaxNum: %d, MinNum: %d, Sum: %d\n",_max,_min,_sum);
103         }
104         else read(v),update(1,1,n);
105     }
106     return ;
107 }
108 void print(){
109
110     return ;
111 }
112 int main(){
113     init();
114     work();
115     print();
116     return 0;
117 }
时间: 2024-08-08 09:30:46

线段树区间修改的相关文章

poj 2777 Count Color(线段树区间修改)

题目链接:http://poj.org/problem?id=2777 题目意思:就是问你在询问的区间里有几种不同的颜色 思路:这题和一般的区间修改差不多,但是唯一不同的就是我们要怎么计算有种颜色,所以这时候我们就需要把延时标记赋予不同的意义,当某段区间有多种颜色时就赋值为-1,当为一种颜色时就把它赋值为这个颜色的号数.这儿我们要怎么统计询问区间不同的颜色数叻,为了不重复计算同一种颜色,那么我们就需要用一个数组来标记计算过的颜色,当我们下次遇到时就不需要再次计算了.... 代码核心处就在计数那儿

hdu-5023 A Corrupt Mayor&#39;s Performance Art (线段树区间修改)

今天集训队打比赛的一道题,很明显是个线段树,我们队照着lrj蓝书敲了一通,机智的将修改值和加和改成了位运算:|= 但是好像哪里出了点小问题,就是不对,赛后又水了一遍,竟然过了...发现还是lrj的书好啊,市面上的模板一点也不好用,连区间修改都没有 . 等集训完了要静心好好系统的学习一下线段树 . 多看多刷lrj的书 . 细节参见代码: #include<bits/stdc++.h> using namespace std; const int maxn = 1000000 + 5; int n

【线段树区间修改】fzu2105Digits Count

/* 题意: 给出数组A,有以下几个操作: 1: AND(opn, L, R):把区间[L, R]中的元素A[i]改为A[i] & opn;;;;;; 2: OR(opn, L, R) :把区间[L, R]中的元素A[i]改为A[i] | opn;;;;;;; 3: XOR(opn, L, R):把区间[L, R]中的元素A[i]改为A[i] ^ opn;;;;;;; 4: SUM(L, R) :对区间[L, R]中的元素求和:::: ------------------------------

Wikilo 1191线段树区间修改单点查询

这题也算比较容易的了. 如果哪个区间已经没有黑色的话,就不用update了,就是因为这个原因WA了2发,唉-- #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #incl

线段树区间修改模板

本来打算把大白书第三章一口气攻下来的,但是这个线段树也是卡了好久. 不敢过题太快,怕自己走马观花到头来结果什么都不会. 可也不能再拖了,在做题中也许有更多的体会. 模板一:1 L R v 表示区间[L, R]所有元素都加上v2 L R   表示查询区间[L, R]的sum, min, maxsumv[o]的定义为:如果只执行节点o及其子孙节点的中的add操作,节点o对应区间中所有数之和 1 //线段树区间修改 2 //1 L R v 表示区间[L, R]所有元素都加上v 3 //2 L R 表示

HDU - 1698 Just a Hook (线段树区间修改)

Description In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length. Now Pudge wants to do some operations on the hook.

线段树区间修改 P3372 【模板】线段树 1

题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. 第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值. 接下来M行每行包含3或4个整数,表示一个操作,具体如下: 操作1: 格式:1 x y k 含义:将区间[x,y]内每个数加上k 操作2: 格式:2 x y 含义:输出区间[x,y]内每个数的和 输出格式: 输出包含若干行整

UVA11992 - Fast Matrix Operations ( 线段树 + 区间修改 + 好题 )

UVA11992 - Fast Matrix Operations ( 线段树 + 区间修改 + 好题 ) 这是大白书上的例题,一直放着没有去A掉,这是一道线段树区间修改的好题. 线段树中需要维护三个域 ,max, min, sum,也就是区间最大值,最小值,区间和 题目大意: r 行 c 列 的全0矩阵,支持三个操作 1 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素增加v 2 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素设为v 3 x1 y1

hdu 4902 Nice boat(线段树区间修改,输出最终序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his peopl

杭电 1698 Just a Hook(线段树区间修改)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17322    Accepted Submission(s): 8640 Problem Description In the game of DotA, Pudge's