》》点击进入原题测试《《

思路:好像是第一次这么印象深刻的写线段树,说实话,这个题确实很有意思,值得学习。

看了大神讲解视频,但是自己写的还是超时了。

参考来自 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的更多相关文章

  1. 2018 杭电多校2 - Naive Operations

    题目链接 Problem Description In a galaxy far, far away, there are two integer sequence a and b of length ...

  2. HDU6315 Naive Operations(多校第二场1007)(线段树)

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  3. 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 ...

  4. HDU 多校对抗 F Naive Operations

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  5. 杭电多校第二场 hdu 6315 Naive Operations 线段树变形

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  6. HDU 6351 Naive Operations(线段树)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=6315 Naive Operations Time Limit: 6000/3000 MS (Java/O ...

  7. hdu Naive Operations 线段树

    题目大意 题目链接Naive Operations 题目大意: 区间加1(在a数组中) 区间求ai/bi的和 ai初值全部为0,bi给出,且为n的排列,多组数据(<=5),n,q<=1e5 ...

  8. HDU-6315:Naive Operations(线段树+思维)

    链接:HDU-6315:Naive Operations 题意: In a galaxy far, far away, there are two integer sequence a and b o ...

  9. HDU 6315: Naive Operations

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

随机推荐

  1. gitlab-ce平台调试

    SMTP使用QQ exmail 遇到的问题:使用qinrui@easyunion.net对516059158@qq.com能发送验证邮件,但是无法打开验证连接

  2. IDEA中Spark往Hbase中写数据

    import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.io.ImmutableBytesWr ...

  3. svchost.exe 占用内存过多

    http://www.tomshardware.com/forum/20583-63-svchost-netsvcs-speed By Lokesh Chandra: Just Go to Contr ...

  4. JEECG框架使用Tomcat启动报ClassNotFound

    JEECG框架缺少一个类,名为AnnotationProcessor,包名为:org.apache package org.apache; import java.lang.reflect.Invoc ...

  5. bzoj 1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会【tarjan】

    几乎是板子,求有几个size>1的scc 直接tarjan即可 #include<iostream> #include<cstdio> #include<cstri ...

  6. 公司5:JrVue表格

    组件名称:jr-dynamic-query-table 组件布局 table组件名称:  jr-dynamic-query-table 分页组件名称: el-pagination <div re ...

  7. python自动化测试学习笔记-10YAML

    之前学习的编写测试用例的方法,都是从excel中编写接口的测试用例,然后通过读取excel文件进行接口自动化测试,这种方式我们叫做数据驱动的方式,由于excel操作起来不灵活,无法实现关联关系的接口测 ...

  8. 【NOI2012】魔幻棋盘

    Description 将要读二年级的小 Q 买了一款新型益智玩具——魔幻棋盘,它是一个N行M列的网格棋盘,每个格子中均有一个正整数.棋盘守护者在棋盘的第X行Y列(行与列均从1开始编号) 并且始终不会 ...

  9. Android 性能优化(15)网络优化( 11)Manipulating Broadcast Receivers On Demand

    Manipulating Broadcast Receivers On Demand This lesson teaches you to Toggle and Cascade State Chang ...

  10. 网上商城 Incorrect datetime value: '' for column 'ordertime' at row 1

    今天在做商城项目的[提交订单]功能的时候,向数据库插入数据报错:Incorrect datetime value: '' for column 'ordertime' at row 1 public ...