Codeforces Testing Round #1_C. Circular RMQ
线段树,懒操作
题目传送门
附代码:
1 #include <iostream> 2 #include <fstream> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cstring> 6 #include <cmath> 7 #include <cstdlib> 8 9 using namespace std; 10 11 typedef long long LL; 12 13 typedef struct node 14 { 15 int left, right, mid; 16 LL val, inc; 17 }node; 18 19 20 LL val[210000]; 21 int N, M; 22 node tree[1000000]; 23 24 void build(int l, int r, int idx) 25 { 26 tree[idx].left = l, tree[idx].right = r; 27 tree[idx].mid = (l + r) / 2; 28 tree[idx].inc = 0; 29 if(l == r) 30 { 31 tree[idx].val = val[l]; 32 return ; 33 } 34 build(l, tree[idx].mid, idx << 1); 35 build(tree[idx].mid + 1, r, (idx << 1) + 1); 36 tree[idx].val = min(tree[idx << 1].val, tree[(idx << 1) + 1].val); 37 } 38 39 void inc(int l, int r, LL v, int idx) 40 { 41 if(l == tree[idx].left && r == tree[idx].right) 42 { 43 tree[idx].inc += v; 44 tree[idx].val += v; 45 return; 46 } 47 if(0 != tree[idx].inc) 48 { 49 inc(tree[idx].left, tree[idx].mid, tree[idx].inc, idx << 1); 50 inc(tree[idx].mid + 1, tree[idx].right, tree[idx].inc, (idx << 1) + 1); 51 tree[idx].inc = 0; 52 } 53 54 int mid = tree[idx].mid; 55 56 if(r <= mid) 57 { 58 inc(l, r, v, idx << 1); 59 } 60 else if(l > mid) 61 { 62 inc(l, r, v, (idx << 1) + 1); 63 } 64 else 65 { 66 inc(l, mid, v, idx << 1); 67 inc(mid + 1, r, v, (idx << 1) + 1); 68 } 69 tree[idx].val = min(tree[idx << 1].val, tree[(idx << 1) + 1].val); 70 71 } 72 73 LL query(int l, int r, int idx) 74 { 75 // printf("%d %d %d\n", l, r, idx); 76 if(l == tree[idx].left && r == tree[idx].right) 77 { 78 return tree[idx].val;; 79 } 80 if(0 != tree[idx].inc) 81 { 82 inc(tree[idx].left, tree[idx].mid, tree[idx].inc, (idx << 1)); 83 inc(tree[idx].mid + 1, tree[idx].right, tree[idx].inc, (idx << 1) + 1); 84 tree[idx].inc = 0; 85 } 86 87 int mid = tree[idx].mid; 88 if(r <= mid) 89 { 90 return query(l, r, idx << 1); 91 } 92 else if(l > mid) 93 { 94 return query(l, r, (idx << 1) + 1); 95 } 96 else 97 { 98 return min(query(l, mid, (idx << 1)), query(mid + 1, r, (idx << 1) + 1)); 99 }100 }101 102 int main()103 {104 // freopen("input.txt", "r", stdin);105 scanf("%d", &N);106 107 for(int i = 0; i < N; i++)108 scanf("%I64d", &val[i]);109 110 build(0, N - 1, 1);111 112 char str[1000];113 114 scanf("%d\n", &M);115 116 int l, r;117 LL v;118 119 while(M--)120 {121 122 gets(str);123 124 if(3 == sscanf(str, "%d %d %I64d", &l, &r, &v))//inc125 {126 if(l > r)127 {128 inc(l, N - 1, v, 1);129 inc(0, r, v, 1);130 }131 else132 {133 inc(l, r, v, 1);134 }135 }136 else137 {138 LL res = 0;139 if(l > r)140 {141 res = min(query(l, N - 1, 1), query(0, r, 1));142 }143 else144 {145 res = query(l, r, 1);146 }147 printf("%I64d\n", res);148 }149 }150 return 0;151 }
转载于:https://www.cnblogs.com/jackiesteed/articles/2122218.html
Codeforces Testing Round #1_C. Circular RMQ相关推荐
- Codeforces Testing Round #10 A. Forgotten Episode
水题,注意数据范围 #include <iostream> using namespace std;int main(){long long n,a;cin >> n;long ...
- Codeforces Testing Round #16 (Unrated)C (集合)
链接 题意 思路 代码 #include<bits/stdc++.h> using namespace std; const int N = 2e5 + 10; set<pair&l ...
- Codeforces Testing Round #14 (Unrated) C. Minimum Sum
给你仅含有字符'a'~'j'的字符串,让你用字符'0'~'9'替换其中的字符,使得替换后所有字符串代表的整数之和最小,假设字符串并不会出现前导零. 数据规模:n(1<n<1000)个字符串 ...
- Codeforces Global Round 1 晕阙记
Codeforces Global Round 1 晕阙记 我做这场比赛的时候晕得要死.做这三道题做太久了,rating涨不起来啊! A 如果愿意的话你可以看做是膜2意义下的运算,写快速幂等各种膜运算 ...
- Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)
Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- Codeforces Beta Round #5 B. Center Alignment 模拟题
B. Center Alignment 题目连接: http://www.codeforces.com/contest/5/problem/B Description Almost every tex ...
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树。单点更新
http://codeforces.com/problemset/problem/91/B 题意: 给你n个数,求得i 到n中小于a[i]的最右边的a[j],然后求a[i]到a[j]之间包含了多少个数 ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp + 状态优化
传送门 文章目录 题意: 思路: 题意: 思路: 数位dpdpdp挺经典的一个题辣,有一个很明显的状态就是f[pos][num][lcm]f[pos][num][lcm]f[pos][num][lcm ...
最新文章
- MySQL的恢复脚本
- mysql里b树_MySQL-B树/B+树
- oracle 时间集合,oracle 日期函数集合(集中版本)第2/2页
- oracle安全性规则,[ORACLE ]安全性
- java实现调查问卷_智能办公进行时丨富士施乐邀您参与有奖问卷调查
- Android 音频tinyalsa开发
- 【轴承故障诊断】一维深度卷积网络实现西储大学轴承故障数据分类
- QuickZIP V1.21 源码 【学习SharpZipLib的必看!】【以SharpZipLib为基础完成】【VB.Net】
- PCS2021:针对游戏内容的视频编码工具分析和数据集
- 5G适合py还是java,5G比4G到底有啥好处?看完就彻底明白了
- 项目工作绩效数据、项目工作绩效信息、项目工作绩效报告
- 餐桌 (Standard IO)
- Java 通过EWS JAVA API发送exchange邮件
- 焦炉集气管压力模糊控制(三输入单输出)
- 蛋糕瓜分殆尽 谁会成为下一个手游造富的加速器?
- 一个屌丝程序猿的人生(一百一十八)
- 爆笑的评论,真是不能相信看起来义正辞严的话啊。
- mysql 生成日历视图_mysql sql语句生成日历表
- BMS算法中定义SOC需考虑哪些因素
- BP神经网络隐藏层的作用,bp神经网络输出层函数