• UVa 12299 线段树 单点更新 RMQ with Shifts


    因为shift操作中的数不多,所以直接用单点更新模拟一下就好了。

    太久不写线段树,手好生啊,不是这错一下就是那错一下。

    PS:输入写的我有点蛋疼,不知道谁有没有更好的写法。

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 using namespace std;
      7 
      8 const int maxn = 100000 + 10;
      9 const int maxnode = (maxn << 2);
     10 const int INF = 0x3f3f3f3f;
     11 
     12 void scan(int& x)
     13 {
     14     x = 0;
     15     char c = ' ';
     16     while(c < '0' || c > '9') c = getchar();
     17     while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
     18 }
     19 
     20 int qL, qR;
     21 int p, v;
     22 int n, Q;
     23 int minv[maxnode];
     24 
     25 int a[maxn];
     26 
     27 void build(int o, int L, int R)
     28 {
     29     if(L == R) { scan(a[L]); minv[o] = a[L]; return ; }
     30     int M = (L + R) / 2;
     31     build(o<<1, L, M);
     32     build(o<<1|1, M+1, R);
     33     minv[o] = min(minv[o<<1], minv[o<<1|1]);
     34 }
     35 
     36 void update(int o, int L, int R)
     37 {
     38     //if(v >= minv[o]) return ;
     39     if(L == R) { minv[o] = v; return ; }
     40 
     41     int M = (L + R) / 2;
     42     if(p <= M) update(o<<1, L, M);
     43     else update(o<<1|1, M+1, R);
     44     minv[o] = min(minv[o<<1], minv[o<<1|1]);
     45 }
     46 
     47 int query(int o, int L, int R)
     48 {
     49     if(qL <= L && R <= qR) return minv[o];
     50     int ans = INF;
     51     int M = (L + R) / 2;
     52     if(qL <= M) ans = min(ans, query(o<<1, L, M));
     53     if(qR > M) ans = min(ans, query(o<<1|1, M+1, R));
     54     return ans;
     55 }
     56 
     57 char op[50];
     58 
     59 int main()
     60 {
     61     while(scanf("%d%d", &n, &Q) == 2 && n)
     62     {
     63         build(1, 1, n);
     64         while(Q--)
     65         {
     66             scanf("%s", op);
     67             int l = strlen(op);
     68             if(op[0] == 'q')
     69             {
     70                 int i = 6;
     71                 qL = 0;
     72                 while(i < l && op[i] >= '0' && op[i] <= '9') { qL = qL * 10 + op[i] - '0'; i++; }
     73                 while(i < l && (op[i] < '0' || op[i] > '9')) i++;
     74                 qR = 0;
     75                 while(i < l && op[i] >= '0' && op[i] <= '9') { qR = qR * 10 + op[i] - '0'; i++; }
     76                 printf("%d
    ", query(1, 1, n));
     77             }
     78             else
     79             {
     80                 vector<int> hehe;
     81 
     82                 for(int i = 6; i < l;)
     83                 {
     84                     while(i < l && (op[i] < '0' || op[i] > '9')) i++;
     85                     if(i >= l) break;
     86                     int x = 0;
     87                     while(i < l && op[i] >= '0' && op[i] <= '9') { x = x * 10 + op[i] - '0'; i++; }
     88                     hehe.push_back(x);
     89                 }
     90 
     91                 int sz = hehe.size();
     92                 for(int i = 0; i < sz - 1; i++)
     93                 {
     94                     v = a[hehe[i + 1]];
     95                     p = hehe[i];
     96                     update(1, 1, n);
     97                 }
     98                 v = a[hehe[0]];
     99                 p = hehe[sz - 1];
    100                 update(1, 1, n);
    101 
    102                 int t = a[hehe[0]];
    103                 for(int i = 0; i < sz - 1; i++) a[hehe[i]] = a[hehe[i+1]];
    104                 a[hehe[sz - 1]] = t;
    105             }
    106         }
    107     }
    108 
    109     return 0;
    110 }
    代码君
  • 相关阅读:
    Mac 或者 iMac 上传应用到appStore 提交失败
    iOS 上传AppStore 被拒汇总
    iOS 最新修改项目名称
    iOS 之 Cocoa框架
    iOS 之 UIKit框架
    iOS 之 Core Animation
    iOS之Block
    单例模式
    js 关于日期
    利用卷积神经网络对大规模可穿戴传感器数据进行运动分类
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4704242.html
Copyright © 2020-2023  润新知