loj2302 「NOI2017」整数

OI 数据结构
文章目录

首先 $n\log^2 n$ 的暴力还是很好想的,二进制分解 $a$ 然后加加减减就行了。退位进位的 $1$ 用线段树找。

然后压个 $30$ 位就好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, opt, uu, vv, va[4000005], vb[4000005], tag[4000005];
const int S=(1<<30)-1;
void rn(int &x){
char ch=getchar();
x = 0;
int f=1;
while(ch<'0' || ch>'9'){
if(ch=='-') f = -1;
ch = getchar();
}
while(ch>='0' && ch<='9'){
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
void pushDown(int o, int lson, int rson){
tag[lson] = tag[rson] = va[lson] = vb[rson] = vb[lson] = va[rson] = tag[o];
tag[o] = -1;
}
void pushUp(int o, int lson, int rson){
va[o] = va[lson] & va[rson];
vb[o] = vb[lson] | vb[rson];
}
int query(int o, int l, int r, int x){
if(l==r) return va[o];
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(~tag[o]) pushDown(o, lson, rson);
if(x<=mid) return query(lson, l, mid, x);
else return query(rson, mid+1, r, x);
}
}
void update(int o, int l, int r, int x, int y){
if(l==r){
va[o] += y;
vb[o] += y;
}
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(~tag[o]) pushDown(o, lson, rson);
if(x<=mid) update(lson, l, mid, x, y);
else update(rson, mid+1, r, x, y);
pushUp(o, lson, rson);
}
}
int findNotAllOne(int o, int l, int r, int x, int y){
if(va[o]==S) return -1;
if(l==r) return l;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(~tag[o]) pushDown(o, lson, rson);
if(x<=mid){
int t=findNotAllOne(lson, l, mid, x, y);
return ~t?t:findNotAllOne(rson, mid+1, r, x, y);
}
return findNotAllOne(rson, mid+1, r, x, y);
}
}
int findNotAllZero(int o, int l, int r, int x, int y){
if(!vb[o]) return -1;
if(l==r) return l;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(~tag[o]) pushDown(o, lson, rson);
if(x<=mid){
int t=findNotAllZero(lson, l, mid, x, y);
return ~t?t:findNotAllZero(rson, mid+1, r, x, y);
}
return findNotAllZero(rson, mid+1, r, x, y);
}
}
void modify(int o, int l, int r, int x, int y, int k){
if(l>=x && r<=y)
va[o] = vb[o] = tag[o] = k;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(~tag[o]) pushDown(o, lson, rson);
if(x<=mid) modify(lson, l, mid, x, y, k);
if(mid<y) modify(rson, mid+1, r, x, y, k);
pushUp(o, lson, rson);
}
}
void add(int p, int x){
int t=query(1, 0, 1000001, p);
if(t+x<=S) update(1, 0, 1000001, p, x);
else{
update(1, 0, 1000001, p, x-(1<<30));
int q=findNotAllOne(1, 0, 1000001, p+1, 1000001);
if(q!=p+1) modify(1, 0, 1000001, p+1, q-1, 0);
update(1, 0, 1000001, q, 1);
}
}
void del(int p, int x){
int t=query(1, 0, 1000001, p);
if(t-x>=0) update(1, 0, 1000001, p, -x);
else{
update(1, 0, 1000001, p, (1<<30)-x);
int q=findNotAllZero(1, 0, 1000001, p+1, 1000001);
if(q!=p+1) modify(1, 0, 1000001, p+1, q-1, S);
update(1, 0, 1000001, q, -1);
}
}
int main(){
memset(tag, -1, sizeof(tag));
rn(n); rn(opt); rn(opt); rn(opt);
while(n--){
rn(opt);
if(opt==1){
rn(uu); rn(vv);
int p=vv/30, q=vv%30;
if(uu>0){
add(p, (uu<<q)&S);
add(p+1, uu>>(30-q));
}
else if(uu<0){
uu *= -1;
del(p, (uu<<q)&S);
del(p+1, uu>>(30-q));
}
}
else{
rn(uu);
int p=uu/30;
int t=query(1, 0, 1000001, p);
p = uu % 30;
printf("%d\n", (t>>p)&1);
}
}
return 0;
}