题目链接: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. 【Sorting Collection】

    排序集锦 各种排序算法,总结一下,一直在遗忘...... [冒泡排序] 就是下面这个鬼啦: c实现代码(升序): #include<stdio.h> void BubbleSort(int ...

  2. JS事件的三种方式

    1.直接在元素上绑定回调函数 <button id="btn" onclick="clickBtn()">click me</button&g ...

  3. 一些js

    //fixed块随滚动条滚动 window.onscroll=function(){ var scroll_left = $(window).scrollLeft(); $('#table_fixed ...

  4. ubuntu 15.10 安装swift开发环境 2016/4/17

    ubuntu 15.10 64位 下载地址 https://swift.org/download/#using-downloads 1.首先在ubuntu终端上 (ctl+alt+t打开) 下载cla ...

  5. python --enable-shared

    https://github.com/docker-library/python/issues/21 例如编译安装python3.5.2,脚本如下: wget https://s3.cn-north- ...

  6. 连载 [ LTS + Top ]

    +---[ LTS List ]--->| 1. 每日被自己坑的debugging.. http://www.cnblogs.com/tmzbot/p/5582302.html| 2. [待添加 ...

  7. CAD打印线条太粗、线条颜色设置

    不管你是使用打印机,还是将CAD转换为PDF文件,如果出现以下情况,线条太粗,根本看不清楚,怎么解决呢? 或者,不想通过图层复杂.繁琐的设置,想将各种颜色线条的CAD全部打印成黑白,或者指定某一种颜色 ...

  8. Python全栈开发【模块】

    Python全栈开发[模块] 本节内容: 模块介绍 time random os sys json & picle shelve XML hashlib ConfigParser loggin ...

  9. SpringMVC文件上传和下载

    上传与下载 1文件上传 1.1加入jar包 文件上传需要依赖的jar包 1.2配置部件解析器 解析二进制流数据. <?xml version="1.0" encoding=& ...

  10. 创建cocos项目并打包