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 ...
- UVA - 10057 A mid-summer night's dream.
偶数时,中位数之间的数都是能够的(包含中位数) 奇数时,一定是中位数 推导请找初中老师 #include<iostream> #include<cstdio> #include ...
- 常见条码类型介绍(Code 39、Code 128、EAN-8、EAN-13、EAN-128、ISSN、TIF、TIF-14、UPC(A)、UPC(E))
常见条码类型,如下: 1.Code 39 Code 39,又称为"Code 3 of 9",是非零售市场中最常用的格式,用于盘存和跟踪.Code 39码编码规则简单,误码率低.所能 ...
- 退役笔记一#MySQL = lambda sql : sql + ' Source Code 4 Explain Plan '
Mysql 查询运行过程 大致分为4个阶段吧: 语法分析(sql_parse.cc<词法分析, 语法分析, 语义检查 >) >>sql_resolver.cc # JOIN.p ...
- UVa 1593 (水题 STL) Alignment of Code
话说STL的I/O流用的还真不多,就着这道题熟练一下. 用了两个新函数: cout << std::setw(width[j]); 这个是设置输出宽度的,但是默认是在右侧补充空格 所 ...
- UVA 1484 - Alice and Bob's Trip(树形DP)
题目链接:1484 - Alice and Bob's Trip 题意:BOB和ALICE这对狗男女在一颗树上走,BOB先走,BOB要尽量使得总路径权和大,ALICE要小,可是有个条件,就是路径权值总 ...
随机推荐
- 自定义Base16加密
自定义Base16加 ...
- JavaScript系列----函数(Function)篇(4)
1.什么是函数? 在W3C中函数的定义是这么说的:函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 诚然,从这种抽象的定义中我们得不到什么有价值的东西.下面,举例来列举出函数的几种定义 ...
- JavaScript系列-----Object之toString()和valueOf()方法 (2)
深入理解toString()和valueOf()函数 1.我们为什么要了解这两种方法 众所周知,toString()函数和valueOf函数,这两个函数是Object类的对象生来就拥有的,而且他们还可 ...
- HTML+CSS+js常见知识点
一.HTML.CSS常见知识点 1.垂直居中盒子 /* 方法一 */ html, body { width: 100%; height: 100%; padding: 0; margin: 0; } ...
- 数组去重+indexOf()应用
说起数组去重大家都不陌生,去重也有好多种方法,这里介绍很好理解的两种. 第一种 首先说一下第一种的逻辑,就是先拿第一个去跟第二个比,再跟第三个比,再跟第四个比--只要发现有相等的,可以用splice( ...
- EAS(学生管理系统)初建
一.确定开发使用的技术 本次开发EAS示例网站,使用Servlet+JSP+MySQL技术,其中包括使用bootstrap工具完成简易前端页面设计.所有数据实体与数据关系皆用数 ...
- Python之上下文管理
http://www.cnblogs.com/coser/archive/2013/01/28/2880328.html 上下文管理协议为代码块提供包含初始化和清理操作的上下文环境.即便代码块发生异常 ...
- SpringBoot之简单日志配置
我的目的指定一个文件夹输出:(不采用指定文件的原因是一个文件的大小时间长了会很大,不利于处理) logging: level: root: INFO org.sselab: controller: I ...
- Java 中实现方法重试的一种机制
有这样一个需求,当调用某个方法抛出异常,比如通过 HttpClient 调用远程接口时由于网络原因报 TimeOut 异常:或者所请求的接口返回类似于"处理中"这样的信息,需要重复 ...
- 有没有必要参加IT培训
近几年,IT培训机构可谓是琳琅满目,稂莠不齐.培训Java的,培训PHP的,培训大数据的等等吧,不一而足. 自己也算是IT技术圈子待了好多年了,面试过一些机构培训出来的学生,也有几个好哥们在培训机构做 ...