codeforces 455D Serega and Fun

本文解析了CodeForces平台上的455D题目,介绍了一种使用分块和双端链表的数据结构解决方案。通过将数组分为多个块并维护每个块内的元素状态,可以有效地处理数组的循环移位和查询特定值出现次数的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/2018/03/02/codeforces-455d/
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:

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].
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 integers a[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: 1 l’i r’i. A query of the second type will be given in format: 2 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.

Examples
Input

Copy
7
6 6 2 7 4 2 5
7
1 3 6
2 2 4 2
2 2 4 7
2 2 2 5
1 2 6
1 1 4
2 1 7 3
Output
2
1
0
0
Input

Copy
8
8 4 2 2 7 7 8 8
8
1 8 8
2 8 1 7
1 8 1
1 7 3
2 8 8 3
1 1 4
1 2 7
1 4 5
Output
2
0
设sum[i][x]表示在i中第K种颜色的数量是多少 那么我sqrt(n)一分块 每次 整块暴力统计 如果有旋转操作 相当于也是只做sqrt块即可

对于这个其实应该用双端链表来搞比较好 每次遍历 块大小的复杂度 找到 删除 插入 但是我一开始比较偷懒写的vector 于是块大小需要sqrt(n)*log2(n)才可以过 因为vector工作原理是每次申请一段连续的空间 如果要删除就整体复制过去 常数很大啊..

#include<cmath>
#include<vector>
#include<cstdio>
#include<algorithm>
#define N 100010
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,s[350][N],ans,left[350],right[350],a[N],b[N],nn,m;
vector<int> block[350];
inline void change(int l,int r){
//  printf("%d %d\n",l,r);
    int v=*(block[b[l]].end()-1),pre=v;
    int v1=block[b[r]][r-left[b[r]]];
    if (b[l]==b[r]){
        block[b[l]].erase(block[b[r]].begin()+r-left[b[r]]);
        block[b[l]].insert(block[b[l]].begin()+l-left[b[l]],v1);
        return;
    }
    --s[b[l]][v];++s[b[l]][v1];
    //vector<int>:: iterator it=block[b[l]].end();
    block[b[l]].erase(block[b[l]].end()-1);
    block[b[l]].insert(block[b[l]].begin()+l-left[b[l]],v1);
    for (int i=b[l]+1;i<=b[r]-1;++i){
        --s[i][*(block[i].end()-1)];++s[i][pre];int pp=block[i][block[i].size()-1];
        block[i].erase(block[i].end()-1);
        block[i].insert(block[i].begin(),pre);pre=pp;
    }
    --s[b[r]][v1];++s[b[r]][pre];
    block[b[r]].erase(block[b[r]].begin()+r-left[b[r]]);
    block[b[r]].insert(block[b[r]].begin(),pre);
/*  for (int i=1;i<=n/nn+1;++i) 
        for (int j=0;j<block[i].size();++j) printf("%d ",block[i][j]);puts("");*/
        /*

    for (int i=1;i<=n/nn+1;++i){
        for (int j=1;j<=n;++j) printf("%d:%d\n",j,s[i][j]);
    }*/
}
inline int query(int l,int r,int k){
    int tmp=0;
    if (b[l]==b[r]) {for (int i=l;i<=r;++i) tmp+=(block[b[l]][i-left[b[l]]]==k);return tmp;}
    for (int i=l;i<=right[b[l]];++i) tmp+=(block[b[l]][i-left[b[l]]]==k);
    for (int i=left[b[r]];i<=r;++i) tmp+=(block[b[r]][i-left[b[r]]]==k);
    for (int i=b[l]+1;i<=b[r]-1;++i) tmp+=s[i][k];return tmp;
}
int main(){
//  freopen("cf455d.in","r",stdin);
//  freopen("cf.out","w",stdout); 
    n=read();nn=sqrt(n)*log2(n);
    for (int i=1;i<=n/nn+1;++i) left[i]=(i-1)*nn+1,right[i]=i*nn; left[n/nn+1]=(n/nn)*nn+1;right[n/nn+1]=n;
//  for (int i=1;i<=n/nn+1;++i) printf("%d %d\n",left[i],right[i]);
    for (int i=1;i<=n;++i){
        a[i]=read();b[i]=(i-1)/nn+1;++s[b[i]][a[i]];block[b[i]].push_back(a[i]);
    }m=read();
    //
    while(m--){
        int op=read(),l1=(read()+ans-1)%n+1,r1=(read()+ans-1)%n+1,k;
        if(l1>r1) swap(l1,r1);
        if (op==1) change(l1,r1);
        if (op==2) k=(read()+ans-1)%n+1,printf("%d\n",ans=query(l1,r1,k));
    }
    return 0;
}
### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值