HDU 3487 Play with Chain

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wang3312362136/article/details/79026190

Problem Description

YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.
At first, the diamonds on the chain is a sequence: 1,2,3,,n.
He will perform two types of operations:
CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.
For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.
FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.
For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8.
He wants to know what the chain looks like after perform m operations. Could you help him?

Input

There will be multiple test cases in a test data.
For each test case, the first line contains two numbers: n and m (1n,m3×100000), indicating the total number of diamonds on the chain and the number of operations respectively.
Then m lines follow, each line contains one operation. The command is like this:
CUT a b c // Means a CUT operation, 1abn,0cn(ba+1).
FLIP a b // Means a FLIP operation, 1a<bn.
The input ends up with two negative numbers, which should not be processed as a case.

Output

For each test case, you should print a line with n numbers. The ith number is the number of the ith diamond on the chain.

Sample Input

8 2
CUT 3 5 4
FLIP 2 6
-1 -1

Sample Output

1 4 3 7 6 2 5 8

Source

2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU

Recommend

zhengfeng | We have carefully selected several similar problems for you: 3486 3479 3480 3481 3482

题目大意

就是说给你一串项链,初始时宝石为1n的顺序,每次两种操作CUT和FLIP,最后项链的样子。

思路

splay直接操作。

代码

#include <cstdio>
#include <cstring>

const int maxn=300000;

int n;

struct splay_tree
{
  int fa[maxn+10],son[2][maxn+10],size[maxn+10],rev[maxn+10],root;

  inline int t(int x)
  {
    return son[1][fa[x]]==x;
  }

  inline int push_rev(int x)
  {
    rev[x]^=1;
    int t=son[0][x];
    son[0][x]=son[1][x];
    son[1][x]=t;
    return 0;
  }

  inline int pushdown(int x)
  {
    if(rev[x])
      {
        if(son[0][x])
          {
            push_rev(son[0][x]);
          }
        if(son[1][x])
          {
            push_rev(son[1][x]);
          }
        rev[x]=0;
      }
    return 0;
  }

  inline int updata(int x)
  {
    return size[x]=size[son[0][x]]+size[son[1][x]]+1;
  }

  inline int rotate(int x)
  {
    int k=t(x),f=fa[x];
    if(fa[f])
      {
        son[t(f)][fa[f]]=x;
      }
    fa[x]=fa[f];
    if(son[!k][x])
      {
        fa[son[!k][x]]=f;
      }
    son[k][f]=son[!k][x];
    fa[f]=x;
    son[!k][x]=f;
    updata(f);
    updata(x);
    return 0;
  }

  inline int splay(int x,int c)
  {
    while(fa[x]!=c)
      {
        int f=fa[x];
        if(fa[f]==c)
          {
            rotate(x);
          }
        else if(t(x)==t(f))
          {
            rotate(f);
            rotate(x);
          }
        else
          {
            rotate(x);
            rotate(x);
          }
      }
    if(!c)
      {
        root=x;
      }
    return 0;
  }

  inline int getkth(int x)
  {
    int now=root;
    while(now)
      {
        pushdown(now);
        if(size[son[0][now]]+1==x)
          {
            return now;
          }
        else if(size[son[0][now]]+1<x)
          {
            x-=size[son[0][now]]+1;
            now=son[1][now];
          }
        else
          {
            now=son[0][now];
          }
      }
    return 0;
  }

  int build(int l,int r)
  {
    int mid=(l+r)>>1;
    rev[mid]=0;
    size[mid]=r-l+1;
    if(l<=mid-1)
      {
        son[0][mid]=build(l,mid-1);
        fa[son[0][mid]]=mid;
      }
    if(mid+1<=r)
      {
        son[1][mid]=build(mid+1,r);
        fa[son[1][mid]]=mid;
      }
    return mid;
  }

  inline int cut(int l,int r,int x)
  {
    int now;
    if(l==1)
      {
        if(r==n)
          {
            now=root;
            root=0;
          }
        else
          {
            int w=getkth(r+1);
            splay(w,0);
            now=son[0][w];
            son[0][w]=0;
            fa[now]=0;
            updata(w);
          }
      }
    else
      {
        int w=getkth(l-1);
        splay(w,0);
        if(r==n)
          {
            now=son[1][w];
            son[1][w]=0;
            fa[now]=0;
          }
        else
          {
            int y=getkth(r+1);
            splay(y,w);
            now=son[0][y];
            son[0][y]=0;
            fa[now]=0;
            updata(y);
          }
        updata(w);
      }
    if(!x)
      {
        if(!root)
          {
            root=now;
          }
        else
          {
            int w=getkth(1);
            splay(w,0);
            son[0][w]=now;
            fa[now]=w;
            updata(w);
          }
      }
    else
      {
        int w=getkth(x);
        splay(w,0);
        if(x==n-(r-l+1))
          {
            son[1][w]=now;
            fa[now]=w;
          }
        else
          {
            int y=getkth(x+1);
            splay(y,w);
            son[0][y]=now;
            fa[now]=y;
            updata(y);
          }
        updata(w);
      }
    return 0;
  }

  inline int reverse(int l,int r)
  {
    if(l==1)
      {
        if(r==n)
          {
            push_rev(root);
          }
        else
          {
            int x=getkth(r+1);
            splay(x,0);
            push_rev(son[0][x]);
          }
      }
    else
      {
        int x=getkth(l-1);
        splay(x,0);
        if(r==n)
          {
            push_rev(son[1][x]);
          }
        else
          {
            int y=getkth(r+1);
            splay(y,x);
            push_rev(son[0][y]);
          }
      }
    return 0;
  }

  inline int mem()
  {
    memset(fa,0,sizeof fa);
    memset(son,0,sizeof son);
    memset(size,0,sizeof size);
    memset(rev,0,sizeof rev);
    return 0;
  }
};

splay_tree st;
int m,a,b,c;
char s[10];

int main()
{
  while(1)
    {
      st.mem();
      scanf("%d%d",&n,&m);
      if((n<0)&&(m<0))
        {
          break;
        }
      st.build(1,n);
      st.root=(n+1)>>1;
      while(m--)
        {
          scanf("%s%d%d",s,&a,&b);
          if(s[0]=='C')
            {
              scanf("%d",&c);
              st.cut(a,b,c);
            }
          else
            {
              st.reverse(a,b);
            }
        }
      for(register int i=1; i<n; ++i)
        {
          printf("%d ",st.getkth(i));
        }
      printf("%d\n",st.getkth(n));
    }
  return 0;
}
展开阅读全文

Play with Chain

10-25

Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.nAt first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.nHe will perform two types of operations:nCUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.nFor example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.nnFLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.nFor example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8nnHe wants to know what the chain looks like after perform m operations. Could you help him?nnInput There will be multiple test cases in a test data.nFor each test case, the first line contains two numbers: n and m (1≤n, m≤3*100000), indicating the total number of diamonds on the chain and the number of operations respectively.nThen m lines follow, each line contains one operation. The command is like this:nCUT a b c // Means a CUT operation, 1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1).nFLIP a b // Means a FLIP operation, 1 ≤ a < b ≤ n.nThe input ends up with two negative numbers, which should not be processed as a case.nnOutput For each test case, you should print a line with n numbers. The ith number is the number of the ith diamond on the chain.nSample Inputn8 2nCUT 3 5 4nFLIP 2 6n-1 -1nnSample Outputn1 4 3 7 6 2 5 8 问答

Frost Chain

12-10

Problem DescriptionnIn the unimaginable popular DotA game, the hero Lich has a wonderful skill: Frost Chain, release a jumping breath of frost that jumps N times against enemy units.nn![](http://acm.hdu.edu.cn/data/images/C300-1006-1.jpg)nnnToday iSea play the role of Lich, at first he randomly chooses an enemy hero to release the skill, then the frost jumps for N times. Each time, it make a damage of one HP unit on this hero (including the first time), then bounces to another hero (can’t be himself) if their distance is no more than D and this hero is alive of course, also randomly. Here random means equal probability.nnNow we know there are always only five enemy heroes, and also their coordinates and HP value. iSea wonders the death probability of each hero. One hero is dead if its HP is equal to or less than zero.nn nnInputnThere are several test cases in the input.nnEach test case begin with two integers N and D (1 <= N <= 25, 1 <= D <= 10000).nThe following line contains ten integers, indicating the coordinates of the five opponents, and -10000 ≤ x, y ≤ 10000.nThen five integers follows, indicating the HP (1 <= HP <= 5) of five opponents.nnThe input terminates by end of file marker.nn nnOutputnFor each test case, output five floating numbers, indicating the death probability of each hero, as the given order, rounded to three fractional digits, and separated by a single blank.n nnSample Inputn3 100n0 1 0 2 0 3 0 4 0 5n1 1 1 1 1n3 1n0 1 0 2 0 3 0 4 0 5n1 1 1 1 1n nnSample Outputn0.800 0.800 0.800 0.800 0.800n0.500 0.800 0.800 0.800 0.500 问答

没有更多推荐了,返回首页