【UESTC 1324】卿学姐与公主 【分块】

传送门:

http://mozhu.today/#/problem/show/1324

思路:

这里用分块做的,练练分块。当然这题线段树也可以做。

代码:

#include <set>
#include <map>
#include <queue>
#include <vector>
#include <math.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using  namespace  std;

#define ff first
#define ss second
#define pb push_back
#define ll long long
#define mod 1000000007
#define ull unsigned long long
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define pl(x) cout << #x << "= " << x << endl;
const int inf = 0x3f3f3f3f;
const int N = 1e5+5;

int n;
int num, blocks, belong[N], l[N], r[N];
//num分块的个数
//belong[i]表示i属于哪一块
//blocks表示块的大小
//l[i]表示i这个块的左端点位置
//r[i]表示右端点的位置

ll a[N], Max[N];


void build(){
    blocks = sqrt(n);
    num = n/blocks; if(n%blocks)num++;
    for(int i=1; i<=num; i++)  
        l[i] = (i-1)*blocks+1, r[i] = i*blocks;
    r[num] = n;
    for(int i=1; i<=n; i++)
        belong[i] = (i-1)/blocks+1;

    for(int i=1; i<=num; i++)
        for(int j=l[i]; j<=r[i]; j++)
            Max[i] = max(Max[i], a[j]);
}

void update(int x, int y){
    a[x] += y;
    Max[belong[x]] = max(Max[belong[x]], a[x]);

}

ll query(int x, int y){
    ll ans = 0;
    if(belong[x] == belong[y]){ //属于同一块的情况特殊处理
        for(int i=x; i<=y; i++)
            ans = max(ans, a[i]);
        return ans;
    }
    for(int i=x; i<=r[belong[x]]; i++)ans = max(ans, a[i]);
    for(int i=l[belong[y]]; i<=y; i++)ans = max(ans, a[i]);
    for(int i=belong[x]+1; i<belong[y]; i++)ans = max(ans, Max[i]);
    return ans;
}

int  main(){
    int q;
    scanf("%d%d", &n,&q);
    build();
    while(q--){
        int op, x, y;
        scanf("%d%d%d", &op, &x, &y);
        if(op == 1)update(x, y);
        else printf("%lld\n", query(x, y));
    }
    return 0;
}

描述:

卿学姐与公主
Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)

Submit Status
某日,百无聊赖的卿学姐打开了某11区的某魔幻游戏

在这个魔幻的游戏里,生活着一个美丽的公主,但现在公主被关押在了魔王的城堡中。

英勇的卿学姐拔出利刃冲向了拯救公主的道路。

走过了荒野,翻越了高山,跨过了大洋,卿学姐来到了魔王的第一道城关。

在这个城关面前的是魔王的精锐部队,这些士兵成一字排开。

卿学姐的武器每次只能攻击一个士兵,并造成一定伤害,卿学姐想知道某时刻从LL到RR这个区间内,从开始到现在累计受伤最严重的士兵受到的伤害。

最开始每个士兵的受到的伤害都是0

Input
第一行两个整数N,QN,Q表示总共有NN个士兵编号从11到NN,和QQ个操作。

接下来QQ行,每行三个整数,首先输入一个tt,如果tt是11,那么输入p,xp,x,表示卿学姐攻击了pp这个位置的士兵,并造成了xx的伤害。如果tt是22,那么输入L,RL,R,表示卿学姐想知道现在[L,R][L,R]闭区间内,受伤最严重的士兵受到的伤害。

1≤N≤1000001≤N≤100000
1≤Q≤1000001≤Q≤100000
1≤p≤N1≤p≤N
1≤x≤1000001≤x≤100000
1≤L≤R≤N1≤L≤R≤N
Output
对于每个询问,回答相应的值

Sample input and output
Sample Input Sample Output
5 4
2 1 2
1 2 4
1 3 5
2 3 3
0
5
Hint
注意可能会爆int哦

Source
2016 UESTC Training for Data Structures

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值