HRBUST 1161——Leyni——————【线段树单点更新,区间查询】

Leyni

Time Limit: 3000 MS Memory Limit: 65536 KB

64-bit integer IO format: %lld , %llu Java class name: Main

[Submit] [Status] [Discuss]

Description

Leyni被人掳走,身在水深火热之中...
小奈叶为了拯救Leyni,独自一人前往森林深处从静竹手中夺回昏迷中的Leyni。
历经千辛万苦,小奈叶救出了Leyni,但是静竹为此极为恼怒,决定对他们发起最强烈的进攻。
不过小奈叶有一个叫做能量保护圈的道具,可以保护他们。
这个保护圈由n个小的小护盾围成一圈,从1到n编号。当某一块小护盾受到攻击的时候,小护盾就会抵消掉这次攻击,也就是说对这一块小护盾的攻击是无效攻击,从而保护圈里的人,不过小护盾在遭到一次攻击后,需要t秒进行冷却,在冷却期间受到的攻击都是有效攻击,此时他们就会遭到攻击, 即假设1秒时受到攻击并成功防御,到1+t秒时冷却才结束并能进行防御,在2到t受到的都是有效攻击。

现在小奈叶专心战斗,Leyni昏迷,他们无法得知小护盾遭受的有效攻击次数,他们需要你的帮助。
只要能帮到他们,Leyni就会赠送出一份小奈叶写真集。

Input

第一行是一个整数T,表示有多少组测试数据。
第一行是三个整数,n,q,t,n表示保护圈的长度,q表示攻击的询问的总次数,t表示能量盾的冷却时间。
接下来的q行,每行表示受到的攻击或者她询问某范围内的能量盾被攻击的次数。
攻击:
Attack   a
表示编号为a的小护盾受到一次攻击, 保证 1 <= a <= n
询问:
Query  a  b
表示询问编号从a到b的小护盾(包括a和b)总共受到了多少次有效攻击。保证 1<=a,b<=n
第k次攻击发生在第k秒,询问不花费时间
1 <= n,q <=100000
1 <= t <= 50。

Output

每一组测试数据,先输出一行"Case i:",i表示第i组测试数据,从1开始计数。
之后对于每一个询问,输出该范围内的小护盾受到的有效攻击次数,一个询问一行。

Sample Input

1
4 7 3
Attack 1
Attack 1
Attack 1
Attack 2
Attack 2
Query 1 4
Query 1 1

Sample Output

Case 1:
3
2

解题思路:只需要处理一下冷却时间这个条件即可,其他的就比较简单了。
 
反思:没有理解清题意,询问是不花时间的,所以时间只有攻击会花费。无限错,不知所错。 ORZ
 
 
#include<stdio.h>
#include<vector>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e6+20;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
struct SegTree{
    int val;
}segs[maxn*4];
int a[maxn];
void PushUp(int rt){
    segs[rt].val = segs[rt*2+1].val + segs[rt*2].val;
}
void buildtree(int rt,int L,int R){
    segs[rt].val = 0;
    if(L == R){
//        scanf("%d",&segs[rt].val);
        return ;
    }
    buildtree(lson);
    buildtree(rson);
    PushUp(rt);
}
void Update(int rt,int L,int R,int _idx,int _val){
    if(L == R && L == _idx){
        segs[rt].val += _val;
        return ;
    }
    if(_idx <= mid)
        Update(lson,_idx,_val);
    else
        Update(rson,_idx,_val);
    PushUp(rt);
}
int query(int rt,int L,int R,int l_ran,int r_ran){
    if(l_ran <= L&&R <= r_ran){
        return segs[rt].val;
    }
    int ret = 0;
    if(l_ran <= mid){
        ret += query(lson,l_ran,r_ran);
    }
    if(r_ran > mid){
        ret += query(rson,l_ran,r_ran);
    }
    return ret;
}
int main(){
    int cass, cas = 0, n, m ,t;
    scanf("%d",&cass);
    int idx, l, r;
    while(cass--){
        printf("Case %d:
",++cas);
        scanf("%d%d%d",&n,&m,&t);
        buildtree(1,1,n);
        for(int i = 1; i <= n; i++){    //
            a[i] = -51;
        }
        char s[333];
        int se = 0;
        for(int i = 1; i <= m; i++){
            scanf("%s",s);
            if(s[0]=='A'){
                se++;
                scanf("%d",&idx);
                if(se - a[idx] < t){
//                    printf("%d %d+++
",i,idx);
                    Update(1,1,n,idx,1);
                }else{
                    a[idx] = se;
                }
            }else{
                scanf("%d%d",&l,&r);
                int res1, res2;
                if(l > r){
                    res1 = query(1,1,n,l,n);
                    res2 = query(1,1,n,1,r);
                    printf("%d
",res1+res2);
                }else{
                    res1 = query(1,1,n,l,r);
                    printf("%d
",res1);
                }
            }

        }
    }

    return 0;
}

  

原文地址:https://www.cnblogs.com/chengsheng/p/5336023.html