其实是个超傻逼的题目,但是交了20几发,就死在一个写惯了的小错误上
这种题目一看建两个标记就好了,
- (tag1):表示区间加标记
- (tag2):表示区间覆盖标记
那么下传方式很显然:
- 先下传 (tag2),更新 (tag2) 和 (v) 和 (tag1)
- 然后下传 (tag1),更新 (v) 和 (tag1)
就没了
然后我烦了一个很智障的错误:
我以为当 (tag=0) 时就是没有标记,
然后看一眼题目:(|v|<=10^5),一脸懵逼去了
以后写线段树判断都要是 (inf) 才行
#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(#x".in", "r", stdin),freopen(#x".out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 100010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf
", x + 0.0000000001) ; }
int n, m, lst ;
struct SegTree {
int l, r, sz, v, tag1, tag2 ; // 一个求和标记,一个赋值标记
#define ls(x) x << 1
#define rs(x) x << 1 | 1
#define l(x) tr[x].l
#define r(x) tr[x].r
#define sz(x) tr[x].sz
#define v(x) tr[x].v
#define tag1(x) tr[x].tag1
#define tag2(x) tr[x].tag2
} tr[N << 2] ;
void pushup(int x) {
v(x) = v(ls(x)) + v(rs(x)) ;
}
void pushdown(int x) {
if (tag2(x) != iinf) {
tag1(ls(x)) = tag1(rs(x)) = iinf ; // 清空?
tag2(ls(x)) = tag2(rs(x)) = tag2(x) ;
v(ls(x)) = sz(ls(x)) * tag2(x) ;
v(rs(x)) = sz(rs(x)) * tag2(x) ;
tag2(x) = iinf ;
}
if (tag1(x) != iinf) {
tag1(ls(x)) = (tag1(ls(x)) == iinf ? 0 : tag1(ls(x))) + tag1(x) ;
tag1(rs(x)) = (tag1(rs(x)) == iinf ? 0 : tag1(rs(x))) + tag1(x) ;
v(ls(x)) += sz(ls(x)) * tag1(x) ;
v(rs(x)) += sz(rs(x)) * tag1(x) ;
tag1(x) = iinf ;
}
}
void build(int x, int l, int r) {
l(x) = l, r(x) = r ;
sz(x) = r(x) - l(x) + 1 ;
tag1(x) = tag2(x) = iinf ;
if (l == r) {
v(x) = lst ;
return ;
}
int mid = (l + r) >> 1 ;
build(ls(x), l, mid) ;
build(rs(x), mid + 1, r) ;
pushup(x) ;
}
void modify(int x, int l, int r, int c) {
if (l <= l(x) && r(x) <= r) {
tag1(x) = (tag1(x) == iinf ? 0 : tag1(x)) + c ;
v(x) += sz(x) * c ;
return ;
}
pushdown(x) ;
int mid = (l(x) + r(x)) >> 1 ;
if (l <= mid) modify(ls(x), l, r, c) ;
if (mid < r) modify(rs(x), l, r, c) ;
pushup(x) ;
}
void cover(int x, int l, int r, int c) {
if (l <= l(x) && r(x) <= r) {
tag1(x) = iinf ; tag2(x) = c ;
v(x) = c * sz(x) ;
return ;
}
pushdown(x) ;
int mid = (l(x) + r(x)) >> 1 ;
if (l <= mid) cover(ls(x), l, r, c) ;
if (mid < r) cover(rs(x), l, r, c) ;
pushup(x) ;
}
int query(int x, int l, int r) {
if (l <= l(x) && r(x) <= r) return v(x) ;
pushdown(x) ;
int mid = (l(x) + r(x)) >> 1, ans = 0 ;
if (l <= mid) ans += query(ls(x), l, r) ;
if (mid < r) ans += query(rs(x), l, r) ;
pushup(x) ;
return ans ;
}
signed main(){
scanf("%lld%lld%lld", &n, &m, &lst) ;
build(1, 1, n) ;
while (m--) {
int op, x, y, v ; scanf("%lld%lld%lld", &op, &x, &y) ;
if (op == 0) {
scanf("%lld", &v) ;
modify(1, x, y, v) ;
}
else if (op == 1) {
scanf("%lld", &v) ;
cover(1, x, y, v) ;
} else {
printf("%lld
", query(1, x, y)) ;
}
}
return 0 ;
}
/*
写代码时请注意:
1.ll?数组大小,边界?数据范围?
2.精度?
3.特判?
4.至少做一些
思考提醒:
1.最大值最小->二分?
2.可以贪心么?不行dp可以么
3.可以优化么
4.维护区间用什么数据结构?
5.统计方案是用dp?模了么?
6.逆向思维?
*/