我们维护一个标记表示区间内的数是否全相同即可。
如果全相同很容易算出 a , b a,b a,b 数组需要更新多少,打标记即可。
否则暴力修改。
#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 M = 320 ;
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\n", x + 0.0000000001) ; }
int n, m, len, num ;
int a[N], b[N], bl[N], l[M], r[M], flg[M], flg2[M], sum[M] ;
void build() {
len = sqrt(n) ; num = (n - 1) / len + 1 ;
rep(i, 1, n) bl[i] = (i - 1) / len + 1 ;
rep(i, 1, num) l[i] = (i - 1) * len + 1, r[i] = i * len ;
r[num] = n ;
rep(i, 1, num) flg[i] = -1 ;
}
void down(int x) { // 对块x更新
rep(i, l[x], r[x]) a[i] = flg[x] ;
flg[x] = -1 ;
}
void modify(int x, int y, int val) {
if (bl[x] == bl[y]) { // 暴力修改
if (flg[bl[x]] != -1) down(bl[x]) ;
rep(i, x, y) {
b[i] += abs(a[i] - val) ;
sum[bl[x]] += abs(a[i] - val) ;
a[i] = val ;
}
} else {
if (flg[bl[x]] != -1) down(bl[x]) ;
rep(i, x, r[bl[x]]) {
b[i] += abs(val - a[i]) ;
sum[bl[x]] += abs(val - a[i]) ;
a[i] = val ;
}
rep(i, bl[x] + 1, bl[y] - 1) {
if (flg[i] != -1) {
flg2[i] += abs(flg[i] - val) ;
sum[i] += abs(flg[i] - val) * (r[i] - l[i] + 1) ;
flg[i] = val ;
} else {
rep(j, l[i], r[i]) {
b[j] += abs(val - a[j]) ;
sum[i] += abs(val - a[j]) ;
a[j] = val ;
}
flg[i] = val ;
}
}
if (flg[bl[y]] != -1) down(bl[y]) ;
rep(i, l[bl[y]], y) {
b[i] += abs(a[i] - val) ;
sum[bl[y]] += abs(a[i] - val) ;
a[i] = val ;
}
}
}
int query(int x, int y) {
int ans = 0 ;
if (bl[x] == bl[y]) {
rep(i, x, y) ans += b[i] + flg2[bl[i]] ;
} else {
rep(i, x, r[bl[x]]) ans += b[i] + flg2[bl[i]] ;
rep(i, bl[x] + 1, bl[y] - 1) ans += sum[i] ;
rep(i, l[bl[y]], y) ans += b[i] + flg2[bl[i]] ;
}
return ans ;
}
signed main(){
scanf("%lld%lld", &n, &m) ;
rep(i, 1, n) a[i] = i ;
build() ;
while (m--) {
int op, l, r, val ; scanf("%lld%lld%lld", &op, &l, &r) ;
if (op == 1) {
scanf("%lld", &val) ;
modify(l, r, val) ;
} else {
printf("%lld\n", query(l, r)) ;
}
}
return 0 ;
}
/*
写代码时请注意:
1.ll?数组大小,边界?数据范围?
2.精度?
3.特判?
4.至少做一些
思考提醒:
1.最大值最小->二分?
2.可以贪心么?不行dp可以么
3.可以优化么
4.维护区间用什么数据结构?
5.统计方案是用dp?模了么?
6.逆向思维?
*/