zoukankan      html  css  js  c++  java
  • CodeChef--EQUAKE

    题目链接

    Earthquake in Bytetown! Situation is getting out of control!

    All buildings in Bytetown stand on straight line. The buildings are numbered 0, 1, ..., N−1 from left to right.

    Every hour one shake occurs. Each shake has 3 parameters: the leftmost building that was damaged during this shake, the corresponding rightmost building, and the force of the shake. Each time all the buildings in the disaster area are damaged equally. Let's consider this process in details.

    At any moment every building has the number that indicates its height (may have leading zeroes). This number corresponds to some string consisting of digits. When some shake affects to the building its string circularly rotates to the left by the value of the force of the shake and its height corresponds to the value of new string. Pay attention that after rotation string may have leading zeroes. For instance: a building with height 950 got in disaster area with force 2, Then its string become 095, so height in reality is 95. If it was one more shake with force 1, then its height would become 950 again.

    Major of Bytetown got some ideas how to protect residents, so sometimes he needs such kind of stats: find height of the highest building on some interval. You are given information about all the heights before the very first shake and then you get queries of two types:

    • Type 0, described by 0 Li Ri Fi: Shake occurred on interval [Li, Ri] with force Fi.
    • Type 1, described by 1 Li Ri: The major wants to know the biggest height on interval [Li, Ri].

    Here, of course, the interval [L, R] contains all the building k such that L ≤ k ≤ R.

    You want to get a promotion and promised to complete this task. Now it's time to do it!

    Input

    Each test file contains only one test case.

    The first line of input contains an integer N denoting the number of buildings in Bytetown. The second line contains N space-separated integers A0, A1, ..., AN−1 denoting the initial height of the buildings. The third line contains an integer M denoting the number of queries. Each of next M lines starts with 0 or1, where 0 denotes a query Type 0 and 1 denotes a Type 1 query. If it's a Type 0 query then 3 integers follows LiRiFi, denoting number of the leftmost building, number of the rightmost building and force of this shake. If it's a Type 1 query then 2 integers follows LiRi, denoting numbers of the leftmost building and the rightmost building of necessary segment.

    Output

    For each Type 1 query, output an integer denoting the answer for the query without leading zeroes.

    Constraints

    • 1 ≤ N ≤ 800000 = 8 × 105
    • 1 ≤ M ≤ 200000 = 2 × 105
    • 0 ≤ Ai < 10000 = 104
    • 0 ≤ Li ≤ Ri < N
    • 0 ≤ Fi ≤ 60
    • Ai does not have leading zeroes

    Example

    Input:
    3
    17 3140 832
    8
    1 0 2
    0 0 2 1
    1 1 2
    1 0 0
    0 0 2 2
    1 0 2
    0 1 1 1
    1 0 2
    Output:
    3140
    1403
    71
    832
    3140
    

    Explanation

    The initial array: [17, 3140, 832].

    The first query is a Type 1 query with numbers of buildings 0 and 2, so the answer is the maximum of the array: 3140.

    The second query is a Type 0 query with numbers of buildings 0 and 2, and force 1, so the array turns to:[71, 1403, 328].

    The third query is a Type 1 query again with numbers of buildings 1 and 2, so the answer is the maximum of 1403 and 3281403

    题意:给n个数,有两种操作,第一种是求区间最大值,第二种是将区间的每个数都循环移动k位,比如345移动1位变成453

    思路:由于数字不大于1e4,可以考虑把每个数的所以状态都记录下来,但是每个数字的位数不一定相同,所以取他们的最大公倍数12即可。

    Accepted Code:

      1 /*************************************************************************
      2     > File Name: EQUAKE.cpp
      3     > Author: Stomach_ache
      4     > Mail: sudaweitong@gmail.com
      5     > Created Time: 2014年09月02日 星期二 22时05分51秒
      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 #define lson(x) (x << 1)
     19 #define rson(x) ((x << 1) | 1)
     20 const int MAX_N = 800050;
     21 int A[MAX_N];
     22 struct node {
     23     int l, r;
     24     int cnt, var[12];
     25 }tr[MAX_N*4];
     26 
     27 int rotate(int x, int k) {
     28     int arr[10], len = 0, res = 0, tmp = x;
     29     while (tmp) {
     30         tmp /= 10;
     31         len++;
     32     }
     33     for (int i = len-1; i >= 0; i--) arr[i] = x % 10, x /= 10;
     34     for (int i = k; i < len + k; i++) res = res * 10 + arr[i%len];
     35 
     36     return res;
     37 }
     38 
     39 void cal(int rt) {
     40     int tmp[12];
     41     for (int i = 0; i < 12; i++) 
     42           tmp[i] = tr[rt].var[(i + tr[rt].cnt) % 12];
     43     for (int i = 0; i < 12; i++) tr[rt].var[i] = tmp[i];
     44 }
     45 
     46 void pushdown(int rt) {
     47       if (tr[rt].cnt != 0) {
     48           cal(rt);
     49           if (tr[rt].l != tr[rt].r) {
     50             tr[lson(rt)].cnt += tr[rt].cnt;
     51             tr[rson(rt)].cnt += tr[rt].cnt;
     52             tr[lson(rt)].cnt %= 12;
     53             tr[rson(rt)].cnt %= 12;
     54         }
     55         tr[rt].cnt = 0;
     56     }
     57 }
     58 
     59 void pushup(int rt) {
     60     for (int i = 0; i < 12; i++) 
     61         tr[rt].var[i] = max(tr[lson(rt)].var[i], tr[rson(rt)].var[i]);
     62 }
     63 
     64 void build(int rt, int L, int R) {
     65     tr[rt].l = L, tr[rt].r = R, tr[rt].cnt = 0;
     66     if (L == R) {
     67         for (int i = 0; i < 12; i++) 
     68               tr[rt].var[i] = rotate(A[L], i);
     69         return ;
     70     }
     71     int mid = (L + R) / 2;
     72     build(lson(rt), L, mid);
     73     build(rson(rt), mid + 1, R);
     74     pushup(rt);
     75 }
     76 
     77 void update(int rt, int L, int R, int v) {
     78     pushdown(rt);
     79     if (L > tr[rt].r || R < tr[rt].l) return ;
     80 
     81       if (tr[rt].l >= L && tr[rt].r <= R) {
     82         tr[rt].cnt += v;
     83         tr[rt].cnt %= 12;
     84         pushdown(rt);
     85         return ;
     86     }
     87     int mid = tr[lson(rt)].r;
     88     update(lson(rt), L, R, v);
     89     update(rson(rt), L, R, v);
     90     pushup(rt);
     91 }
     92 
     93 int query(int rt, int L, int R) {
     94     pushdown(rt);
     95     if (tr[rt].r < L || tr[rt].l > R) return -1;
     96       if (tr[rt].l >= L && tr[rt].r <= R) {
     97           return tr[rt].var[0];
     98     }
     99 
    100     int ql = query(lson(rt), L, R);
    101     int qr = query(rson(rt), L, R);
    102     return max(ql, qr);
    103 }
    104 
    105 int main(void) {
    106     ios_base::sync_with_stdio(false);
    107     int n, m;
    108     cin >> n;
    109     for (int i = 0; i < n; i++) cin >> A[i];
    110     build(1, 0, n - 1);
    111     cin >> m;
    112     while (m--) {
    113         int t;
    114         cin >> t;
    115         if (t == 0) {
    116             int l, r, f;
    117             cin >> l >> r >> f;
    118             update(1, l, r, f);
    119         } else {
    120             int l, r;
    121             cin >> l >> r;
    122             cout << query(1, l, r) << endl;
    123         }
    124     }
    125 
    126     return 0;
    127 }
  • 相关阅读:
    CSS3的常用属性(一)
    Shiro授权认证原理和流程
    mysql存储引擎
    mysql索引的注意事项
    CSS的常用属性(三)
    CSS的常用属性(二)
    CSS的常用属性(一)
    常用文档站点
    Spring和SpringMVC父子容器关系初窥
    内部类学习资料分享
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3955408.html
Copyright © 2011-2022 走看看