#10117 「一本通 4.1 练习 2」简单题

1539:简单题

时间限制: 1000 ms         内存限制: 524288 KB
提交数: 299     通过数: 161

【题目描述】

题目来源:CQOI 2006

有一个 nn 个元素的数组,每个元素初始均为 00。有 mm 条指令,要么让其中一段连续序列数字反转——00 变 11,11 变 00(操作 11),要么询问某个元素的值(操作 22)。

例如当 n=20n=20 时,1010 条指令如下:

操作 回答 操作后的数组
1 1 101 1 10 N/AN/A 1111111111000000000011111111110000000000
2 62 6 11 111111–11110000000000111111_11110000000000
2 122 12 00 111111111100–00000000111111111100_00000000
1 5 121 5 12 N/AN/A 1111000000110000000011110000001100000000
2 62 6 00 111100–00001100000000111100_00001100000000
2 152 15 00 111100000011000–00000111100000011000_00000
1 6 161 6 16 N/AN/A 1111011111001111000011110111110011110000
1 11 171 11 17 N/AN/A 1111011111110000100011110111111100001000
2 122 12 11 111101111111–00001000111101111111_00001000
2 62 6 11 111101–11111100001000111101_11111100001000

【输入】

第一行包含两个整数 n,mn,m,表示数组的长度和指令的条数;

以下 mm 行,每行的第一个数 tt 表示操作的种类:

若 t=1t=1,则接下来有两个数 L,RL,R,表示区间 [L,RL,R] 的每个数均反转;

若 t=2t=2,则接下来只有一个数 ii,表示询问的下标。

【输出】

每个操作 22 输出一行(非 00 即 11),表示每次操作 22 的回答。

【输入样例】

20 10
1 1 10
2 6
2 12
1 5 12
2 6
2 15
1 6 16
1 11 17
2 12
2 6

【输出样例】

1
0
0
0
1
1

【提示】

数据范围与提示:

对于 50% 的数据,1≤n≤103,1≤m≤1041≤n≤103,1≤m≤104 ;

对于 100% 的数据,1≤n≤105,1≤m≤5×1051≤n≤105,1≤m≤5×105 ,保证 L≤RL≤R。

【来源】

#include<bits/stdc++.h>
using namespace std;
int a[100005];
int n,k;
int Add(int x,int p){
    for(;x<=n;x+=x&(-x))a[x]+=p;
}
int Que(int x){
    int res=0;
    for(;x>0;x-=x&(-x))res+=a[x];
    return res;
}
int main(){
    //freopen("test.in","r",stdin);
    scanf("%d%d",&n,&k);
    while(k--){
        int t,l,r;
        scanf("%d",&t);
        if(t-1)scanf("%d",&l),printf("%d\n",Que(l)%2);
        else scanf("%d%d",&l,&r),Add(l,1),Add(r+1,-1);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/fdezlsq/p/11432294.html

时间: 2024-08-30 17:40:13

#10117 「一本通 4.1 练习 2」简单题的相关文章

「一本通 4.1 练习 2」简单题

题目描述 题目来源:CQOI 2006 有一个 n 个元素的数组,每个元素初始均为 0.有 m 条指令,要么让其中一段连续序列数字反转--0 变 1,1变 0(操作 1),要么询问某个元素的值(操作 2). 例如当 n=20 时,10 条指令如下: 操作 回答 操作后的数组 1 1 10 N/A 11111111110000000000 2 6 1 11111111110000000000 2 12 0 11111111110000000000 1 5 12 N/A 11110000001100

10249「一本通 1.3 例 5」weight

#10249「一本通 1.3 例 5」weight 题目描述 原题来自:USACO 已知原数列a1,a2,...,an中的前1项,前2项,前3项,... ,前 n 项的和,以及后 1 项,后 2 项,后 3 项,...,后 n 项的和,但是==所有的数都被打乱了顺序==.此外,我们还知道数列中的数存在于集合 S 中.试求原数列.当存在多组可能的数列时,求字典序最小的数列. 输入格式 第 1 行,一个整数 n . 第 2 行, 2 × n 个整数,注意:数据已被打乱. 第 3 行,一个整数 m ,

「一本通 4.2 例 2」最敏捷的机器人(loj10120)

题目描述 Wind 设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了-- 机器人们都想知道谁是最敏捷的,于是它们进行了如下一个比赛.首先,他们面前会有一排共 n 个数,它们比赛看谁能最先把每连续 k 个数中最大和最小值写下来,当然,这些机器人运算速度都很快,它们比赛的是谁写得快. 但是 Wind 也想知道答案,你能帮助他吗? 输入格式 第一行为 n,k,意义如题目描述. 第二行共 n 个数,为数字序列,所有数字均在 Pascal 的 longint 范围内,即所有数均为整数,且

loj10139. 「一本通 4.5 练习 1」树上操作(loj2125. 「HAOI2015」树上操作 )

思路: 第二遍dfs时记录end[x]为在结点序列中以x为根的子树最后访问的节点,写个线段树标记下传即可.与值有关的数据注意long long #include<cstdio> #include<iostream> #include<vector> #include<cmath> #include<string> using namespace std; const int maxn = 100010; inline void qread(int

loj10143. 「一本通 4.6 例 1」营业额统计

思路: 使用treap在线存数据,每次取出新读入数据的前驱与后继,与该数据差值较小的就是.(注意若已有同一个数据,特判ans+=0) #include<cstdio> #include<iostream> #include<cstdlib> #include<cmath> using namespace std; const int maxn = 100010; inline void qread(int &x) { x = 0; register

loj10144. 「一本通 4.6 练习 1」宠物收养所

思路: treap+简单模拟. 我只能说:注意取模!!!! #include<cstdio> #include<iostream> #include<cstdlib> #include<cmath> using namespace std; const int maxn = 80010; inline void qread(int &x) { x = 0; register int ch = getchar(), flag = 0; while(ch

loj10145. 「一本通 4.6 练习 2」郁闷的出纳员

思路: 简单的treap+模拟. 但这道题的题面有误导群众的嫌疑,因为题中并没有明确刚进入公司就离开的算不算在离开公司的人中. 当然,对于这道题的数据,题目意思是不算在内. #include<cstdio> #include<iostream> #include<cstdlib> #include<string> using namespace std; const int maxn = 80010; inline void qread(int &x

loj10141. 「一本通 4.5 练习 3」染色

思路: 利用树链剖分转化为线段树问题,考虑线段上经这两种操作后的sum求值方法,并着重考虑树链合并的情况即可.线段树为了打cov,要注意将所有的颜色统一加一. #include<cstdio> #include<iostream> using namespace std; const int maxn = 100010; inline void qread(int &x){ x = 0; register int ch = getchar(); while(ch <

loj10147. 「一本通 5.1 例 1」石子合并

思路: 经典的区间dp,题解到处都是... #include<cstdio> #include<iostream> #include<cstdio> using namespace std; const int maxn = 1010; inline void qread(int &x){ x = 0; register int ch = getchar(); while(ch < '0' || ch > '9') ch = getchar(); w