DZY Loves Colors
题面翻译
- 有一个 \(n\) 个元素组成的序列,每个元素有两个属性:颜色 \(c_i\) 和权值 \(w_i\)。\(c_i\) 初始为 \(i\),\(w_i\) 初始为 \(0\)。\(m\) 次操作,操作有两种:
1 l r x
:对 \(i\in [l,r]\) 的所有 \(i\) 进行如下操作:设第 \(i\) 个元素 原来 的颜色为 \(y\),您要把第 \(i\) 个元素的颜色改为 \(x\),权值 增加 \(|y-x|\)。2 l r
:求 \(\displaystyle\sum_{i=l}^r w_i\)。
- \(1\le n,m\le 10^5\),\(1\le x\le 10^8\)。
输入格式
The first line contains two space-separated integers $ n,m (1<=n,m<=10^{5}) $ .
Each of the next $ m $ lines begins with a integer $ type (1<=type<=2) $ , which represents the type of this operation.
If $ type=1 $ , there will be $ 3 $ more integers $ l,r,x (1<=l<=r<=n; 1<=x<=10^{8}) $ in this line, describing an operation $ 1 $ .
If $ type=2 $ , there will be $ 2 $ more integers $ l,r (1<=l<=r<=n) $ in this line, describing an operation $ 2 $ .
输出格式
For each operation $ 2 $ , print a line containing the answer — sum of colorfulness.
样例 #1
样例输入 #1
3 3
1 1 2 4
1 2 3 5
2 1 3
样例输出 #1
8
样例 #2
样例输入 #2
3 4
1 1 3 4
2 1 1
2 2 2
2 3 3
样例输出 #2
3
2
1
样例 #3
样例输入 #3
10 6
1 1 5 3
1 2 7 9
1 10 10 11
1 3 8 12
1 1 10 3
2 1 10
样例输出 #3
129
分析:
修改涉及到绝对值,可以通过判断区间内的数是否全部相等再进行区间操作;
通过懒标记 same 判断该区间的所有颜色是否都相等,
· 相等时就可以将懒标记下传,更新add;
实现:
#include <bits/stdc++.h>
using namespace std;
#define mst(x, y) memset(x, y, sizeof x)
#define endl '\n'
#define INF LONG_LONG_MAX
#define int long long
#define Lson u << 1, l, mid
#define Rson u << 1 | 1, mid + 1, r
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const int N = 500010, MOD = 1e9 + 7;
const double EPS = 1e-6;
typedef pair<int, int> PII;
typedef unordered_map<int, int> Ump;
int T;
int n, m;
int op;
struct Node
{
int l, r;
int sum, same, add;
} tr[N << 2];
void pushup(Node &u, Node &l, Node &r)
{
u.sum = l.sum + r.sum;
if (l.same == r.same)
u.same = l.same;
else
u.same = 0;
}
void pushup(int u)
{
pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}
void pushdown(int u)
{
if (tr[u].same)
{
tr[u << 1].sum += (tr[u << 1].r - tr[u << 1].l + 1) * tr[u].add;
tr[u << 1 | 1].sum += (tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1) * tr[u].add;
tr[u << 1].add += tr[u].add;
tr[u << 1 | 1].add += tr[u].add;
tr[u << 1].same = tr[u].same;
tr[u << 1 | 1].same = tr[u].same;
tr[u].add = 0;
}
}
void build(int u, int l, int r)
{
if (l == r)
tr[u] = {r, r, 0, l, 0};
else
{
tr[u] = {l, r};
int mid = l + r >> 1;
build(Lson), build(Rson);
pushup(u);
}
}
void modify(int u, int l, int r, int k)
{
if (tr[u].l >= l && tr[u].r <= r && tr[u].same)
{
tr[u].sum += (tr[u].r - tr[u].l + 1) * abs(k - tr[u].same);
tr[u].add += abs(k - tr[u].same);
tr[u].same = k;
}
else
{
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid)
modify(u << 1, l, r, k);
if (r > mid)
modify(u << 1 | 1, l, r, k);
pushup(u);
}
}
int query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r)
return tr[u].sum;
else
{
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
int res = 0;
if (l <= mid)
res = query(u << 1, l, r);
if (r > mid)
res += query(u << 1 | 1, l, r);
return res;
}
}
void solve()
{
cin >> n >> m;
build(1, 1, n);
while (m--)
{
int op;
cin >> op;
if (op == 1)
{
int l, r, x;
cin >> l >> r >> x;
modify(1, l, r, x);
}
else if (op == 2)
{
int l, r;
cin >> l >> r;
cout << query(1, l, r) << endl;
}
}
}
signed main()
{
FAST;
T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}
标签:10,le,int,样例,Colors,Loves,DZY,op,define
From: https://www.cnblogs.com/Aidan347/p/17238345.html