zoukankan      html  css  js  c++  java
  • bzoj2333

    题解:

    题目编号很牛逼

    2333.。。

    题目就是一个模板。。。

    至于你打不打得过就是问题了

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int N=300005;
    char s[10];
    int n,m,x,y,root,q,val[N],fa[N],_fa[N],c[N][2],_c[N][2],tg[N];
    int down(int x)
    {
        if (tg[x])
         {
            val[c[x][0]]+=tg[x];
            val[c[x][1]]+=tg[x];
            tg[c[x][0]]+=tg[x];
            tg[c[x][1]]+=tg[x];
            tg[x]=0;
         }
    }
    int merge(int x,int y)
    {
        if (!x||!y)return x+y;
        if (val[x]<val[y])swap(x,y);
        down(x);
        c[x][1]=merge(c[x][1],y);
        fa[c[x][1]]=x;
        swap(c[x][0],c[x][1]);
        return x;
    }
    int _merge(int x,int y)
    {
        if (!x||!y)return x+y;
        if (val[x]<val[y])swap(x,y);
        _c[x][1]=_merge(_c[x][1],y);
        _fa[_c[x][1]]=x;
        swap(_c[x][0],_c[x][1]);
        return x;
    }
    int sum(int x)
    {
        int ret=0;
        while (fa[x])x=fa[x],ret+=tg[x];
        return ret;
    }
    int find(int x)
    {
        while (fa[x])x=fa[x];
        return x;
    }
    int del(int x)
    {
        down(x);
        int t=merge(c[x][0],c[x][1]);
        fa[t]=fa[x];
        if (c[fa[x]][0]==x)c[fa[x]][0]=t;
        else c[fa[x]][1]=t;
        c[x][0]=c[x][1]=fa[x]=0;
        return find(t);
    }
    void _del(int x)
    {
        int t=_merge(_c[x][0],_c[x][1]);
        _fa[t]=_fa[x];
        if (root==x)root=t;
        else _c[_fa[x]][0]==x?_c[_fa[x]][0]=t:_c[_fa[x]][1]=t;
        _c[x][0]=_c[x][1]=_fa[x]=0;
    }
    int main()
    {    
        scanf("%d",&n);
        for (int i=1;i<=n;i++)scanf("%d",&val[i]),root=_merge(root,i);
        scanf("%d",&m); 
        while (m--)
         {
            scanf("%s",s);
            if (s[0]=='U')
             {
                scanf("%d%d",&x,&y);
                x=find(x),y=find(y);
                if (x!=y)if (merge(x,y)==x)_del(y);else _del(x);
             }
            if (s[0]=='A')
             {
                if (s[1]=='1')
                 {
                    scanf("%d%d",&x,&y);
                    int k=find(x);_del(k);
                    val[x]+=y+sum(x);
                    k=merge(x,del(x));
                    root=_merge(root,k);
                 }
                if (s[1]=='2')
                 {
                    scanf("%d%d",&x,&y);
                    int k=find(x);_del(k);
                    tg[k]+=y,val[k]+=y;
                    root=_merge(root,k);
                 }
                if (s[1]=='3')scanf("%d",&x),q+=x;
             }
            if (s[0]=='F')
             {
                if (s[1]=='1')scanf("%d",&x),printf("%d
    ",sum(x)+val[x]+q);
                if (s[1]=='2')scanf("%d",&x),printf("%d
    ",val[find(x)]+q);
                if (s[1]=='3')printf("%d
    ",val[root]+q);
             }
         }
        return 0;
    }
  • 相关阅读:
    配置postgres9.3间的fdw——实现不同postgres数据库间的互访问
    linux安装配置postgres及使用dblink
    一次“峰回路转”的troubleshooting经历
    10分钟内把永远跑不完的存储过程变为2秒跑完
    C++ friend关键字
    每天学点Linux命令之 vi 命令
    Shell
    九大排序算法及其实现- 插入.冒泡.选择.归并.快速.堆排序.计数.基数.桶排序.堆排序
    到位
    【LeetCode】-- 260. Single Number III
  • 原文地址:https://www.cnblogs.com/xuanyiming/p/8075472.html
Copyright © 2011-2022 走看看