POJ 3468 线段树+状压
题意:给你n个数,有对区间的加减操作,问某个区间的和是多少。
思路:状压+线段树(要用lazy标记,否则会TLE)
//By SiriusRen
#include <cstdio>
#include <cstring>
#define N 100001
using namespace std;
long long tree[N*4],lazy[N*4];
int xx,yy,zz,n,q;
char jy;
void push_down(int pos,int num){
lazy[pos*2]+=lazy[pos],lazy[pos*2+1]+=lazy[pos];
tree[pos*2]+=(num-num/2)*lazy[pos],tree[pos*2+1]+=(num/2)*lazy[pos];
lazy[pos]=0;
}
void build(int l,int r,int pos){
if(l==r){scanf("%lld",&tree[pos]);return;}
int mid=(l+r)/2;
build(l,mid,pos*2),build(mid+1,r,pos*2+1);
tree[pos]=tree[pos*2]+tree[pos*2+1];
}
void add(int l,int r,int pos){
if(l>=xx&&r<=yy){lazy[pos]+=zz;tree[pos]+=(long long)(r-l+1)*zz;return;}
if(lazy[pos])push_down(pos,r-l+1);
int mid=(l+r)/2;
if(mid>=xx)add(l,mid,pos*2);
if(mid<yy)add(mid+1,r,pos*2+1);
tree[pos]=tree[pos*2]+tree[pos*2+1];
}
long long query(int l,int r,int pos){
if(l>=xx&&r<=yy)return tree[pos];
if(lazy[pos])push_down(pos,r-l+1);
int mid=(l+r)/2;
if(mid<xx)return query(mid+1,r,pos*2+1);
else if(mid>=yy)return query(l,mid,pos*2);
else return query(l,mid,pos*2)+query(mid+1,r,pos*2+1);
}
int main(){
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--){
scanf("\n%c%d%d",&jy,&xx,&yy);
if(jy=='C')scanf("%d",&zz),add(1,n,1);
else printf("%lld\n",query(1,n,1));
}
}
POJ 3468 线段树+状压的更多相关文章
- POJ:2777-Count Color(线段树+状压)
Count Color Time Limit: 1000MS Memory Limit: 65536K Description Chosen Problem Solving and Program d ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
- hdu 5023 线段树+状压
http://acm.hdu.edu.cn/showproblem.php?pid=5023 在片段上着色,有两种操作,如下: 第一种:P a b c 把 a 片段至 b 片段的颜色都变为 c . 第 ...
- Bzoj 3813 奇数国 题解 数论+线段树+状压
3813: 奇数国 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 748 Solved: 425[Submit][Status][Discuss] ...
- POJ 3468(树状数组的威力)
之前说过这是线段树的裸题,但是当看了http://kenby.iteye.com/blog/962159 这篇题解后我简直震惊了,竟然能如此巧妙地转化为用树状数组来处理,附上部分截图(最好还是进入原网 ...
- poj2777Count Color——线段树+状压
题目:http://poj.org/problem?id=2777 状压每个颜色的选择情况,取答案时 | 一番: 注意题目中的区间端点可能大小相反,在读入时换一下位置: 注意pushdown()中要l ...
- POJ 3468 线段树裸题
这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...
- poj 3468 线段树区间更新/查询
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
随机推荐
- CAD导出黑白色的pdf(com接口)
主要用到函数说明: IMxDrawModifyTheColor 接口 用来修改图面所有对象的颜色,把它的颜色都修改成一个指定的值. IMxDrawModifyTheColor::Do 修改颜色,详细说 ...
- vue-router的基本使用和配置
1.在main.js文件中引入相关模块以及组件及实例化vue对象配置选项路由及渲染App组件 默认设置如下: import Vue from 'vue' import App from './App' ...
- copy the content of a file muliptle times and save as ordered files:
input: transient.case outputs: transient_1.case, transient_2.case,...transient_101.case ********** n ...
- PAT 1103 Integer Factorization
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- SCU Right turn
Right turn frog is trapped in a maze. The maze is infinitely large and divided into grids. It also c ...
- Surround the Trees HDU 1392 凸包
Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...
- Spell checker POJ 1035 字符串
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25426 Accepted: 9300 De ...
- asp.net--解决上传文件大小限制
原文地址 第一种方法,主要适用于IIS6.0版本 一.修改配置Web.Config文件中的httpRuntime节点 对于asp.net,默认只允许上传4M文件,增加如下配置,一般可以自定义最大文件大 ...
- LeetCode258——Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- Codeforces Round #306 (Div. 2) D
D. Regular Bridge time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...