【HDOJ】5063 Operation the Sequence

本文深入探讨了使用C++语言实现的一种高效数据查询算法,通过定义宏、函数和变量,实现了一个灵活的数据结构。文章详细解释了如何通过循环、条件判断和位运算等操作进行数据查询,提供了具体的代码实例,并通过输入输出操作验证了算法的正确性和性能。此算法特别适用于需要频繁查询大量数据的应用场景,能够显著提高数据处理效率。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 
 5 #define MAXN 100005
 6 #define MOD 1000000007
 7 int que[MAXN], front = 0, rear = 0;
 8 int n;
 9 
10 void query(int index) {
11     int r = rear;
12     int square = 0;
13     int half = (n+1)>>1;
14 
15     while (r-- > front) {
16         if (que[r] == 3) {
17             ++square;
18         } else if (que[r] == 2) {
19             index = n+1-index;
20         } else {
21             if (index > half) {
22                 index = (index - half)*2;
23             } else {
24                 index = 2*index - 1;
25             }
26         }
27     }
28     __int64 ret = index%MOD;
29     for (int i=0; i<square; ++i) {
30         ret = ret*ret%MOD;
31     }
32     printf("%I64d\n", ret);
33 }
34 
35 int main() {
36     int t, m;
37     int x;
38     char cmd[3];
39 
40     scanf("%d", &t);
41     while (t--) {
42         rear = 0;
43         scanf("%d%d", &n, &m);
44         while (m--) {
45             scanf("%s %d", cmd, &x);
46             if (cmd[0] == 'Q') {
47                 query(x);
48             } else {
49                 que[rear++] = x;
50             }
51         }
52     }
53     return 0;
54 }

 

转载于:https://www.cnblogs.com/bombe1013/p/4019877.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值