LTIME16小结(CodeChef)

题目链接

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

A

 1 /*************************************************************************
 2     > File Name: A.cpp
 3     > Author: Stomach_ache
 4     > Mail: [email protected]
 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: [email protected]
 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: [email protected]
  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

时间: 2024-08-01 10:42:02

LTIME16小结(CodeChef)的相关文章

使用Apache POI导出Excel小结--导出XLS格式文档

使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI导出Excel小结--导出XLSX格式文档 使用Apache POI导出Excel--大数量导出 导出XLS格式文档 做企业应用项目难免会有数据导出到Excel的需求,最近在使用其,并对导出Excel封装成工具类开放出来供大家参考.关于Apache POI Excel基本的概念与操作我在这里就不啰嗦

【转载】小结一下linux 2.6内核的四种IO调度算法

在LINUX 2.6中,有四种关于IO的调度算法,下面综合小结一下: 1) NOOP NOOP算法的全写为No Operation.该算法实现了最最简单的FIFO队列,所有IO请求大致按照先来后到的顺序进行操作.之所以说“大致”,原因是NOOP在FIFO的基础上还做了相邻IO请求的合并,并不是完完全全按照先进先出的规则满足IO请求.NOOP假定I/O请求由驱动程序或者设备做了优化或者重排了顺序(就像一个智能控制器完成的工作那样).在有些SAN环境下,这个选择可能是最好选择.Noop 对于 IO

Android基础入门教程——8.1.3 Android中的13种Drawable小结 Part 3

Android基础入门教程--8.1.3 Android中的13种Drawable小结 Part 3 标签(空格分隔): Android基础入门教程 本节引言: 本节我们来把剩下的四种Drawable也学完,他们分别是: LayerDrawable,TransitionDrawable,LevelListDrawable和StateListDrawable, 依旧贴下13种Drawable的导图: 1.LayerDrawable 层图形对象,包含一个Drawable数组,然后按照数组对应的顺序来

Android基础入门教程——8.1.2 Android中的13种Drawable小结 Part 2

Android基础入门教程--8.1.2 Android中的13种Drawable小结 Part 2 标签(空格分隔): Android基础入门教程 本节引言: 本节我们继续来学习Android中的Drawable资源,上一节我们学习了: ColorDrawable:NinePatchDrawable: ShapeDrawable:GradientDrawable!这四个Drawable~ 而本节我们继续来学习接下来的五个Drawable,他们分别是: BitmapDrawable:Insert

安卓小结《1》

Activity的生命周期和启动模式的知识点小结: 1.如果Activity切换的时候,新Activity是透明,旧的不会走onStop方法. 2.新的Activity切换的时候,旧Activity  会先执行,onpause,然后才会启动新的activity. 3. Activity在异常情况下被回收时,onSaveInstanceState方法会被回调,回调时机是在onStop之前,当Activity被重新创建的时 候,onRestoreInstanceState方法会被回调,时序在onSt

date命令小结

在写linux shell脚本时,date是经常要用到的一个命令,这篇文章就此做个小结,以防自己用到时到处找 1.最基本的,显示当前的具体时期:直接敲入 date即可,如下, [email protected]:~/scripts$ date 2015年 01月 03日 星期六 21:46:49 CST 2.显示某个文件上次修改的时间:date -r file [email protected]:~/scripts$ date -r save.sh 2015年 01月 02日 星期五 23:29

java 小结2 多态问题。

面向对象这个东西,其实我们一直是不是都没有感觉到自己在用,以后我一定要用用.以前学c#时候认真的看过一次,最近一直研究java.随便再看看. 多态问题: 在java中多态分为(1)编译时多态和(2)运行时多态 (1)编译时多态比较容易理解:其实就是通过方法重载,就是方法的重载,同一个函数名但是可以参数不一样.这就是重载(so easy) (2)运行时多态:这个是通过方法覆盖实现的,就是子类在继承父类的时候,通过对某个方法的重写,覆盖父类方法. 简单的说:比如我们有个父类A,子类B通过Extend

php操作xml小结

<?php #php操作xml,SimpleXMLElement类小结 header('Content-type:text/html;charset=utf-8;'); //1.构造函数 /* $xmlstring=<<<XML <?xml version="1.0" encoding="utf-8"?> <note  xmlns:b="http://www.w3school.com.cn/example/&quo

网络安全解决之个人小结

方案分为安全技术部分和安全管理部分. 安全技术部分: 1.物理安全 需要建设独立的计算机机房,满足防水.防火.防静电等要求.机房设置门禁和视频监控. 2.网络安全 采用防火墙进行安全区域分割,把公司网络分为服务器区和办公区.设置不同的安全规则以防范黑客攻击.采用上网行为管理产品对网络行为和流量进行管控. 3.系统安全 采用终端安全管理系统,对客户端进行管控,重点管控网络行为.补丁升级和软件分发等.对服务器进行安全加固,保障服务器安全. 4.应用安全 对Web电子商务服务器进行漏洞扫描和加固,防范