【HDOJ】5057 Argestes and Sequence

树状数组,其实很简单。只是MLE。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 #define MAXN 100005
 7
 8 short sum[10][10][MAXN];
 9 char bit[10][10][MAXN];
10 int a[MAXN];
11 int t, n, m;
12 int d, p;
13 const int MOD = 32768;
14
15 int lowbit(int x) {
16     return x & -x;
17 }
18
19 int getSum(int x, int d, int p) {
20     int ret = 0;
21     while (x > 0) {
22         ret += (int)sum[d][p][x] + ((int)bit[d][p][x]) * MOD;
23         x -= lowbit(x);
24     }
25     return ret;
26 }
27
28 void update(int x, int d, int p, int r) {
29     int tmp;
30     while (x <= n) {
31         tmp = (int)sum[d][p][x] + r;
32         sum[d][p][x] = tmp%MOD;
33         bit[d][p][x] += tmp/MOD;
34         x += lowbit(x);
35     }
36 }
37
38 int query(int l, int r) {
39     return getSum(r, d-1, p) - getSum(l-1, d-1, p);
40 }
41
42 void insert(int x, int i, int delta) {
43     int j;
44     for (j=1; j<=10; ++j) {
45         update(i, j-1, x%10, delta);
46         x /= 10;
47     }
48 }
49
50 int main() {
51     int i, j, ans;
52     int x, y;
53     char cmd[3];
54
55     scanf("%d", &t);
56     while (t--) {
57         scanf("%d %d", &n, &m);
58         for(i=0; i<10; i++) {
59             for(j=0; j<10; j++) {
60                 memset(sum[i][j], 0, sizeof(short)*(n+1));
61                 memset(bit[i][j], 0, sizeof(char)*(n+1));
62             }
63         }
64         for (i=1; i<=n; ++i) {
65             scanf("%d", &a[i]);
66             insert(a[i], i, 1);
67         }
68         while (m--) {
69             scanf("%*c%s %d %d", cmd, &x, &y);
70             if (cmd[0] == ‘S‘) {
71                 insert(a[x], x, -1);
72                 a[x] = y;
73                 insert(a[x], x, 1);
74             } else {
75                 scanf("%d %d", &d, &p);
76                 ans = query(x, y);
77                 printf("%d\n", ans);
78             }
79         }
80     }
81
82     return 0;
83 }
时间: 2024-10-13 14:15:04

【HDOJ】5057 Argestes and Sequence的相关文章

【HDOJ】5063 Operation the Sequence

1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 100005 6 #define MOD 1000000007 7 int que[MAXN], front = 0, rear = 0; 8 int n; 9 10 void query(int index) { 11 int r = rear; 12 int square = 0; 13 int half =

【HDOJ】5288 OO’s Sequence

二分寻找对于指定pos的最左因数点和最右因数点. 1 /* 5288 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <

【分块】hdu5057 Argestes and Sequence

分块,v[i][j][k]表示第i块内第j位是k的元素数.非常好写.注意初始化 要注意题意,①第i位是从右往左算的. ②若x没有第i位,则用前导零补齐10位.比如103---->0000000103. 1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 using namespace std; 5 const int MOD[]={1,10,100,1000,10000,100000,1000000

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t