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

来源:互联网 发布:淘宝运营什么意思 编辑:程序博客网 时间:2024/04/29 05:47

传送门:

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
原创粉丝点击