码迷,mamicode.com
首页 > 其他好文 > 详细

LTIME16小结(CodeChef)

时间:2014-09-28 23:06:15      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

题目链接

最后一题是Splay...还没有学会。。蒟蒻!!!

A

 1 /*************************************************************************
 2     > File Name: A.cpp
 3     > Author: Stomach_ache
 4     > Mail: sudaweitong@gmail.com
 5     > Created Time: 2014年09月28日 星期日 13时33分32秒
 6     > Propose: 
 7  ************************************************************************/
 8 
 9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <fstream>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 /*Let‘s fight!!!*/
18 
19 int n, A[100005];
20 
21 int main(void) {
22     ios::sync_with_stdio(false);
23     int t;
24     cin >> t;
25     while (t--) {
26         cin >> n;
27         for (int i = 0; i < n; i++) cin >> A[i];
28         sort(A, A + n);
29         long long res = 0;
30         for (int i = n - 1; i >= 0; i -= 2) res += A[i];
31         cout << res << endl;
32     }
33     return 0;
34 }

B

处理出每个素数在所有数中出现的次数最多的个数。

 1 /*************************************************************************
 2     > File Name: B.cpp
 3     > Author: Stomach_ache
 4     > Mail: sudaweitong@gmail.com
 5     > Created Time: 2014年09月28日 星期日 13时36分46秒
 6     > Propose: 
 7  ************************************************************************/
 8 #include <map>
 9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <vector>
13 #include <fstream>
14 #include <cstring>
15 #include <iostream>
16 #include <algorithm>
17 using namespace std;
18 /*Let‘s fight!!!*/
19 
20 typedef pair<int, int> pii;
21 const int MAX_N = 100005;
22 const int MAX_M = 1000005;
23 bool vis[MAX_M];
24 int prime[MAX_N], A[MAX_N], cnt;
25 
26 void init() {
27       cnt = 0;
28       memset(vis, false, sizeof(vis));
29     for (int i = 2; i < MAX_M; i++) {
30           if (!vis[i]) {
31               prime[++cnt] = i;
32             for (int j = 2*i; j < MAX_M; j += i) vis[j] = true;
33         }
34     }
35 }
36 
37 #define rep(i, n) for (int i = (1); i <= (n); i++)
38 
39 vector<pii> factor(MAX_M);
40 vector<pii>::iterator it;
41 
42 void work(int x, int y) {
43     if (factor[x].second == 0) { 
44         factor[x].second = y; } 
45     else {
46         factor[x].second = max(y, factor[x].second);
47     }
48 }
49 
50 int main(void) {
51     init();
52     ios::sync_with_stdio(false);
53     int t, n;
54     cin >> t;
55     while (t--) {
56         rep (i, MAX_M) factor[i-1].second = 0;
57         cin >> n;
58         rep (i, n) cin >> A[i];
59         rep (i, n) {
60               int x = A[i];
61             if (x == 1) continue;
62               rep (j, cnt) {
63                   if (prime[j]*prime[j] > A[i]) break;
64                   if (x % prime[j] == 0) {
65                       int tmp = 0;
66                     while (x % prime[j] == 0) tmp++, x /= prime[j];
67                     work(prime[j], tmp);
68                 }
69             }
70             if (x > 1) work(x, 1);
71         }
72         int res = 0;
73         for (it = factor.begin(); it != factor.end(); ++it) {
74             res += it->second;
75         }
76         cout << res << endl;
77     } 
78 
79     return 0;
80 }

C

线段树。

对于第一种修改,就是区间减1

对于第二种修改,就是单点更新

最后,查询每个点2,3,5因子的个数。

  1 /*************************************************************************
  2     > File Name: C.cpp
  3     > Author: Stomach_ache
  4     > Mail: sudaweitong@gmail.com
  5     > Created Time: 2014年09月28日 星期日 14时06分24秒
  6     > Propose: 
  7  ************************************************************************/
  8 #include <cmath>
  9 #include <string>
 10 #include <cstdio>
 11 #include <fstream>
 12 #include <cstring>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 /*Let‘s fight!!!*/
 17 
 18 const int MAX_N = 100050;
 19 int factor[3][MAX_N], A[MAX_N], id[MAX_N];
 20 #define rep(i, n) for (int i = (1); i <= (n); i++)
 21 #define lson(x) ((x<<1))
 22 #define rson(x) ((x<<1) | 1)
 23 
 24 struct node {
 25     int l, r, Min[3];
 26 }Tr[MAX_N<<2];
 27 
 28 void build(int rt, int l, int r) {
 29     Tr[rt].l = l, Tr[rt].r = r;
 30     rep(i, 3) Tr[rt].Min[i-1] = 0;
 31     if (l == r) {
 32         rep (i, 3) Tr[rt].Min[i-1] = factor[i-1][l];
 33         id[l] = rt;
 34         return ;
 35     }
 36     int mid = (l + r) / 2;
 37     build(lson(rt), l, mid);
 38     build(rson(rt), mid + 1, r);
 39 }
 40 
 41 void pushdown(int rt, int p) {
 42     if (Tr[rt].Min[p] != 0) {
 43         Tr[lson(rt)].Min[p] += Tr[rt].Min[p];
 44         Tr[rson(rt)].Min[p] += Tr[rt].Min[p];
 45         Tr[rt].Min[p] = 0;
 46     }
 47 }
 48 
 49 void update1(int rt, int l, int r, int p) {
 50     if (Tr[rt].l >= l && Tr[rt].r <= r) {
 51         Tr[rt].Min[p]--;
 52         return ;
 53     }
 54     pushdown(rt, p);
 55     int mid = Tr[lson(rt)].r;
 56     if (l <= mid) update1(lson(rt), l, r, p);
 57     if (r > mid) update1(rson(rt), l, r, p);
 58 }
 59 
 60 void update2(int rt, int l, int p, int d) {
 61     if (Tr[rt].l == Tr[rt].r && Tr[rt].l == l) {
 62         Tr[rt].Min[p] = d;    
 63         return ;
 64     }
 65     pushdown(rt, p);
 66     int mid = Tr[lson(rt)].r;
 67     if (l <= mid) update2(lson(rt), l, p, d);
 68     else update2(rson(rt), l, p, d);
 69 }
 70 
 71 int query(int rt, int l, int p) {
 72     if (Tr[rt].l == Tr[rt].r && Tr[rt].l == l) {
 73         return max(Tr[rt].Min[p], 0);
 74     }
 75     pushdown(rt, p);
 76     int mid = Tr[lson(rt)].r;
 77     if (l <= mid) query(lson(rt), l, p);
 78     else query(rson(rt), l, p);
 79 }
 80 
 81 void read(int &res) {
 82     res = 0;
 83     char c =  ;
 84     while (c < 0 || c > 9) c = getchar();
 85     while (c >= 0 && c <= 9) res = res*10+c-0, c = getchar();
 86 }
 87 
 88 int POW(int a, int b) {
 89     int res = 1;
 90     while (b) {
 91         if (b & 1) res *= a;
 92         a *= a;
 93         b >>= 1;
 94     }
 95     return res;
 96 }
 97 
 98 int main(void) {
 99     int N, M, a[4] = {0, 2, 3, 5};
100     read(N);
101     rep (i, N) {
102         read(A[i]);
103         int x = A[i];
104         factor[0][i] = factor[1][i] = factor[2][i] = 0;
105         rep (j, 3) {
106             int tmp = 0, p = a[j];
107             while (x % p == 0) tmp++, x /= p;
108             factor[j-1][i] = tmp;
109         }
110         A[i] = x;
111     }
112     build(1, 1, N);
113     read(M);
114     int t, l, r, p, d, tmp;
115     while (M--) {
116         read(t);
117         if (t == 1) {
118             read(l), read(r), read(p);
119             update1(1, l, r, (p+1)/2-1);
120         } else {
121             read(l), read(d);
122             rep (i, 3) {
123                 tmp = 0, p = a[i];
124                 while (d % p == 0) tmp++, d /= p;
125                 update2(1, l, i-1, tmp);
126             }
127             A[l] = d;
128         }
129     }
130     rep (i, N) {
131          rep (j, 3) {
132            tmp = query(1, i, j-1);
133            A[i] *= POW(a[j], tmp);
134        }
135        printf("%d%c", A[i], i == N ? \n :  );
136     }
137 
138     return 0;
139 }

 

D

LTIME16小结(CodeChef)

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/Stomach-ache/p/3998897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!