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

uva 12003 块状链表

时间:2015-09-05 23:45:05      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

先分块。对于查询,块内排好序二分,对于修改,直接暴力,注意需要维护原来的有序性,不断和块内相邻元素交换即可。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 const int N = 300000;
 9 const int M = 650;
10 int a[N];
11 int b[M][M];
12 int n, m, u, ps, num;
13 
14 void modify( int pos, int val )
15 {
16     int cur = pos / ps;
17     if ( cur < num - 1 )
18     {
19         int p = lower_bound( b[cur], b[cur] + ps, a[pos] ) - b[cur];
20         while ( p + 1 < ps && b[cur][p + 1] < val ) 
21         {
22             b[cur][p] = b[cur][p + 1];
23             p++;
24         }
25         while ( p - 1 >= 0 && b[cur][p - 1] > val )
26         {
27             b[cur][p] = b[cur][p - 1];
28             p--;
29         }
30         b[cur][p] = val;
31     }
32     a[pos] = val;
33 }
34 
35 void update( int l, int r, int v, int p )
36 {
37     int cur = l / ps, ncur = r / ps, k = 0;
38     if ( cur != ncur )
39     {
40         for ( int i = cur + 1; i <= ncur - 1; i++ )
41         {
42             k += lower_bound( b[i], b[i] + ps, v ) - b[i];
43         }
44         for ( int i = l; i < ( cur + 1 ) * ps; i++ )
45         {
46             if ( a[i] < v ) k++;
47         }
48         for ( int i = ncur * ps; i <= r; i++ )
49         {
50             if ( a[i] < v ) k++;
51         }
52     }
53     else
54     {
55         for ( int i = l; i <= r; i++ )
56         {
57             if ( a[i] < v ) k++;
58         }
59     }
60     modify( p, u * 1ll * k / ( r - l + 1 ) );
61 }
62 
63 int main ()
64 {
65     while ( scanf("%d%d%d", &n, &m, &u) != EOF )
66     {
67         ps = 600;
68         for ( int i = 0; i < n; i++ )
69         {
70             scanf("%d", &a[i]);
71             b[i / ps][i % ps] = a[i];
72         }
73         num = ( n + ps - 1 ) / ps;
74         for ( int i = 0; i < num - 1; i++ )
75         {
76             sort( b[i], b[i] + ps );
77         }
78         while ( m-- )
79         {
80             int l, r, v, p;
81             scanf("%d%d%d%d", &l, &r, &v, &p);
82             l--, r--, p--;
83             update( l, r, v, p );
84         }
85         for ( int i = 0; i < n; i++ )
86         {
87             printf("%d\n", a[i]);
88         }
89     }
90     return 0;
91 }

 

uva 12003 块状链表

标签:

原文地址:http://www.cnblogs.com/huoxiayu/p/4784060.html

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