题目链接: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. 我们是怎么管理QQ群的

    文章背景:腾讯平台上的qq群数以千万百万计,但99%的在吹水扯蛋,从早上的问好开始,到晚上的晚安,无一不浪费青春之时间,看之痛心,无力改变,只好自己建了一个,希望能以此来改变群内交流的氛围或环境. 以 ...

  2. Android 项目结构图

    src:存放Java源代码 gen:存放系统自动生成的配置文件 Android 4.4.2:包含Android.jar文件,包含构建应用程序所需的所有Android SDK库 asssets:存放资源 ...

  3. Linux搭建Nginx

    1.Nginx安装 1.1 pcre (1)下载编译包 http://www.pcre.org/ (注意需要的是pcce,而非pcre2) (2)tar -zxvf pcre-8.36.tar.gz  ...

  4. MySQL 子查询与连接操作笔记

    SQL语句之间是可以进行连接操作的,在一些复杂的数据操作中必须用到连接操作.简单的说就是一个SQL语句的结果可以作为相连接的SQL操作的一部分.SQL结构化查询语句,子查询是指的所有的SQL操作,并非 ...

  5. es6中的promise对象

    Promise是异步里面的一种解决方案,解决了回调嵌套的问题,es6将其进行了语言标准,同意了用法,提供了`promise`对象, promise对象有三种状态:pending(进行中) .Resol ...

  6. 使用CSS隐藏HTML元素的4种常用方法

    现在的网页设计越来越动态化,我们经常需要隐藏某些元素,在特定的时候才显示它们.我们通常可以使用4种方法来隐藏和显示元素. 这4种显示和隐藏元素的技术各自有它们自己的优点的缺点,下面来举例说明. 在这篇 ...

  7. localstorage 和 sessionstorage 本地存储

    在我们日常的工作和实际项目中,做好数据数据缓存可以是我们的程序执行效率更高,可以使我们避免重复请求 服务器,减轻服务器的压力,可以提高使用户的体验度. 那么 HTML5 存储的目标是什么? 1.解决存 ...

  8. bzoj4555题解

    我们计算$f(i)=\sum_{j=1}^i S(i,j)\times 2^j\times (j!)$,容(o)易(e)知(i)道(s)$f(i)$的指数生成函数为$\frac{1}{3-2\time ...

  9. 面向对象Part2

    `变量: 成员变量:又叫全局变量,定义在类中,方法外面. 1).类成员变量.   使用Static 2).实例成员变量.  没有使用Static. 局部变量:出了成员变量,其他的都是局部变量. 1). ...

  10. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...