总时间限制: 
10000ms

单个测试点时间限制: 
1000ms

内存限制: 
262144kB
描述

给一个长为N的数列,有M次操作,每次操作是以下两种之一:

(1)将某连续一段同时改成一个数

(2)求数列中某连续一段的和

输入
第一行两个正整数N和M。
第二行N的整数表示这个数列。
接下来M行,每行开头是一个字符,若该字符为'M',则表示一个修改操作,接下来三个整数x、y和z,表示在[x,y]这段区间的数改为z;若该字符为'Q',则表示一个询问操作,接下来两个整数x和y,表示求[x,y]这段区间的和。
输出
对每一个询问操作单独输出一行,表示答案。
样例输入
5 3
1 2 3 4 5
Q 1 5
M 2 3 2
Q 3 5
样例输出
15
11
提示
1<=N<=10^5,1<=M<=10^5,输入保证合法,且所有整数及答案可用带符号32位整型存储。
对于线段树的直接修改,
我们首先考虑要维护一个修改标记,
注意这个标记是可以每次被覆盖的!
然后值直接区间修改就好
 #include<iostream>
#include<cstdio>
#include<cstring>
#define ls k<<1
#define rs k<<1|1
using namespace std;
const int MAXN=;
const int maxn=0x7ffff;
void read(int &n)
{
char c='+';int x=;bool flag=;
while(c<''||c>''){c=getchar();if(c=='-')flag=;}
while(c>=''&&c<='')
x=(x<<)+(x<<)+c-,c=getchar();
flag==?n=-x:n=x;
}
int n,m;
int ans=;
struct node
{
int l,r,w,f;
node()
{
l=r=w=;
f=-maxn;
}
}tree[MAXN<<];
void update(int k)
{
tree[k].w=tree[ls].w+tree[rs].w;
}
void build(int ll,int rr,int k)
{
tree[k].l=ll;tree[k].r=rr;
if(ll==rr)
{
read(tree[k].w);
return ;
}
int mid=(ll+rr)>>;
build(ll,mid,ls);
build(mid+,rr,rs);
update(k);
}
void push(int k)
{
tree[ls].w=(tree[ls].r-tree[ls].l+)*tree[k].f;
tree[rs].w=(tree[rs].r-tree[rs].l+)*tree[k].f;
tree[ls].f=tree[k].f;
tree[rs].f=tree[k].f;
tree[k].f=-maxn; }
void change(int k,int wl,int wr,int v)
{
if(wr<tree[k].l||wl>tree[k].r)
return ;
if(wl<=tree[k].l&&tree[k].r<=wr)
{
tree[k].w=(tree[k].r-tree[k].l+)*v;
tree[k].f=v;
return ;
}
int mid=(tree[k].l+tree[k].r)>>;
if(tree[k].f!=-maxn)
push(k);
change(ls,wl,wr,v);
change(rs,wl,wr,v);
update(k);
}
void ask(int k,int wl,int wr)
{
if(wr<tree[k].l||wl>tree[k].r)
return ;
if(wl<=tree[k].l&&tree[k].r<=wr)
{
ans+=tree[k].w;
return ;
}
int mid=(tree[k].l+tree[k].r)>>;
if(tree[k].f!=-maxn)
push(k);
ask(ls,wl,wr);
ask(rs,wl,wr);
update(k);
}
int main()
{
read(n);read(m);
build(,n,);
for(int i=;i<=m;i++)
{
char c;int x,y;
cin>>c;
read(x);read(y);
if(c=='M')
{
int v;
read(v);
change(,x,y,v);
}
else
{
ans=;
ask(,x,y);
printf("%d\n",ans);
}
}
return ;
}
: Challenge 5最近的提交

12:Challenge 5(线段树区间直接修改)的更多相关文章

  1. hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)

    #1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...

  2. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  3. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  4. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  5. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  6. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...

  7. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  8. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

    http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...

随机推荐

  1. 4.QList

    #include "mainwindow.h" #include <QApplication> #include <QLabel> #include < ...

  2. 给SearchView设置样式

    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http:/ ...

  3. HDU 1213 How Many Tables【并查集】

    解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  4. CDR中如何将对象在页面居中显示

    利用CorelDRAW在做设计排版时,如果想让对象在页面居中显示你会用什么方法?用鼠标拖?还是更准确的做法选择参照物对象,利用对齐与分布命令?或者还有更简单快速的方法,一起来看看吧! 最简单的方法(页 ...

  5. BarTender无法连接到数据库?原来是微软补丁包捣的鬼

    近期有很多BarTender用户反映,在使用BarTender设计打印条码时,经常会出现错误消息6670 的提示,使得BarTender无法连接到数据库,究其原因,原来是微软补丁包捣的鬼.目前海鸥科技 ...

  6. 路飞学城Python-Day19(Py_Notes)

    # 先定义类 class LuffyStudent: school = 'luffy' def learn(self): print('学习使我快乐') def eat(self): print('吃 ...

  7. Python 函数部分练习题

    函 数 基 础 1.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作2.写函数,计算传入字符串中[数字].[字母].[空格] 以及 [其他]的个数 3.写函数,判断用户传入的 ...

  8. 注解实战aftersuite和beforesuite

    package com.course.testng;import org.testng.annotations.*; public class BasicAnnotation { //最基本的注解,用 ...

  9. 数据库常用sql语句积累

    组合一个新表 select p.*,(select value from as_info where key = 'v51_products') as v51_products from AP_POR ...

  10. done