您的位置:首页 > 其它

HDU4107 Gangster 线段树 段更新

2014-07-15 12:54 316 查看
转载注明出处 http://blog.csdn.net/moedane

 

 

传送门 http://acm.hdu.edu.cn/showproblem.php?pid=4107

题意

代表有n个数(区间为[1,n]),m种操作。每个操作是给区间[l,r]加上一个数c,如果这个区间里面某个数大于等于给出的界限P,则加上2*c。输出全部操作结束之后最终的数列。

思路

线段树断更新。要记录的信息有区间最大值Max、区间最小值Min、区间累加的值addv。

要判断是否大于等于界限,则只要判断Min是否大于等于P、或者Max是否小于P。

if(Min >= P) add(2*c);

if(Max < p) add(c);

其他情况只需要往下再跑一层,直到达到上面的条件则可以做出相应操作。

这道题有点恶心的地方就是时间卡得紧,我把大部分预算换成位运算加上用输入挂才能950+ms勉强过。

代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <fstream>
#include <map>
#include <set>
#define bug puts("here");

using namespace std;

typedef long long ll;

const int maxn = 2 * 101000;
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const double PI = atan(1.0) * 4.0;
const double eps = 1e-8;

int n,m,P;

struct Node
{
int o,l,r;
int Max,Min;
int addv;
int lenth() {return r - l + 1;}
int mid() {return l + (r-l) / 2;}
bool in(int L,int R) {return L <= l && R >= r;}
};

struct T
{
Node t[maxn * 4];
void build(int o,int l,int r)
{
t[o].l = l;
t[o].r = r;
t[o].o = o;
t[o].addv = t[o].Min = t[o].Max = 0;
if(l == r) return;
build(o<<1, l , t[o].mid());
build(o<<1|1, t[o].mid() + 1 , r);
}

void pushdown(int o)
{
if(t[o].addv && t[o].lenth() > 1)
{
t[o<<1].addv += t[o].addv;
t[o<<1|1].addv += t[o].addv;
t[o<<1|1].Min += t[o].addv;
t[o<<1].Min += t[o].addv;
t[o<<1|1].Max += t[o].addv;
t[o<<1].Max += t[o].addv;
t[o].addv = 0;
}
}

void add(int o,int l,int r,int v)
{
if(t[o].in(l,r)){
if(t[o].Max < P){
t[o].addv += v;
t[o].Max += v;
t[o].Min += v;
return;
}
if(t[o].Min >= P) {
t[o].addv += v << 1;
t[o].Max += v << 1;
t[o].Min += v << 1;
return;
}
}
pushdown(o);
if(l <= t[o].mid()) add(o<<1, l, r, v);
if(r > t[o].mid()) add(o<<1|1, l , r, v);
t[o].Min = min(t[o<<1].Min , t[o<<1|1].Min);
t[o].Max = max(t[o<<1].Max , t[o<<1|1].Max);
return;
}

int query(int o,int x)
{
if(t[o].lenth() == 1 && t[o].l == x){
return t[o].addv;
}
pushdown(o);
if(x <= t[o].mid()) query(o<<1, x);
else query(o<<1|1, x);
}
}tree;

inline void scan(int &n)
{
char cc;
for (; cc = getchar(), cc<'0' || cc>'9';);
n = cc - '0';
for (; cc = getchar(), cc >= '0'&&cc <= '9';)
n = n * 10 + cc - '0';
}

int main()
{
while(~scanf("%d%d%d",&n,&m,&P))
{
tree.build(1,1,n);
for(int i=0;i<m;i++)
{
int l,r,c;
scan(l);
scan(r);
scan(c);
tree.add(1,l,r,c);

}
for(int i=1;i<n;i++){
printf("%d ",tree.query(1,i));
}
printf("%d\n",tree.query(1,n));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息