多校1007 Naive Operations
思路:好像是第一次这么印象深刻的写线段树,说实话,这个题确实很有意思,值得学习。
看了大神讲解视频,但是自己写的还是超时了。
参考来自 https://blog.csdn.net/yiqzq/article/details/81211652 个人认为可以作为模板
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <cctype>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MOD 1e9+7
#define PI acos(-1)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ; int n, q;
int MIN[maxn << ];//记录区间最小值
int lazy[maxn << ];//延迟标记减法
int ans[maxn << ];//记录答案数量
int b[maxn << ];//记录b数组 //线段树基本操作,pushup和pushdown
void pushup(int rt) {
MIN[rt] = min(MIN[rt << ], MIN[rt << | ]);
ans[rt] = ans[rt << ] + ans[rt << | ];
} void pushdown(int rt) {
if(lazy[rt]) {
lazy[rt << ] += lazy[rt];
lazy[rt << | ] += lazy[rt];
MIN[rt << ] -= lazy[rt];
MIN[rt << | ] -= lazy[rt];
lazy[rt] = ;
}
}
//建树
void build(int l, int r, int rt) {
lazy[rt] = ;
ans[rt] = ;
if(l == r) {
scanf("%d", &b[rt]);
MIN[rt] = b[rt];
return ;
}
int m = (l + r) >> ;
build(lson);
build(rson);
pushup(rt);
}
//区间更新,需要注意当NIN减为0的时候,要ans++并且重新将b的值赋给MIN
void updata(int L, int R, int l, int r, int rt) {
if(L <= l && R >= r) {
MIN[rt]--;
if(MIN[rt]) {
lazy[rt]++;
return;
} else {
if(l == r) {
ans[rt]++;
MIN[rt] = b[rt];
return;
/*血的教训,这个return不能放大括号外面,因为如果递归到某个节点MIN是0
但是又不是叶子节点,那么还是需要继续递归直到找到叶子节点为止
*/
} }
}
pushdown(rt);
int m = (l + r) >> ;
if(L <= m) updata(L, R, lson);
if(R > m) updata(L, R, rson);
pushup(rt);
}
//查询
int query(int L, int R, int l, int r, int rt) {
if(L <= l && R >= r) {
return ans[rt];
}
pushdown(rt);
int m = (l + r) >> ;
int sum = ;
if(m >= L) sum += query(L, R, lson);
if(m < R) sum += query(L, R, rson);
return sum;
}
int main() {
while(~scanf("%d%d", &n, &q)) {
build(, n, );
for(int i = ; i <= q; i++) {
char op[];
int a, b;
scanf("%s%d%d", op, &a, &b);
if(op[] == 'q') {
printf("%d\n", query(a, b, , n, ));
} else {
updata(a, b, , n, );
}
}
}
return ;
}
复制来的代码
#include<queue>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 1e5 + ; int add[maxn << ];
int tree[maxn << ];
int MAX[maxn << ];
int a[maxn]; void build(int l, int r,int rt)
{ add[rt] = ;
MAX[rt] = (int)-1e9;
tree[rt] = ; if (l == r){
add[rt] = ;
tree[rt] = ;
MAX[rt] = -a[l];
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
tree[rt] = tree[rt << ] + tree[rt << | ];
MAX[rt] = max(MAX[rt << ], MAX[rt << | ]);
}
void PushDown(int rt)
{
if (add[rt]){
add[rt << ] += add[rt];
add[rt << | ] += add[rt];
MAX[rt << ] += add[rt];
MAX[rt << | ] += add[rt];
add[rt] = ;
}
}
void update1(int L, int R, int l, int r, int rt)
{
if (L <= l&&r <= R){
add[rt]++;
MAX[rt]++;
return;
}
PushDown(rt);
int m = (l + r) >> ;
if (L <= m)update1(L, R, lson);
if (R > m)update1(L, R, rson);
tree[rt] = tree[rt << ] + tree[rt << | ];
MAX[rt] = max(MAX[rt << ], MAX[rt << | ]);
}
void update2(int L, int R, int l, int r, int rt)
{
if (l == r){ if (MAX[rt] >= ){ tree[rt]++;
MAX[rt] = -a[l];
}
return;
}
PushDown(rt);
int m = (l + r) >> ;
if (L <= m&&MAX[rt << ] >= )update2(L, R, lson);
if (R > m&&MAX[rt << | ] >= )update2(L, R, rson);
tree[rt] = tree[rt << ] + tree[rt << | ];
MAX[rt] = max(MAX[rt << ], MAX[rt << | ]);
}
int query(int L, int R, int l, int r, int rt)
{
if (L <= l&&r <= R){
return tree[rt];
}
int m = (l + r) >> , ans = ;
if (L <= m)ans += query(L, R, lson);
if (R > m)ans += query(L, R, rson);
return ans;
}
void Print(int l, int r, int rt)
{
if (l == r){
cout << rt << " = " << MAX[rt] << endl;
return;
}
cout << rt << " = " << MAX[rt] << endl;
int m = (l + r) >> ;
if (l <= m)Print(lson);
if (r > m)Print(rson);
}
int main()
{
std::ios::sync_with_stdio(false);
int n, q;
while (cin >> n >> q){
for (int i = ; i <= n; i++)
cin >> a[i]; build(, n, ); string flag; int l, r;
for (int i = ; i < q; i++){
cin >> flag >> l >> r; if (flag == "add"){
update1(l, r, , n, );
update2(l, r, , n, );
//Print(1, n, 1);
}
else cout << query(l, r, , n, ) << endl;
}
} return ;
}
多校1007 Naive Operations的更多相关文章
- 2018 杭电多校2 - Naive Operations
题目链接 Problem Description In a galaxy far, far away, there are two integer sequence a and b of length ...
- HDU6315 Naive Operations(多校第二场1007)(线段树)
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- hdu 6315 Naive Operations (2018 Multi-University Training Contest 2 1007)
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- HDU 多校对抗 F Naive Operations
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- 杭电多校第二场 hdu 6315 Naive Operations 线段树变形
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
- HDU 6351 Naive Operations(线段树)
题目: http://acm.hdu.edu.cn/showproblem.php?pid=6315 Naive Operations Time Limit: 6000/3000 MS (Java/O ...
- hdu Naive Operations 线段树
题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...
- HDU-6315:Naive Operations(线段树+思维)
链接:HDU-6315:Naive Operations 题意: In a galaxy far, far away, there are two integer sequence a and b o ...
- HDU 6315: Naive Operations
Naive Operations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Other ...
随机推荐
- A bad vacation
My story happened in the winter of 2012, the first year I began to work in Beijing. It was a cold we ...
- PCB .NET Reactor 加密工具(NecroBit加密技术)
在PCB行业中,我们使用的软件或脚本绝大多数都用非编译型语言写程序,而从一开始选择这种编程语言,就注定了程序的源码有被曝露风险.我们PCB工程系统用.NET语言编写,C#编译后会被转换为IL代码的中间 ...
- 为什么JavaWeb项目要分层
首先让我们坐着时光机回到n年前的web开发.那个时候最早都是静态的html页面,后来有了数据库,有了所谓的动态页面,然后程序猿在编码的时候,会把所有的代码都写在页面上,包括数据库连接,包括事务控制,接 ...
- O(1)的快速乘
那么 有位神仙已经说了O(1)的算法(当然不是我) 这是一种骚操作 直接放代码了啊 inline LL mul(LL a,LL b,LL Mod){ LL lf = a * ( b >> ...
- Y-C
1.asp.net服务控件生命周期 11个生命阶段 (1)初始化: 初始化在传入Web请求生命周期内所需的设置,.跟踪视图状态.页面框架通过默认方式引发Init事件,并调用OnInit()方法,控件开 ...
- Vue知识点小总结1
ES6常用语法 变量的定义 let定义变量 不会变量提升 有全局作用域和函数作用域,块级作用域{} 不能重复定义 var定义变量 会变量提升 只有全局作用域和函数作用域 能够重复定义 const定义变 ...
- Linux系统下强制踢掉登录用户
1,利用who命令,找出用户登录的终端代号 who root pts/0 2017-03-14 22:30 (223.1.1.1) root pts/1 2 ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析
转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...
- Sublime——基本操作
基本安装 程序下载地址:https://www.sublimetext.com/ package control安装 View -> Show Console打开控制台或者用快捷键ctrl+~打 ...
- (转)全文检索技术学习(二)——配置Lucene的开发环境
http://blog.csdn.net/yerenyuan_pku/article/details/72589380 Lucene下载 Lucene是开发全文检索功能的工具包,可从官方网站http: ...