NOI模拟 颜色 - 带修莫队/树套树
题意:
一个颜色序列,\(a_1, a_2, ...a_i\)表示第i个的颜色,给出每种颜色的美丽度\(w_i\),定义一段颜色的美丽值为该段颜色的美丽值之和(重复的只计算一次),每次都会修改某个位置的颜色或者查询l到r之间的美丽值。
分析:
带修改莫队:在所有询问中多记录一个时间,每次跳转询问前,处理当前时间(上一次操作所在的时间)到目的时间(本次询问所在时间)中的所有修改操作,如果时间是倒退的,就将值改回来,否则就更改值,并且如果修改的位置不在当前的莫队指针之间就直接修改,否则就先删除再添加。
树套树留坑。
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<ctime>
#include<vector>
#include<queue>
using namespace std;
namespace IO{
template<typename T>
inline void read(T &x){
T i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
x = i * f;
}
template<typename T>
inline void wr(T x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO;
const int N = 1e5 + 50, M = 1e5 + 50;
typedef long long ll;
int n, head, tail, cur, cnt[N], m, val[N], S;
int col[N], cc[N], typ[M], last[M], a[M], b[M];
long long ans[M], now;
struct node{
int l, r, bl, br, id;
inline bool operator < (const node &p) const{
if(bl != p.bl) return bl < p.bl;
if(br != p.br) return br < p.br;
return id < p.id;
}
node(){}
node(int bb, int c, int d, int o, int e):l(bb), r(c), bl(d), br(o), id(e){}
}qry[M];
int q;
bool sta[N];
inline void modify(int k, int v){
if(!sta[k]) col[k] = v;
else {
cnt[col[k]]--;
if(!cnt[col[k]]) now -= val[col[k]];
col[k] = v;
cnt[col[k]]++;
if(!(cnt[col[k]] - 1)) now += val[col[k]];
}
}
inline void change(int curT, int tarT){
while(curT < tarT){
curT++;
if(typ[curT] == 1) modify(a[curT], b[curT]);
}
while(curT > tarT){
if(typ[curT] == 1) modify(a[curT], last[curT]);
curT--;
}
}
inline void go(int l, int r){
while(tail < r){
tail++;
sta[tail] ^= 1;
cnt[col[tail]]++;
if(!(cnt[col[tail]] - 1)) now += 1ll*val[col[tail]];
}
while(tail > r){
cnt[col[tail]]--;
sta[tail] ^= 1;
if(!cnt[col[tail]]) now -= 1ll*val[col[tail]];
tail--;
}
while(head < l){
cnt[col[head]]--;
sta[head] ^= 1;
if(!cnt[col[head]]) now -= 1ll*val[col[head]];
head++;
}
while(head > l){
head--;
sta[head] ^= 1;
cnt[col[head]]++;
if(!(cnt[col[head]] - 1)) now += 1ll*val[col[head]];
}
}
int main(){
freopen("color.in", "r", stdin);
// freopen("color.out", "w", stdout);
// int tt = clock();
read(n), read(m);
S = pow(n, 2.1 / 3);
for(int i = 1; i <= n; i++) read(col[i]), cc[i] = col[i];
for(int i = 1; i <= n; i++) read(val[i]);
for(int i = 1; i <= m; i++){
int x, y;
read(typ[i]), read(x), read(y);
a[i] = x, b[i] = y;
if(typ[i] == 1){
last[i] = cc[x];
cc[x] = y;
}
else {
qry[++q] = node(x, y, x / S + (x % S ? 1 : 0), y / S + (y % S ? 1 : 0), i);
}
}
sort(qry + 1, qry + q + 1);
qry[0].id = 0;
head = tail = now = 0;
for(int i = 1; i <= q; i++){
int l = qry[i].l, r = qry[i].r;
change(qry[i - 1].id, qry[i].id);
go(l, r);
ans[qry[i].id] = now;
}
for(int i = 1; i <= m; i++)
if(typ[i] == 2) wr(ans[i]), putchar('\n');
// cout<<clock() - tt<<endl;
return 0;
}
NOI模拟 颜色 - 带修莫队/树套树的更多相关文章
- bzoj 2120 数颜色 (带修莫队)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2120 题意:两种操作:Q 询问区间 l - r 内颜色的种类 ,R 单点修改 思路 ...
- BZOJ 2120 数颜色 (带修莫队)
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 6367 Solved: 2537[Submit][Status][Discuss] ...
- bzoj2120 数颜色——带修莫队
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 带修改的莫队: 用结构体存下修改和询问,排好序保证时间后就全局移动修改即可: 参考了T ...
- 【bzoj2120】数颜色 带修莫队
数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画 ...
- bzoj2120: 数颜色 带修莫队
墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...
- luogu4074 [WC2013]糖果公园(树上带修莫队)
link 题目大意:给一个树,树上每个点都有一种颜色,每个颜色都有一个收益 每次修改一个点上的颜色 或询问一条链上所有颜色第i次遇到颜色j可以获得w[i]*v[j]的价值,求链上价值和 题解:树上带修 ...
- 「洛谷1903」「BZOJ2120」「国家集训队」数颜色【带修莫队,树套树】
题目链接 [BZOJ传送门] [洛谷传送门] 题目大意 单点修改,区间查询有多少种数字. 解法1--树套树 可以直接暴力树套树,我比较懒,不想写. 稍微口胡一下,可以直接来一个树状数组套主席树,也就是 ...
- 【BZOJ2120】数颜色(带修莫队)
点此看题面 大致题意:告诉你\(n\)只蜡笔的颜色,有两种操作:第一种操作将第\(x\)只蜡笔颜色改成\(y\),第二种操作询问区间\([l,r]\)内有多少种颜色的蜡笔. 考虑普通莫队 这题目第一眼 ...
- P1903 [国家集训队]数颜色 / 维护队列(带修莫队)
题目描述: 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. ...
随机推荐
- java list 容器的ConcurrentModificationException
java中的很多容器在遍历的同时进行修改里面的元素都会ConcurrentModificationException,包括多线程情况和单线程的情况.多线程的情况就用说了,单线程出现这个异常一般是遍历( ...
- Kaggle实战分类问题2
Kaggle实战之二分类问题 0. 前言 1. MNIST 数据集 2. 二分类器 3. 效果评测 4. 多分类器与误差分析 5. Kaggle 实战 0. 前言 “尽管新技术新算法层出不穷,但是掌握 ...
- 【原创】基于pyautogui进行自动化测试
前期准备: python3.6 pyautogui pywinauto 以下代码实现内容: 1.打开记事本 2.记事本中输入This is a test 3.保存内容 4.退出进程 import py ...
- Day2:购物车小程序
一.购物车小程序第一版 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan product_list = [ (&quo ...
- 【Codeforces Round #301 (Div. 2) D】 Bad Luck Island
[链接] 我是链接,点我呀:) [题意] 剪刀.石头.布各有r,s,p个生活在同一个村子里. 它们两两之间相遇的几率都相同(相遇后就会按照划拳的规则判断输赢,输的人就死掉了). 问你最后只剩下剪刀,只 ...
- [D3] Modify DOM Elements with D3 v4
Once you can get hold of DOM elements you’re ready to start changing them. Whether it’s changing col ...
- pat 2-05. 求集合数据的均方差(水题)
代码: #include<cstdio> #include<iostream> #include<cmath> using namespace std; doubl ...
- 【习题 5-7 UVA - 12100】Printer Queue
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用队列和multiset就能完成模拟 [代码] #include <bits/stdc++.h> using names ...
- 【z09】关押罪犯
描述 S城现有两座监狱,一共关押着N名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"(一个正整 ...
- 【微信小程序】自定义模态框实例
原文链接:https://mp.weixin.qq.com/s/23wPVFUGY-lsTiQBtUdhXA 1 概述 由于官方API提供的显示模态弹窗,只能简单地显示文字内容,不能对对话框内容进行自 ...