NOJ [1120] Reimu\'s Teleport

来源:互联网 发布:风险矩阵法判断准则 编辑:程序博客网 时间:2024/06/05 05:07
  • 问题描述
  • Hakurei Reimu(はくれい·れいむ) has an ability - Teleport. She can transport herself from here to anywhere flashily.
    When she is teleporting above a grassland from left to right, the grass between the distance she teleported will face to the right because of the teleporting wind. When she is from right to left, it will face to left.
    At the very beginning, all the grass is face to the sky (neither facing to left nor to right).

    Your task is to calculate out that how many grasses is facing to left, how many is facing to right and how many is facing to the sky between a querying distance.

  • 输入
  • This problem has several cases.
    The first line of each case is two integer A and T(0 < A <= 10000, 0 < T <= 100000), A means the number of action and T is the max coordinate of the grassland.
    Then follows A lines.
    Each line contains a command, 'F' of 'Q'.
    If the command is F, then it follows 2 integers D1 and D2(0 < D1, D2 <= T, D1 ≠ D2), means Reimu teleports from D1 to D2. The smaller one is left and the bigger one is right.
    If the command is Q, then it follows 2 integers D1 and D2(0 < D1 <= D2 <= T), means you have to calculate out now how many grasses is facing to left, how many is facing to right and how many is facing to the sky between D1 and D2.
  • 输出
  • For each command 'Q', you have to print the number of each facing side in a single line(from left, top to right).
  • 样例输入
  • 3 10F 1 2F 10 9Q 1 10
  • 样例输出
  • 2 6 2
  • 提示
  • For the sample, the grass will be "rrttttttll" when querying.
  • 线段树区间更新,区间查询。至于状态我原本用的数组保存,参考了下题解,用了个整数保存
  • #include<stdio.h>
    #include<string.h>

    const int maxn=100010;
    struct node
    {
    int l,r;
    __int64 add;
    __int64 status;//0表示朝天
    }tree[maxn<<2];

    void pushdown(int p)
    {
    int m=(tree[p].r-tree[p].l+1);
    if(tree[p].add)
    {
    tree[p<<1].add=tree[p].add;
    tree[p<<1|1].add=tree[p].add;
    tree[p<<1].status=(m-(m>>1)) * tree[p].add;
    tree[p<<1|1].status=(m>>1) * tree[p].add;
    tree[p].add=0;
    }
    }

    void build(int p,int l,int r)
    {
    tree[p].l=l;
    tree[p].r=r;
    tree[p].add=0;
    tree[p].status=0;
    if(l==r)
    return ;
    int mid=(l+r)>>1;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
    }

    void update(int p,int l,int r,__int64 val)
    {
    if(l<=tree[p].l && tree[p].r<=r)
    {
    tree[p].add=val;
    tree[p].status=(__int64)(tree[p].r-tree[p].l+1)*val;
    return ;
    }
    pushdown(p);
    int mid=(tree[p].l + tree[p].r)>>1;
    if(r<=mid)
    update(p<<1,l,r,val);
    else if(l>mid)
    update(p<<1|1,l,r,val);
    else
    {
    update(p<<1,l,mid,val);
    update(p<<1|1,mid+1,r,val);
    }
    tree[p].status=tree[p<<1].status+tree[p<<1|1].status;
    }
    __int64 a;
    void query(int p,int l,int r)
    {
    if(l<=tree[p].l && r>=tree[p].r)
    {
    a+=tree[p].status;
    return ;
    }
    pushdown(p);
    int mid=(tree[p].l + tree[p].r)>>1;
    if(r<=mid)
    query(p<<1,l,r);
    else if(l>mid)
    query(p<<1|1,l,r);
    else
    {
    query(p<<1,l,mid);
    query(p<<1|1,mid+1,r);
    }
    }


    int main()
    {
    int len,Q;
    while(~scanf("%d%d",&Q,&len))
    {
    char op[3];
    build(1,1,len);
    int x,y;
    while(Q--)
    {
    scanf("%s",op);
    if(op[0]=='Q')
    {
    a=0;
    scanf("%d%d",&x,&y);
    query(1,x,y);
    printf("%I64d %I64d %I64d\n",a/1000000,y-x+1-(a/1000000)-(a%1000000),a%1000000 );
    }
    else
    {
    scanf("%d%d",&x,&y);
    if(x>y)
    update(1,y,x,1000000);
    else
    update(1,x,y,1);
    }
    }
    }
    return 0;
    }



0 0