#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int tree[1005][1005]={0},n;
int offline()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
return 0;
}
int exit()
{
fclose(stdin);
fclose(stdout);
return 0;
}
int lowbit(int t)
{
return t&(-t);
}
int sum(int x,int y)
{
int ans=0;
for(int i=x;i>0;i-=lowbit(i))
for(int j=y;j>0;j-=lowbit(j))
ans+=tree[i][j];
return ans;
}
void plus(int x,int y,int num)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
tree[i][j]+=num;
}
int main()
{
offline();
int cnt,t,a,b,x1,x2,y1,y2;
char order;
scanf("%d",&cnt);
while(cnt--)
{
memset(tree,0,sizeof(tree));
scanf("%d %d",&n,&t);
getchar();
while(t--)
{
scanf("%c",&order);
if(order=='C')
{
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
getchar();
plus(x1,y1,1);
plus(x1,y2+1,1);
plus(x2+1,y1,1);
plus(x2+1,y2+1,1);
}
else
{
scanf("%d %d",&a,&b);
getchar();
printf("%d\n",sum(a,b)%2);
}
}
printf("\n");
}
exit();
return 0;
}
POJ2155
2015-03-28 17:43:12 By zmhx2
KMP&AC自动机PPT讲解
2015-03-28 17:35:27 By zmhx2
AC自动机模板
2015-03-28 17:10:29 By zmhx2
AC自动机模板
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 10000000;
struct node
{
int count;
int id;
int deepth;
node *next[26];
node *fail;
};
node *q[MAXN];
char keyword[55];
char str[1000010];
int head,tail;
int position[100001];
node *root;
void insert(char *word,node *root,int id)
{
int index,len;
node *p = root,*newnode;
len = strlen(word);
for(int i=0 ; i < len ; i++ )
{
index=word[i]-'a';
if(!p->next[index])
{
newnode=(struct node *)malloc(sizeof(struct node));
for(int j=0; j<26; j++) newnode->next[j]=0;
newnode->id=0;
newnode->count=0;
newnode->fail=0;
newnode->deepth=p->deepth+1;
p->next[index]=newnode;
}
p=p->next[index];
}
p->count++;
p->id=id;
}
void build_ac_automation(node *root)
{
head=0;
tail=1;
q[head]=root;
node *temp,*p;
while(head<tail)
{
temp=q[head++];
for(int i=0; i< 26 ; i ++)
{
if(temp->next[i])
{
if(temp==root)temp->next[i]->fail=root;
else
{
p=temp->fail;
while(p)
{
if(p->next[i])
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(!p)temp->next[i]->fail=root;
}
q[tail++]=temp->next[i];
}
}
}
}
int query(node *root)
{
int i,cnt=0,index,len=strlen(str);
node *p=root;
for(i=0; i < len ; i ++)
{
index=str[i]-'a';
while( !p->next[index] && p != root)p=p->fail;
p=p->next[index];
if(!p)p=root;
node *temp=p;
while(temp != root )
{
if(temp->count>=0)
{
cnt+=temp->count;
position[temp->id]=i+1-temp->deepth;
temp->count=-1;
}
else break;
temp=temp->fail;
}
}
return cnt;
}
int main()
{
int i,t,n,ans;
scanf("%d",&t);
while(t--)
{
memset(position,-1,sizeof(position));
root=(struct node *)malloc(sizeof(struct node));
for(int j=0; j<26; j++) root->next[j]=0;
root->fail=0;
root->count=0;
scanf("%d",&n);
getchar();
for(i=0; i<n; i++)
{
gets(keyword);
insert(keyword,root,i+1);
}
build_ac_automation(root);
gets(str);
ans=query(root);
printf("%d\n",ans);
//for(int i=1; i<=n; i++)printf("position:%d\n",position[i]);打出匹配位置
}
fclose(stdin);
fclose(stdout);
return 0;
}
二维树状数组模板
2015-03-28 17:09:01 By zmhx2
二维树状数组模板
#include<cstdio>
#include<cstdlib>
#include<cstring>
int num[10001][10001]={0},tree[10001][10001]={0};
int n,x,y;
int lowbit(int x)
{
return x&(-x);
}
int sum(int x,int y)
{
int sum = 0;
for(int i=x;i>0;i-=lowbit(i))
for(int j=y;j>0;j-=lowbit(j))
sum += tree[i][j];
return sum;
}
void plus(int x,int y,int num)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
tree[i][j] += num;
}
int main()
{
scanf("%d",&n);
scanf("%d %d",&x,&y);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&num[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
plus(i,j,num[i][j]);
printf("%d\n",sum(x,y));
return 0;
}
一维树状数组模板
2015-03-28 17:08:29 By zmhx2
一维树状数组模板
#include<cstdio>
#include<cstdlib>
#include<cstring>
int num[100002]={0},tree[100002]={0};
int n,l,r;
int lowbit(int x)
{
return x&(-x);
}
int sum(int pos)
{
int sum=0;
while(pos>0)
{
sum+=tree[pos];
pos-=lowbit(pos);
}
return sum;
}
void plus(int pos, int num)
{
while(pos<=n)
{
tree[pos]+=num;
pos+=lowbit(pos);
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&num[i]);
for(int i=1;i<=n;i++)plus(i,num[i]);
printf("%d\n",sum(n));
return 0;
}