CF455D. Serega and Fun

D. Serega and Fun
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.

You are given an array a consisting of n positive integers and queries to it. The queries can be of two types:

  1. Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:
    a[l], a[l + 1], ..., a[r - 1], a[r] → a[r], a[l], a[l + 1], ..., a[r - 1].
  2. Count how many numbers equal to k are on the segment from l to r (both borders inclusive).

Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of the array. The second line contains n integersa[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).

The third line contains a single integer q (1 ≤ q ≤ 105) — the number of queries. The next q lines contain the queries.

As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: l'i r'i. A query of the second type will be given in format: l'i r'i k'i. All the number in input are integer. They satisfy the constraints: 1 ≤ l'i, r'i, k'i ≤ n.

To decode the queries from the data given in input, you need to perform the following transformations:

li = ((l'i + lastans - 1) mod n) + 1; ri = ((r'i + lastans - 1) mod n) + 1; ki = ((k'i + lastans - 1) mod n) + 1.

Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.

Output

For each query of the 2-nd type print the answer on a single line. 

 

分块搞搞。。比较坑,不好调试。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const int maxs = 800;
const int maxb = maxn / maxs + 100;
const int maxr = 1000;
const int maxv = maxs + maxr + 100;

const bool D = false;

struct Block {
    int size, ele[maxv], cnt[maxn], vals[maxv], tot;
    Block() {
        size = tot = 0;
        memset(cnt, 0, sizeof cnt);
        memset(vals, 0, sizeof vals);
    }
    void append(const int &val) {
        ++cnt[val];
        vals[++tot] = val;
        ele[++size] = val;
    }
    void init() {
        for(int i = 1; i <= tot; ++i) 
            cnt[vals[i]] = 0;
        tot = size = 0;
    }
} b[maxb];
int a[maxn], t[maxn], n, q, nb;

void print() {
    if(D) {
        printf("%d\n", nb);
        for(int i = 1; i <= nb; ++i) {
            for(int j = 1; j <= b[i].size; ++j)
                printf("%d ", b[i].ele[j]);
            puts("");
        }
    }
}


void build() {
    nb = 0;
    for(int i = 1; i <= n; i += maxs) {
        ++nb;
        for(int up = min(n, i + maxs - 1), j = i; j <= up; ++j)
            b[nb].append(a[j]);
    }
}
void re_build() {
    n = 0;
    for(int i = 1; i <= nb; ++i) {
        for(int j = 1; j <= b[i].size; ++j) 
            t[++n] = b[i].ele[j];
        b[i].init();
    }
    for(int i = 1; i <= n; ++i) a[i] = t[i];
    build();
}

int erase(int pos) {
    for(int i = 1, sum = 0; i <= nb; ++i) {
        sum += b[i].size;
        if(pos <= sum) {
            sum -= b[i].size;
            pos -= sum;
            int ret = b[i].ele[pos];
            for(int j = pos + 1; j <= b[i].size; ++j)
                b[i].ele[j - 1] = b[i].ele[j];
            --b[i].cnt[ret];
            --b[i].size;
            return ret;
        }
    }
    return 0;
}

void insert(int pos, int val) {
    for(int i = 1, sum = 0; i <= nb; ++i) {
        sum += b[i].size;
        if(pos <= sum) {
            sum -= b[i].size;
            pos -= sum;
            ++b[i].size;
            for(int j = b[i].size; pos < j; --j)
                b[i].ele[j] = b[i].ele[j - 1];
            b[i].vals[++b[i].tot] = val;
            ++b[i].cnt[val];
            b[i].ele[pos + 1] = val;
            return ;
        }
    }
}

void shift(int l, int r) {
    if(l == r) return ;
    insert(l - 1, erase(r));
}

int count(int pos, int val) {
    if(pos <= 0) return 0;
    int ret = 0;
    for(int i = 1, sum = 0; i <= nb; ++i) {
        sum += b[i].size;
        if(pos <= sum) {
            sum -= b[i].size;
            pos -= sum;
            for(int j = 1; j <= pos; ++j)
                ret += (b[i].ele[j] == val);
            return ret;
        } else {
            ret += b[i].cnt[val];
        }
    }
    return 0;
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; scanf("%d", &a[i]), ++i);
    build();
    print();
    scanf("%d", &q);
    for(int T = 1, last_ans = 0, type, l, r, val; T <= q; ++T) {
        scanf("%d%d%d", &type, &l, &r);
        l = (last_ans + l - 1) % n + 1, r = (last_ans + r - 1) % n + 1;
        if(r < l) swap(l, r);
        if(type == 1) {
            shift(l, r);
        } else {
            scanf("%d", &val);
            val = (last_ans + val - 1) % n + 1;
            last_ans = count(r, val) - count(l - 1, val);
            printf("%d\n", last_ans);
        }
        if(T % maxr == 0) re_build();
    }
    return 0;
}

转载于:https://www.cnblogs.com/hzf-sbit/p/4014381.html

<think>嗯,用户遇到了ModuleNotFoundError: No module named 'pystrat.cst_fun'的错误,需要解决这个问题。首先,我要回忆一下常见的ModuleNotFoundError的原因,通常可能有几个方面:模块未安装、安装路径问题、模块命名错误或者导入语句错误。 首先,用户提到的模块是pystrat.cst_fun,这意味着模块pystrat下有一个子模块cst_fun。可能的两种情况:要么pystrat是一个第三方库,用户需要安装它;要么这是用户自己写的本地模块,但路径配置有问题。 先检查是否安装了pystrat。用户可能没有安装这个库,或者安装的版本不包含cst_fun子模块。这时候需要让用户尝试用pip安装。比如执行pip install pystrat,如果不存在,可能这个库是私有的或者名称有误。如果找不到,可能需要确认库的正确名称,或者是否是内部开发的库。 另外,用户可能在虚拟环境中运行代码,而没有在正确的环境中安装模块。这时候需要确认激活的虚拟环境是否正确,以及是否在该环境中安装了pystrat。 如果模块是本地项目的一部分,那么需要检查文件结构是否正确。比如,是否存在pystrat目录,里面是否有__init__.py文件,以及是否包含cst_fun.py或者cst_fun子目录。同时,项目根目录是否在Python的搜索路径中,或者是否将项目路径添加到sys.path中。 还有一种可能是导入语句的问题。用户可能试图从pystrat中导入cst_fun,但实际模块结构可能不同。例如,可能需要使用from pystrat import cst_fun,或者检查是否存在拼写错误,比如大小写不一致,或者模块名中的下划线是否正确。 另外,参考用户提供的引用信息,其中提到了安装特定版本的ansible,可能用户之前遇到过类似的问题,通过安装特定版本解决。但这里需要确认pystrat是否有版本要求,或者是否安装的版本过旧或过新导致的问题。 总结下来,可能的解决步骤是: 1. 确认是否安装了pystrat库,如果没有,尝试安装。 2. 检查安装是否正确,是否存在子模块cst_fun。 3. 检查文件结构和导入语句是否正确。 4. 确认运行环境和路径配置。 可能还需要用户提供更多的信息,比如他们如何安装pystrat,项目结构如何等,但根据现有信息,先给出常见的解决方案。</think>### 解决方案:ModuleNotFoundError: No module named 'pystrat.cst_fun' #### 1. 确认模块安装状态 - **原因**:`pystrat` 或 `pystrat.cst_fun` 未正确安装。 - **操作**: 1. 尝试安装 `pystrat` 包: ```bash pip install pystrat ``` 2. 若安装失败,可能该库未公开发布或名称有误,需确认是否为私有库或拼写错误[^1]。 #### 2. 检查模块结构 - **原因**:模块路径或文件结构不符合 Python 导入规范。 - **操作**: 1. 若 `pystrat` 是本地项目,确保目录结构如下: ``` your_project/ ├── pystrat/ │ ├── __init__.py │ └── cst_fun.py # 或 cst_fun/ 子目录 ``` 2. 确认项目根目录已添加到 Python 路径: ```python import sys sys.path.append("/path/to/your_project") ``` #### 3. 虚拟环境问题 - **原因**:未在正确的虚拟环境中安装模块。 - **操作**: ```bash # 激活虚拟环境后再安装 source venv/bin/activate # Linux/macOS .\venv\Scripts\activate # Windows pip install pystrat ``` #### 4. 导入语句修正 - **原因**:导入语法错误或模块层级不匹配。 - **操作**: 1. 尝试调整导入语句: ```python from pystrat import cst_fun # 若 cst_fun 是子模块 # 或 from pystrat.cst_fun import specific_function # 若需导入具体函数 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值