bestcoder #45 分类: bestcoder 2015-06-26 02:00 21人阅读 评论(0) 收藏

试试bestcoder题目质量,感觉还不错,有时间可以打打。

题解去bestcoder找吧:http://bestcoder.hdu.edu.cn/


以下是我的1001~1003代码,bestcoder的题目都可以在hdu上提交。


注意hdu是windows系统,栈空间极小。。。1003遍历树的时候注意栈溢出的问题。。。


#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<map>
#include<string>
#include<stack>
#include<queue>
#include<utility>
#include<iostream>
#include<algorithm>

#define ONLINE_JUDGE

void work()
{
    long long n;
    int ans = 0;
    int pre = 0, now;

    std::cin >> n;

    while(n)
    {
        now = n&1;

        if(!pre && now)
            ans++;

        pre = now, n >>= 1; 
    }

    std::cout << ans << std::endl;
}

int main()
{
    int T;
#ifndef ONLINE_JUDGE
    freopen("bc1001.in","r",stdin);
    freopen("bc1001.out","w",stdout);
#endif

    std::cin >> T;

    while(T--) work();

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<map>
#include<string>
#include<stack>
#include<queue>
#include<utility>
#include<iostream>
#include<algorithm>

const int maxn = 1005;

int n, Q;
int a[maxn], b[maxn];
int g[maxn][maxn];

void Bound()
{
    static int bowl[maxn]; int bl;

    for(int i = 1; i <= n; i++)
        bowl[i] = a[i];

    std::sort(bowl+1, bowl+n+1);
    bl = std::unique(bowl+1, bowl+n+1)-(bowl+1);

    for(int i = 1; i <= n; i++)
        a[i] = std::lower_bound(bowl+1, bowl+bl+1, a[i])-bowl;      
}
#define lowbit(x) (-(x)&(x))
int count(int x)
{
    int sum = 0;

    while(x > 0)
        sum += b[x], x -= lowbit(x);

    return sum;
}
void add(int x)
{
    while(x <= n)
        b[x] ++, x += lowbit(x);     
}
void inver(int st, int ans[])
{
    memset(b, 0, sizeof(b));

    for(int i = st; i <= n; i++)
        ans[i] = ans[i-1] + count(n)-count(a[i]), add(a[i]);
}

void Work()
{
    for(int i = 1; i <= n; i++)
        scanf("%d",a+i);    

    Bound();

    for(int i = 1; i <= n; i++)
        inver(i, g[i]);     
}
void Answer()
{
    for(int i = 1, l, r; i <= Q; i++)
        scanf("%d%d",&l,&r), printf("%d\n",g[l][r]);    
}

int main()
{
    int T;
#ifndef ONLINE_JUDGE
    freopen("bc1002.in","r",stdin);
    freopen("bc1002.out","w",stdout);
#endif

    while(scanf("%d%d",&n,&Q)!=EOF)
        Work(), Answer();

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<map>
#include<string>
#include<stack>
#include<queue>
#include<utility>
#include<iostream>
#include<algorithm>

const int maxn = 1e5+5, logN = 17;

int n, Q;
struct Edge
{
    int v, next;
    Edge(){}
    Edge(int v,int next):v(v),next(next){};
}edge[maxn<<1];

int el, head[maxn], fa[maxn][logN];
int dl, st[maxn], ed[maxn], dfn[maxn];
int tree[maxn<<2], w[maxn], dep[maxn];

void NewEdge(int u,int v)
{
    edge[++el] = Edge(v, head[u]), head[u] = el;
}
void Build(int a)
{
    dfn[++dl] = a;

    st[a] = dl;

    dep[a] = dep[fa[a][0]]+1;

    for(int i = head[a]; i; i = edge[i].next)
    {
        if(edge[i].v == fa[a][0]) continue;

        fa[edge[i].v][0] = a, Build(edge[i].v);
    }

    ed[a] = dl;
}
#define L(x) ((x)<<1)
#define R(x) ((x)<<1|1)
void PushDown(int x)
{
    tree[L(x)] ^= tree[x];
    tree[R(x)] ^= tree[x];
    tree[x] = 0;
}


void xoradd(int l,int r,int val,int ll,int rr,int s)
{
    if(l == ll && r == rr)
        tree[s] ^= val;
    else
    {
        int mid = (ll + rr)>>1;

        PushDown(s);

        if(r <= mid)
            xoradd(l, r, val, ll, mid, L(s));
        else if(l > mid)
            xoradd(l, r, val, mid+1, rr, R(s));
        else
            xoradd(l, mid, val, ll, mid, L(s)),
            xoradd(mid+1, r, val, mid+1, rr, R(s));     
    }
}
int ask(int k, int ll, int rr, int s)
{
    if(ll == rr)
        return tree[s];
    else
    {
        int mid = (ll + rr)>>1;

        PushDown(s);

        if(k <= mid)
            return ask(k , ll, mid, L(s));
        else
            return ask(k, mid+1, rr, R(s));
    }
}
void PreLCA()
{    
    for(int j = 1; j < logN; j++)
        for(int i = 1; i <= n; i++)
            fa[i][j] = fa[fa[i][j-1]][j-1]; 
}
int LCA(int u,int v)
{
    if(dep[u] > dep[v]) std::swap(u,v);

    for(int i = logN - 1; i >= 0; i--)
        if(dep[fa[v][i]] >= dep[u]) v = fa[v][i];

    if(u == v) return u;

    for(int i = logN - 1; i >= 0; i--)
        if(fa[u][i] != fa[v][i])
            u = fa[u][i], v = fa[v][i]; 

    return fa[u][0];    
}
void Solve()
{
    scanf("%d%d",&n,&Q);

    dl = 0, el = 0;
    memset(head, 0, sizeof(head));
    memset(edge, 0, sizeof(edge));
    memset(tree, 0, sizeof(tree));
    memset(w, 0, sizeof(w));
    memset(st, 0, sizeof(st));
    memset(ed, 0, sizeof(ed));
    memset(dfn, 0, sizeof(dfn));
    memset(fa, 0, sizeof(fa));


    for(int i = 1, u, v; i < n; i++)
        scanf("%d%d",&u,&v),
        NewEdge(u, v), NewEdge(v, u);

    Build(1), PreLCA(); 

    for(int i = 1; i <= n; i++)
        scanf("%d",w+i), ++w[i], xoradd(st[i], ed[i], w[i], 1, n, 1);

    for(int i = 1, c, x, y; i <= Q; i++)
    {
        scanf("%d%d%d",&c,&x,&y);

        switch(c)
        {
            case 0:
                xoradd(st[x], ed[x], w[x], 1, n, 1);
                w[x] = ++y;
                xoradd(st[x], ed[x], w[x], 1, n, 1);
                break;
            case 1:
                int ans = w[LCA(x,y)]^ask(st[x], 1, n, 1)^ask(st[y], 1, n, 1);
                printf("%d\n",--ans);
                break;
        }   
    }
}

int main()
{
    int T;
#ifndef ONLINE_JUDGE
    freopen("bc1003.in","r",stdin);
    freopen("bc1003.out","w",stdout);
#endif

    std::cin >> T;

    while(T--) Solve();

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-06-26 02:00  <Dash>  阅读(123)  评论(0编辑  收藏  举报