历届试题_log大侠


标题:Log大侠
    atm参加了速算训练班,经过刻苦修炼,对以2为底的对数算得飞快,人称Log大侠。
    一天,Log大侠的好友 drd 有一些整数序列需要变换,Log大侠正好施展法力...
    变换的规则是: 对其某个子序列的每个整数变为: [log_2 (x) + 1]  其中 [] 表示向下取整,就是对每个数字求以2为底的对数,然后取下整。
    例如对序列 3 4 2 操作一次后,这个序列会变成 2 3 2。
   
    drd需要知道,每次这样操作后,序列的和是多少。
【输入格式】
第一行两个正整数 n m 。
第二行 n 个数,表示整数序列,都是正数。
接下来 m 行,每行两个数 L R 表示 atm 这次操作的是区间 [L, R],数列序号从1开始。
【输出格式】
输出 m 行,依次表示 atm 每做完一个操作后,整个序列的和。
例如,输入:
3 3
5 6 4
1 2
2 3
1 3
程序应该输出:
10
8
6
【数据范围】
对于 30% 的数据, n, m <= 10^3
对于 100% 的数据, n, m <= 10^5

资源约定:
峰值内存消耗 < 256M
CPU消耗  < 1000ms

请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意: main函数需要返回0
注意: 只使用ANSI C/ANSI C++ 标准,不要调用依赖于编译环境或操作系统的特殊函数。
注意: 所有依赖的函数必须明确地在源文件中 #include <xxx>, 不能通过工程设置而省略常用头文件。
提交时,注意选择所期望的编译器类型。
 
思路:首先我的算法可以解决较小的用例,但是否可以通过所有的用例我不清楚,因为蓝桥杯练习系统没有这道题,应该超时,不可能那么简单。个人就是按照题中的要求来的,整个代码的
时间复杂度为O(n^2),只不过要注意log函数在c++下标是e,所以要转换为求下标为2的对数函数则可以表示为log(x)/log(2);
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n,m;
 4 int array[100000];
 5 int f(int a1,int a2)
 6 {
 7     for(int i=a1;i<=a2;i++){
 8         array[i]=(int)(log(array[i])/log(2)+1);
 9     }
10     int sum=0;
11     for(int i=0;i<sizeof(array)/sizeof(int);i++){
12         sum+=array[i];
13     }
14     return sum;
15 }
16 int main()
17 {
18     cin >> n >> m;
19     int ans[m];
20     memset(array,0,sizeof(array));
21     memset(ans,0,sizeof(ans));
22     for(int i=0;i<n;i++){
23         cin >> array[i];
24     }
25     int a1,a2;
26     int t=0;
27     while(m--){
28         cin >> a1 >> a2;
29         a1--;
30         a2--;
31         ans[t++]=f(a1,a2);
32     }
33     for(int i=0;i<t;i++){
34         cout << ans[i] << endl;
35     }
36 }

里对区间里的值进行对数运算,可以看做是更新,区间更新,求和,很明显是线段树...

下面提供一个大佬的代码只供参考:

  1 #include<bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5  
  6 
  7 const int maxn = 100010;
  8 
  9 int num[maxn];
 10 
 11 int tree[maxn*2];
 12 
 13 int n,m;
 14 
 15 int l,r;
 16 
 17 int cnt;
 18 
 19  
 20 
 21  
 22 
 23 void build(int x,int l, int r)
 24 
 25 {    
 26 
 27     if (l == r){
 28 
 29         cin >> tree[x];
 30 
 31         if (tree[x] == 1){//统计数值1的个数 ,方便优化程序 
 32 
 33             cnt++;    
 34 
 35             tree[x] = 2;//将所有1均变为2,防止1干扰程序优化 
 36 
 37         }
 38 
 39         return;
 40 
 41     }
 42 
 43     
 44 
 45     int mid = (l+r)/2;
 46 
 47     build(x*2,l,mid);
 48 
 49     build(x*2+1,mid+1,r);
 50 
 51     tree[x] = tree[x*2]+tree[x*2+1];
 52 
 53 }
 54 
 55  
 56 
 57 void update(int x,int l,int r,int L,int R)
 58 
 59 {
 60 
 61     if (tree[x] == (r-l+1)*2){        //如果全为2,直接返回 
 62 
 63         return ;
 64 
 65     }
 66 
 67     if (l == r){
 68 
 69         tree[x] = num[tree[x]];
 70 
 71         return;
 72 
 73     } 
 74 
 75     
 76 
 77     int mid = (l+r)/2;
 78 
 79     if (R <= mid)
 80 
 81         update(x*2,l,mid,L,R);
 82 
 83     else if (L > mid)
 84 
 85         update(x*2+1,mid+1,r,L,R);
 86 
 87     else{
 88 
 89         update(x*2,l,mid,L,mid);
 90 
 91         update(x*2+1,mid+1,r,mid+1,R);
 92 
 93     }
 94 
 95     tree[x] = tree[x*2]+tree[x*2+1]; 
 96 
 97 } 
 98 
 99  
100 
101 int main()
102 
103 {
104 
105     for(int i = 1; i <= maxn; i++)    //打表 
106 
107         num[i] = (int)(log2(i) + 1);
108 
109     cin >> n >> m;
110 
111  
112 
113     build(1,1,n);
114 
115     while(m--){
116 
117         cin >> l >> r;
118 
119         update(1,1,n,l,r);
120 
121         cout << tree[1]-cnt << endl;
122 
123     }
124 
125     return 0;    
126 
127 } 

猜你喜欢

转载自www.cnblogs.com/henuliulei/p/10909084.html