题意:给一个序列a,以及K,有Q个询问,每个询问四个数,L,R,U,V, 求L<=i<=R,U<=j<=V,a[i]+a[j]=K的(i, j)对数(题目保证了L <= R < U <= V)。
思路:首先用容斥原理把询问变为i,j同区间,记f(A,B)为答案,'+'为区间的并,A=[L,R],B=[U,V],C=[u+1,v-1],则f(A,B) = f(A+B+C,A+B+C)+f(C,C)-f(A+C,A+C)-f(B+C,B+C)。令g(L,R) = f([L,R],[L,R]) = f(A,A)。对于g函数区间变化为1时,可以做到o(1)的维护。用莫队算法的知识把询问排个序,按顺序维护当前区间的答案,同时更新最后答案。
总结:莫队算法并不是某一具体问题的解法,而是一种通用算法,对于任意无修改的区间统计问题,都可以用莫队算法试试。对于所有询问(L,R),复杂度等于sigma(|Li - Li-1| + |Ri - Ri-1|)*o(x),o(x)是区间变化为1时的维护代价,而前面的sigma用莫队算法的知识可以降低到o(m*sqrt(n))。
1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <set> 16 #include <bitset> 17 #include <functional> 18 #include <numeric> 19 #include <stdexcept> 20 #include <utility> 21 22 using namespace std; 23 24 #define mem0(a) memset(a, 0, sizeof(a)) 25 #define mem_1(a) memset(a, -1, sizeof(a)) 26 #define lson l, m, rt << 1 27 #define rson m + 1, r, rt << 1 | 1 28 #define define_m int m = (l + r) >> 1 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 32 #define rep_down1(a, b) for (int a = b; a > 0; a--) 33 #define all(a) (a).begin(), (a).end() 34 #define lowbit(x) ((x) & (-(x))) 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 38 #define pchr(a) putchar(a) 39 #define pstr(a) printf("%s", a) 40 #define sstr(a) scanf("%s", a) 41 #define sint(a) scanf("%d", &a) 42 #define sint2(a, b) scanf("%d%d", &a, &b) 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c) 44 #define pint(a) printf("%d\n", a) 45 #define test_print1(a) cout << "var1 = " << a << endl 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl 48 #define mp(a, b) make_pair(a, b) 49 #define pb(a) push_back(a) 50 51 typedef long long LL; 52 typedef pair<int, int> pii; 53 typedef vector<int> vi; 54 55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 57 const int maxn = 3e4 + 7; 58 const int md = 10007; 59 const int inf = 1e9 + 7; 60 const LL inf_L = 1e18 + 7; 61 const double pi = acos(-1.0); 62 const double eps = 1e-6; 63 64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 67 template<class T>T condition(bool f, T a, T b){return f?a:b;} 68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 69 int make_id(int x, int y, int n) { return x * n + y; } 70 71 struct Node { 72 int L, R, id, kth; 73 constructInt4(Node, L, R, id, kth); 74 }; 75 Node node[maxn * 4]; 76 77 int block; 78 int a[maxn], ans[maxn], cnt[maxn]; 79 80 bool cmp(const Node a, const Node b) { 81 int lb = a.L / block, rb = b.L / block; 82 return lb < rb || lb == rb && a.R < b.R; 83 } 84 85 int main() { 86 //freopen("in.txt", "r", stdin); 87 int n, m, k; 88 while (cin >> n) { 89 cin >> k; 90 rep_up0(i, n) { 91 sint(a[i]); 92 } 93 cin >> m; 94 rep_up0(i, m) { 95 int L, R, U, V; 96 scanf("%d %d %d %d", &L, &R, &U, &V); 97 L --; R --; U --; V --; 98 node[i * 4] = Node(L, V, i, 0); 99 node[i * 4 + 1] = Node(R + 1, U - 1, i, 1); 100 node[i * 4 + 2] = Node(L, U - 1, i, 2); 101 node[i * 4 + 3] = Node(R + 1, V, i, 3); 102 } 103 104 block = (int)sqrt(n + 0.5); 105 sort(node, node + 4 * m, cmp); 106 107 int cur_ans = 0, L = 0, R = -1; 108 mem0(cnt); 109 mem0(ans); 110 111 rep_up0(i, 4 * m) { 112 Node query = node[i]; 113 while (L < query.L) { 114 cnt[a[L]] --; 115 if (k > a[L] && k - a[L] <= n) cur_ans -= cnt[k - a[L]]; 116 L ++; 117 } 118 while (R > query.R) { 119 cnt[a[R]] --; 120 if (k > a[R] && k - a[R] <= n) cur_ans -= cnt[k - a[R]]; 121 R --; 122 } 123 while (L > query.L) { 124 L --; 125 if (k > a[L] && k - a[L] <= n) cur_ans += cnt[k - a[L]]; 126 cnt[a[L]] ++; 127 } 128 while (R < query.R) { 129 R ++; 130 if (k > a[R] && k - a[R] <= n) cur_ans += cnt[k - a[R]]; 131 cnt[a[R]] ++; 132 } 133 if (query.kth < 2) ans[query.id] += cur_ans; 134 else ans[query.id] -= cur_ans; 135 } 136 137 rep_up0(i, m) { 138 printf("%d\n", ans[i]); 139 } 140 } 141 return 0; 142 }