zoukankan      html  css  js  c++  java
  • Codeforces_GYM_100741 A

    http://codeforces.com/gym/100741/problem/A

    A. Queries

    time limit per test
    0.25 seconds
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    Mathematicians are interesting (sometimes, I would say, even crazy) people. For example, my friend, a mathematician, thinks that it is very fun to play with a sequence of integer numbers. He writes the sequence in a row. If he wants he increases one number of the sequence, sometimes it is more interesting to decrease it (do you know why?..) And he likes to add the numbers in the interval [l;r]. But showing that he is really cool he adds only numbers which are equal some mod (modulo m).

    Guess what he asked me, when he knew that I am a programmer? Yep, indeed, he asked me to write a program which could process these queries (n is the length of the sequence):

    • + p r It increases the number with index p by r. ()

      You have to output the number after the increase.

    • - p r It decreases the number with index p by r. () You must not decrease the number if it would become negative.

      You have to output the number after the decrease.

    • s l r mod You have to output the sum of numbers in the interval  which are equal mod (modulo m). () ()
    Input

    The first line of each test case contains the number of elements of the sequence n and the number m. (1 ≤ n ≤ 10000) (1 ≤ m ≤ 10)

    The second line contains n initial numbers of the sequence. (0 ≤ number ≤ 1000000000)

    The third line of each test case contains the number of queries q (1 ≤ q ≤ 10000).

    The following q lines contains the queries (one query per line).

    Output

    Output q lines - the answers to the queries.

    Examples
    input
    3 4
    1 2 3
    3
    s 1 3 2
    + 2 1
    - 1 2
    output
    2
    3
    1

    m个 树状数组记录a[i]%m,得值。

     1 #define LL long long
     2 #include <iostream>
     3 
     4 using namespace std;
     5 
     6 const int MAXN(1000000000);
     7 const int N(10000+15);
     8 LL n,m,a[N],q,u,v,w;
     9 
    10 struct Tree
    11 {
    12     LL mm;
    13     LL val[N];
    14     #define lowbit(x) (x&(-x))
    15     void Update(int now,int x)
    16     {
    17         for(;now<=mm;now+=lowbit(now)) val[now]+=x;
    18     }
    19     LL Query(int x)
    20     {
    21         LL ret=0;
    22         for(;x;x-=lowbit(x)) ret+=val[x];
    23         return ret;
    24     }
    25 }tree[110];
    26 
    27 int main()
    28 {
    29     cin>>n>>m;
    30     for(LL i=0;i<m;i++) tree[i].mm=n;
    31     for(LL i=1;i<=n;i++)
    32     {
    33         cin>>a[i];
    34         tree[a[i]%m].Update(i,a[i]);
    35     }
    36     cin>>q;
    37     for(char ch[2];q--;)
    38     {
    39         cin>>ch>>u>>v;
    40         if(ch[0]=='+')
    41         {
    42             tree[a[u]%m].Update(u,-a[u]);
    43             a[u]+=v;
    44             tree[a[u]%m].Update(u,a[u]);
    45             cout<<a[u]<<endl;
    46         } else 
    47         if(ch[0]=='-')
    48         {
    49             if(a[u]<v) cout<<a[u]<<endl;
    50             else
    51             {
    52                 tree[a[u]%m].Update(u,-a[u]);
    53                 a[u]-=v;
    54                 tree[a[u]%m].Update(u,a[u]);
    55                 cout<<a[u]<<endl;
    56             }
    57         } else
    58         if(ch[0]=='s')
    59         {
    60             cin>>w;
    61             cout<<tree[w].Query(v)-tree[w].Query(u-1)<<endl;
    62         }
    63     }
    64     return 0;
    65 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    android selector失效的原因
    Android TabActivity使用方法
    Android Build.VERSION.SDK_INT兼容介绍
    数组与指针
    字符串与字符串函数
    C控制语句:分支和跳转
    C控制语句:循环
    运算符、表达式、语句
    select
    正则验证数字
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7245653.html
Copyright © 2011-2022 走看看