CDOJ-1324-卿学姐与公主(分块)

卿学姐与公主

Time Limit: 1000 MS     Memory Limit: 64 MB

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哦

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
#define ll long long
inline int read()
{
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}
int belong[maxn],num,l[maxn],r[maxn],n,q,block;
ll a[maxn], Max[maxn];
void build()
{
    block = sqrt(n);
    num = n/block; if(n % block) num++;
    for(int i = 1; i <= num; i++)
        l[i] = (i-1)*block+1, r[i] = i * block;
    r[num] = n;
    for(int i = 1; i <= n; i++)
        belong[i] = (i - 1) / block + 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)
{
    int t1 = belong[x], t2 = belong[y];
    ll ans = 0;
    for(int i = x; i <= min(r[t1], y); i++)
        ans = max(ans, a[i]);
    if(t1 != t2) for(int i = l[t2]; i <= y; i++)
        ans = max(ans, a[i]);
    for(int i = t1 + 1; i < t2; i++)
        ans = max(ans,Max[i]);

    return ans;
}
int main()
{
    n = read(); q = read();
    build();
    for(int i = 1; i <= q; i++)
    {
        int op, l, r;
        op = read(); l = read(); r = read();
        if(op == 1) update(l, r);
        else printf("%lld\n",query(l,r));
    }
}

猜你喜欢

转载自blog.csdn.net/sugarbliss/article/details/81206906