多校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 ...
随机推荐
- JForum论坛添加UEditor文本编辑器
在使用JForum论坛中发现论坛自带的文本编辑器不好用,不能上传图片,样式编辑也很麻烦,就想着把这个文本编辑器替换掉,我这里选用的是百度的开源富文本编辑器UEditor 替换后的效果图 替换方法如下 ...
- Java多线程系列四——控制线程执行顺序
假设有线程1/线程2/线程3,线程3必须在线程1/线程2执行完成之后开始执行,有两种方式可实现 Thread类的join方法:使宿主线程阻塞指定时间或者直到寄生线程执行完毕 CountDownLatc ...
- Java多线程系列二——Thread类的方法
Thread实现Runnable接口并实现了大量实用的方法 public static native void yield(); 此方法释放CPU,但并不释放已获得的锁,其它就绪的线程将可能得到执行机 ...
- 两个局域网(办公网-IDC)安全互通方案2:by GRE and linux server&深入理解GRE
(0)gre的turnel的打通 1. 这个过程就是双方建立turnel的过程. (1)局域网路由过程 1.主机A发送一个源为192.168.1.2,目的为10.1.1.2的包 ( ...
- CSS里#和.以及大小写
# 选定ID . 选定class 大小写严格区分,因此选定class和设定class等要一致
- [Swift通天遁地]一、超级工具-(17)自定义的CVCalendar日历
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 282 Expression Add Operators 给表达式添加运算符
给定一个仅包含0-9的字符串和一个目标值,返回在数字之间添加了二元运算符(不是一元的) +.-或*之后所有能得到目标值的情况.例如:"123", 6 -> ["1+ ...
- SQL数据库基础知识——抽象类
抽象类,只为继承而出现,不定义具体的内容,只规定该有哪些东西:一般抽象类中只放置抽象方法,只规定了返回类型和参数:比如: 人 - 有吃饭,睡觉方法: 男人 - 继承人抽象类,必须实现吃饭,睡觉的方法主 ...
- JavaScript(八)日期对象
Date对象 1.创建方式 var now = new Date(); //现在返回的直接就是 当前的时间 不需要进行换算了 返回格式 (星期 月 日 年 时 分 秒 时区) 2.日期的格式化方 ...
- Django基础之admin功能
Django默认开起了后台 1.访问admin后台 2.用户和密码进行登录 ============================================================== ...