【HDOJ】4267 A Simple Problem with Integers

树状数组。Easy.

  1 /* 4267 */
  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 <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 #include <iterator>
 20 #include <iomanip>
 21 using namespace std;
 22 //#pragma comment(linker,"/STACK:102400000,1024000")
 23 
 24 #define sti                set<int>
 25 #define stpii            set<pair<int, int> >
 26 #define mpii            map<int,int>
 27 #define vi                vector<int>
 28 #define pii                pair<int,int>
 29 #define vpii            vector<pair<int,int> >
 30 #define rep(i, a, n)     for (int i=a;i<n;++i)
 31 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 32 #define clr                clear
 33 #define pb                 push_back
 34 #define mp                 make_pair
 35 #define fir                first
 36 #define sec                second
 37 #define all(x)             (x).begin(),(x).end()
 38 #define SZ(x)             ((int)(x).size())
 39 #define lson            l, mid, rt<<1
 40 #define rson            mid+1, r, rt<<1|1
 41 
 42 const int maxn = 50005;
 43 const int maxm = 55;
 44 int n;
 45 
 46 
 47 inline int lowest(int x) {
 48     return -x & x;
 49 }
 50 
 51 typedef struct arr_t {
 52     int s[maxn];
 53     
 54     void clr() {
 55         memset(s, 0, sizeof(s));
 56     }
 57     
 58     void update(int x, int delta) {
 59         while (x <= n) {
 60             s[x] += delta;
 61             x += lowest(x);
 62         }
 63     }
 64     
 65     int sum(int x) {
 66         int ret = 0;
 67         
 68         while (x) {
 69             ret += s[x];
 70             x -= lowest(x);
 71         }
 72         return ret;
 73     }
 74 } arr_t;
 75 
 76 arr_t ts[maxm];
 77 int A[maxn], M[11][maxn];
 78 int ID[11][11];
 79 
 80 void init_() {
 81     int cnt = 0;
 82     
 83     rep(i, 1, 11) {
 84         rep(j, 0, i) {
 85             ID[i][j] = cnt++;
 86         }
 87     }
 88     
 89     rep(i, 1, 11) {
 90         int k = i-1;
 91         rep(j, 1, maxn) {
 92             M[k][j] = M[k][j-1] + 1;
 93             if (M[k][j] == i)
 94                 M[k][j] = 0;
 95         }
 96     }
 97 }
 98 
 99 void init() {
100     rep(i, 0, maxm)
101         ts[i].clr();
102 }
103 
104 void update(int a, int b, int k, int delta) {
105     int from = a;
106     int to = a+(b-a)/k*k;
107     int findex = (from+k-1)/k;
108     int tindex = (to+k-1)/k;
109     int id = ID[k][a%k];
110     
111     ts[id].update(findex, delta);
112     ts[id].update(tindex+1, -delta);
113 }
114 
115 int sum(int x) {
116     int id, index, delta;
117     int ret = 0;
118     
119     rep(i, 1, 11) {
120         index = (x+i-1)/i;
121         id = ID[i][M[i-1][x]];
122         delta = ts[id].sum(index);
123         ret += delta;
124     }
125     return ret;
126 }
127 
128 int main() {
129     ios::sync_with_stdio(false);
130     #ifndef ONLINE_JUDGE
131         freopen("data.in", "r", stdin);
132         freopen("data.out", "w", stdout);
133     #endif
134     
135     int type;
136     int a, b, k, c;
137     int q;
138     int ans;
139     
140     init_();
141     while (scanf("%d", &n)!=EOF) {
142         rep(i, 1, n+1)
143             scanf("%d", &A[i]);
144         init();
145         scanf("%d", &q);
146         while (q--) {
147             scanf("%d %d", &type, &a);
148             if (type == 1) {
149                 scanf("%d %d %d", &b, &k, &c);
150                 update(a, b, k, c);
151             } else {
152                 ans = sum(a) + A[a];
153                 printf("%d\n", ans);
154             }
155         }
156     }
157     
158     #ifndef ONLINE_JUDGE
159         printf("time = %d.\n", (int)clock());
160     #endif
161     
162     return 0;
163 }

 

posted on 2015-11-13 17:59  Bombe  阅读(315)  评论(0编辑  收藏  举报

导航