传送门
题目大意:
一个长为
n
(
n
2
≤
250000
)
n(n^2leq250000)
n(n2≤250000)的序列,每个元素
a
i
(
1
≤
a
i
≤
1
0
9
)
a_{i}(1leq a_{i}leq 10^9)
ai(1≤ai≤109)
可以执行若干次操作,每次在序列中的某个位置插入两个相同的数字,给出一组操作序列,使得操作后的序列可以划分为若干个长为偶数的段,每段长度为
k
i
k_{i}
ki,在该段内满足
a
j
=
a
j
+
k
i
2
a_{j}=a_{j+frac {k_{i}}{2}}
aj=aj+2ki,并给出最终各个段的长度,如果操作序列不存在,输出
−
1
-1
−1。
思路:
考虑到在执行操作的过程当中,每种数字个数的奇偶性不会发生变化,而最终的序列如果要满足要求,那么每种数字的个数必须都是偶数,于是当一开始存在某种数字的个数是奇数时,就不存在操作序列。
之后我们可以用以下的方法来进行构造,不断从前往后找一对相同的数字,假设找到的为 1 1 1,并且该序列为 1 1 1 2 2 2 3 3 3 1 1 1 . . . ... ... 那么我们可以在一对相同的数中的后一个的后面不断对称地插入位于这一对数之间的数字,之后序列变为 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3 3 3 3 2 2 2 . . . ... ...,于是可以发现, 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3就构成了一个合法的段,将它们拿掉后,序列变为 3 3 3 2 2 2 . . . ... ...,也就相当于删除了我们之前找到的一对数字,并将他们之间的数字翻转,于是重复这个步骤,就可以给出合法的操作序列了。显然这个步骤最多执行 n 2 frac{n}{2} 2n次,每次执行需要 O ( n ) O(n) O(n)的时间,所以可以在 O ( n 2 ) O(n^2) O(n2)内完成。
代码:
#includeE. Anonymity Is important#include #include using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair PII; #define all(x) x.begin(),x.end() //#define int LL #define lc p*2+1 #define rc p*2+2 #define endl 'n' #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #pragma warning(disable :4996) const double eps = 1e-8; const LL mod = 1000000007; const LL MOD = 998244353; const int maxn = 200010; int T, N; vector A; map mp; void solve() { for (auto& c : mp) { if (c.second % 2) { cout << -1 << endl; return; } } vector op; vector len; int base = 0; while(!A.empty()) { auto beg = A.begin(); auto it2 = beg + 1; while (*it2 != *beg) it2++; int num = it2 - beg; if (it2 == beg + 1) len.push_back(2); else { len.push_back(num * 2); auto tmp = beg + 1; int pos = base + num + 1; while (tmp != it2) { op.push_back(PII(pos++, *tmp)); tmp++; } reverse(A.begin(), it2); } A.erase(A.begin() + num - 1); A.erase(A.begin() + num - 1); base += num * 2; } cout << op.size() << endl; for (auto& c : op) cout << c.first << ' ' << c.second << endl; cout << len.size() << endl; for (auto& c : len) cout << c << ' '; cout << endl; } int main() { IOS; cin >> T; while (T--) { cin >> N; A.clear(), mp.clear(); int num; for (int i = 1; i <= N; i++) { cin >> num; A.push_back(num); mp[num]++; } solve(); } return 0; }
传送门
题目大意:
n
(
1
≤
n
≤
2
×
1
0
5
)
n(1leq nleq2times10^5)
n(1≤n≤2×105)个人,编号
1
∼
n
1sim n
1∼n,
q
(
1
≤
q
≤
2
×
1
0
5
)
q(1leq qleq2times10^5)
q(1≤q≤2×105)次询问:
0
0
0
l
l
l
r
r
r
x
x
x:当
x
=
0
x=0
x=0时,表示
[
l
,
r
]
[l,r]
[l,r]内没有人生病;
x
=
1
x=1
x=1时,
[
l
,
r
]
[l,r]
[l,r]内至少
1
1
1个人生病。
1
1
1
j
j
j:回答第
j
j
j个人是否生病,无法确定则输出N/A
思路:
x
=
0
x=0
x=0 的情况我们很容易处理,用set来维护所有尚未确定不会生病的人,对于每条
0
0
0
l
l
l
r
r
r
0
0
0 的信息,可以将
[
l
,
r
]
[l,r]
[l,r] 内所有人从set中删去。
当查询到
j
j
j 时,如果
j
j
j 不在set内,那么就可以确定
j
j
j 没有生病。否则
j
j
j 要么生病,要么状态无法确定。如果要确定
j
j
j 是生病的,必须在查询之前出现过一条形如
0
0
0
l
l
l
r
r
r
1
1
1 的信息,使得当前在
[
l
,
r
]
[l,r]
[l,r] 内,有且仅有
j
j
j 一个人仍在set当中(如果多于一个说明每个人都有可能是病人,就无法判断了)。于是我们在set中找到
j
j
j 的前驱
l
l
l 以及后继
r
r
r (可以在set中额外加入
0
0
0 和
n
+
1
n+1
n+1 两个人便于实现),我们可以枚举所有位于
[
l
+
1
,
j
]
[l+1,j]
[l+1,j] 内的查询信息的左端点,来看是否有一个对应的右端点的值
<
r
用一个线段树来维护每个左端点区间对应的右端点的最小值,对于每条 0 0 0 l l l r r r 1 1 1 的信息。我们将左端点 l l l处的值单点修改为 m i n ( d a t [ l ] , r ) min(dat[l],r) min(dat[l],r) 即可。在查询时只需要查询区间 [ l + 1 , j ] [l+1,j] [l+1,j] 即可,复杂度 O ( ( n + q ) l o g n ) O((n+q)logn) O((n+q)logn)。
代码:
#include#include #include using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair PII; #define all(x) x.begin(),x.end() //#define int LL #define lc p*2+1 #define rc p*2+2 #define endl 'n' #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #pragma warning(disable :4996) const double eps = 1e-8; const LL mod = 1000000007; const LL MOD = 998244353; const int maxn = 200010; int N, Q; set S; struct { int l, r, dat; }tr[maxn * 4]; void build(int p, int l, int r) { tr[p].l = l, tr[p].r = r; if (l + 1 == r) { tr[p].dat = inf; return; } int mid = (l + r) / 2; build(lc, l, mid), build(rc, mid, r); tr[p].dat = min(tr[lc].dat, tr[rc].dat); } void modify(int p, int a, int x) { if (tr[p].l + 1 == tr[p].r) { tr[p].dat = min(tr[p].dat, x); return; } int mid = (tr[p].l + tr[p].r) / 2; if (a < mid) modify(lc, a, x); else modify(rc, a, x); tr[p].dat = min(tr[lc].dat, tr[rc].dat); } int query(int p, int l, int r) { if (tr[p].l >= l && tr[p].r <= r) return tr[p].dat; int mid = (tr[p].l + tr[p].r) / 2; if (r <= mid) return query(lc, l, r); if (l >= mid) return query(rc, l, r); return min(query(lc, l, mid), query(rc, mid, r)); } void solve() { build(0, 1, N + 1); for (int i = 0; i <= N + 1; i++) S.insert(i); int type, l, r, x, j; while (Q--) { cin >> type; if (type == 0) { cin >> l >> r >> x; if (x == 0) { while (!S.empty()) { int pos = *S.lower_bound(l); if (pos <= r) S.erase(pos); else break; } } else modify(0, l, r); } else { cin >> j; if (!S.count(j)) cout << "NO" << endl; else { auto itl = S.lower_bound(j), itr = S.upper_bound(j); itl--; int l = *itl, r = *itr; if (query(0, l + 1, j + 1) < r) cout << "YES" << endl; else cout << "N/A" << endl; } } } } int main() { IOS; cin >> N >> Q; solve(); return 0; }



