# UOJ #314. 【NOI2017】整数 | 线段树 压位

2018/03/06 17:35

UOJ 134

## 题解

001111111
10
---------
010000001


110000001
10
---------
101111111


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}

const int N = 500005, S = 60;
const ll INF = (1LL << S) - 1;
int n, m, t, pos[4*N];
ll data[N], tag[4*N];
bool all[4*N][2];

void single_change(int k, ll x){
if(pos[k] != -1) data[pos[k]] = x;
if(x == 0) all[k][0] = 1, all[k][1] = 0, tag[k] = 0;
else if(x == INF) all[k][0] = 0, all[k][1] = 1, tag[k] = INF;
else all[k][0] = all[k][1] = 0, tag[k] = -1;
}
void pushdown(int k){
if(tag[k] == -1) return;
single_change(k << 1, tag[k]);
single_change(k << 1 | 1, tag[k]);
tag[k] = -1;
}
void pushup(int k){
all[k][0] = all[k << 1][0] & all[k << 1 | 1][0];
all[k][1] = all[k << 1][1] & all[k << 1 | 1][1];
}
void build(int k, int l, int r){
all[k][0] = 1, tag[k] = pos[k] = -1;
if(l == r) return (void)(pos[k] = l);
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
}
void range_change(int k, int l, int r, int ql, int qr, ll x){
if(ql <= l && qr >= r) return single_change(k, x);
pushdown(k);
int mid = (l + r) >> 1;
if(ql <= mid) range_change(k << 1, l, mid, ql, qr, x);
if(qr > mid) range_change(k << 1 | 1, mid + 1, r, ql, qr, x);
pushup(k);
}
int find_nxt(int k, int l, int r, int p, int o){
if(all[k][!o]) return -1;
if(l == r) return l;
pushdown(k);
int mid = (l + r) >> 1, tmp;
if(p <= mid && (tmp = find_nxt(k << 1, l, mid, p, o)) != -1) return tmp;
return find_nxt(k << 1 | 1, mid + 1, r, p, o);
}
ll query(int k, int l, int r, int p){
if(l == r) return data[l];
pushdown(k);
int mid = (l + r) >> 1;
if(p <= mid) return query(k << 1, l, mid, p);
else return query(k << 1 | 1, mid + 1, r, p);
}
ll tmp = query(1, 0, n, p);
range_change(1, 0, n, p, p, (tmp + x) & INF);
if(tmp + x > INF){
int tar = find_nxt(1, 0, n, p + 1, 0);
range_change(1, 0, n, tar, tar, data[tar] + 1);
if(p + 1 <= tar - 1) range_change(1, 0, n, p + 1, tar - 1, 0);
}
}
void sub(int p, ll x){
ll tmp = query(1, 0, n, p);
range_change(1, 0, n, p, p, (tmp - x) & INF);
if(tmp - x < 0){
int tar = find_nxt(1, 0, n, p + 1, 1);
range_change(1, 0, n, tar, tar, data[tar] - 1);
if(p + 1 <= tar - 1) range_change(1, 0, n, p + 1, tar - 1, INF);
}
}

int main(){

read(n), m = n, n = n / 2 + 2;
build(1, 0, n);
ll op, a, b;
while(m--){
if(op == 1){
if(a > 0){
int p = b / S, rst = b % S;
ll x = a << rst & INF;
p++, a >>= (S - rst);
}
else{
a = -a;
int p = b / S, rst = b % S;
ll x = a << rst & INF;
if(x) sub(p, x);
p++, a >>= (S - rst);
if(b) sub(p, a);
}
}
else write(query(1, 0, n, a / S) >> (a % S) & 1), enter;
}

return 0;
}


0
0 收藏

0 评论
0 收藏
0