当前位置:网站首页>CF1638E. Colorful operations Kodori tree + differential tree array
CF1638E. Colorful operations Kodori tree + differential tree array
2022-07-01 04:36:00 【HeartFireY】
The question
Given length is n n n Sequence , All elements are initially set to 0 0 0. The following three operations are required :
Color(l, r, c): The interval [ l , r ] [l, r] [l,r] Interval coloring is c c c;Add(c, x): Set color to c c c All elements of + x +x +x;Query(i): Query the first i i i The value of the elements .
Ideas
Contains multiple interval assignment operations , First, consider the maintenance sequence coloring of the Kodori tree . First of all, we need to shout ( Fog )
Kodori is the happiest girl in the world !!!

First, create a color sequence and initialize it to 0 0 0, And then for each C o l o r Color Color The operation of , With the petaly tree assign Operate the violent pushing interval . However, it should be noted that when the interval is leveled, the whole e r a s e erase erase fall , It needs to start from iterator_left So let's start walking through iterator_right, Lower the color mark for each section .
Color marking for each point , We maintain a differential tree array , Perform interval addition with differential operation :
add(l, x), add(r, -x);
Then, when querying a single point, directly getsum You can get the final value of this point .
Then summarize the idea of maintenance :
First of all, use the... Of the cadoli tree split Operation obtains interval iterator , Then traverse the intervals and place color marks on each interval , Then erase the whole section and replace it with a new one .
Note that after the replacement is completed , You need to perform a color subtraction operation on the entire tree array range , Avoid repetition when the final answer is accumulated ( The final answer is the result of the tree array query + The color mark obtained by query , If a point is marked repeatedly, it will have the consequence of repeated addition ).
Both the Kodori tree and the tree array are directly pulled boards , So some operations are useless …
Accepted Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
namespace ct{
struct node{
int l, r;
mutable int val;
node (int lpos): l(lpos) {
}
node (int lpos, int rpos, int vall): l(lpos), r(rpos), val(vall) {
}
bool operator< (const node &a) const {
return l < a.l; }
};
set<node> s;
using sit = set<node>::iterator;
inline void init(int l, int r){
s.insert(node(l, r, 1)); }
//^ChthollyTree-SetSplit
sit split(int pos){
sit it = s.lower_bound(node(pos));
if(it != s.end() && it -> l == pos) return it;
--it;
int l = it -> l, r = it -> r, val = it -> val;
s.erase(it);
if(l <= pos - 1) s.insert(node(l, pos - 1, val));
if(r >= pos) return s.insert(node(pos, r, val)).first;
return s.end();
}
//^ChthollyTree-PartValueAssign
void assign(int l, int r, int val){
sit itr = split(r + 1), itl = split(l);
s.erase(itl, itr);
s.insert(node(l, r, val));
}
//^ChthollyTree-PartValueAddition
void add(int l, int r, int val){
sit itr = split(r + 1), itl = split(l);
//for(auto it = itl; it != itr; it++) it -> val += val;
while(itl != itr) itl -> val += val, itl++;
}
//^ChthollyTree-SinglePointQuery
int query(int pos){
auto it = s.lower_bound(pos);
--it;
return it -> val;
}
}//ChthollyTreeTemplate
namespace BIT{
int tree[N], len;
#define lowbit(x) ((x) & (-x))
inline void init(int n){
len = n; }
inline void update(int i, int x){
for(int pos = i; pos <= len; pos += lowbit(pos)) tree[pos] += x;
}
inline int getsum(int i, int ans = 0){
for(int pos = i; pos; pos -= lowbit(pos)) ans += tree[pos];
return ans;
}
inline int query(int l, int r){
return getsum(r) - getsum(l - 1); }
}//BITTemplate
int colcnt[N];
inline void update(int l, int r, int c){
ct::sit itr = ct::split(r + 1), itl = ct::split(l);
for(auto it = itl; it != itr;){
BIT::update(it -> r + 1, -colcnt[it -> val]);
BIT::update(it -> l, colcnt[it -> val]);
auto nxt = next(it);
ct::s.erase(it);
it = nxt;
}
ct::s.insert(ct::node(l, r, c));
BIT::update(r + 1, colcnt[c]);
BIT::update(l, -colcnt[c]);
}
inline int query(int pos, int res = 0){
res = BIT::getsum(pos);
auto it = ct::s.upper_bound(pos);
if(it != ct::s.begin()){
it = prev(it);
if(it -> l <= pos && it -> r >= pos) res += colcnt[it -> val];
}
return res;
}
inline void solve(){
int n, q; cin >> n >> q;
memset(colcnt, 0, sizeof(colcnt));
BIT::init(n);
ct::init(1, n);
while(q--){
string operation; cin >> operation;
if(operation[0] == 'C'){
int l, r, c; cin >> l >> r >> c;
update(l, r, c);
}
else if(operation[0] == 'A'){
int color, cnt; cin >> color >> cnt;
colcnt[color] += cnt;
}
else if(operation[0] == 'Q'){
int pos = 0; cin >> pos;
cout << query(pos) << endl;
}
}
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
solve();
return 0;
}
边栏推荐
- 使用WinMTR软件简单分析跟踪检测网络路由情况
- Threejs opening
- What is uid? What is auth? What is a verifier?
- 2022 t elevator repair question bank and simulation test
- Announcement on the list of Guangdong famous high-tech products to be selected in 2021
- (12) Somersault cloud case (navigation bar highlights follow)
- Unity's 3D multi-point arrow navigation
- Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
- Procurement intelligence is about to break out, and Alipay'3+2'system helps enterprises build core competitive advantages
- 网站服务器:好用的网站服务器怎么选这五方面要关注
猜你喜欢

Knowledge supplement: basic usage of redis based on docker

Odeint et GPU
![[learn C and fly] S1E20: two dimensional array](/img/68/34fad73ff23d3e0719ef364fc60cb5.jpg)
[learn C and fly] S1E20: two dimensional array
![[godot] unity's animator is different from Godot's animplayer](/img/51/48f40a7b6736d7f78040eabbbd3395.jpg)
[godot] unity's animator is different from Godot's animplayer

Internet winter, how to spend three months to make a comeback

2022 gas examination question bank and online simulation examination

Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)

2022 question bank and answers for safety production management personnel of hazardous chemical production units

This may be your last chance to join Tencent
随机推荐
扩展-Fragment
2022年上海市安全员C证考试题模拟考试题库及答案
Offline installation of Wireshark 2.6.10
206. reverse linked list
OSPF notes [multiple access, two multicast addresses with OSPF]
Embedded System Development Notes 80: using QT designer to design the main interface
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
Maixll-Dock 快速上手
Knowledge supplement: basic usage of redis based on docker
Valid @suppresswarnings warning name
软件研发的十大浪费:研发效能的另一面
Maixll dock quick start
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
Embedded System Development Notes 81: Using Dialog component to design prompt dialog box
Embedded System Development Notes 79: why should I get the IP address of the local network card
Task04 | statistiques mathématiques
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
One job hopping up 8K, three times in five years
Do280 management application deployment --rc
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)