Uva 12436 Rip Van Winkle's Code
Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequired to perform three types of update operations (A, B, C), and one query operation S over an arraydata[]. Initially all elements of data are equal to 0. Though
Rip Van Winkle is going to sleep for 20years, and his code is also super slow, you need to perform the same update operations and output theresult for the query operation S in an efficient way.
long long data[250001];
void A( int st, int nd ) {
for( int i = st; i \le nd; i++ ) data[i] = data[i] + (i - st + 1);
}
void B( int st, int nd ) {
for( int i = st; i \le nd; i++ ) data[i] = data[i] + (nd - i + 1);
}
void C( int st, int nd, int x ) {
for( int i = st; i \le nd; i++ ) data[i] = x;
}
long long S( int st, int nd ) {
long long res = 0;
for( int i = st; i \le nd; i++ ) res += data[i];
return res;
}
Input
The first line of input will contain T (≤ 4 ∗ 105) denoting the number of operations. Each of the nextT lines starts with a character (‘A’, ‘B’, ‘C’ or ‘S’), which indicates the type of operation. Character ‘A’,‘B’ or ‘S’ will be followed by two integers,
st and nd in the same line. Character ‘C’ is followed by threeintegers, st, nd and x. It’s assumed that, 1 ≤ st ≤ nd ≤ 250000 and −105 ≤ x ≤ 105. The meaningsof these integers are explained by the code of Rip Van Winkle.
Output
For each line starting with the character ‘S’, print S(st, nd) as defined in the code.
Sample Input
7
A 1 4
B 2 3
S 1 3
C 3 4 -2
S 2 4
B 1 3
S 2 4
Sample Output
9
0
3
这题是区间更新,这题比较麻烦,做了很长时间。先用线段树维护l,r,add1(线段左端点加的值),add2(线段右端点加的值),step(区间的公差,右边减去左边的),sum(区间总和),flag(判断区间是否数字相同),value(区间数字都相同时的数值大小).我的思路是每一次更新,都把这一段的sum值直接表示出来,如果更新的这条线段小于当前线段,那么先不更新sum值,而是b[th].sum=b[lth].sum+b[rth].sum;
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define lth th<<1
#define rth th<<1|1
#define inf 99999999
#define pi acos(-1.0)
#define MOD 100000007
#define maxn 250050
struct node{
int l,r;
ll value,flag; //flag表示这段是不是值都是相同的,value是这段的值
ll add1,step,add2; //add1表示左端点加的值,add2表示右端点,step表示这段的公差
ll sum;
}b[4*maxn];
void build(int l,int r,int th)
{
int mid;
b[th].l=l;b[th].r=r;
b[th].value=0;b[th].flag=1;
b[th].add1=b[th].step=b[th].add2=0;
b[th].sum=0;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,lth);
build(mid+1,r,rth);
}
void pushdown(int th)
{
int mid;
mid=(b[th].l+b[th].r)/2;
if(b[th].flag){
b[th].flag=0;
b[lth].flag=1;
b[lth].value=b[th].value;
b[lth].add1=b[lth].add2=b[lth].step=0;
b[lth].sum=(b[lth].r-b[lth].l+1)*b[th].value;
b[rth].flag=1;
b[rth].value=b[th].value;
b[rth].add1=b[rth].add2=b[rth].step=0;
b[rth].sum=(b[rth].r-b[rth].l+1)*b[th].value;
}
ll add1,add2;
add1=b[th].add1; add2=b[th].add1+(mid-b[th].l)*b[th].step;
b[lth].add1+=add1;
b[lth].add2+=add2;
b[lth].step+=b[th].step;
b[lth].sum+=(add1+add2)*(b[lth].r-b[lth].l+1)/2;
ll add3,add4;
add3=add2+b[th].step;add4=add3+(b[th].r-(mid+1))*b[th].step;
b[rth].add1+=add3;
b[rth].add2+=add4;
b[rth].step+=b[th].step;
b[rth].sum+=(add3+add4)*(b[rth].r-b[rth].l+1)/2;
b[th].add1=b[th].add2=b[th].step=0;
}
void pushup(int th)
{
b[th].sum=b[lth].sum+b[rth].sum;
}
void update(int l,int r,ll add,int f,int th)
{
int mid;
if(b[th].l==l && b[th].r==r){
if(f==1){
b[th].add1+=add;
b[th].add2+=add+b[th].r-b[th].l;
b[th].step+=1;
b[th].sum+=(add+add+b[th].r-b[th].l)*(b[th].r-b[th].l+1)/2;
return;
}
else if(f==2){
b[th].add1+=add+b[th].r-b[th].l;
b[th].add2+=add;
b[th].step-=1;
b[th].sum+=(add+add+b[th].r-b[th].l)*(b[th].r-b[th].l+1)/2;
return;
}
else if(f==3){
b[th].flag=1;
b[th].value=add;
b[th].sum=b[th].value*(b[th].r-b[th].l+1);
b[th].add1=b[th].add2=b[th].step=0;
return;
}
}
pushdown(th);
mid=(b[th].l+b[th].r)/2;
if(r<=mid)update(l,r,add,f,lth);
else if(l>mid)update(l,r,add,f,rth);
else{
if(f==1){
update(l,mid,add,f,lth);
update(mid+1,r,add+(mid+1-l),f,rth);
}
else if(f==2){
update(l,mid,add+(r-mid),f,lth);
update(mid+1,r,add,f,rth);
}
else if(f==3){
update(l,mid,add,f,lth);
update(mid+1,r,add,f,rth);
}
}
pushup(th);
}
ll question(int l,int r,int th)
{
int mid;
if(b[th].l==l && b[th].r==r){
return b[th].sum;
}
pushdown(th);
mid=(b[th].l+b[th].r)/2;
if(r<=mid)return question(l,r,lth);
else if(l>mid)return question(l,r,rth);
else{
return question(l,mid,lth)+question(mid+1,r,rth);
}
}
int main()
{
int m,i,j,T,c,d;
ll n,num;
char s[10];
while(scanf("%lld",&n)!=EOF)
{
build(1,250010,1);
for(i=1;i<=n;i++){
scanf("%s%d%d",s,&c,&d);
if(s[0]=='A'){
update(c,d,1,1,1);
}
else if(s[0]=='B'){
update(c,d,1,2,1);
}
else if(s[0]=='C'){
scanf("%lld",&num);
update(c,d,num,3,1);
}
else if(s[0]=='S'){
printf("%lld\n",question(c,d,1) );
}
}
}
return 0;
}
Uva 12436 Rip Van Winkle's Code的更多相关文章
- UVA 12436 - Rip Van Winkle's Code(线段树)
UVA 12436 - Rip Van Winkle's Code option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- Uva 12436 Rip Van Winkle's Code
Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...
- UVA-12436 Rip Van Winkle's Code (线段树区间更新)
题目大意:一个数组,四种操作: long long data[250001]; void A( int st, int nd ) { for( int i = st; i <= nd; i++ ...
- UVA 12436-Rip Van Winkle's Code(线段树的区间更新)
题意: long long data[250001]; void A( int st, int nd ) { for( int i = st; i \le nd; i++ ) data[i] = da ...
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
- UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
- FLUENT多孔介质数值模拟设置【转载】
转载自:http://zhengjun0228.blog.163.com/blog/static/71377014200971895419613/ 多孔介质条件 多孔介质模型可以应用于很多问题,如通过 ...
- 编译调试 .NET Core 5.0 Preview 并分析 Span 的实现原理
很久没有写过 .NET Core 相关的文章了,目前关店在家休息所以有些时间写一篇新的
- UVA 11754 - Code Feat(数论)
UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...
随机推荐
- Java JDK8下载 (jdk-8u251-windows-x64和jdk-8u271-linux-x64.tar)
jdk-8u251-windows-x64 和 jdk-8u271-linux-x64.tar 链接:https://pan.baidu.com/s/1gci6aSIFhEhjY8F48qH39Q 提 ...
- 关于 percona monitoring plugins插件报slave is stoped on ip地址
思路:肯定是某个item触发了触发器 去看触发器,找到 slave is stoped,如下图 看到键是mysql.running-slave ,然后去定义key的文件中查看该键对应的脚本,修改脚本. ...
- STM32延时函数的四种方法
单片机编程过程中经常用到延时函数,最常用的莫过于微秒级延时delay_us()和毫秒级delay_ms().本文基于STM32F207介绍4种不同方式实现的延时函数. 1.普通延时 这种延时方式应该是 ...
- UVM基础总结——基于《UVM实战》示例
一.前言 工作一直在做SoC验证,更关注模块间的连接性和匹配性,所以相比于擅长随机约束激励的UVM来说,定向测试的概念更容易debug.当然前提是IP已经被充分验证.因此觉得接触UVM的机会较少.到现 ...
- Lakehouse: 统一数据仓库和高级分析的新一代开放平台
1. 摘要 数仓架构在未来一段时间内会逐渐消亡,会被一种新的Lakehouse架构取代,该架构主要有如下特性 基于开放的数据格式,如Parquet: 机器学习和数据科学将被作为头等公民支持: 提供卓越 ...
- 采用Sharding-JDBC解决分库分表
源码:Sharding-JDBC(分库分表) 一.Sharding-JDBC介绍 1,介绍 Sharding-JDBC是当当网研发的开源分布式数据库中间件,从 3.0 开始Sharding-JDBC被 ...
- Vue项目之实现登录功能的表单验证!
Vue项目之实现登录功能的表单验证! 步骤: 配置 Form表单验证; 1.必须给el-from组件绑定model 为表单数据对象 2 给需要验证的表单项 el-form-item 绑定 prop 属 ...
- Java并发组件二之CyclicBarriar
使用场景: 多个线程相互等待,直到都满足条件之后,才能执行后续的操作.CyclicBarrier描述的是各个线程之间相互等待的关系. 使用步骤: 正常实例化:CyclicBarrier sCyclic ...
- Promise用法
1.概述 Promise是一步编程的一种解决方案,从语法上讲,promise是一个对象,从它可以获取异步的问题 Promise的优点: 可以避免多次异步调用嵌套导致的回调地域 提供了简洁的api,使得 ...
- 洛谷P4317
Description 定义 \(sum(i)\) 表示 \(i\) 的二级制中 1 的个数 给定一个 N,求 \(\prod_{i=1}^N sum(i)\) Solution 显然是数位 DP 考 ...