Codeforces Round #435 (Div. 2) E. Mahmoud and Ehab and the function[二分]
题目:http://codeforces.com/problemset/problem/862/E
Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array a of length n and array b of length m. He introduced a function f(j) which is defined for integers j, which satisfy 0 ≤ j ≤ m - n. Suppose, ci = ai - bi + j. Then f(j) = |c1 - c2 + c3 - c4... cn|. More formally, .
Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid j. They found it a bit easy, so Dr. Evil made their task harder. He will give them q update queries. During each update they should add an integer xi to all elements in a in range [li;ri]i.e. they should add xi to ali, ali + 1, ... , ari and then they should calculate the minimum value of f(j) for all valid j.
Please help Mahmoud and Ehab.
The first line contains three integers n, m and q (1 ≤ n ≤ m ≤ 105, 1 ≤ q ≤ 105) — number of elements in a, number of elements in b and number of queries, respectively.
The second line contains n integers a1, a2, ..., an. ( - 109 ≤ ai ≤ 109) — elements of a.
The third line contains m integers b1, b2, ..., bm. ( - 109 ≤ bi ≤ 109) — elements of b.
Then q lines follow describing the queries. Each of them contains three integers li ri xi (1 ≤ li ≤ ri ≤ n, - 109 ≤ x ≤ 109) — range to be updated and added value.
The first line should contain the minimum value of the function f before any update.
Then output q lines, the i-th of them should contain the minimum value of the function f after performing the i-th update .
5 6 3
1 2 3 4 5
1 2 3 4 5 6
1 1 10
1 1 -9
1 5 -1
0
9
0
0
For the first example before any updates it's optimal to choose j = 0, f(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0.
After the first update a becomes {11, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9.
After the second update a becomes {2, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(2 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6)| = |0| = 0.
After the third update a becomes {1, 1, 2, 3, 4} and it's optimal to choose j = 0, f(0) = |(1 - 1) - (1 - 2) + (2 - 3) - (3 - 4) + (4 - 5)| = |0| = 0.
题意:a数组含有n个元素,b数组含有m个元素。含有q次区间修改,每次由公式计算f(j)最小值。
题解:也许是见到最水的一个E...分开计算a和b,a组的数值固定,由于加减相互交错,每次只需要加一次更新的值或者不加。所有可能的b组数值存一个set,每次在set二分搜索和a差值最小的b,做差得到绝对值最小值。
1 #define _CRT_SECURE_NO_DEPRECATE
2 #pragma comment(linker, "/STACK:102400000,102400000")
3 #include<iostream>
4 #include<cstdio>
5 #include<fstream>
6 #include<iomanip>
7 #include<algorithm>
8 #include<cmath>
9 #include<deque>
10 #include<vector>
11 #include<bitset>
12 #include<queue>
13 #include<string>
14 #include<cstring>
15 #include<map>
16 #include<stack>
17 #include<set>
18 #include<functional>
19 #define pii pair<int, int>
20 #define mod 1000000007
21 #define mp make_pair
22 #define pi acos(-1)
23 #define eps 0.00000001
24 #define mst(a,i) memset(a,i,sizeof(a))
25 #define all(n) n.begin(),n.end()
26 #define lson(x) ((x<<1))
27 #define rson(x) ((x<<1)|1)
28 #define inf 0x3f3f3f3f
29 typedef long long ll;
30 typedef unsigned long long ull;
31 using namespace std;
32 const int maxn = 1e5 + 5;
33 set<ll>b;
34 ll orib[maxn];
35
36 void getbest(ll suma)
37 {
38 auto it = b.lower_bound(suma);
39 ll best;
40 if (it != b.end())
41 {
42 best = abs(suma - *it);
43 if (it != b.begin())
44 {
45 it--;
46 best = min(best, abs(suma - *it));
47 }
48 }
49 else
50 {
51 it--;
52 best = abs(suma - *it);
53 }
54 cout << best << endl;
55 return;
56 }
57
58 int main()
59 {
60 ios::sync_with_stdio(false);
61 cin.tie(0); cout.tie(0);
62 int i, j, m, n, q;
63 cin >> n >> m >> q;
64 ll suma = 0;
65 ll flag = 1, k;
66 for (int i = 1; i <= n; ++i)
67 {
68 cin >> k;
69 suma += k*flag;
70 flag *= -1;
71 }
72 for (int i = 1; i <= m; ++i)
73 cin >> orib[i];
74 ll sumb = 0;
75 flag = 1;
76 for (int i = 1; i <= n; ++i)
77 {
78 sumb += orib[i] * flag;
79 flag *= -1;
80 }
81 b.insert(sumb);
82 if (n % 2 == 0)flag = -1;
83 else flag = 1;
84 for (int i = n + 1; i <= m; ++i)
85 {
86 sumb *= -1;
87 sumb += orib[i - n];
88 sumb += orib[i] * flag;
89 b.insert(sumb);
90 }
91 int ta, tb, tc;
92 getbest(suma);
93 for (int i = 1; i <= q; ++i)
94 {
95 cin >> ta >> tb >> tc;
96 if ((tb - ta + 1) % 2 != 0)
97 {
98 if (ta & 1)suma += tc;
99 else suma -= tc;
100 }
101 getbest(suma);
102 }
103 return 0;
104 }