CF498D Traffic Jams in the Land
题面:有n条公路一次连接着n + 1个城市,每一条公路有一个堵塞时刻a[i],如果当前时间能被a[i]整除,那么通过这条公路需要2分钟;否则需要1分钟。
现给出n条公路的a[i],以及m次操作。每一次操作:1.C x d:将第x条的堵塞时刻改为d。2.A x y:询问从城市x到城市y的所需时间。
这能想到是一个线段树的题,虽然做过好多道线段树的题,但遇到这种思路比较新奇的题,独立的想出来还是有一点困难。
于是稍微参照了一下题解。
我们观察一下a[i],2 <= a[i] <= 6,很小,而且这些数的最小公倍数最大只有60,那么对于每一个节点,我们开一个tim[60]的数组,tim[j]维护在时刻 j ,通过这段区间需要的时间。
那么区间合并的时候就是 j 从0~59枚举,这个区间的第 j 个时刻花费的时间等于左区间的从 j 时刻花费的时间x,加上右区间从j + x时刻花费的时间。于是这题就完事啦。
时间复杂度O(mlogn * 60)。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 1e5 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, m;
char s[]; struct Tree
{
int l, r;
int tim[];
}t[maxn << ];
void pushup(int now)
{
for(int i = ; i < ; ++i)
t[now].tim[i] = t[now << ].tim[i] + t[now << | ].tim[(i + t[now << ].tim[i]) % ];
}
void build(int L, int R, int now)
{
t[now].l = L; t[now].r = R;
if(L == R)
{
int x = read();
for(int i = ; i < ; ++i)
if(!(i % x)) t[now].tim[i] = ;
else t[now].tim[i] = ;
return;
}
int mid = (L + R) >> ;
build(L, mid, now << );
build(mid + , R, now << | );
pushup(now);
}
void update(int idx, int d, int now)
{
if(t[now].l == t[now].r)
{
for(int i = ; i < ; ++i)
if(!(i % d)) t[now].tim[i] = ;
else t[now].tim[i] = ;
return;
}
int mid = (t[now].l + t[now].r) >> ;
if(idx <= mid) update(idx, d, now << );
else update(idx, d, now << | );
pushup(now);
}
int query(int L, int R, int now, int Tim)
{
if(t[now].l == L && t[now].r == R) return t[now].tim[Tim % ];
int mid = (t[now].l + t[now].r) >> ;
if(R <= mid) return query(L, R, now << , Tim);
else if(L > mid) return query(L, R, now << | , Tim);
else
{
int ret = query(L, mid, now << , Tim);
ret += query(mid + , R, now << | , (Tim + ret) % );
return ret;
}
} int main()
{
n = read();
build(, n, );
m = read();
for(int i = ; i <= m; ++i)
{
scanf("%s", s);
if(s[] == 'C')
{
int x = read(), d = read();
update(x, d, );
}
else
{
int L = read(), R = read();
write(query(L, R - , , )); enter; //别忘 R - 1
}
}
return ;
}
CF498D Traffic Jams in the Land的更多相关文章
- CF498D:Traffic Jams in the Land——题解
https://vjudge.net/problem/CodeForces-498D http://codeforces.com/problemset/problem/498/D 题面描述: 一些国家 ...
- CF #284 div1 D. Traffic Jams in the Land 线段树
大意是有n段路,每一段路有个值a,通过每一端路需要1s,如果通过这一段路时刻t为a的倍数,则需要等待1s再走,也就是需要2s通过. 比较头疼的就是相邻两个数之间会因为数字不同制约,一开始想a的范围是2 ...
- [codeforces] 498D Traffic Jams in th Land
原题 简单的线段树问题. 对于题目中,a[i]的范围是2~6,我们仔细思考可以得出第0秒和第60秒是一样的(因为2~6的最小公倍数是60,),然后我们可以建一个线段树,里面记录0~59秒时刻开始通过这 ...
- Codeforces 498D Traffic Jams in the Land | 线段树
题目大意: 给坐标轴1~n的点,每个点有一个权值,从一个点走到下一个点需要1s,如果当前时间是权值的倍数就要多花1s 给出q组操作,C表示单点修改权值,A表示询问0时刻x出发到y的时间 题解:因为权值 ...
- Codeforces Round #284 (Div. 1)
A. Crazy Town 这一题只需要考虑是否经过所给的线,如果起点和终点都在其中一条线的一侧,那么很明显从起点走点终点是不需要穿过这条线的,否则则一定要经过这条线,并且步数+1.用叉积判断即可. ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- Gym 100507I Traffic Jam in Flower Town (模拟)
Traffic Jam in Flower Town 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/I Description ...
- ural 2020 Traffic Jam in Flower Town(模拟)
2020. Traffic Jam in Flower Town Time limit: 1.0 secondMemory limit: 64 MB Having returned from Sun ...
随机推荐
- shell特殊字符汇总【转】
Linux下无论如何都是要用到shell命令的,在Shell的实际使用中,有编程经验的很容易上手,但稍微有难度的是shell里面的那些个符号,各种特殊的符号在我们编写Shell脚本的时候如果能够用的好 ...
- 迪米特法則 Law of Demeter
又稱為"最小知識"原則, 若對Law of Demeter做一個簡單總結: 任何對象的任何方法只能調用以下對象中的方法: (1) 該對象本身 (2) 所傳入的參數對象 (3) 它所 ...
- SpringSecurity 3.2入门(5)自定义登录页面
增加spring-security.xml文件配置如下 <!-- 配置SpringSecurity的http安全服务 --> <security:http auto-config=& ...
- 2.C#编程语言
C#(sharp):是一种编程语言,可以开发基于.net平台的应用. java即是一种平台,也是一名语言. 在.net平台当中,C#是主流语言.C#语言开发的应用不能脱离.net环境而独立运行 ...
- C语言的前世今生
1.计算机语言的发展 机器语言:其实就是二进制0和1,最小为00000000,最大为11111111,8位比特为1个字节(byte),1k=1024byte,1m=1024k,1g=1024m[第一代 ...
- JavaScript中文拼音排序函数
要对很多设备根据名称排序,找了找没有找到特别适合的,然后就自己写了一个根据中文拼音首字母排序的方法. github: https://github.com/haboll/sort.git
- oracle查询所有用户表的表名、主键名称、索引、外键等
1.查找表的所有索引(包括索引名,类型,构成列): select t.*,i.index_type from user_ind_columns t,user_indexes i where t.ind ...
- C++异步编程资料汇集贴
C++异步编程 http://www.cnblogs.com/zjjcy/archive/2012/03/18/2404214.htmlhttp://www.cnblogs.com/zjjcy/arc ...
- angularJS 单页面 两个及以上个 ng-app 的处理方式
<div ng-app="myApp1" ng-controller="myCtrl1"> 名: <input type="text ...
- xcode选择开发者时"The Apple Developer Program License Agreement has been updated"
解决方法:进入开发者中心查看红色提示信息,同意就行