Comet OJ - Contest #14 转转的数据结构题 珂朵莉树+树状数组
题意:有两个操作
操作1:给出n个操作,将区间为l到r的数字改为x
操作2:给出q个操作,输出进行了操作1中的第x到x+y-1操作后的结果
解法:
把询问离线,按照r从小到大排序
每次询问的时候,用珂朵莉树推平区间
求和,这个我们用树状数组维护即可
树状数组求出>=l的和
#include <bits/stdc++.h>
#define IT set<node>::iterator
using namespace std;
typedef long long ll;
const int M = 5e5 + ;
int n, m, q;
ll anss[M], bit[M];
struct node{
int l, r, w;
mutable ll v;
bool operator < (const node &rhs) const {
return l < rhs.l;
}
};
struct note{
int l, r, id;
ll v;
}a[M], b[M];
set <node> s;
void add(int i, ll x) {
if(!i) return;
while(i <= m) {
bit[i] += x;
i += i & (-i);
}
}
ll query(int i) {
ll ans = ;
while(i) {
ans += bit[i];
i -= i & (-i);
}
return ans;
}
IT split(int pos) {
IT it = s.lower_bound(node{pos});
if(it != s.end() && it->l == pos) return it;
it--;
int L = it->l, R = it->r, w = it->w;
ll V = it->v;
s.erase(it);
s.insert(node{L, pos - , w, V});
return s.insert(node{pos, R, w, V}).first;
}
void modify(int l, int r, int w, ll v) {
IT it2 = split(r + ), it1 = split(l), it3 = it1;
for(; it1 != it2; it1++) add(it1->w, -(it1->v) * (it1->r - it1->l + ));
s.erase(it3, it2);
s.insert(node{l, r, w, v});
add(w, v * (ll)(r - l + ));
}
bool cmp(note p, note q) {
return p.r < q.r;
}
int main(){
scanf("%d%d%d", &m, &n, &q);
s.insert(node{, n, , });
for(int i = ; i <= m; i++) scanf("%d%d%lld", &a[i].l, &a[i].r, &a[i].v);
for(int i = ; i <= q; i++) scanf("%d%d", &b[i].l, &b[i].r), b[i].id = i;
sort(b + , b + q + , cmp);
int j = ;
for(int i = ; i <= q; i++) {
while(j <= b[i].r && j <= m) modify(a[j].l, a[j].r, j, a[j].v), j++;
//printf("anss %lld %lld\n", query(n), query(b[i].l - 1));
anss[b[i].id] = query(m) - query(b[i].l - );
}
for(int i = ; i <= q; i++) printf("%lld\n", anss[i]);
return ;
}
Comet OJ - Contest #14 转转的数据结构题 珂朵莉树+树状数组的更多相关文章
- [数据结构]ODT(珂朵莉树)实现及其应用,带图
[数据结构]ODT(珂朵莉树)实现及其应用,带图 本文只发布于博客园,其他地方若出现本文均是盗版 算法引入 需要一种这样的数据结构,需要支持区间的修改,区间不同值的分别操作. 一般的,我们会想到用线段 ...
- Comet OJ - Contest #14
Rank38. 还是比较不满意吧,C卡了太久,E没调出来,D也没空去做了. A 签到题. #include<bits/stdc++.h> using namespace std; #def ...
- Comet OJ - Contest #14题解
Contest14的本质:区间覆盖+Tarjan( A 把距离公式两边平方即可 注意要long long code #include <algorithm> #include <io ...
- Comet OJ - Contest #6 C 一道树题 数学 + 推导
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
- Comet OJ - Contest #4 D求和 思维题
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
- Comet OJ - Contest #11 D isaster 重构树+倍增+dfs序+线段树
发现对于任意一条边,起决定性作用的是节点编号更大的点. 于是,对于每一条边,按照节点编号较大值作为边权,按照最小生成树的方式插入即可. 最后用线段树维护 dfs 序做一个区间查询即可. Code: # ...
- [转]我的数据结构不可能这么可爱!——珂朵莉树(ODT)详解
参考资料: Chtholly Tree (珂朵莉树) (应某毒瘤要求,删除链接,需要者自行去Bilibili搜索) 毒瘤数据结构之珂朵莉树 在全是珂学家的珂谷,你却不知道珂朵莉树?来跟诗乃一起学习珂朵 ...
- Comet OJ - Contest #11 题解&赛后总结
Solution of Comet OJ - Contest #11 A.eon -Problem designed by Starria- 在模 10 意义下,答案变为最大数的最低位(即原数数位的最 ...
- Comet OJ - Contest #4--前缀和
原题:Comet OJ - Contest #4-B https://www.cometoj.com/contest/39/problem/B?problem_id=1577传送门 一开始就想着暴力打 ...
随机推荐
- Data-Structure-Notes
Data Structure Notes Chapter-1 Sorting Algorithm Selection Sorting: /* * Selection Sort */ template& ...
- sqlite 安装与编译
本文简述了SQLite的概念,并详细描述了SQLite在Linux和Windows平台下的编译方法 关于 SQLite SQLite是一个进程内的库,实现了自给自足的.无服务器的.零配置的.事务性的 ...
- 【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221
package y2019.Algorithm.dynamicprogramming.easy; /** * @ProjectName: cutter-point * @Package: y2019. ...
- 解决 Url is blocked: Requests to the local network are not allowed
问题: 我在学习GitLab + Jenkins 自动化部署时,在GitLab的 MyProject => Settings => Integrations中输入完 &qu ...
- 阿里云主机centos7系统创建SWAP区,并启动挂载(适合无SWAP区虚拟化平台)
以root用户登录建立交换区文件: fallocate -l 2G /swapfile /swapfile //赋予仅root用户的权限,确保安全 mkswap /swapfile swapon /s ...
- Android为TV端助力记录EditText.setInputType的坑
如XML中设置android:inputType=”numberDecimal”在Java代码中仅设置setInputType(EditorInfo.TYPE_NUMBER_FLAG_DECIMAL) ...
- echarts Y轴名称显示不全(转载)
转载来源:https://blog.csdn.net/qq8241994/article/details/90720657今天在项目的开发中遇到的一个问题,echarts Y轴左侧的文字太多了,显示不 ...
- day 08作业 预科
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中 lt=[11,22,3 ...
- 二十五、sql中where条件在数据库中提取与应用浅析
问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生命周期,这是一个非常巨大的问题,涵盖了SQL的词法解析.语法解析.权限检查. ...
- githe和github连接,上传
Git入门 如果你完全没有接触过Git,你现在只需要理解通过Git的语法(敲入一些命令)就可以将代码上传到远程的仓库或者下载到本地的仓库(服务器),可知我们此时应该有两个仓库,就是两个放代码的地方,一 ...