// Problem: E. Colorful Operations
// Contest: Codeforces - Codeforces Round #771 (Div. 2)
// URL: https://codeforces.com/contest/1638/problem/E
// Memory Limit: 256 MB
// Time Limit: 4000 ms
// 2022-02-16 23:29:55
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,l,r) for(int i=(l);i>=(r);i--)
#define ll long long
#define pii pair<int, int>
#define mset(s,t) memset(s,t,sizeof(t))
#define mcpy(s,t) memcpy(s,t,sizeof(t))
#define fir first
#define pb push_back
#define sec second
#define sortall(x) sort((x).begin(),(x).end())
inline int read () {
int x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= (ch=='-'),ch= getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return f?-x:x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x/10);
putchar(x % 10 + '0');
}
#define int long long
int n;
ll Tag[1000005];
class BIT {
public :
ll data[1000005];
int lowbit (int x) {
return x & (-x);
}
void Add (int pos, ll val) {
while (pos <= n) {
data[pos] += val;
pos += lowbit(pos);
}
}
void modify (int L, int R, ll val) {
Add (L, val);
Add (R + 1, -val);
}
ll query (int pos) {
ll res = 0;
while (pos) {
res += data[pos];
pos -= lowbit(pos);
}
return res;
}
}T;
struct Node {
int L, R, col;
};
set<Node> st;
bool operator < (const Node &a,const Node &b ) {
if (a.L==b.L) return a.R < b.R;
return a.L < b.L;
}
void Modify (int L, int R, int col) {
auto It = --st.upper_bound((Node){L + 1, 0, 0});//找交集
while (It != st.end() && It->L <= R) {
if (It->R < L) {
It ++;
continue;
}
auto temp = It;
temp ++;
if (It->L < L) {
st.insert(Node{It->L, L - 1, It->col});
}
if (It->R >R) {
st.insert(Node{R + 1, It->R, It->col});
}
T.modify (max(It->L, L), min (It->R, R), Tag[It->col]-Tag[col]);
//把将要目标颜色减去然后加上当前颜色。
st.erase(It);
It = temp;
}
//把所有贡献算完之后再加进来
st.insert(Node{L, R, col});
}//分割区间
ll query (ll pos) {
auto It = --st.upper_bound(Node{pos + 1, 0, 0});
if (It->R < pos)
return T.query(pos);
return Tag[It->col] + T.query(pos);
//逆向操作
}
void solve() {
char s[20];
int q;
cin >> n >> q;
st.insert(Node{0, 0, 0});
st.insert(Node{1, n, 1});
st.insert(Node{n + 1, n + 1, 0});
while (q --) {
scanf("%s", s);
if (s[0] == 'C') {
ll l, r;
ll c;
scanf("%lld%lld%lld", & l, & r, & c);
Modify (l, r, c);
}
else if (s[0] == 'Q') {
ll pos;
scanf("%lld", & pos);
printf("%lld\n", query(pos));
}
else if (s[0] == 'A') {
ll pos, num;
scanf("%lld%lld", & pos, & num);
Tag[pos] += num;
}
}
}
signed main () {
int t;
t = 1;
while (t --) solve();
}
珂朵莉树 就是set,每个元素包含左端年,右端点,以及颜色的信息。 然后在线维护L,R的信息。 对于插入的L,R,要找出所有相交的区间,然后进行分割。最后再将这个区间插入 对于值的维护,我们用树状数组来维护。