题意:

一个颜色序列,\(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模拟 颜色 - 带修莫队/树套树的更多相关文章

  1. bzoj 2120 数颜色 (带修莫队)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2120 题意:两种操作:Q 询问区间  l - r  内颜色的种类 ,R 单点修改 思路 ...

  2. BZOJ 2120 数颜色 (带修莫队)

    2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit: 6367  Solved: 2537[Submit][Status][Discuss] ...

  3. bzoj2120 数颜色——带修莫队

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 带修改的莫队: 用结构体存下修改和询问,排好序保证时间后就全局移动修改即可: 参考了T ...

  4. 【bzoj2120】数颜色 带修莫队

    数颜色 题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画 ...

  5. bzoj2120: 数颜色 带修莫队

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2. R P ...

  6. luogu4074 [WC2013]糖果公园(树上带修莫队)

    link 题目大意:给一个树,树上每个点都有一种颜色,每个颜色都有一个收益 每次修改一个点上的颜色 或询问一条链上所有颜色第i次遇到颜色j可以获得w[i]*v[j]的价值,求链上价值和 题解:树上带修 ...

  7. 「洛谷1903」「BZOJ2120」「国家集训队」数颜色【带修莫队,树套树】

    题目链接 [BZOJ传送门] [洛谷传送门] 题目大意 单点修改,区间查询有多少种数字. 解法1--树套树 可以直接暴力树套树,我比较懒,不想写. 稍微口胡一下,可以直接来一个树状数组套主席树,也就是 ...

  8. 【BZOJ2120】数颜色(带修莫队)

    点此看题面 大致题意:告诉你\(n\)只蜡笔的颜色,有两种操作:第一种操作将第\(x\)只蜡笔颜色改成\(y\),第二种操作询问区间\([l,r]\)内有多少种颜色的蜡笔. 考虑普通莫队 这题目第一眼 ...

  9. P1903 [国家集训队]数颜色 / 维护队列(带修莫队)

    题目描述: 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. ...

随机推荐

  1. Express框架是什么

    Express框架是什么 一.总结 1.express框架:基于node.js的web应用框架,可快速搭建一个完整功能的网站,丰富的HTTP工具以及来自Connect框架的中间件随取随用. 二.Exp ...

  2. String.Empty,NULL和""的区别

    String.Empty,NULL和""的区别 string.Empty就相当于"" 一般用于字符串的初始化 比如: string a; Console.Wri ...

  3. ThinkPHP5.0---URL访问

    ThinkPHP 5.0 在没有启用路由的情况下典型的URL访问规则是(采用 PATH_INFO 访问地址): http://serverName/index.php(或者其它应用入口文件)/模块/控 ...

  4. Spring学习总结(7)——applicationContext.xml 配置文详解

    web.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件 ...

  5. RMAN-03002、RMAN-06059

    使用RMAN备份的时候无法正常备份,抛出以下错误: RMAN-03002: failure of backup command at 04/20/2015 18:55:45 RMAN-06059: e ...

  6. js的AJAX请求有关知识总结

    什么是AJAX?AJAX作用是什么? async javascript and xml(异步的javascript和xml) 作用:实现局部刷新 async:我们真实项目中一般使用AJAX从服务器端获 ...

  7. 扩展的方法:es6 安装模块builder

    https://github.com/es-shims/es5-shim/ Image.png 检测浏览器可支持es5,不支持就扩展,做兼容: 扩展的方法: Image.png 取所有对象的键值: o ...

  8. 【例题 6-15 UVA - 10129】Play on Words

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 拓扑大水题 [代码] #include <bits/stdc++.h> using namespace std; con ...

  9. 如何把canvas元素作为网站背景总结详解

    如何把canvas元素作为网站背景总结详解 一.总结 一句话总结:最简单的做法是绝对定位并且z-index属性设置为负数. 1.如何把canvas元素作为网站背景的两种方法? a.设置层级(本例代码就 ...

  10. SIMPLE QUERY几个原则

    1.减少查询对象的数据页(db block)数量. 尽量避免使用 * 用准确的列明减少不必要的一些资源浪费.   2.查看是否使用了index. 索引是SQL性能调优的重要手段,下面几个是有索引不能使 ...