题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4467

题意:给定n个点m条边的无向图,点被染色(黑0/白1),边带边权。然后q个询问。询问分为两种: Change u:把点u的颜色反转(黑变白,白变黑),Asksum a b(a,b的值为0/1):统计所以边的权值和,边的两个点满足一个点的颜色为a,一个点的颜色为b。

思路:考虑暴力的做法。修改可以做法O(1),但是查询就得O(m).所以总复杂度为O(m*q)会TLE。然后考虑图分块。参考HDU 4858的做法,将点分为重点和轻点。重点(度数>=sqrt(m)),轻点(度数sqrt(m))。 由于此题查询比较大,所以需要预处理一下优化之后的运算。我们定义每个顶点维护3个属性:顶点的颜色color,与该点相连的边并且另外一个点是白点的边权和W, 同理B是黑色点的边权和。 然后维护3个变量ansWW:边上两个点都是白色的边权和。同理ansWB,ansBB。

修改:

轻点更新自己的color,W,B。同时更新所有的邻点的W,B值

重点更新自己的color,W,B。同时只更新相邻重点的W,B值(所以重点不需要连边到轻点)

对于更新。若未更新时该点的颜色为白色,那么更新后该点的颜色为黑色。那么对于相邻的点的W就要减去对应边的权值,相邻点的B就要加上对应边的权值。

对于更新。若未更新时该点的颜色为白色,那么更新后该点的颜色为黑色。那么边为WW的答案就要减去与该点相连的W的总和,边WB的答案就要减去与该点相邻的B的总和。  并且边为BB的答案就要加上与该点相邻的B的总和,边为WB的答案就要加上与该点相邻的W的总和。

查询:

直接输出对应的值

性质:

与重点相邻的重点不超过sqrt(m)个。

与轻点相邻的所有点不超过sqrt(m)个。

注意:

该题会有重边,如果不合并重边的话会TLE。所以用个map来合并下重边的值即可。

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
typedef long long int LL;
const int MAXN = + ;
struct Edges{
int u, v; LL w;
Edges(int u = , int v = ,LL w=) :u(u), v(v),w(w){};
};vector<Edges>G[MAXN];
map<pair<int, int>, LL>edge;
struct Node{
int val;
LL W, B;
Node(int val = , LL W = , LL B = ) :val(val), W(W), B(B){};
}node[MAXN];
LL ansWB, ansWW, ansBB;
int du[MAXN], block;
void init(int n,int m){
edge.clear(); ansWB = ansWW = ansBB = ;
block = (int)sqrt(m + 0.5);
for (int i = ; i <= n; i++){
G[i].clear(); du[i] = ; node[i].W = node[i].B = ;
}
}
void makeGraph(int n, int m){
for (map<pair<int,int>,LL>::iterator it=edge.begin(); it!=edge.end(); it++){
int u = it->first.first, v = it->first.second; LL w = it->second;
if (du[u] >= block&&du[v] >= block){
G[u].push_back(Edges(u, v, w)); G[v].push_back(Edges(v, u, w));
}
if (du[u] < block){
G[u].push_back(Edges(u, v, w));
}
if (du[v] < block){
G[v].push_back(Edges(v, u, w));
}
if (node[u].val&&node[v].val){
ansWW += w; node[u].W += w; node[v].W += w;
}
else if (!node[u].val&&!node[v].val){
ansBB += w; node[u].B += w; node[v].B += w;
}
else{
ansWB += w;
if (node[u].val){
node[u].B += w; node[v].W += w;
}
else{
node[u].W += w; node[v].B += w;
}
}
}
}
void modify(int pos){
if (node[pos].val){
ansWW -= node[pos].W; ansWB -= node[pos].B;
ansBB += node[pos].B; ansWB += node[pos].W;
for (int i = ; i < G[pos].size(); i++){
node[G[pos][i].v].W -= G[pos][i].w;
node[G[pos][i].v].B += G[pos][i].w;
}
}
else{
ansBB -= node[pos].B; ansWB -= node[pos].W;
ansWW += node[pos].W; ansWB += node[pos].B;
for (int i = ; i < G[pos].size(); i++){
node[G[pos][i].v].W += G[pos][i].w;
node[G[pos][i].v].B -= G[pos][i].w;
}
}
node[pos].val = !node[pos].val;
}
LL query(int u, int v){
if (u&&v){ return ansWW; }
if (!u&&!v){ return ansBB; }
return ansWB;
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
int n, m, q,Case=;
while (~scanf("%d%d", &n, &m)){
init(n, m);
for (int i = ; i <= n; i++)
scanf("%d", &node[i].val);
for (int i = ; i <= m; i++){
int u, v; LL w; scanf("%d%d%I64d", &u, &v, &w);
if (u > v){ swap(u, v); }
if (edge.find(make_pair(u, v)) == edge.end()){
du[u]++; du[v]++;
edge.insert(make_pair(make_pair(u, v), w));
}
else{
edge[make_pair(u, v)] += w;
}
}
makeGraph(n, m);
printf("Case %d:\n", Case++);
scanf("%d", &q);
while (q--){
int u,v; char type[];
scanf("%s", type);
if (type[]=='A'){
scanf("%d%d", &u, &v);
printf("%I64d\n", query(u, v));
}
else{
scanf("%d", &u); modify(u);
}
//Debug:printf("BB=%I64d WB=%I64d WW=%I64d\n", ansBB, ansWB, ansWW);
}
}
return ;
}

HDU 4467 分块的更多相关文章

  1. HDU 5213 分块 容斥

    给出n个数,给出m个询问,询问 区间[l,r] [u,v],在两个区间内分别取一个数,两个的和为k的对数数量. $k<=2*N$,$n <= 30000$ 发现可以容斥简化一个询问.一个询 ...

  2. HDU 5145 分块 莫队

    给定n个数,q个询问[l,r]区间,每次询问该区间的全排列多少种. 数值都是30000规模 首先考虑计算全排列,由于有同种元素存在,相当于每次在len=r-l+1长度的空格随意放入某种元素即$\bin ...

  3. HDU 2920 分块底数优化 暴力

    其实和昨天写的那道水题是一样的,注意爆LL $1<=n,k<=1e9$,$\sum\limits_{i=1}^{n}(k \mod i) = nk - \sum\limits_{i=1}^ ...

  4. HDU 4858 分块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 题意:中文题面 思路:来自此博客 对每个点定义两个值:val,sum,val记录自己的特征值,s ...

  5. Successor HDU - 4366 分块

    代码+注释: 1 /* 2 题意: 3 一共有n个人,其中0号是总裁(金字塔顶尖).后面输入其他n-1个人的信息啊a.b.c,分别代表第i个人的上级是a,他的 4 忠诚度为b,他的能力为c.后面有m次 ...

  6. hdu 4467 Graph

    P. T. Tigris is a student currently studying graph theory. One day, when he was studying hard, GS ap ...

  7. HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)

    Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...

  8. Graph HDU - 4467

    https://vjudge.net/problem/HDU-4467 大概就是,设一个块大小T 对于度数<=T的点,设为1类点,在改变颜色的时候暴力查询与其相邻点,更新答案 对于度数>T ...

  9. HDU 2608 底数优化分块 暴力

    T(n) as the sum of all numbers which are positive integers can divied n. and S(n) = T(1) + T(2) + T( ...

随机推荐

  1. windows命令

    开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...

  2. Hadoop基础——第一弹:Hadoop介绍

    一.基础 1.了解Java.Linux操作系统相关知识 2.如需精进,应为水平要达到一定标准,能够阅读国外相关技术网站,eg:http://hadoop.apache.org/ 二.什么是Hadoop ...

  3. IIS7.0发布Web服务器0002

    asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” 分类: BS学 ...

  4. IIS7.0发布Web服务-0001

    配置错误 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (overrideModeDefault="Deny"),或者是通过包含 ove ...

  5. 微信的audio无法自动播放的问题

    一.问题 最近做了一个html5的项目,里面涉及到音乐播放,项目要求音乐进入页面就自动播放,于是我就想到了html5的audio标签,将mp3引入进去. 1.在audio标签里引入了autoplay属 ...

  6. erlang 虚机CPU 占用高排查

    -问题起因 近期线上一组服务中,个别节点服务器CPU使用率很低,只有其他1/4.排除业务不均,曾怀疑是系统top统计错误,从Erlang调度器的利用率调查 找到通过erlang:statistics( ...

  7. shiro session计算timeout

    /** * Determines if this session is expired. * * @return true if the specified session has expired, ...

  8. 设置html title标题左侧的小图标

    网页title旁边的小图片设置,图片要求格式必须是.ico,可以使用在线的转换工具把jpg和png图片转换为ico图片,工具地址:http://www.ico.la/ 在html文件中的<hea ...

  9. 【linux】scp命令

    scp的作用是在不同主机之间传输文件. 用法: scp user@host:/path1 path2 说明: 把远程主机host中path1的内容拷贝到当前主机的path2 user是远程主机登陆用户 ...

  10. 经受时间沉淀的15 个 Android 通用流行框架大全

    1. 缓存 名称描述 DiskLruCache: Java实现基于LRU的磁盘缓存 2.图片加载 名称描述 Android    Universal Image Loader 一个强大的加载,缓存,展 ...