考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间复杂度大概是O(M(logM)^2)
-----------------------------------------------------------------------
#include
#include
#include
using namespace std;
typedef long long ll;
#define cal(x, y) (ll(Val) * (x) * (y))
#define lowbit(x) ((x) & -(x))
#define h(v) (lower_bound(Y, Y + Yn, v) - Y + 1)
#define Q(x) o[Que[x]]
#define A(x) o[Ad[x]]
const int MAXN = 640009;
const int MAXQ = 40009;
inline int read() {
char c = getchar();
int ret = 0, t = 0;
for(; !isdigit(c); c = getchar()) if(c == '-') t = 1;
for(; isdigit(c); c = getchar()) ret = ret * 10 + c - '0';
return t ? -ret : ret;
}
int Val, On, Qn;
int ans[MAXQ];
int Ad[MAXN], Que[MAXQ], Adn, Quen;
int Y[MAXN], Yn, X[MAXN], Xn;
struct O {
int p, x, y, v;
inline void Set(int _p, int _x, int _y, int _v) {
p = _p; x = _x; y = _y; v = _v;
}
bool operator < (const O &o) const {
return x < o.x;
}
} o[MAXN + MAXQ];
void Init() {
Val = read();
Qn = On = 0;
for(int t = read(); (t = read()) != 3; ) {
if(t == 1) {
int x = read(), y = read();
o[On++].Set(-1, x, y, read());
} else {
int x0 = read() - 1, y0 = read() - 1, x1 = read(), y1 = read();
o[On++].Set(Qn, x0, y0, 1);
o[On++].Set(Qn, x1, y1, 1);
o[On++].Set(Qn, x0, y1, 0);
o[On++].Set(Qn, x1, y0, 0);
ans[Qn++] = cal(x0, y0) + cal(x1, y1) - cal(x1, y0) - cal(x0, y1);
}
}
}
struct BIT {
int b[MAXN];
BIT() {
memset(b, 0, sizeof b);
}
void Add(int p, int v) {
for(; p <= Yn; p += lowbit(p)) b[p] += v;
}
int Query(int p) {
int ret = 0;
for(; p; p -= lowbit(p)) ret += b[p];
return ret;
}
} Bit;
bool Cmp(const int &l, const int &r) {
return o[l].x < o[r].x;
}
void Solve() {
Yn = Xn = 0;
for(int i = 0; i < Quen; i++)
Y[Yn++] = Q(i).y, X[Xn++] = Q(i).x;
for(int i = 0; i < Adn; i++)
Y[Yn++] = A(i).y, X[Xn++] = A(i).x;
sort(Que, Que + Quen, Cmp);
sort(Ad, Ad + Adn, Cmp);
sort(Y, Y + Yn); Yn = unique(Y, Y + Yn) - Y;
sort(X, X + Xn); Xn = unique(X, X + Xn) - X;
int _Adn = 0, _Quen = 0;
for(int i = 0; i < Xn; i++) {
while(_Adn < Adn && A(_Adn).x == X[i])
Bit.Add(h(A(_Adn).y), A(_Adn).v), _Adn++;
while(_Quen < Quen && Q(_Quen).x == X[i]) {
int ret = Bit.Query(h(Q(_Quen).y));
ans[Q(_Quen).p] += Q(_Quen).v ? ret : -ret;
_Quen++;
}
}
for(int i = 0; i < Adn; i++)
Bit.Add(h(A(i).y), -A(i).v);
}
// [l, r)
void CDQ(int l, int r) {
if(l + 1 >= r) return;
int m = (l + r) >> 1;
CDQ(l, m); CDQ(m, r);
Adn = Quen = 0;
for(; l < m; l++)
if(!~o[l].p) Ad[Adn++] = l;
for(; l < r; l++)
if(~o[l].p) Que[Quen++] = l;
if(Quen && Adn) Solve();
}
int main() {
Init();
CDQ(0, On);
for(int i = 0; i < Qn; i++)
printf("%d
", ans[i]);
return 0;
}
-----------------------------------------------------------------------
1176: [Balkan2007]Mokia
Time Limit: 30 Sec Memory Limit: 162 MB
Submit: 1281 Solved: 537
[Submit][Status][Discuss]Description
维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000.
Input
第一行两个整数,S,W;其中S为矩阵初始值;W为矩阵大小
接下来每行为一下三种输入之一(不包含引号):
"1 x y a"
"2 x1 y1 x2 y2"
"3"
输入1:你需要把(x,y)(第x行第y列)的格子权值增加a
输入2:你需要求出以左上角为(x1,y1),右下角为(x2,y2)的矩阵内所有格子的权值和,并输出
输入3:表示输入结束
Output
Sample Input
0 4
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3
Sample Output
3
5
HINT
Source