Codeforces #284 div1 D. Traffic Jams in the Land 数论 线段树
来源:互联网 发布:多益网络加班怎么样 编辑:程序博客网 时间:2024/06/05 22:51
题目
题目链接:http://codeforces.com/problemset/problem/498/D?csrf_token=c6cf98daaac2acd774c9eb4fd2d609cd
题目来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105318#problem/I
简要题意:直线上有些路,到达路的时间为
ai 倍数两秒通过,否则一秒,操作为改ai ,询问l到r 多久。
题解
首先可以想到要保存所有结果需要模
60 下的结果即可,1到6 最小公倍数。然后就是构建
60 个线段树第i 个线段树里头表示t≡imod60 时通过区间的时间。然后于是我们在更新的时候,给定初始时间,先去求左边的加上然后模掉作为右边的初始时间再去求右边的即可。求和的时候也是类似的。
神奇的地方在于题目中将求和的任务分散到了各个线段树中。
代码
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <stack>#include <queue>#include <string>#include <vector>#include <set>#include <map>#define pb push_back#define mp make_pair#define all(x) (x).begin(),(x).end()#define sz(x) ((int)(x).size())#define fi first#define se secondusing namespace std;typedef long long LL;typedef vector<int> VI;typedef pair<int,int> PII;LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}// headconst int MOD = 60;const int N = 1E5+5;struct SegmentTree {#define lsonPara (rt<<1), L, MID#define rsonPara (rt<<1|1), MID+1, R#define lson (rt<<1)#define rson (rt<<1|1)#define MID ((L+R)>>1) static const int TN = N<<2; int tree[MOD][TN]; void update(int rt) { for (int i = 0; i < MOD; i++) { int temp = tree[i][lson]; int nxt = (i+temp) % MOD; tree[i][rt] = temp + tree[nxt][rson]; } } void build(int rt, int L, int R) { if (L == R) { int v; scanf("%d", &v); for (int i = 0; i < MOD; i++) { tree[i][rt] = i%v ? 1 : 2; } return; } build(lsonPara); build(rsonPara); update(rt); } void modify(int rt, int L, int R, int x, int v) { if (x < L || x > R) return; if (L == R) { for (int i = 0; i < MOD; i++) { tree[i][rt] = i%v ? 1 : 2; } return; } modify(lsonPara, x, v); modify(rsonPara, x, v); update(rt); } int query(int rt, int L, int R, int l, int r, int rem) { if (r < L || l > R || l > r) return 0; if (L == l && R == r) { return tree[rem][rt]; } int temp = query(lsonPara, l, min(r, MID), rem); return temp + query(rsonPara, max(l, MID+1), r, (rem+temp) % MOD); }};SegmentTree st;char s[5];int main() { int n, q, x, y; scanf("%d", &n); st.build(1, 1, n); scanf("%d", &q); while (q--) { scanf("%s%d%d", s, &x, &y); if (s[0] == 'A') { printf("%d\n", st.query(1, 1, n, x, y-1, 0)); } else { st.modify(1, 1, n, x, y); } } return 0;}
0 0
- Codeforces #284 div1 D. Traffic Jams in the Land 数论 线段树
- CodeForces 498 D.Traffic Jams in the Land(线段树)
- codeforces 498 d Traffic Jams in the Land
- CodeForces 498D Traffic Jams in the Land
- [题解]codeforces498d Traffic Jams in the Land
- codeforces-div1-284-D
- Codeforces Round 200 Div1 D Water Tree (树上线段树)
- Codeforces Round #352 div1 C D (智商+线段树)
- codeforces-div1-282-D
- codeforces-div1-286-D
- codeforces-div1-285-D
- codeforces 250 div1 D
- Codeforces round339 div1 D
- Codeforces #383 div1 D
- Codeforces 671D Roads in Yusland dfs序+线段树
- Codeforces 834D The Bakery DP(线段树维护)
- Codeforces 834D The Bakery 线段树优化DP
- Codeforces-834D The Bakery(线段树+dp)
- HQL和SQL的区别
- 计算广告与机器学习-技术共享平台
- 动画之补间平移动画
- 敏捷测试(一)敏捷测试探索
- java基础练习 杨辉三角
- Codeforces #284 div1 D. Traffic Jams in the Land 数论 线段树
- Runtime使ViewController具有name属性
- 基于AT89C51教程之软件篇(二)学习前的知识储备
- 利用SQL语句修改记录
- dl dt dd 标签的用法(一)
- 学习记录 - 1
- vxWorks的二值信号量示例
- PAT1008.数组元素循环右移
- 蓝牙(GameKit)