zoukankan      html  css  js  c++  java
  • hdu 1166 敌兵布阵

    http://acm.hdu.edu.cn/showproblem.php?pid=1166

    可以直接暴力过:

    View Code
     1 #include <stdio.h>
    2 int main()
    3 {
    4 int a[50005],n,j,p,q,t,i,sum;
    5 char ch[10];
    6 scanf("%d",&t);
    7 for(j = 1;j <= t;j ++)
    8 {
    9 scanf("%d",&n);
    10 for(i = 1;i <= n;i ++)
    11 scanf("%d",&a[i]);
    12 printf("Case %d:\n",j);
    13 while(scanf("%s",ch) != EOF)
    14 {
    15 if(ch[0] == 'E') break;
    16 scanf("%d %d",&p,&q);
    17 if(ch[0] == 'A')
    18 a[p] += q;
    19 if(ch[0] == 'S')
    20 a[p] -= q;
    21 if(ch[0] == 'Q')
    22 {
    23 sum = 0;
    24 for(i = p;i <= q;i ++)
    25 sum += a[i];
    26 printf("%d\n",sum);
    27 }
    28 }
    29 }
    30 return 0;
    31 }
    也可以用树状数组过:
    #include <stdio.h> 
    #include <memory.h>
    #define lowbit(x) ((x)&(-x)) 
    int a[50005],b[50005];
    
    void update(int x,int y,int n) 
    { 
        while(x <= n) 
        {
            a[x] += y;
            x += lowbit(x);
        } 
    }
    int getsum(int x)
    {
        int ret = 0;
        while(x)
        {
            ret += a[x];
            x -= lowbit(x);
        }
        return ret;
    }
    int main()
    {
        int t,i,n,p,q,j,tmp;
        char ch[10];
        scanf("%d",&t);
        for(j = 1;j <= t;j ++)
        {
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            scanf("%d",&n);
            for(i = 1;i <= n;i ++)
            {
                scanf("%d",&tmp);
                b[i] = b[i-1] + tmp;
                a[i] = b[i] - b[i-lowbit(i)];
            }
            printf("Case %d:\n",j);
            while(scanf("%s",ch) != EOF)
            {
                if(ch[0] == 'E') break;
                scanf("%d %d",&p,&q);
                if(ch[0] == 'A')
                update(p,q,n);
                if(ch[0] == 'S')
                update(p,-q,n);
                if(ch[0] == 'Q')
                printf("%d\n",getsum(q) - getsum(p-1));
            }
            }
        return 0;
    }
    
    还可以用线段树过:
    View Code
     1 #include <iostream>
    2 #include <stdio.h>
    3 #include <stdlib.h>
    4 using namespace std;
    5 struct node
    6 {
    7 int l,r,sum;
    8 node *lc,*rc;
    9 void fun()
    10 {
    11 sum = l = r = 0;
    12 lc = rc = NULL;
    13 }
    14 }*tree;
    15 node *build(int a,int b,node *tree)
    16 {
    17 node *root = new node;
    18 root->fun();
    19 root->l=a,root->r=b;
    20 tree=root;
    21 if(a != b)
    22 {
    23 tree->lc=build(a,(a+b)>>1,tree->lc);
    24 tree->rc=build(1+(a+b)>>1,b,tree->rc);
    25 }
    26 return tree;
    27 }
    28 void update(int way,int num,node *tree)
    29 {
    30 tree->sum += num;
    31 if(tree->l != tree->r)
    32 {
    33 if(way<=(tree->l+tree->r)>>1)
    34 update(way,num,tree->lc);
    35 if(way>(tree->l+tree->r)>>1)
    36 update(way,num,tree->rc);
    37 }
    38 }
    39 int query(int a,int b,node *tree)
    40 {
    41 int sum = 0;
    42 if(a<=tree->l && b>=tree->r)
    43 sum += tree->sum;
    44 else
    45 {
    46 if(a<=(tree->l+tree->r)>>1)
    47 sum += query(a,b,tree->lc);
    48 if(b>(tree->l+tree->r)>>1)
    49 sum += query(a,b,tree->rc);
    50 }
    51 return sum;
    52 }
    53 void release(node *tree)
    54 {
    55 if(tree->lc) release(tree->lc);
    56 if(tree->rc) release(tree->rc);
    57 free(tree);
    58 }
    59 int main()
    60 {
    61 int t,n,m,p,q,i,j;
    62 char ch[10];
    63 scanf("%d",&t);
    64 for(j = 1;j <= t;j ++)
    65 {
    66 scanf("%d",&n);
    67 tree=build(1,n,tree);
    68 for(i = 1;i <= n;i ++)
    69 {
    70 scanf("%d",&m);
    71 update(i,m,tree);
    72 }
    73 printf("Case %d:\n",j);
    74 while(scanf("%s",ch) != EOF && ch[0]!='E')
    75 {
    76 scanf("%d %d",&p,&q);
    77 if(ch[0] == 'A')
    78 update(p,q,tree);
    79 if(ch[0] == 'S')
    80 update(p,-q,tree);
    81 if(ch[0] == 'Q')
    82 printf("%d\n",query(p,q,tree));
    83 }
    84 release(tree);
    85 }
    86 return 0;
    87 }

     1 #include <stdio.h>
    2 #include <memory.h>
    3 #define lowbit(x) ((x)&(-x))
    4 int a[50005],b[50005];
    5
    6 void update(int x,int y,int n)
    7 {
    8 while(x <= n)
    9 {
    10 a[x] += y;
    11 x += lowbit(x);
    12 }
    13 }
    14 int getsum(int x)
    15 {
    16 int ret = 0;
    17 while(x)
    18 {
    19 ret += a[x];
    20 x -= lowbit(x);
    21 }
    22 return ret;
    23 }
    24 int main()
    25 {
    26 int t,i,n,p,q,j,tmp;
    27 char ch[10];
    28 scanf("%d",&t);
    29 for(j = 1;j <= t;j ++)
    30 {
    31 memset(a,0,sizeof(a));
    32 memset(b,0,sizeof(b));
    33 scanf("%d",&n);
    34 for(i = 1;i <= n;i ++)
    35 {
    36 scanf("%d",&tmp);
    37 b[i] = b[i-1] + tmp;
    38 a[i] = b[i] - b[i-lowbit(i)];
    39 }
    40 printf("Case %d:\n",j);
    41 while(scanf("%s",ch) != EOF)
    42 {
    43 if(ch[0] == 'E') break;
    44 scanf("%d %d",&p,&q);
    45 if(ch[0] == 'A')
    46 update(p,q,n);
    47 if(ch[0] == 'S')
    48 update(p,-q,n);
    49 if(ch[0] == 'Q')
    50 printf("%d\n",getsum(q) - getsum(p-1));
    51 }
    52 }
    53 return 0;
    54 }
  • 相关阅读:
    Java基础知识(四)使用多线程插入数据
    Java基础知识(三)重写equals和hashCode
    Java基础知识(二)基本数据类型转换
    Java基础知识(一)基本数据类型默认值
    C# DES加密,KEY和IV不同设置的写法
    [异常记录(三)] 从 bcp 客户端收到一个对 colid 12 无效的列长度
    ADO.NET 使用DELETE语句批量删除操作,提示超时,删除失败,几种优化解决思路
    [异常记录(二)] 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 <machineKey> 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。
    [解决]JS失效,提示HTML1114: (UNICODE 字节顺序标记)的代码页 utf-8 覆盖(META 标记)的冲突的代码页 utf-8
    SQL SERVER 2012/ 2014 分页,用 OFFSET,FETCH NEXT改写ROW_NUMBER的用法
  • 原文地址:https://www.cnblogs.com/zhangteng512/p/2106980.html
Copyright © 2011-2022 走看看