Codeforces 707E Garlands(二维树状数组)
2016-08-24 14:03
323 查看
给你n∗m的矩阵,然后k个花环,每个有leni个灯,然后一开始都是开的
10W次操作,可以把这个花环的所有灯都变一个状态,然后求子矩阵内有多少亮的
求和的操作就2000次
想了个20003的暴力肯定不行
题解也没怎么懂,看lxc巨菊二维树状数组过了,但是算了下复杂度有2000∗2000∗50∗log2000啊
只要记录一下这次求和和上次操作的时候状态一不一样,不一样就修改
玄学复杂度过题。。。就当复习二维树状数组了
代码:
10W次操作,可以把这个花环的所有灯都变一个状态,然后求子矩阵内有多少亮的
求和的操作就2000次
想了个20003的暴力肯定不行
题解也没怎么懂,看lxc巨菊二维树状数组过了,但是算了下复杂度有2000∗2000∗50∗log2000啊
只要记录一下这次求和和上次操作的时候状态一不一样,不一样就修改
玄学复杂度过题。。。就当复习二维树状数组了
代码:
#include <map> #include <set> #include <ctime> #include <stack> #include <queue> #include <cmath> #include <bitset> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> #pragma comment(linker,"/STACK:102400000,102400000") using namespace std; #define MAX 2005 #define MAXN 1000005 #define maxnode 205 #define sigma_size 26 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lrt rt<<1 #define rrt rt<<1|1 #define middle int m=(r+l)>>1 #define LL long long #define ull unsigned long long #define mem(x,v) memset(x,v,sizeof(x)) #define lowbit(x) (x&-x) #define pii pair<int,int> #define bits(a) __builtin_popcount(a) #define mk make_pair #define limit 10000 //const int prime = 999983; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-4; const LL mod = 1e9+7; const ull mx = 133333331; /*****************************************************/ inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; } /*****************************************************/ LL c[MAX][MAX]; int n,m; void add(int x, int y, int w) { for (int i = x; i <= n; i += lowbit(i)) { for (int j = y; j <= m; j += lowbit(j)) { c[i][j] += w; } } } LL sum(int x, int y) { LL ret = 0; for (int i = x; i > 0; i -= lowbit(i)) { for (int j = y; j > 0; j -= lowbit(j)) { ret += c[i][j]; } } return ret; } vector<pair<pii,int> > v[MAX]; int vis[MAX]; int last[MAX]; int main(){ int k; while(cin>>n>>m>>k){ mem(c,0); for(int i=1;i<=k;i++){ v[i].clear(); int len; scanf("%d",&len); while(len--){ int a,b,c; scanf("%d%d%d",&a,&b,&c); v[i].push_back(mk(mk(a,b),c)); add(a,b,c); } } int q; cin>>q; mem(last,0);mem(vis,0); while(q--){ char op[10]; scanf("%s",op); if(op[0]=='S'){ int a; scanf("%d",&a); vis[a]^=1; } else{ int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); for(int i=1;i<=k;i++){ if(last[i]!=vis[i]){ for(int j=0;j<v[i].size();j++){ if(vis[i]) add(v[i][j].first.first,v[i][j].first.second,-v[i][j].second); else add(v[i][j].first.first,v[i][j].first.second,v[i][j].second); } last[i]=vis[i]; } } LL ans=sum(x2,y2)-sum(x2,y1-1)-sum(x1-1,y2)+sum(x1-1,y1-1); cout<<ans<<endl; } } } return 0; }
相关文章推荐
- Codeforces 869 E. The Untended Antiquity 二维树状数组
- codeforces 835C Star sky(二维树状数组)
- codeforces--707E. Garlands(二维树状数组)
- CodeForces 341D Iahub and Xors(二维树状数组)
- [Codeforces 341D] Iahub and Xors (二维树状数组)
- Codeforces 707 E. Garlands (二维树状数组)
- [CodeForces 677D] Vanya and Treasure (二维树状数组+静态前缀最小值)
- HDU-2642-Stars(二维树状数组应用)
- POJ 1195 Mobile phones(裸的二维树状数组)
- Codeforces 570D Tree Requests(DFS重标号+树状数组)
- poj 2155 二维树状数组
- HDOJ 4456 Crowd 离散化+二维树状数组
- 5.3.1 See you~ (二维树状数组)(UNSOLVED)
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
- poj1195——Mobile phones(二维树状数组)
- Codeforces 12D (线段树 树状数组
- Codeforces 387E 树状数组
- HihoCoder1336 Matrix Sum(二维树状数组求和)
- Codeforces 369E Valera and Queries --树状数组+离线操作
- poj 1195 Mobile phones 二维树状数组