华中农大HZAUOJ1100 Plant trees 线段树懒惰树 最长不下降子序列nlogn

1100: Plant trees

Time Limit: 5 Sec   Memory Limit: 128 MB
Submit: 187   Solved: 13
[ Submit][ Status][ Web Board]

Description

Because I’m too vegetable, so the Big Elder Wu who is my roommate ask me to go home to plant trees . Because he is the Big Elder, I have to go home to plant trees. I plant N trees in a line , the ith tree is ai meters tall. One day , Big Elder Wu comes and he tell me that he will chose X trees from N trees , the X is the length of the longest non declining sub sequence of N, then he tell me the X, but I’m too vegetable, so I chose the shortest X trees from the N trees, and plant the X trees in a new line from the shortest to the tallest, (eg: if the N trees are 1,2,3,3,2,3,1,the longest non declining sub sequence  is 1,2,3,3,3, so the X is 5, and the new line are 1, 1, 2, 2, 3).then, there will be two types of operations:
feed  x y z : it means that I will feed the trees from xth tree to the yth tree with the Big Elder Wu’s magic water , then all the trees between [x,y] will grow z meters.
sell  x y : it means that the Big Elder Wu will ask me how much money will I get if I sell the trees between [x,y], 1 meter tree can sell 1 RMB.

Input

The first line contain one integer T , it means there are T test cases, for each case , the first line contain one integer N (1<=N<=100000), the next line contain N numbers which means the N trees height, the next line contain one integer M (1<=M<=100000), then flows M lines, each line is “feed x y z” or “sell x y” (1<=x<=y<=X,0<=z<=100000),please notice that for each case , even if you sell all the trees after all the operations , the money will never greater than 2^32-1RMB, because the Big Elder Wu think that the money I earn should not greater than his pin money.

Output

For each case, the first line print “Case#k:”, K means the which case it is, it start from 1, then, for the operations of this case, if it is “sell x y” ,print one line contain one integer which means the money I can earn if I sell the trees between [x,y]. 

Sample Input

1
7
1 2 3 3 2 3 1
5
feed 1 3 10
sell 1 1
sell 1 3
feed 1 5 10
sell 1 5

Sample Output

Case#1:
11
34
89

HINT

Source

题意:主人公要种树啊,他的种树方法是:从一列排开的n棵树中先求出最长不下降子序列的长度(如此生硬地塞了个最长上升子序列进来哈哈哈哈),假设这个长度是x,主人公会从n棵树中挑出前x小的树出来,重新种一排,然后对着一排新的树进行听上去就像要用线段树般的工作,即

1、feed  a b c :令编号a~b间树长高z米

2、sell a b :把编号a~b间的树卖了,一米一块钱

这题本应该是不可能做出来的题目。。。为什么呢。。因为题目都在骗人啊!!!!

说什么种树的时候a、b<=x个毛线啊!!后来看测试数据a、b是<=n的啊!!!难怪status一堆RE,数据和题目根本不对应啊!!还有“the money will never greater than 2^32-1RMB”也是骗你的啊!!不开longlong 把你wa到死为止哦~~

还有啊!“(1<=N<=100000)”也是骗年的哦~n会到1e6的哦~不开大点RE到死的哦~

总之这题目都在骗你的哦~

如此坑爹且出题错误这么多的居然!居然有一位勇士挂了10次过了。。。。。太强了。。。。

不过因为遇见了这题知道了懒惰树的存在,懒惰树嘛。。这玩意儿是线段树的进化版本,题目里面对线段的更新一直都是成区间更新的,如果用以往的线段树的话难免有些费时,因为每次更新一个区间的时候我们要深入到每一个left==right的节点去更新。。。费时啊

懒惰树的优点就在于,更新的时候只会更新到题目询问的举荐那一层,那么它下面的那些层怎么办呢?当前层会将下面一层要更新的树枝记录下来(lazy),只有题目询问比当前层还要深的层时,当前层才会将lazy数值推向更深的层中。

还有一点要注意的是最长不下降子序列要用nlogn方法来求,用n^2的话会超时。。。反正这题是我学习懒惰树用的。。。并不推荐别人去写。。。这题面是个啥玩意儿啊。。。

优化、优化。。。。详细见代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 1e6+10;
const int INF=0x3f3f3f3f;
struct unit{
    int left;
    int right;
    long long ans;
    long long lazy;
}save[maxn];
int a[maxn];
void build(int l,int r,int index){
    save[index].left = l;
    save[index].right = r;
    save[index].lazy = 0;
    if(l==r){
        save[index].ans = a[l];
        return;
    }
    int middle = (l+r)/2;
    build(l, middle, index<<1);
    build(middle+1, r, index<<1|1);
    save[index].ans = save[index<<1].ans+save[index<<1|1].ans;
}
void push(int index){
    if(save[index].left==save[index].right){
        return ;
    }
    if(save[index].lazy!=0){
        save[index<<1].lazy += save[index].lazy;
        save[index<<1|1].lazy += save[index].lazy;
        save[index<<1].ans += ((save[index<<1].right-save[index<<1].left+1)*save[index].lazy);
        save[index<<1|1].ans += ((save[index<<1|1].right-save[index<<1|1].left+1)*save[index].lazy);
        save[index].lazy = 0;
    }
}
void update(int l,int r,int num,int index){
    save[index].ans += (r-l+1)*num;
    push(index);
    if(save[index].left==l&&save[index].right==r){
        save[index].lazy =num;
        return ;
    }
    int middle = (save[index].left+save[index].right)/2;
    if(r<=middle){
        update(l, r, num, index<<1);
    }else if(l>middle){
        update(l, r, num, index<<1|1);
    }else{
        update(l, middle, num, index<<1);
        update(middle+1, r, num, index<<1|1);
    }
}
long long getSum(int l,int r,int index){
    //cout<<save[index].left<<" "<<save[index].right<<endl;
    if(save[index].left==l&&save[index].right==r){
        return save[index].ans;
    }
    push(index);
    int middle = (save[index].left+save[index].right)/2;
    if(r<=middle){
        return getSum(l,r,index<<1);
    }else if(l>middle){
        return getSum(l,r,index<<1|1);
    }else{
        return getSum(l, middle, index<<1) + getSum(middle+1, r, index<<1|1);
    }
}
int another[maxn],s[maxn];
char op[10];
int main(){
    //freopen("d.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
    int t,n,i,j,m;
    int rnd = 1;
    scanf("%d",&t);
    while(t--){
        printf("Case#%d:\n",rnd);
        rnd++;
        scanf("%d",&n);
        for(i=1;i<=n;i++){
            scanf("%d",&a[i]);
            another[i-1] = a[i];
        }
        
        memset(s, INF, sizeof(s));
        for(i=0;i<n;i++){
            int k=upper_bound(s,s+n,another[i])-s;
            s[k]=another[i];
        }
        int len=lower_bound(s,s+n,INF)-s;
        
        sort(a+1, a+1+n);
        //cout<<"len "<<len<<endl;
        build(1, len, 1);
        int x,y,z;
        scanf("%d",&m);
        for(i=1;i<=m;i++){
            scanf("%s",op);
            if(op[0]=='f'){
                scanf("%d%d%d",&x,&y,&z);
                if(x>len){
                    continue;
                }
                if(y>len){
                    y = len;
                }
                update(x, y, z, 1);
            }else{
                scanf("%d%d",&x,&y);
                if(x>len){
                    printf("0\n");
                    continue;
                }
                if(y>len){
                    y = len;
                }
                printf("%lld\n",getSum(x, y, 1));
            }
        }
    }
    return 0;
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值