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 ...
随机推荐
- 文献综述三:基于JSP的商品信息管理系统设计与开发
一.基本信息 标题:基于JSP的商品信息管理系统设计与开发 时间:2015 出版源:Computer Knowledge and Technology 文件分类:jsp技术的系统开发 二.研究背景 通 ...
- 01-oracle限定查询-20190404
关系型数据库和半结构化数据(xml文件) oracle12c:c代表云计算 PDB,CDB sql语句执行顺序: 第一步:from子句控制数据来源: 第二步:where子句使用限定符对数据行过滤: 第 ...
- hibernate框架的搭建
1 导入所需的jar包 1 导入hibernate必须的jar包 2 导入驱动包 2 创建数据库,准备表,实体 1 创建hibernate数据库 CREATE DATABASE hibernate; ...
- 生成ks.cfg文件
RHEL 7下生成ks.cfg文件 环境 + RHEL 7 + 字符界面, 没有安装图形界面 软件包安装 + `yum install system-config-kickstart -y` + `y ...
- gcc链接非标准(non-standard)命名库
标准命名库: -lnamespace 标准链接库以lib开头, 并以so/a结尾. example gcc test.c -o test -L. -lhello 非标准命名库: -l:libname ...
- Linux内存管理机制简析
Linux内存管理机制简析 本文对Linux内存管理机制做一个简单的分析,试图让你快速理解Linux一些内存管理的概念并有效的利用一些管理方法. NUMA Linux 2.6开始支持NUMA( Non ...
- DevExpress 14.2 批量汉化
1.下载DevExpress_.NET_Localization_Resources_14.2汉化包 2.解压后将zh-CN或zh-CHS复制到安装目录如D:\Program Files (x86)\ ...
- ezdpl:完全依赖脚本和ssh的自动化部署方案
ezdpl是easy deployment的简写,使用简单的ssh和shell脚本来部署.升级.回滚和重新配置linux服务器. 重要提示:警告:这个项目还处于测试过程中,请仔细阅读说明,并且自己承担 ...
- GUID 格式化
1.Guid.NewGuid().ToString("N") 结果为: 38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid().ToSt ...
- matlab练习程序(模拟退火SA)
模拟退火首先从某个初始候选解开始,当温度大于0时执行循环. 在循环中,通过随机扰动产生一个新的解,然后求得新解和原解之间的能量差,如果差小于0,则采用新解作为当前解. 如果差大于0,则采用一个当前温度 ...