ACM-ICPC 2018 徐州赛区网络预赛

H. Ryuji doesn’t want to study

Ryuji is not a good student, and he doesn’t want to study. But there are n books he should learn, each book has its knowledge a[i]. Unfortunately, the longer he learns, the fewer he gets. That means, if he reads books from l to r, he will get a[l]*L+a[l+1]*(L-1)+…+a[r-1]*2+a[r] (L is the length of [l, r] that equals to r-l+1).

Now Ryuji has qq questions, you should answer him:

1. If the question type is 1, you should answer how much knowledge he will get after he reads books [ l, r ].

2. If the question type is 2, Ryuji will change the ith book's knowledge to a new value.

Input

First line contains two integers n and q (n, q <= 100000).
The next line contains n integers represent a[i]( a[i] <= 1e9)
Then in next q line each line contains three integers a, b, c, if a = 1, it means question type is 1, and b, c represents [l, r]. if a = 2, it means question type is 2, and b, c means Ryuji changes the bth book’ knowledge to c.

Output

For each question, output one line with one integer represent the answer.

样例输入

5 3
1 2 3 4 5
1 1 3
2 5 0
1 4 5

样例输出

10
8
解题思路:BIT,纸上减减画画便可得出。
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn=1e5+5;
 5 LL val[maxn],sum1[maxn],sum2[maxn];int n,q,a,b,c;
 6 int lowbit(int x){return x&-x;}
 7 void add(LL *sum,int x,LL val){
 8     for(int i=x;i<=n;i+=lowbit(i))
 9         sum[i]+=val;
10 }
11 LL query(LL *sum,int x){
12     LL mul=0;
13     for(int i=x;i>0;i-=lowbit(i))
14         mul+=sum[i];
15     return mul;
16 }
17 int main() {
18     while(~scanf("%d%d",&n,&q)){
19         memset(sum1,0,sizeof(sum1));
20         memset(sum2,0,sizeof(sum2));
21         for(int i=1;i<=n;++i){
22             scanf("%lld",&val[i]);
23             add(sum1,i,val[i]);
24             add(sum2,i,val[i]*(n-i+1));
25         }
26         while(q--){
27             scanf("%d%d%d",&a,&b,&c);
28             if(a==2){
29                 add(sum1,b,c-val[b]);
30                 add(sum2,b,(c-val[b])*(n-b+1));
31                 val[b]=c;
32             }
33             else printf("%lld\n",query(sum2,c)-query(sum2,b-1)-(n-b+1-(c-b+1))*(query(sum1,c)-query(sum1,b-1)));
34         }
35     }
36     return 0;
37 }

I. Characters with Hash

Mur loves hash algorithm, and he sometimes encrypt another one’s name, and call him with that encrypted value. For instance, he calls Kimura KMR, and calls Suzuki YJSNPI. One day he read a book about SHA-256, which can transit a string into just 256 bits. Mur thought that is really cool, and he came up with a new algorithm to do the similar work. The algorithm works this way: first we choose a single letter L as the seed, and for the input(you can regard the input as a string s, s[i] represents the i th character in the string)we calculates the value(|(int) L – s[i]|), and write down the number(keeping leading zero. The length of each answer equals to 2 because the string only contains letters and numbers). Numbers writes from left to right,finally transfer all digits into a single integer(without leading zero(s)). For instance, if we choose ‘z’ as the seed, the string “oMl” becomes “11 45 14”. It’s easy to find out that the algorithm cannot transfer any input string into the same length. Though in despair, Mur still wants to know the length of the answer the algorithm produces. Due to the silliness of Mur, he can even not figure out this, so you are assigned with the work to calculate the answer.

Input

First line a integer T, the number of test cases(T <= 10)
For each test case:
First line contains a integer N and a character z, (N <= 1000000)
Second line contains a string with length N. Problem makes sure that all characters referred in the problem are only letters.

Output

A single number which gives the answer.

样例输入

2
3 z
oMl
6 Y
YJSNPI

样例输出

6
10
解题思路:水~
AC代码:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e6+5;
 4 char str[maxn];int res[maxn];
 5 int getd(int x){
 6     int num=0;
 7     while(x){num++,x/=10;}
 8     return num;
 9 }
10 int main(){
11     int t,n,pos,st;char ch;
12     while(~scanf("%d",&t)){
13         while(t--){
14             scanf("%d %c",&n,&ch);
15             scanf("%s",str);st=pos=0;
16             for(int i=0;i<n;++i){
17                 int tmp=fabs((int)ch-str[i]);
18                 int dig=getd(tmp);
19                 if(dig==1)res[pos++]=0,res[pos++]=tmp;
20                 else res[pos++]=tmp/10,res[pos++]=tmp%10;
21             }
22             while(!res[st]&&st<pos)st++;
23             if(st==pos)puts("1");
24             else printf("%d\n",pos-st);
25         }
26     }
27     return 0;
28 }

猜你喜欢

转载自www.cnblogs.com/acgoto/p/9614814.html
今日推荐