Developing School's Contest 2012-8 by HVCST problem -- 1010

weak node

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)

Problem Description

Country C preparing to attack Country J, but the shortage of troops Country C, Country C to find out the location of weak Country J defense.

Input

Line 1: n m(1<=n<=100,000, 1<=m<=120,000 )
Line 2: n numbers, respectively said the defense force of the Country J n nodes
Line 3 to 2+m: m action, expressed as follows
  find:(i,j) 
  From i to j nodes to identify the weakest defense of the node, and the output of Defense
  move:(i1,i2.....)
  These points will move relative to each other position
  For example, if [8,7,6,5,4,3,2,1], then move(3,5,7) yields [8,7,4,5,2,3,6,1]

Output

For each find, print the weakest defense of the value

Sample Input

8 3
8 7 6 5 4 3 2 1
find(2,5)
move(3,5,7)
find(2,5)

Sample Output

4
2

题目意思:给定一个数组,有两种指令
find指令包含两个数a和b,求区间a到b间最小的值
move指令包含不确定的数,让数组的按这些数循环左移一个位置
这题和去年湖南程序大赛K题一样

采用线段树处理:
find指令直接输出区间最小值
move通过处理更新线段树
View Code
 1 //Accepted    1010    203 MS    1672 KB
 2 
 3 #include <stdio.h>
 4 #include <string.h>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 #define lson l , m , rt << 1
10 #define rson m + 1 , r , rt << 1 | 1
11 const int maxn = 100001;
12 
13 int st_min[maxn<<2];
14 int A[maxn];
15 inline int minn(int a,int b) { return a>b?b:a; }
16 
17 void PushUP(int rt)
18 {
19     st_min[rt] = minn(st_min[rt<<1],st_min[rt<<1|1]);
20 
21 }
22 void build(int l,int r,int rt) {
23     if (l == r)
24     {
25         st_min[rt] = A[l] ;
26         return ;
27     }
28     int m = (l + r) >> 1;
29     build(lson);
30     build(rson);
31     PushUP(rt);
32 }
33 void update(int p,int add,int l,int r,int rt) {
34     if (l == r) 
35     {
36         st_min[rt] = add;
37         return ;
38     }
39     int m = (l + r) >> 1;
40     if (p <= m) update(p , add , lson);
41     else update(p , add , rson);
42     PushUP(rt);
43 }
44 int query_min(int L,int R,int l,int r,int rt) 
45 {
46     if (L <= l && r <= R) 
47     {
48         return st_min[rt];
49     }
50     int m = (l + r) >> 1;
51     int ret1 = 0x7fffffff,ret2 = 0x7fffffff;
52     if (L <= m) ret1 = query_min(L , R , lson);
53     if (R > m) ret2 = query_min(L , R , rson);
54     return minn(ret1,ret2);
55 }
56 
57 int k[maxn];
58 char op[100];
59 
60 int main(void)
61 {
62     int i,n,a,b,m,t;
63     scanf("%d%d",&n,&m);    
64     for(i=1;i<=n;i++)
65         scanf("%d",&A[i]);
66     build(1 , n , 1);
67     while(m--)
68     {
69         scanf("%s",op);
70         if(op[0]=='f')
71         {
72             sscanf(op,"find(%d,%d)",&a,&b);
73             printf("%d\n",query_min(a,b,1,n,1));
74         }
75         else
76         {
77 
78             char *p = strtok(op + 5, "," );
79             int x = 0;
80             while( p )
81             {
82                 sscanf( p, "%d", &k[x] );
83                 p = strtok( NULL, "," );
84                 x++;
85             }
86             t = A[k[0]];
87             for(i=0;i<x-1;i++)
88             {
89                 A[k[i]] = A[k[i+1]];
90                 update(k[i],A[k[i+1]],1,n,1);
91             }
92             A[k[x-1]] = t;
93             update(k[x-1],t,1,n,1);
94                 
95         }
96     }
97     return 0;
98 }

 





posted @ 2012-08-24 19:33  Wheat″  阅读(214)  评论(0)    收藏  举报